You are here

public function DiffRevisionTest::testRevisionDiffOverview in Diff 8

Tests the revision diff overview.

File

tests/src/Functional/DiffRevisionTest.php, line 35

Class

DiffRevisionTest
Tests the diff revisions overview.

Namespace

Drupal\Tests\diff\Functional

Code

public function testRevisionDiffOverview() {
  $this
    ->drupalPlaceBlock('system_breadcrumb_block');

  // Login as admin with the required permission.
  $this
    ->loginAsAdmin([
    'delete any article content',
  ]);

  // Create an article.
  $title = 'test_title_a';
  $edit = array(
    'title[0][value]' => $title,
    'body[0][value]' => '<p>Revision 1</p>
      <p>first_unique_text</p>
      <p>second_unique_text</p>',
  );

  // Set to published if content moderation is enabled.
  if (\Drupal::moduleHandler()
    ->moduleExists('content_moderation')) {
    $edit['moderation_state[0][state]'] = 'published';
  }
  $this
    ->drupalPostNodeForm('node/add/article', $edit, t('Save and publish'));
  $node = $this
    ->drupalGetNodeByTitle($title);
  $this
    ->drupalGet('node/' . $node
    ->id());

  // Create a second revision, with a revision comment.
  $this
    ->drupalGet('node/add/article');
  $edit = array(
    'body[0][value]' => '<p>Revision 2</p>
      <p>first_unique_text</p>
      <p>second_unique_text</p>',
    'revision' => TRUE,
    'revision_log[0][value]' => 'Revision 2 comment',
  );

  // Set to published if content moderation is enabled.
  if (\Drupal::moduleHandler()
    ->moduleExists('content_moderation')) {
    $edit['moderation_state[0][state]'] = 'published';
  }
  $this
    ->drupalPostNodeForm('node/' . $node
    ->id() . '/edit', $edit, t('Save and keep published'));
  $this
    ->drupalGet('node/' . $node
    ->id());

  // Check the revisions overview.
  $this
    ->clickLink(t('Revisions'));
  $rows = $this
    ->xpath('//tbody/tr');

  // Make sure only two revisions available.
  $this
    ->assertEqual(count($rows), 2);

  // Assert the revision summary.
  $this
    ->assertUniqueText('Revision 2 comment');

  // Compare the revisions in standard mode.
  $this
    ->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
  $this
    ->clickLink('Split fields');

  // Assert breadcrumbs are properly displayed.
  $this
    ->assertRaw('<nav class="breadcrumb"');
  $trail = [
    '' => 'Home',
    "node/" . $node
      ->id() => $node
      ->label(),
    "node/" . $node
      ->id() . "/revisions" => 'Revisions',
  ];
  $this
    ->assertBreadcrumb(NULL, $trail);

  // Extract the changes.
  $this
    ->assertText('Body');
  $rows = $this
    ->xpath('//tbody/tr');
  $head = $this
    ->xpath('//thead/tr');
  $diff_row = $rows[1]
    ->findAll('xpath', '/td');

  // Assert the revision comment.
  $this
    ->assertRaw('diff-revision__item-message">Revision 2 comment');

  // Assert changes made to the body, text 1 changed to 2.
  $this
    ->assertEqual($diff_row[0]
    ->getText(), '1');
  $this
    ->assertEqual($diff_row[1]
    ->getText(), '-');
  $this
    ->assertEqual($diff_row[2]
    ->find('xpath', 'span')
    ->getText(), '1');
  $this
    ->assertEqual(htmlspecialchars_decode(strip_tags($diff_row[2]
    ->getHtml())), '<p>Revision 1</p>');
  $this
    ->assertEqual($diff_row[3]
    ->getText(), '1');
  $this
    ->assertEqual($diff_row[4]
    ->getText(), '+');
  $this
    ->assertEqual($diff_row[5]
    ->find('xpath', 'span')
    ->getText(), '2');
  $this
    ->assertEqual(htmlspecialchars_decode(strip_tags($diff_row[5]
    ->getHtml())), '<p>Revision 2</p>');

  // Compare the revisions in markdown mode.
  $this
    ->clickLink('Strip tags');
  $rows = $this
    ->xpath('//tbody/tr');

  // Assert breadcrumbs are properly displayed.
  $this
    ->assertRaw('<nav class="breadcrumb"');
  $trail = [
    '' => 'Home',
    "node/" . $node
      ->id() => $node
      ->label(),
    "node/" . $node
      ->id() . "/revisions" => 'Revisions',
  ];
  $this
    ->assertBreadcrumb(NULL, $trail);

  // Extract the changes.
  $diff_row = $rows[1]
    ->findAll('xpath', '/td');

  // Assert changes made to the body, text 1 changed to 2.
  $this
    ->assertEqual($diff_row[0]
    ->getText(), '-');
  $this
    ->assertEqual($diff_row[1]
    ->find('xpath', 'span')
    ->getText(), '1');
  $this
    ->assertEqual(htmlspecialchars_decode(trim(strip_tags($diff_row[1]
    ->getHtml()))), 'Revision 1');
  $this
    ->assertEqual($diff_row[2]
    ->getText(), '+');
  $this
    ->assertEqual($diff_row[3]
    ->find('xpath', 'span')
    ->getText(), '2');
  $this
    ->assertEqual(htmlspecialchars_decode(trim(strip_tags($diff_row[3]
    ->getHtml()))), 'Revision 2');

  // Compare the revisions in single column mode.
  $this
    ->clickLink('Unified fields');

  // Assert breadcrumbs are properly displayed.
  $this
    ->assertRaw('<nav class="breadcrumb"');
  $trail = [
    '' => 'Home',
    "node/" . $node
      ->id() => $node
      ->label(),
    "node/" . $node
      ->id() . "/revisions" => 'Revisions',
  ];
  $this
    ->assertBreadcrumb(NULL, $trail);

  // Extract the changes.
  $rows = $this
    ->xpath('//tbody/tr');
  $diff_row = $rows[1]
    ->findAll('xpath', '/td');

  // Assert changes made to the body, text 1 changed to 2.
  $this
    ->assertEqual($diff_row[0]
    ->getText(), '1');
  $this
    ->assertEqual($diff_row[1]
    ->getText(), '');
  $this
    ->assertEqual($diff_row[2]
    ->getText(), '-');
  $this
    ->assertEqual($diff_row[3]
    ->find('xpath', 'span')
    ->getText(), '1');
  $this
    ->assertEqual(htmlspecialchars_decode(strip_tags($diff_row[3]
    ->getHtml())), '<p>Revision 1</p>');
  $diff_row = $rows[2]
    ->findAll('xpath', '/td');
  $this
    ->assertEqual($diff_row[0]
    ->getText(), '');
  $this
    ->assertEqual($diff_row[1]
    ->getText(), '1');
  $this
    ->assertEqual($diff_row[2]
    ->getText(), '+');
  $this
    ->assertEqual($diff_row[3]
    ->find('xpath', 'span')
    ->getText(), '2');
  $this
    ->assertEqual(htmlspecialchars_decode(strip_tags($diff_row[3]
    ->getHtml())), '<p>Revision 2</p>');
  $this
    ->assertUniqueText('first_unique_text');
  $this
    ->assertUniqueText('second_unique_text');
  $diff_row = $rows[3]
    ->findAll('xpath', '/td');
  $this
    ->assertEqual($diff_row[0]
    ->getText(), '2');
  $this
    ->assertEqual($diff_row[1]
    ->getText(), '2');
  $diff_row = $rows[4]
    ->findAll('xpath', '/td');
  $this
    ->assertEqual($diff_row[0]
    ->getText(), '3');
  $this
    ->assertEqual($diff_row[1]
    ->getText(), '3');
  $this
    ->clickLink('Strip tags');

  // Extract the changes.
  $rows = $this
    ->xpath('//tbody/tr');
  $diff_row = $rows[1]
    ->findAll('xpath', '/td');

  // Assert changes made to the body, with strip_tags filter and make sure
  // there are no line numbers.
  $this
    ->assertEqual($diff_row[0]
    ->getText(), '-');
  $this
    ->assertEqual($diff_row[1]
    ->find('xpath', 'span')
    ->getText(), '1');
  $this
    ->assertEqual(htmlspecialchars_decode(trim(strip_tags($diff_row[1]
    ->getHtml()))), 'Revision 1');
  $diff_row = $rows[2]
    ->findAll('xpath', '/td');
  $this
    ->assertEqual($diff_row[0]
    ->getText(), '+');
  $this
    ->assertEqual($diff_row[1]
    ->find('xpath', 'span')
    ->getText(), '2');
  $this
    ->assertEqual(htmlspecialchars_decode(trim(strip_tags($diff_row[1]
    ->getHtml()))), 'Revision 2');
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->clickLink(t('Revisions'));

  // Revert the revision, confirm.
  $this
    ->clickLink(t('Revert'));
  $this
    ->drupalPostForm(NULL, NULL, t('Revert'));
  $this
    ->assertText('Article ' . $title . ' has been reverted to the revision from');

  // Make sure three revisions are available.
  $rows = $this
    ->xpath('//tbody/tr');
  $this
    ->assertEqual(count($rows), 3);

  // Make sure the reverted comment is there.
  $this
    ->assertText('Copy of the revision from');

  // Delete the first revision (last entry in table).
  $this
    ->clickLink(t('Delete'), 0);
  $this
    ->drupalPostForm(NULL, NULL, t('Delete'));
  $this
    ->assertText('of Article ' . $title . ' has been deleted.');

  // Make sure two revisions are available.
  $rows = $this
    ->xpath('//tbody/tr');
  $this
    ->assertEqual(count($rows), 2);

  // Delete one revision so that we are left with only 1 revision.
  $this
    ->clickLink(t('Delete'), 0);
  $this
    ->drupalPostForm(NULL, NULL, t('Delete'));
  $this
    ->assertText('of Article ' . $title . ' has been deleted.');

  // Make sure we only have 1 revision now.
  $rows = $this
    ->xpath('//tbody/tr');
  $this
    ->assertEqual(count($rows), 0);

  // Assert that there are no radio buttons for revision selection.
  $this
    ->assertNoFieldByXPath('//input[@type="radio"]');

  // Assert that there is no submit button.
  $this
    ->assertNoFieldByXPath('//input[@type="submit" and text()="Compare selected revisions"]');

  // Create two new revisions of node.
  $edit = [
    'title[0][value]' => 'new test title',
    'body[0][value]' => '<p>new body</p>',
    'revision_log[0][value]' => 'this revision message will appear twice',
  ];

  // Set to published if content moderation is enabled.
  if (\Drupal::moduleHandler()
    ->moduleExists('content_moderation')) {
    $edit['moderation_state[0][state]'] = 'published';
  }
  $this
    ->drupalPostNodeForm('node/' . $node
    ->id() . '/edit', $edit, 'Save and keep published');
  $edit = [
    'title[0][value]' => 'newer test title',
    'body[0][value]' => '<p>newer body</p>',
    'revision_log[0][value]' => 'this revision message will appear twice',
  ];

  // Set to published if content moderation is enabled.
  if (\Drupal::moduleHandler()
    ->moduleExists('content_moderation')) {
    $edit['moderation_state[0][state]'] = 'published';
  }
  $this
    ->drupalPostNodeForm('node/' . $node
    ->id() . '/edit', $edit, 'Save and keep published');
  $this
    ->clickLink(t('Revisions'));

  // Assert the revision summary.
  $this
    ->assertNoUniqueText('this revision message will appear twice');
  $this
    ->assertText('Copy of the revision from');
  $edit = [
    'radios_left' => 3,
    'radios_right' => 4,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Compare selected revisions'));
  $this
    ->clickLink('Strip tags');

  // Check markdown layout is used when navigating between revisions.
  $assert_session = $this
    ->assertSession();
  $assert_session
    ->elementTextContains('css', 'tr:nth-child(4) td:nth-child(4)', 'new body');
  $this
    ->clickLink('Next change');

  // The filter should be the same as the previous screen.
  $assert_session
    ->elementTextContains('css', 'tr:nth-child(4) td:nth-child(4)', 'newer body');

  // Get the node, create a new revision that is not the current one.
  $node = $this
    ->getNodeByTitle('newer test title');
  $node
    ->setNewRevision(TRUE);
  $node
    ->isDefaultRevision(FALSE);
  if ($node
    ->hasField('moderation_state')) {

    // If testing with content_moderation enabled, set as draft.
    $node->moderation_state = 'draft';
  }
  $node
    ->save();
  $this
    ->drupalGet('node/' . $node
    ->id() . '/revisions');

  // Check that the last revision is not the current one.
  $this
    ->assertLink(t('Set as current revision'));
  $text = $this
    ->xpath('//tbody/tr[2]/td[4]/em');
  $this
    ->assertEqual($text[0]
    ->getText(), 'Current revision');

  // Set the last revision as current.
  $this
    ->clickLink('Set as current revision');
  $this
    ->drupalPostForm(NULL, [], t('Revert'));
  if (\Drupal::moduleHandler()
    ->moduleExists('content_moderation')) {

    // With content moderation, the new revision will not be current.
    // @see https://www.drupal.org/node/2899719
    $text = $this
      ->xpath('//tbody/tr[1]/td[4]/div/div/ul/li/a');
    $this
      ->assertEqual($text[0]
      ->getText(), 'Set as current revision');
  }
  else {

    // Check the last revision is set as current.
    $text = $this
      ->xpath('//tbody/tr[1]/td[4]/em');
    $this
      ->assertEqual($text[0]
      ->getText(), 'Current revision');
    $this
      ->assertNoLink(t('Set as current revision'));
  }
}