View source
<?php
namespace Drupal\Tests\diff\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait;
class DiffRevisionTest extends DiffTestBase {
use AssertBreadcrumbTrait;
use CoreVersionUiTestTrait;
protected $defaultTheme = 'classy';
protected static $modules = [
'diff_test',
'content_translation',
'field_ui',
];
public function testRevisionDiffOverview() {
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->loginAsAdmin([
'delete any article content',
]);
$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>',
);
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());
$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',
);
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());
$this
->clickLink(t('Revisions'));
$rows = $this
->xpath('//tbody/tr');
$this
->assertEqual(count($rows), 2);
$this
->assertUniqueText('Revision 2 comment');
$this
->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
$this
->clickLink('Split fields');
$this
->assertRaw('<nav class="breadcrumb"');
$trail = [
'' => 'Home',
"node/" . $node
->id() => $node
->label(),
"node/" . $node
->id() . "/revisions" => 'Revisions',
];
$this
->assertBreadcrumb(NULL, $trail);
$this
->assertText('Body');
$rows = $this
->xpath('//tbody/tr');
$head = $this
->xpath('//thead/tr');
$diff_row = $rows[1]
->findAll('xpath', '/td');
$this
->assertRaw('diff-revision__item-message">Revision 2 comment');
$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>');
$this
->clickLink('Strip tags');
$rows = $this
->xpath('//tbody/tr');
$this
->assertRaw('<nav class="breadcrumb"');
$trail = [
'' => 'Home',
"node/" . $node
->id() => $node
->label(),
"node/" . $node
->id() . "/revisions" => 'Revisions',
];
$this
->assertBreadcrumb(NULL, $trail);
$diff_row = $rows[1]
->findAll('xpath', '/td');
$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');
$this
->clickLink('Unified fields');
$this
->assertRaw('<nav class="breadcrumb"');
$trail = [
'' => 'Home',
"node/" . $node
->id() => $node
->label(),
"node/" . $node
->id() . "/revisions" => 'Revisions',
];
$this
->assertBreadcrumb(NULL, $trail);
$rows = $this
->xpath('//tbody/tr');
$diff_row = $rows[1]
->findAll('xpath', '/td');
$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');
$rows = $this
->xpath('//tbody/tr');
$diff_row = $rows[1]
->findAll('xpath', '/td');
$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'));
$this
->clickLink(t('Revert'));
$this
->drupalPostForm(NULL, NULL, t('Revert'));
$this
->assertText('Article ' . $title . ' has been reverted to the revision from');
$rows = $this
->xpath('//tbody/tr');
$this
->assertEqual(count($rows), 3);
$this
->assertText('Copy of the revision from');
$this
->clickLink(t('Delete'), 0);
$this
->drupalPostForm(NULL, NULL, t('Delete'));
$this
->assertText('of Article ' . $title . ' has been deleted.');
$rows = $this
->xpath('//tbody/tr');
$this
->assertEqual(count($rows), 2);
$this
->clickLink(t('Delete'), 0);
$this
->drupalPostForm(NULL, NULL, t('Delete'));
$this
->assertText('of Article ' . $title . ' has been deleted.');
$rows = $this
->xpath('//tbody/tr');
$this
->assertEqual(count($rows), 0);
$this
->assertNoFieldByXPath('//input[@type="radio"]');
$this
->assertNoFieldByXPath('//input[@type="submit" and text()="Compare selected revisions"]');
$edit = [
'title[0][value]' => 'new test title',
'body[0][value]' => '<p>new body</p>',
'revision_log[0][value]' => 'this revision message will appear twice',
];
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',
];
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'));
$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');
$assert_session = $this
->assertSession();
$assert_session
->elementTextContains('css', 'tr:nth-child(4) td:nth-child(4)', 'new body');
$this
->clickLink('Next change');
$assert_session
->elementTextContains('css', 'tr:nth-child(4) td:nth-child(4)', 'newer body');
$node = $this
->getNodeByTitle('newer test title');
$node
->setNewRevision(TRUE);
$node
->isDefaultRevision(FALSE);
if ($node
->hasField('moderation_state')) {
$node->moderation_state = 'draft';
}
$node
->save();
$this
->drupalGet('node/' . $node
->id() . '/revisions');
$this
->assertLink(t('Set as current revision'));
$text = $this
->xpath('//tbody/tr[2]/td[4]/em');
$this
->assertEqual($text[0]
->getText(), 'Current revision');
$this
->clickLink('Set as current revision');
$this
->drupalPostForm(NULL, [], t('Revert'));
if (\Drupal::moduleHandler()
->moduleExists('content_moderation')) {
$text = $this
->xpath('//tbody/tr[1]/td[4]/div/div/ul/li/a');
$this
->assertEqual($text[0]
->getText(), 'Set as current revision');
}
else {
$text = $this
->xpath('//tbody/tr[1]/td[4]/em');
$this
->assertEqual($text[0]
->getText(), 'Current revision');
$this
->assertNoLink(t('Set as current revision'));
}
}
public function testOverviewPager() {
$this
->config('diff.settings')
->set('general_settings.revision_pager_limit', 10)
->save();
$this
->loginAsAdmin([
'view article revisions',
]);
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
for ($i = 0; $i < 11; $i++) {
$edit = [
'revision' => TRUE,
'body[0][value]' => 'change: ' . $i,
];
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
}
$this
->drupalGet('node/' . $node
->id() . '/revisions');
$element = $this
->xpath('//*[@id="edit-node-revisions-table"]/tbody/tr');
$this
->assertEqual(count($element), 10);
$this
->assertRaw('page=1');
$this
->clickLink('Next page');
$element = $this
->xpath('//*[@id="edit-node-revisions-table"]/tbody/tr');
$this
->assertEqual(count($element), 2);
$this
->assertRaw('page=0');
$this
->clickLink('Previous page');
}
public function testRevisionOverviewErrorMessages() {
$language = ConfigurableLanguage::createFromLangcode('de');
$language
->save();
$this
->loginAsAdmin([
'administer node form display',
'administer languages',
'administer content translation',
'create content translations',
'translate any entity',
]);
$edit = [
'entity_types[node]' => TRUE,
'settings[node][article][translatable]' => TRUE,
'settings[node][article][settings][language][language_alterable]' => TRUE,
];
$this
->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
$title = 'test_title_b';
$edit = [
'title[0][value]' => $title,
'body[0][value]' => '<p>Revision 1</p>',
];
$this
->drupalPostNodeForm('node/add/article', $edit, t('Save and publish'));
$node = $this
->drupalGetNodeByTitle($title);
$revision1 = $node
->getRevisionId();
$edit = [
'langcode[0][value]' => 'de',
'body[0][value]' => '<p>Revision 2</p>',
'revision' => TRUE,
];
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$this
->clickLink(t('Revisions'));
$rows = $this
->xpath('//tbody/tr');
$this
->assertEqual(count($rows), 1);
$this
->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
$this
->assertText('Multiple revisions are needed for comparison.');
$edit = [
'langcode[0][value]' => 'en',
'body[0][value]' => '<p>Revision 3</p>',
'revision' => TRUE,
];
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$node = $this
->drupalGetNodeByTitle($title, TRUE);
$revision3 = $node
->getRevisionId();
$this
->clickLink(t('Revisions'));
$rows = $this
->xpath('//tbody/tr');
$this
->assertEqual(count($rows), 2);
$this
->assertNoFieldChecked('edit-node-revisions-table-0-select-column-one');
$this
->assertFieldChecked('edit-node-revisions-table-0-select-column-two');
$this
->assertNoFieldChecked('edit-node-revisions-table-1-select-column-one');
$this
->assertNoFieldChecked('edit-node-revisions-table-1-select-column-two');
$this
->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
$this
->assertText('Select two revisions to compare.');
$edit = [
'radios_left' => $revision3,
'radios_right' => $revision3,
];
$this
->drupalPostForm('/node/' . $node
->id() . '/revisions', $edit, 'Compare selected revisions');
$this
->assertText('Select different revisions to compare.');
$edit = [
'radios_left' => $revision3,
'radios_right' => $revision1,
];
$this
->drupalPostForm('/node/' . $node
->id() . '/revisions', $edit, 'Compare selected revisions');
$this
->assertLinkByHref('node/' . $node
->id() . '/revisions/view/' . $revision1 . '/' . $revision3);
}
public function testEntityReference() {
$this
->loginAsAdmin([
'administer node fields',
]);
$this
->drupalPostForm('admin/structure/types/manage/article/fields/add-field', [
'new_storage_type' => 'field_ui:entity_reference:node',
'label' => 'Content reference test',
'field_name' => 'content',
], t('Save and continue'));
$title = 'test_title_c';
$edit = [
'title[0][value]' => $title,
'body[0][value]' => '<p>First article</p>',
];
if (\Drupal::moduleHandler()
->moduleExists('content_moderation')) {
$edit['moderation_state[0][state]'] = 'published';
}
$this
->drupalPostNodeForm('node/add/article', $edit, t('Save and publish'));
$node_one = $this
->drupalGetNodeByTitle($title);
$title = 'test_title_d';
$edit = [
'title[0][value]' => $title,
'body[0][value]' => '<p>Second article</p>',
];
if (\Drupal::moduleHandler()
->moduleExists('content_moderation')) {
$edit['moderation_state[0][state]'] = 'published';
}
$this
->drupalPostNodeForm('node/add/article', $edit, t('Save and publish'));
$node_two = $this
->drupalGetNodeByTitle($title);
$edit = [
'body[0][value]' => '<p>First Revision</p>',
'field_content[0][target_id]' => $node_two
->getTitle(),
'revision' => TRUE,
];
if (\Drupal::moduleHandler()
->moduleExists('content_moderation')) {
$edit['moderation_state[0][state]'] = 'published';
}
$this
->drupalPostNodeForm('node/' . $node_one
->id() . '/edit', $edit, t('Save and keep published'));
$node_two
->delete();
$this
->drupalGet('/node/' . $node_one
->id());
$this
->clickLink(t('Revisions'));
$this
->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
$this
->assertResponse(200);
}
}