3 namespace Drupal\diff\Tests;
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\diff\Functional\CoreVersionUiTestTrait;
11 * Tests the Diff module plugins.
15 class DiffPluginTest extends DiffPluginTestBase {
18 use CoreVersionUiTestTrait;
25 public static $modules = [
32 * @param string $field_name
33 * The machine field name.
34 * @param string $label
36 * @param string $field_type
38 * @param string $widget_type
41 protected function addArticleTextField($field_name, $label, $field_type, $widget_type) {
43 $field_storage = FieldStorageConfig::create([
44 'field_name' => $field_name,
45 'entity_type' => 'node',
46 'type' => $field_type,
48 $field_storage->save();
50 'field_storage' => $field_storage,
51 'bundle' => 'article',
54 $this->formDisplay->load('node.article.default')
55 ->setComponent($field_name, ['type' => $widget_type])
57 $this->viewDisplay->load('node.article.default')
58 ->setComponent($field_name)
63 * Runs all independent tests.
65 public function testAll() {
66 $this->doTestFieldWithNoPlugin();
67 $this->doTestFieldNoAccess();
68 $this->doTestApplicablePlugin();
69 $this->doTestTrimmingField();
73 * Tests the changed field without plugins.
75 public function doTestFieldWithNoPlugin() {
77 $node = $this->drupalCreateNode([
81 // Update the article and add a new revision, the "changed" field should be
82 // updated which does not have plugins provided by diff.
85 'body[0][value]' => 'change',
87 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
89 // Check the difference between the last two revisions.
90 $this->clickLink(t('Revisions'));
91 $this->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
93 // "changed" field is not displayed since there is no plugin for it. This
94 // should not break the revisions comparison display.
95 $this->assertResponse(200);
96 $this->assertLink(t('Split fields'));
100 * Tests the access check for a field while comparing revisions.
102 public function doTestFieldNoAccess() {
103 // Add a text and a text field to article.
104 $this->addArticleTextField('field_diff_deny_access', 'field_diff_deny_access', 'string', 'string_textfield');
106 // Create an article.
107 $this->drupalCreateNode([
109 'title' => 'Test article access',
110 'field_diff_deny_access' => 'Foo',
113 // Create a revision of the article.
114 $node = $this->getNodeByTitle('Test article access');
115 $node->setTitle('Test article no access');
116 $node->set('field_diff_deny_access', 'Fighters');
117 $node->setNewRevision(TRUE);
120 // Check the "Text Field No Access" field is not displayed.
121 $this->drupalGet('node/' . $node->id() . '/revisions');
122 $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
123 $this->assertResponse(200);
124 $this->assertNoText('field_diff_deny_access');
125 $rows = $this->xpath('//tbody/tr');
126 $this->assertEqual(count($rows), 2);
130 * Tests plugin applicability and weight relevance.
132 * @covers \Drupal\diff_test\Plugin\diff\Field\TestHeavierTextPlugin
133 * @covers \Drupal\diff_test\Plugin\diff\Field\TestLighterTextPlugin
135 public function doTestApplicablePlugin() {
136 // Add three text fields to the article.
137 $this->addArticleTextField('test_field', 'Test Applicable', 'text', 'text_textfield');
138 $this->addArticleTextField('test_field_lighter', 'Test Lighter Applicable', 'text', 'text_textfield');
139 $this->addArticleTextField('test_field_non_applicable', 'Test Not Applicable', 'text', 'text_textfield');
141 // Create an article, setting values on fields.
142 $node = $this->drupalCreateNode([
144 'title' => 'Test article',
145 'test_field' => 'first_nice_applicable',
146 'test_field_lighter' => 'second_nice_applicable',
147 'test_field_non_applicable' => 'not_applicable',
150 // Edit the article and update these fields, creating a new revision.
152 'test_field[0][value]' => 'first_nicer_applicable',
153 'test_field_lighter[0][value]' => 'second_nicer_applicable',
154 'test_field_non_applicable[0][value]' => 'nicer_not_applicable',
157 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
159 // Check differences between revisions.
160 $this->clickLink(t('Revisions'));
161 $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
163 // Check diff for an applicable field of testTextPlugin.
164 $this->assertText('Test Applicable');
165 $this->assertText('first_nice_heavier_test_plugin');
166 $this->assertText('first_nicer_heavier_test_plugin');
168 // Check diff for an applicable field of testTextPlugin and
169 // testLighterTextPlugin. The plugin selected for this field should be the
171 $this->assertText('Test Lighter Applicable');
172 $this->assertText('second_nice_lighter_test_plugin');
173 $this->assertText('second_nicer_lighter_test_plugin');
175 // Check diff for a non applicable field of both test plugins.
176 $this->assertText('Test Not Applicable');
177 $this->assertText('not_applicable');
178 $this->assertText('nicer_not_applicable');
182 * Tests field content trimming.
184 public function doTestTrimmingField() {
186 $node = $this->drupalCreateNode([
188 'title' => 'test_trim',
189 'body' => '<p>body</p>',
191 // Save a second revision.
194 // Create a revision adding a new empty line to the body.
195 $node = $this->drupalGetNodeByTitle('test_trim');
198 'body[0][value]' => '<p>body</p>
201 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
203 // Assert the revision comparison.
204 $this->drupalGet('node/' . $node->id() . '/revisions');
205 $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
206 $this->assertNoText('No visible changes.');
207 $rows = $this->xpath('//tbody/tr');
208 $diff_row = $rows[1]->td;
209 $this->assertEqual(count($rows), 3);
210 $this->assertEqual(htmlspecialchars_decode(strip_tags($diff_row[2]->asXML())), '<p>body</p>');
212 // Create a new revision and update the body.
215 'body[0][value]' => '<p>body</p>
220 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
221 $this->drupalGet('node/' . $node->id() . '/revisions');
222 $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
223 $this->assertNoText('No visible changes.');
224 // Assert that empty rows also show a line number.
225 $rows = $this->xpath('//tbody/tr');
226 $this->assertEqual(count($rows), 5);
227 $diff_row = $rows[4]->td;
228 $this->assertEqual(htmlspecialchars_decode(strip_tags($diff_row[3]->asXML())), '4');
229 $this->assertEqual(htmlspecialchars_decode(strip_tags($diff_row[0]->asXML())), '2');