View source
<?php
namespace Drupal\Tests\diff\Functional;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class DiffPluginTest extends DiffPluginTestBase {
use CommentTestTrait;
use CoreVersionUiTestTrait;
protected static $modules = [
'comment',
];
protected function addArticleTextField($field_name, $label, $field_type, $widget_type) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => $field_type,
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
'label' => $label,
])
->save();
$this->formDisplay
->load('node.article.default')
->setComponent($field_name, [
'type' => $widget_type,
])
->save();
$this->viewDisplay
->load('node.article.default')
->setComponent($field_name)
->save();
}
public function testFieldWithNoPlugin() {
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$edit = [
'revision' => TRUE,
'body[0][value]' => 'change',
];
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$this
->clickLink(t('Revisions'));
$this
->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
$this
->assertResponse(200);
$this
->assertLink(t('Split fields'));
}
public function testFieldNoAccess() {
$this
->addArticleTextField('field_diff_deny_access', 'field_diff_deny_access', 'string', 'string_textfield');
$this
->drupalCreateNode([
'type' => 'article',
'title' => 'Test article access',
'field_diff_deny_access' => 'Foo',
]);
$node = $this
->getNodeByTitle('Test article access');
$node
->setTitle('Test article no access');
$node
->set('field_diff_deny_access', 'Fighters');
$node
->setNewRevision(TRUE);
$node
->save();
$this
->drupalGet('node/' . $node
->id() . '/revisions');
$this
->drupalPostForm(NULL, [], t('Compare selected revisions'));
$this
->assertResponse(200);
$this
->assertNoText('field_diff_deny_access');
$rows = $this
->xpath('//tbody/tr');
$this
->assertEqual(count($rows), 2);
}
public function testApplicablePlugin() {
$this
->addArticleTextField('test_field', 'Test Applicable', 'text', 'text_textfield');
$this
->addArticleTextField('test_field_lighter', 'Test Lighter Applicable', 'text', 'text_textfield');
$this
->addArticleTextField('test_field_non_applicable', 'Test Not Applicable', 'text', 'text_textfield');
$node = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'Test article',
'test_field' => 'first_nice_applicable',
'test_field_lighter' => 'second_nice_applicable',
'test_field_non_applicable' => 'not_applicable',
]);
$edit = [
'test_field[0][value]' => 'first_nicer_applicable',
'test_field_lighter[0][value]' => 'second_nicer_applicable',
'test_field_non_applicable[0][value]' => 'nicer_not_applicable',
'revision' => TRUE,
];
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$this
->clickLink(t('Revisions'));
$this
->drupalPostForm(NULL, [], t('Compare selected revisions'));
$this
->assertText('Test Applicable');
$this
->assertText('first_nice_heavier_test_plugin');
$this
->assertText('first_nicer_heavier_test_plugin');
$this
->assertText('Test Lighter Applicable');
$this
->assertText('second_nice_lighter_test_plugin');
$this
->assertText('second_nicer_lighter_test_plugin');
$this
->assertText('Test Not Applicable');
$this
->assertText('not_applicable');
$this
->assertText('nicer_not_applicable');
}
public function testTrimmingField() {
$node = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'test_trim',
'body' => '<p>body</p>',
]);
$node
->save();
$node = $this
->drupalGetNodeByTitle('test_trim');
$edit = [
'revision' => TRUE,
'body[0][value]' => '<p>body</p>
',
];
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$this
->drupalGet('node/' . $node
->id() . '/revisions');
$this
->drupalPostForm(NULL, [], t('Compare selected revisions'));
$this
->assertNoText('No visible changes.');
$rows = $this
->xpath('//tbody/tr');
$diff_row = $rows[1]
->findAll('xpath', '/td');
$this
->assertEqual(count($rows), 3);
$this
->assertEqual(htmlspecialchars_decode(strip_tags($diff_row[2]
->getHtml())), '<p>body</p>');
$edit = [
'revision' => TRUE,
'body[0][value]' => '<p>body</p>
<p>body_new</p>
',
];
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$this
->drupalGet('node/' . $node
->id() . '/revisions');
$this
->drupalPostForm(NULL, [], t('Compare selected revisions'));
$this
->assertNoText('No visible changes.');
$rows = $this
->xpath('//tbody/tr');
$this
->assertEqual(count($rows), 5);
$diff_row = $rows[4]
->findAll('xpath', '/td');
$this
->assertEqual(htmlspecialchars_decode(strip_tags($diff_row[3]
->getHtml())), '4');
$this
->assertEqual(htmlspecialchars_decode(strip_tags($diff_row[0]
->getHtml())), '2');
}
}