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