Chris@0: save();
Chris@0: ConfigurableLanguage::createFromLangcode('it')->save();
Chris@0:
Chris@0: $field_storage_definition = [
Chris@0: 'field_name' => 'untranslatable_string_field',
Chris@0: 'entity_type' => 'node',
Chris@0: 'type' => 'string',
Chris@0: 'cardinality' => 1,
Chris@0: 'translatable' => FALSE,
Chris@0: ];
Chris@0: $field_storage = FieldStorageConfig::create($field_storage_definition);
Chris@0: $field_storage->save();
Chris@0:
Chris@0: $field_definition = [
Chris@0: 'field_storage' => $field_storage,
Chris@0: 'bundle' => 'page',
Chris@0: ];
Chris@0: $field = FieldConfig::create($field_definition);
Chris@0: $field->save();
Chris@0:
Chris@14: // Enable translation for page nodes.
Chris@14: \Drupal::service('content_translation.manager')->setEnabled('node', 'page', TRUE);
Chris@14:
Chris@0: // Create and log in user.
Chris@0: $web_user = $this->drupalCreateUser(
Chris@0: [
Chris@0: 'view page revisions',
Chris@0: 'revert page revisions',
Chris@0: 'delete page revisions',
Chris@0: 'edit any page content',
Chris@0: 'delete any page content',
Chris@0: 'access contextual links',
Chris@0: 'translate any entity',
Chris@0: 'administer content types',
Chris@0: ]
Chris@0: );
Chris@0:
Chris@0: $this->drupalLogin($web_user);
Chris@0:
Chris@0: // Create initial node.
Chris@0: $node = $this->drupalCreateNode();
Chris@0: $settings = get_object_vars($node);
Chris@0: $settings['revision'] = 1;
Chris@0: $settings['isDefaultRevision'] = TRUE;
Chris@0:
Chris@0: $nodes = [];
Chris@0: $logs = [];
Chris@0:
Chris@0: // Get original node.
Chris@0: $nodes[] = clone $node;
Chris@0:
Chris@0: // Create three revisions.
Chris@0: $revision_count = 3;
Chris@0: for ($i = 0; $i < $revision_count; $i++) {
Chris@0: $logs[] = $node->revision_log = $this->randomMachineName(32);
Chris@0:
Chris@0: // Create revision with a random title and body and update variables.
Chris@0: $node->title = $this->randomMachineName();
Chris@0: $node->body = [
Chris@0: 'value' => $this->randomMachineName(32),
Chris@0: 'format' => filter_default_format(),
Chris@0: ];
Chris@0: $node->untranslatable_string_field->value = $this->randomString();
Chris@0: $node->setNewRevision();
Chris@0:
Chris@16: // Edit the 1st and 2nd revision with a different user.
Chris@16: if ($i < 2) {
Chris@0: $editor = $this->drupalCreateUser();
Chris@0: $node->setRevisionUserId($editor->id());
Chris@0: }
Chris@0: else {
Chris@0: $node->setRevisionUserId($web_user->id());
Chris@0: }
Chris@0:
Chris@0: $node->save();
Chris@0:
Chris@0: // Make sure we get revision information.
Chris@0: $node = Node::load($node->id());
Chris@0: $nodes[] = clone $node;
Chris@0: }
Chris@0:
Chris@0: $this->nodes = $nodes;
Chris@0: $this->revisionLogs = $logs;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Checks node revision related operations.
Chris@0: */
Chris@0: public function testRevisions() {
Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node');
Chris@0: $nodes = $this->nodes;
Chris@0: $logs = $this->revisionLogs;
Chris@0:
Chris@0: // Get last node for simple checks.
Chris@0: $node = $nodes[3];
Chris@0:
Chris@0: // Confirm the correct revision text appears on "view revisions" page.
Chris@0: $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/view");
Chris@0: $this->assertText($node->body->value, 'Correct text displays for version.');
Chris@0:
Chris@0: // Confirm the correct log message appears on "revisions overview" page.
Chris@0: $this->drupalGet("node/" . $node->id() . "/revisions");
Chris@0: foreach ($logs as $revision_log) {
Chris@0: $this->assertText($revision_log, 'Revision log message found.');
Chris@0: }
Chris@0: // Original author, and editor names should appear on revisions overview.
Chris@0: $web_user = $nodes[0]->revision_uid->entity;
Chris@0: $this->assertText(t('by @name', ['@name' => $web_user->getAccountName()]));
Chris@0: $editor = $nodes[2]->revision_uid->entity;
Chris@0: $this->assertText(t('by @name', ['@name' => $editor->getAccountName()]));
Chris@0:
Chris@0: // Confirm that this is the default revision.
Chris@0: $this->assertTrue($node->isDefaultRevision(), 'Third node revision is the default one.');
Chris@0:
Chris@0: // Confirm that the "Edit" and "Delete" contextual links appear for the
Chris@0: // default revision.
Chris@0: $ids = ['node:node=' . $node->id() . ':changed=' . $node->getChangedTime()];
Chris@0: $json = $this->renderContextualLinks($ids, 'node/' . $node->id());
Chris@0: $this->verbose($json[$ids[0]]);
Chris@0:
Chris@0: $expected = '
Edit';
Chris@0: $this->assertTrue(strstr($json[$ids[0]], $expected), 'The "Edit" contextual link is shown for the default revision.');
Chris@0: $expected = 'Delete';
Chris@0: $this->assertTrue(strstr($json[$ids[0]], $expected), 'The "Delete" contextual link is shown for the default revision.');
Chris@0:
Chris@0: // Confirm that revisions revert properly.
Chris@0: $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionid() . "/revert", [], t('Revert'));
Chris@0: $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
Chris@0: '@type' => 'Basic page',
Chris@0: '%title' => $nodes[1]->label(),
Chris@0: '%revision-date' => format_date($nodes[1]->getRevisionCreationTime())
Chris@0: ]), 'Revision reverted.');
Chris@0: $node_storage->resetCache([$node->id()]);
Chris@0: $reverted_node = $node_storage->load($node->id());
Chris@0: $this->assertTrue(($nodes[1]->body->value == $reverted_node->body->value), 'Node reverted correctly.');
Chris@16: // Confirm the revision author is the user performing the revert.
Chris@16: $this->assertTrue($reverted_node->getRevisionUserId() == $this->loggedInUser->id(), 'Node revision author is user performing revert.');
Chris@16: // And that its not the revision author.
Chris@16: $this->assertTrue($reverted_node->getRevisionUserId() != $nodes[1]->getRevisionUserId(), 'Node revision author is not original revision author.');
Chris@0:
Chris@0: // Confirm that this is not the default version.
Chris@0: $node = node_revision_load($node->getRevisionId());
Chris@0: $this->assertFalse($node->isDefaultRevision(), 'Third node revision is not the default one.');
Chris@0:
Chris@0: // Confirm that "Edit" and "Delete" contextual links don't appear for
Chris@0: // non-default revision.
Chris@0: $ids = ['node_revision::node=' . $node->id() . '&node_revision=' . $node->getRevisionId() . ':'];
Chris@0: $json = $this->renderContextualLinks($ids, 'node/' . $node->id() . '/revisions/' . $node->getRevisionId() . '/view');
Chris@0: $this->verbose($json[$ids[0]]);
Chris@0:
Chris@0: $this->assertFalse(strstr($json[$ids[0]], ''), 'The "Edit" contextual link is not shown for a non-default revision.');
Chris@0: $this->assertFalse(strstr($json[$ids[0]], ''), 'The "Delete" contextual link is not shown for a non-default revision.');
Chris@0:
Chris@0: // Confirm revisions delete properly.
Chris@0: $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", [], t('Delete'));
Chris@0: $this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.', [
Chris@0: '%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
Chris@0: '@type' => 'Basic page',
Chris@0: '%title' => $nodes[1]->label(),
Chris@0: ]), 'Revision deleted.');
Chris@0: $this->assertTrue(db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid and vid = :vid', [':nid' => $node->id(), ':vid' => $nodes[1]->getRevisionId()])->fetchField() == 0, 'Revision not found.');
Chris@0: $this->assertTrue(db_query('SELECT COUNT(vid) FROM {node_field_revision} WHERE nid = :nid and vid = :vid', [':nid' => $node->id(), ':vid' => $nodes[1]->getRevisionId()])->fetchField() == 0, 'Field revision not found.');
Chris@0:
Chris@0: // Set the revision timestamp to an older date to make sure that the
Chris@0: // confirmation message correctly displays the stored revision date.
Chris@0: $old_revision_date = REQUEST_TIME - 86400;
Chris@0: db_update('node_revision')
Chris@0: ->condition('vid', $nodes[2]->getRevisionId())
Chris@0: ->fields([
Chris@0: 'revision_timestamp' => $old_revision_date,
Chris@0: ])
Chris@0: ->execute();
Chris@0: $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[2]->getRevisionId() . "/revert", [], t('Revert'));
Chris@0: $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
Chris@0: '@type' => 'Basic page',
Chris@0: '%title' => $nodes[2]->label(),
Chris@0: '%revision-date' => format_date($old_revision_date),
Chris@0: ]));
Chris@0:
Chris@0: // Make a new revision and set it to not be default.
Chris@0: // This will create a new revision that is not "front facing".
Chris@0: $new_node_revision = clone $node;
Chris@0: $new_body = $this->randomMachineName();
Chris@0: $new_node_revision->body->value = $new_body;
Chris@0: // Save this as a non-default revision.
Chris@0: $new_node_revision->setNewRevision();
Chris@0: $new_node_revision->isDefaultRevision = FALSE;
Chris@0: $new_node_revision->save();
Chris@0:
Chris@0: $this->drupalGet('node/' . $node->id());
Chris@0: $this->assertNoText($new_body, 'Revision body text is not present on default version of node.');
Chris@0:
Chris@0: // Verify that the new body text is present on the revision.
Chris@0: $this->drupalGet("node/" . $node->id() . "/revisions/" . $new_node_revision->getRevisionId() . "/view");
Chris@0: $this->assertText($new_body, 'Revision body text is present when loading specific revision.');
Chris@0:
Chris@0: // Verify that the non-default revision vid is greater than the default
Chris@0: // revision vid.
Chris@0: $default_revision = db_select('node', 'n')
Chris@0: ->fields('n', ['vid'])
Chris@0: ->condition('nid', $node->id())
Chris@0: ->execute()
Chris@0: ->fetchCol();
Chris@0: $default_revision_vid = $default_revision[0];
Chris@0: $this->assertTrue($new_node_revision->getRevisionId() > $default_revision_vid, 'Revision vid is greater than default revision vid.');
Chris@0:
Chris@0: // Create an 'EN' node with a revision log message.
Chris@0: $node = $this->drupalCreateNode();
Chris@0: $node->title = 'Node title in EN';
Chris@0: $node->revision_log = 'Simple revision message (EN)';
Chris@0: $node->save();
Chris@0:
Chris@0: $this->drupalGet("node/" . $node->id() . "/revisions");
Chris@0: $this->assertResponse(403);
Chris@0:
Chris@0: // Create a new revision and new log message.
Chris@0: $node = Node::load($node->id());
Chris@0: $node->body->value = 'New text (EN)';
Chris@0: $node->revision_log = 'New revision message (EN)';
Chris@0: $node->setNewRevision();
Chris@0: $node->save();
Chris@0:
Chris@0: // Check both revisions are shown on the node revisions overview page.
Chris@0: $this->drupalGet("node/" . $node->id() . "/revisions");
Chris@0: $this->assertText('Simple revision message (EN)');
Chris@0: $this->assertText('New revision message (EN)');
Chris@0:
Chris@0: // Create an 'EN' node with a revision log message.
Chris@0: $node = $this->drupalCreateNode();
Chris@0: $node->langcode = 'en';
Chris@0: $node->title = 'Node title in EN';
Chris@0: $node->revision_log = 'Simple revision message (EN)';
Chris@0: $node->save();
Chris@0:
Chris@0: $this->drupalGet("node/" . $node->id() . "/revisions");
Chris@0: $this->assertResponse(403);
Chris@0:
Chris@0: // Add a translation in 'DE' and create a new revision and new log message.
Chris@0: $translation = $node->addTranslation('de');
Chris@0: $translation->title->value = 'Node title in DE';
Chris@0: $translation->body->value = 'New text (DE)';
Chris@0: $translation->revision_log = 'New revision message (DE)';
Chris@0: $translation->setNewRevision();
Chris@0: $translation->save();
Chris@0:
Chris@0: // View the revision UI in 'IT', only the original node revision is shown.
Chris@0: $this->drupalGet("it/node/" . $node->id() . "/revisions");
Chris@0: $this->assertText('Simple revision message (EN)');
Chris@0: $this->assertNoText('New revision message (DE)');
Chris@0:
Chris@0: // View the revision UI in 'DE', only the translated node revision is shown.
Chris@0: $this->drupalGet("de/node/" . $node->id() . "/revisions");
Chris@0: $this->assertNoText('Simple revision message (EN)');
Chris@0: $this->assertText('New revision message (DE)');
Chris@0:
Chris@0: // View the revision UI in 'EN', only the original node revision is shown.
Chris@0: $this->drupalGet("node/" . $node->id() . "/revisions");
Chris@0: $this->assertText('Simple revision message (EN)');
Chris@0: $this->assertNoText('New revision message (DE)');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Checks that revisions are correctly saved without log messages.
Chris@0: */
Chris@0: public function testNodeRevisionWithoutLogMessage() {
Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node');
Chris@0: // Create a node with an initial log message.
Chris@0: $revision_log = $this->randomMachineName(10);
Chris@0: $node = $this->drupalCreateNode(['revision_log' => $revision_log]);
Chris@0:
Chris@0: // Save over the same revision and explicitly provide an empty log message
Chris@0: // (for example, to mimic the case of a node form submitted with no text in
Chris@0: // the "log message" field), and check that the original log message is
Chris@0: // preserved.
Chris@0: $new_title = $this->randomMachineName(10) . 'testNodeRevisionWithoutLogMessage1';
Chris@0:
Chris@0: $node = clone $node;
Chris@0: $node->title = $new_title;
Chris@0: $node->revision_log = '';
Chris@0: $node->setNewRevision(FALSE);
Chris@0:
Chris@0: $node->save();
Chris@0: $this->drupalGet('node/' . $node->id());
Chris@0: $this->assertText($new_title, 'New node title appears on the page.');
Chris@0: $node_storage->resetCache([$node->id()]);
Chris@0: $node_revision = $node_storage->load($node->id());
Chris@0: $this->assertEqual($node_revision->revision_log->value, $revision_log, 'After an existing node revision is re-saved without a log message, the original log message is preserved.');
Chris@0:
Chris@0: // Create another node with an initial revision log message.
Chris@0: $node = $this->drupalCreateNode(['revision_log' => $revision_log]);
Chris@0:
Chris@0: // Save a new node revision without providing a log message, and check that
Chris@0: // this revision has an empty log message.
Chris@0: $new_title = $this->randomMachineName(10) . 'testNodeRevisionWithoutLogMessage2';
Chris@0:
Chris@0: $node = clone $node;
Chris@0: $node->title = $new_title;
Chris@0: $node->setNewRevision();
Chris@0: $node->revision_log = NULL;
Chris@0:
Chris@0: $node->save();
Chris@0: $this->drupalGet('node/' . $node->id());
Chris@0: $this->assertText($new_title, 'New node title appears on the page.');
Chris@0: $node_storage->resetCache([$node->id()]);
Chris@0: $node_revision = $node_storage->load($node->id());
Chris@0: $this->assertTrue(empty($node_revision->revision_log->value), 'After a new node revision is saved with an empty log message, the log message for the node is empty.');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Gets server-rendered contextual links for the given contextual links IDs.
Chris@0: *
Chris@0: * @param string[] $ids
Chris@0: * An array of contextual link IDs.
Chris@0: * @param string $current_path
Chris@0: * The Drupal path for the page for which the contextual links are rendered.
Chris@0: *
Chris@0: * @return string
Chris@0: * The decoded JSON response body.
Chris@0: */
Chris@0: protected function renderContextualLinks(array $ids, $current_path) {
Chris@0: $post = [];
Chris@0: for ($i = 0; $i < count($ids); $i++) {
Chris@0: $post['ids[' . $i . ']'] = $ids[$i];
Chris@0: }
Chris@0: $response = $this->drupalPost('contextual/render', 'application/json', $post, ['query' => ['destination' => $current_path]]);
Chris@0:
Chris@0: return Json::decode($response);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests the revision translations are correctly reverted.
Chris@0: */
Chris@0: public function testRevisionTranslationRevert() {
Chris@0: // Create a node and a few revisions.
Chris@0: $node = $this->drupalCreateNode(['langcode' => 'en']);
Chris@0:
Chris@0: $initial_revision_id = $node->getRevisionId();
Chris@0: $initial_title = $node->label();
Chris@0: $this->createRevisions($node, 2);
Chris@0:
Chris@0: // Translate the node and create a few translation revisions.
Chris@0: $translation = $node->addTranslation('it');
Chris@0: $this->createRevisions($translation, 3);
Chris@0: $revert_id = $node->getRevisionId();
Chris@0: $translated_title = $translation->label();
Chris@0: $untranslatable_string = $node->untranslatable_string_field->value;
Chris@0:
Chris@0: // Create a new revision for the default translation in-between a series of
Chris@0: // translation revisions.
Chris@0: $this->createRevisions($node, 1);
Chris@0: $default_translation_title = $node->label();
Chris@0:
Chris@0: // And create a few more translation revisions.
Chris@0: $this->createRevisions($translation, 2);
Chris@0: $translation_revision_id = $translation->getRevisionId();
Chris@0:
Chris@0: // Now revert the a translation revision preceding the last default
Chris@0: // translation revision, and check that the desired value was reverted but
Chris@0: // the default translation value was preserved.
Chris@0: $revert_translation_url = Url::fromRoute('node.revision_revert_translation_confirm', [
Chris@0: 'node' => $node->id(),
Chris@0: 'node_revision' => $revert_id,
Chris@0: 'langcode' => 'it',
Chris@0: ]);
Chris@0: $this->drupalPostForm($revert_translation_url, [], t('Revert'));
Chris@0: /** @var \Drupal\node\NodeStorage $node_storage */
Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node');
Chris@0: $node_storage->resetCache();
Chris@0: /** @var \Drupal\node\NodeInterface $node */
Chris@0: $node = $node_storage->load($node->id());
Chris@0: $this->assertTrue($node->getRevisionId() > $translation_revision_id);
Chris@0: $this->assertEqual($node->label(), $default_translation_title);
Chris@0: $this->assertEqual($node->getTranslation('it')->label(), $translated_title);
Chris@0: $this->assertNotEqual($node->untranslatable_string_field->value, $untranslatable_string);
Chris@0:
Chris@0: $latest_revision_id = $translation->getRevisionId();
Chris@0:
Chris@0: // Now revert the a translation revision preceding the last default
Chris@0: // translation revision again, and check that the desired value was reverted
Chris@0: // but the default translation value was preserved. But in addition the
Chris@0: // untranslated field will be reverted as well.
Chris@0: $this->drupalPostForm($revert_translation_url, ['revert_untranslated_fields' => TRUE], t('Revert'));
Chris@0: $node_storage->resetCache();
Chris@0: /** @var \Drupal\node\NodeInterface $node */
Chris@0: $node = $node_storage->load($node->id());
Chris@0: $this->assertTrue($node->getRevisionId() > $latest_revision_id);
Chris@0: $this->assertEqual($node->label(), $default_translation_title);
Chris@0: $this->assertEqual($node->getTranslation('it')->label(), $translated_title);
Chris@0: $this->assertEqual($node->untranslatable_string_field->value, $untranslatable_string);
Chris@0:
Chris@0: $latest_revision_id = $translation->getRevisionId();
Chris@0:
Chris@0: // Now revert the entity revision to the initial one where the translation
Chris@0: // didn't exist.
Chris@0: $revert_url = Url::fromRoute('node.revision_revert_confirm', [
Chris@0: 'node' => $node->id(),
Chris@0: 'node_revision' => $initial_revision_id,
Chris@0: ]);
Chris@0: $this->drupalPostForm($revert_url, [], t('Revert'));
Chris@0: $node_storage->resetCache();
Chris@0: /** @var \Drupal\node\NodeInterface $node */
Chris@0: $node = $node_storage->load($node->id());
Chris@0: $this->assertTrue($node->getRevisionId() > $latest_revision_id);
Chris@0: $this->assertEqual($node->label(), $initial_title);
Chris@0: $this->assertFalse($node->hasTranslation('it'));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Creates a series of revisions for the specified node.
Chris@0: *
Chris@0: * @param \Drupal\node\NodeInterface $node
Chris@0: * The node object.
Chris@0: * @param $count
Chris@0: * The number of revisions to be created.
Chris@0: */
Chris@0: protected function createRevisions(NodeInterface $node, $count) {
Chris@0: for ($i = 0; $i < $count; $i++) {
Chris@0: $node->title = $this->randomString();
Chris@0: $node->untranslatable_string_field->value = $this->randomString();
Chris@0: $node->setNewRevision(TRUE);
Chris@0: $node->save();
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: }