3 namespace Drupal\diff\Tests;
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
10 * Tests the Diff module plugins.
14 class DiffPluginTest extends DiffPluginTestBase {
23 public static $modules = [
30 * @param string $field_name
31 * The machine field name.
32 * @param string $label
34 * @param string $field_type
36 * @param string $widget_type
39 protected function addArticleTextField($field_name, $label, $field_type, $widget_type) {
41 $field_storage = FieldStorageConfig::create([
42 'field_name' => $field_name,
43 'entity_type' => 'node',
44 'type' => $field_type,
46 $field_storage->save();
48 'field_storage' => $field_storage,
49 'bundle' => 'article',
52 $this->formDisplay->load('node.article.default')
53 ->setComponent($field_name, ['type' => $widget_type])
55 $this->viewDisplay->load('node.article.default')
56 ->setComponent($field_name)
61 * Runs all independent tests.
63 public function testAll() {
64 $this->doTestFieldWithNoPlugin();
65 $this->doTestFieldNoAccess();
66 $this->doTestApplicablePlugin();
67 $this->doTestTrimmingField();
71 * Tests the changed field without plugins.
73 public function doTestFieldWithNoPlugin() {
75 $node = $this->drupalCreateNode([
79 // Update the article and add a new revision, the "changed" field should be
80 // updated which does not have plugins provided by diff.
84 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
86 // Check the difference between the last two revisions.
87 $this->clickLink(t('Revisions'));
88 $this->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
90 // "changed" field is not displayed since there is no plugin for it. This
91 // should not break the revisions comparison display.
92 $this->assertResponse(200);
93 $this->assertLink(t('Split fields'));
97 * Tests the access check for a field while comparing revisions.
99 public function doTestFieldNoAccess() {
100 // Add a text and a text field to article.
101 $this->addArticleTextField('field_diff_deny_access', 'field_diff_deny_access', 'string', 'string_textfield');
103 // Create an article.
104 $this->drupalCreateNode([
106 'title' => 'Test article access',
107 'field_diff_deny_access' => 'Foo',
110 // Create a revision of the article.
111 $node = $this->getNodeByTitle('Test article access');
112 $node->setTitle('Test article no access');
113 $node->set('field_diff_deny_access', 'Fighters');
114 $node->setNewRevision(TRUE);
117 // Check the "Text Field No Access" field is not displayed.
118 $this->drupalGet('node/' . $node->id() . '/revisions');
119 $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
120 $this->assertResponse(200);
121 $this->assertNoText('field_diff_deny_access');
122 $rows = $this->xpath('//tbody/tr');
123 $this->assertEqual(count($rows), 2);
127 * Tests plugin applicability and weight relevance.
129 * @covers \Drupal\diff_test\Plugin\diff\Field\TestHeavierTextPlugin
130 * @covers \Drupal\diff_test\Plugin\diff\Field\TestLighterTextPlugin
132 public function doTestApplicablePlugin() {
133 // Add three text fields to the article.
134 $this->addArticleTextField('test_field', 'Test Applicable', 'text', 'text_textfield');
135 $this->addArticleTextField('test_field_lighter', 'Test Lighter Applicable', 'text', 'text_textfield');
136 $this->addArticleTextField('test_field_non_applicable', 'Test Not Applicable', 'text', 'text_textfield');
138 // Create an article, setting values on fields.
139 $node = $this->drupalCreateNode([
141 'title' => 'Test article',
142 'test_field' => 'first_nice_applicable',
143 'test_field_lighter' => 'second_nice_applicable',
144 'test_field_non_applicable' => 'not_applicable',
147 // Edit the article and update these fields, creating a new revision.
149 'test_field[0][value]' => 'first_nicer_applicable',
150 'test_field_lighter[0][value]' => 'second_nicer_applicable',
151 'test_field_non_applicable[0][value]' => 'nicer_not_applicable',
154 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
156 // Check differences between revisions.
157 $this->clickLink(t('Revisions'));
158 $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
160 // Check diff for an applicable field of testTextPlugin.
161 $this->assertText('Test Applicable');
162 $this->assertText('first_nice_heavier_test_plugin');
163 $this->assertText('first_nicer_heavier_test_plugin');
165 // Check diff for an applicable field of testTextPlugin and
166 // testLighterTextPlugin. The plugin selected for this field should be the
168 $this->assertText('Test Lighter Applicable');
169 $this->assertText('second_nice_lighter_test_plugin');
170 $this->assertText('second_nicer_lighter_test_plugin');
172 // Check diff for a non applicable field of both test plugins.
173 $this->assertText('Test Not Applicable');
174 $this->assertText('not_applicable');
175 $this->assertText('nicer_not_applicable');
179 * Tests field content trimming.
181 public function doTestTrimmingField() {
183 $node = $this->drupalCreateNode([
185 'title' => 'test_trim',
186 'body' => '<p>body</p>',
188 // Save a second revision.
191 // Create a revision adding a new empty line to the body.
192 $node = $this->drupalGetNodeByTitle('test_trim');
195 'body[0][value]' => '<p>body</p>
198 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
200 // Assert the revision summary.
201 $this->drupalGet('node/' . $node->id() . '/revisions');
202 $this->assertText('Changes on: Body');
204 // Assert the revision comparison.
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->drupalPostForm('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');