67c683b383c85c535b3133dac170d073dca75f67
[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
9 /**
10  * Tests the Diff module plugins.
11  *
12  * @group diff
13  */
14 class DiffPluginTest extends DiffPluginTestBase {
15
16   use CommentTestTrait;
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = [
24     'comment',
25   ];
26
27   /**
28    * Adds a text field.
29    *
30    * @param string $field_name
31    *   The machine field name.
32    * @param string $label
33    *   The field label.
34    * @param string $field_type
35    *   The field type.
36    * @param string $widget_type
37    *   The widget type.
38    */
39   protected function addArticleTextField($field_name, $label, $field_type, $widget_type) {
40     // Create a field.
41     $field_storage = FieldStorageConfig::create([
42       'field_name' => $field_name,
43       'entity_type' => 'node',
44       'type' => $field_type,
45     ]);
46     $field_storage->save();
47     FieldConfig::create([
48       'field_storage' => $field_storage,
49       'bundle' => 'article',
50       'label' => $label,
51     ])->save();
52     $this->formDisplay->load('node.article.default')
53       ->setComponent($field_name, ['type' => $widget_type])
54       ->save();
55     $this->viewDisplay->load('node.article.default')
56       ->setComponent($field_name)
57       ->save();
58   }
59
60   /**
61    * Runs all independent tests.
62    */
63   public function testAll() {
64     $this->doTestFieldWithNoPlugin();
65     $this->doTestFieldNoAccess();
66     $this->doTestApplicablePlugin();
67     $this->doTestTrimmingField();
68   }
69
70   /**
71    * Tests the changed field without plugins.
72    */
73   public function doTestFieldWithNoPlugin() {
74     // Create an article.
75     $node = $this->drupalCreateNode([
76       'type' => 'article',
77     ]);
78
79     // Update the article and add a new revision, the "changed" field should be
80     // updated which does not have plugins provided by diff.
81     $edit = array(
82       'revision' => TRUE,
83     );
84     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
85
86     // Check the difference between the last two revisions.
87     $this->clickLink(t('Revisions'));
88     $this->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
89
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'));
94   }
95
96   /**
97    * Tests the access check for a field while comparing revisions.
98    */
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');
102
103     // Create an article.
104     $this->drupalCreateNode([
105       'type' => 'article',
106       'title' => 'Test article access',
107       'field_diff_deny_access' => 'Foo',
108     ]);
109
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);
115     $node->save();
116
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);
124   }
125
126   /**
127    * Tests plugin applicability and weight relevance.
128    *
129    * @covers \Drupal\diff_test\Plugin\diff\Field\TestHeavierTextPlugin
130    * @covers \Drupal\diff_test\Plugin\diff\Field\TestLighterTextPlugin
131    */
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');
137
138     // Create an article, setting values on fields.
139     $node = $this->drupalCreateNode([
140       'type' => 'article',
141       'title' => 'Test article',
142       'test_field' => 'first_nice_applicable',
143       'test_field_lighter' => 'second_nice_applicable',
144       'test_field_non_applicable' => 'not_applicable',
145     ]);
146
147     // Edit the article and update these fields, creating a new revision.
148     $edit = [
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',
152       'revision' => TRUE,
153     ];
154     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
155
156     // Check differences between revisions.
157     $this->clickLink(t('Revisions'));
158     $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
159
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');
164
165     // Check diff for an applicable field of testTextPlugin and
166     // testLighterTextPlugin. The plugin selected for this field should be the
167     // lightest one.
168     $this->assertText('Test Lighter Applicable');
169     $this->assertText('second_nice_lighter_test_plugin');
170     $this->assertText('second_nicer_lighter_test_plugin');
171
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');
176   }
177
178   /**
179    * Tests field content trimming.
180    */
181   public function doTestTrimmingField() {
182     // Create a node.
183     $node = $this->drupalCreateNode([
184       'type' => 'article',
185       'title' => 'test_trim',
186       'body' => '<p>body</p>',
187     ]);
188     // Save a second revision.
189     $node->save();
190
191     // Create a revision adding a new empty line to the body.
192     $node = $this->drupalGetNodeByTitle('test_trim');
193     $edit = [
194       'revision' => TRUE,
195       'body[0][value]' => '<p>body</p>
196 ',
197     ];
198     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
199
200     // Assert the revision summary.
201     $this->drupalGet('node/' . $node->id() . '/revisions');
202     $this->assertText('Changes on: Body');
203
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>');
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->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');
230   }
231
232 }