Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / diff / src / Tests / DiffPluginTest.php
1 <?php
2
3 namespace Drupal\diff\Tests;
4
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;
9
10 /**
11  * Tests the Diff module plugins.
12  *
13  * @group diff
14  */
15 class DiffPluginTest extends DiffPluginTestBase {
16
17   use CommentTestTrait;
18   use CoreVersionUiTestTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = [
26     'comment',
27   ];
28
29   /**
30    * Adds a text field.
31    *
32    * @param string $field_name
33    *   The machine field name.
34    * @param string $label
35    *   The field label.
36    * @param string $field_type
37    *   The field type.
38    * @param string $widget_type
39    *   The widget type.
40    */
41   protected function addArticleTextField($field_name, $label, $field_type, $widget_type) {
42     // Create a field.
43     $field_storage = FieldStorageConfig::create([
44       'field_name' => $field_name,
45       'entity_type' => 'node',
46       'type' => $field_type,
47     ]);
48     $field_storage->save();
49     FieldConfig::create([
50       'field_storage' => $field_storage,
51       'bundle' => 'article',
52       'label' => $label,
53     ])->save();
54     $this->formDisplay->load('node.article.default')
55       ->setComponent($field_name, ['type' => $widget_type])
56       ->save();
57     $this->viewDisplay->load('node.article.default')
58       ->setComponent($field_name)
59       ->save();
60   }
61
62   /**
63    * Runs all independent tests.
64    */
65   public function testAll() {
66     $this->doTestFieldWithNoPlugin();
67     $this->doTestFieldNoAccess();
68     $this->doTestApplicablePlugin();
69     $this->doTestTrimmingField();
70   }
71
72   /**
73    * Tests the changed field without plugins.
74    */
75   public function doTestFieldWithNoPlugin() {
76     // Create an article.
77     $node = $this->drupalCreateNode([
78       'type' => 'article',
79     ]);
80
81     // Update the article and add a new revision, the "changed" field should be
82     // updated which does not have plugins provided by diff.
83     $edit = [
84       'revision' => TRUE,
85       'body[0][value]' => 'change',
86     ];
87     $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
88
89     // Check the difference between the last two revisions.
90     $this->clickLink(t('Revisions'));
91     $this->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
92
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'));
97   }
98
99   /**
100    * Tests the access check for a field while comparing revisions.
101    */
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');
105
106     // Create an article.
107     $this->drupalCreateNode([
108       'type' => 'article',
109       'title' => 'Test article access',
110       'field_diff_deny_access' => 'Foo',
111     ]);
112
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);
118     $node->save();
119
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);
127   }
128
129   /**
130    * Tests plugin applicability and weight relevance.
131    *
132    * @covers \Drupal\diff_test\Plugin\diff\Field\TestHeavierTextPlugin
133    * @covers \Drupal\diff_test\Plugin\diff\Field\TestLighterTextPlugin
134    */
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');
140
141     // Create an article, setting values on fields.
142     $node = $this->drupalCreateNode([
143       'type' => 'article',
144       'title' => 'Test article',
145       'test_field' => 'first_nice_applicable',
146       'test_field_lighter' => 'second_nice_applicable',
147       'test_field_non_applicable' => 'not_applicable',
148     ]);
149
150     // Edit the article and update these fields, creating a new revision.
151     $edit = [
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',
155       'revision' => TRUE,
156     ];
157     $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
158
159     // Check differences between revisions.
160     $this->clickLink(t('Revisions'));
161     $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
162
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');
167
168     // Check diff for an applicable field of testTextPlugin and
169     // testLighterTextPlugin. The plugin selected for this field should be the
170     // lightest one.
171     $this->assertText('Test Lighter Applicable');
172     $this->assertText('second_nice_lighter_test_plugin');
173     $this->assertText('second_nicer_lighter_test_plugin');
174
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');
179   }
180
181   /**
182    * Tests field content trimming.
183    */
184   public function doTestTrimmingField() {
185     // Create a node.
186     $node = $this->drupalCreateNode([
187       'type' => 'article',
188       'title' => 'test_trim',
189       'body' => '<p>body</p>',
190     ]);
191     // Save a second revision.
192     $node->save();
193
194     // Create a revision adding a new empty line to the body.
195     $node = $this->drupalGetNodeByTitle('test_trim');
196     $edit = [
197       'revision' => TRUE,
198       'body[0][value]' => '<p>body</p>
199 ',
200     ];
201     $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
202
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>');
211
212     // Create a new revision and update the body.
213     $edit = [
214       'revision' => TRUE,
215       'body[0][value]' => '<p>body</p>
216
217 <p>body_new</p>
218 ',
219     ];
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');
230   }
231
232 }