Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / diff / src / Tests / DiffPluginVariousTest.php
1 <?php
2
3 namespace Drupal\diff\Tests;
4
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\comment\Tests\CommentTestTrait;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\link\LinkItemInterface;
10 use Drupal\Tests\diff\Functional\CoreVersionUiTestTrait;
11
12 /**
13  * Tests the Diff module plugins.
14  *
15  * @group diff
16  */
17 class DiffPluginVariousTest extends DiffPluginTestBase {
18
19   use CommentTestTrait;
20   use CoreVersionUiTestTrait;
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = [
28     'comment',
29   ];
30
31   /**
32    * Tests the module plugins.
33    */
34   public function testPlugins() {
35     $this->doTestCommentPlugin();
36     $this->doTestCorePlugin();
37     $this->doTestCorePluginTimestampField();
38     $this->doTestLinkPlugin();
39     $this->doTestListPlugin();
40     $this->doTestTextPlugin();
41     $this->doTestTextWithSummaryPlugin();
42   }
43
44   /**
45    * Adds a text field.
46    *
47    * @param string $field_name
48    *   The machine field name.
49    * @param string $label
50    *   The field label.
51    * @param string $field_type
52    *   The field type.
53    * @param string $widget_type
54    *   The widget type.
55    */
56   protected function addArticleTextField($field_name, $label, $field_type, $widget_type) {
57     // Create a field.
58     $field_storage = FieldStorageConfig::create([
59       'field_name' => $field_name,
60       'entity_type' => 'node',
61       'type' => $field_type,
62     ]);
63     $field_storage->save();
64     FieldConfig::create([
65       'field_storage' => $field_storage,
66       'bundle' => 'article',
67       'label' => $label,
68     ])->save();
69     $this->formDisplay->load('node.article.default')
70       ->setComponent($field_name, ['type' => $widget_type])
71       ->save();
72     $this->viewDisplay->load('node.article.default')
73       ->setComponent($field_name)
74       ->save();
75   }
76
77   /**
78    * Tests the comment plugin.
79    *
80    * @covers \Drupal\diff\Plugin\diff\Field\CommentFieldBuilder
81    */
82   public function doTestCommentPlugin() {
83     // Add the comment field to articles.
84     $this->addDefaultCommentField('node', 'article');
85
86     // Create an article with comments enabled..
87     $title = 'Sample article';
88     $edit = array(
89       'title[0][value]' => $title,
90       'body[0][value]' => '<p>Revision 1</p>',
91       'comment[0][status]' => CommentItemInterface::OPEN,
92     );
93     $this->drupalPostNodeForm('node/add/article', $edit, t('Save and publish'));
94     $node = $this->drupalGetNodeByTitle($title);
95
96     // Edit the article and close its comments.
97     $edit = array(
98       'comment[0][status]' => CommentItemInterface::CLOSED,
99       'revision' => TRUE,
100     );
101     $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
102
103     // Check the difference between the last two revisions.
104     $this->clickLink(t('Revisions'));
105     $this->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
106     $this->assertText('Comments');
107     $this->assertText('Comments for this entity are open.');
108     $this->assertText('Comments for this entity are closed.');
109   }
110
111   /**
112    * Tests the Core plugin.
113    *
114    * @covers \Drupal\diff\Plugin\diff\Field\CoreFieldBuilder
115    */
116   public function doTestCorePlugin() {
117     // Add an email field (supported by the Diff core plugin) to the Article
118     // content type.
119     $field_name = 'field_email';
120     $field_storage = FieldStorageConfig::create([
121       'field_name' => $field_name,
122       'entity_type' => 'node',
123       'type' => 'email',
124     ]);
125     $field_storage->save();
126     FieldConfig::create([
127       'field_storage' => $field_storage,
128       'bundle' => 'article',
129       'label' => 'Email',
130     ])->save();
131
132     // Add the email field to the article form.
133     $this->formDisplay->load('node.article.default')
134       ->setComponent($field_name, ['type' => 'email_default'])
135       ->save();
136
137     // Add the email field to the default display.
138     $this->viewDisplay->load('node.article.default')
139       ->setComponent($field_name, ['type' => 'basic_string'])
140       ->save();
141
142     // Create an article with an email.
143     $node = $this->drupalCreateNode([
144       'type' => 'article',
145       'field_email' => 'foo@example.com',
146     ]);
147
148     // Edit the article and change the email.
149     $edit = array(
150       'field_email[0][value]' => 'bar@example.com',
151       'revision' => TRUE,
152     );
153     $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
154
155     // Check the difference between the last two revisions.
156     $this->clickLink(t('Revisions'));
157     $this->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
158     $this->assertText('Email');
159     $this->assertText('foo@example.com');
160     $this->assertText('bar@example.com');
161   }
162
163   /**
164    * Tests the Core plugin with a timestamp field.
165    *
166    * @covers \Drupal\diff\Plugin\diff\Field\CoreFieldBuilder
167    */
168   public function doTestCorePluginTimestampField() {
169     // Add a timestamp field (supported by the Diff core plugin) to the Article
170     // content type.
171     $field_name = 'field_timestamp';
172     $field_storage = FieldStorageConfig::create([
173       'field_name' => $field_name,
174       'entity_type' => 'node',
175       'type' => 'timestamp',
176     ]);
177     $field_storage->save();
178     FieldConfig::create([
179       'field_storage' => $field_storage,
180       'bundle' => 'article',
181       'label' => 'Timestamp test',
182     ])->save();
183
184     // Add the timestamp field to the article form.
185     $this->formDisplay->load('node.article.default')
186       ->setComponent($field_name, ['type' => 'datetime_timestamp'])
187       ->save();
188
189     // Add the timestamp field to the default display.
190     $this->viewDisplay->load('node.article.default')
191       ->setComponent($field_name, ['type' => 'timestamp'])
192       ->save();
193
194     $old_timestamp = '321321321';
195     $new_timestamp = '123123123';
196
197     // Create an article with an timestamp.
198     $this->drupalCreateNode([
199       'title' => 'timestamp_test',
200       'type' => 'article',
201       'field_timestamp' => $old_timestamp,
202     ]);
203
204     // Create a new revision with an updated timestamp.
205     $node = $this->drupalGetNodeByTitle('timestamp_test');
206     $node->field_timestamp = $new_timestamp;
207     $node->setNewRevision(TRUE);
208     $node->save();
209
210     // Compare the revisions.
211     $this->drupalGet('node/' . $node->id() . '/revisions');
212     $this->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
213
214     // Assert that the timestamp field does not show a unix time format.
215     $this->assertText('Timestamp test');
216     $date_formatter = \Drupal::service('date.formatter');
217     $this->assertText($date_formatter->format($old_timestamp));
218     $this->assertText($date_formatter->format($new_timestamp));
219   }
220
221   /**
222    * Tests the Link plugin.
223    *
224    * @covers \Drupal\diff\Plugin\diff\Field\LinkFieldBuilder
225    */
226   public function doTestLinkPlugin() {
227     // Add a link field to the article content type.
228     $field_name = 'field_link';
229     $field_storage = FieldStorageConfig::create([
230       'field_name' => $field_name,
231       'entity_type' => 'node',
232       'type' => 'link',
233     ]);
234     $field_storage->save();
235     FieldConfig::create([
236       'field_storage' => $field_storage,
237       'bundle' => 'article',
238       'label' => 'Link',
239       'settings' => array(
240         'title' => DRUPAL_OPTIONAL,
241         'link_type' => LinkItemInterface::LINK_GENERIC,
242       ),
243     ])->save();
244     $this->formDisplay->load('node.article.default')
245       ->setComponent($field_name, [
246         'type' => 'link_default',
247         'settings' => [
248           'placeholder_url' => 'http://example.com',
249         ],
250       ])
251       ->save();
252     $this->viewDisplay->load('node.article.default')
253       ->setComponent($field_name, ['type' => 'link'])
254       ->save();
255
256     // Enable the comparison of the link's title field.
257     $config = \Drupal::configFactory()->getEditable('diff.plugins');
258     $settings['compare_title'] = TRUE;
259     $config->set('fields.node.field_link.type', 'link_field_diff_builder');
260     $config->set('fields.node.field_link.settings', $settings);
261     $config->save();
262
263     // Create an article, setting values on the link field.
264     $node = $this->drupalCreateNode([
265       'type' => 'article',
266       'title' => 'Test article',
267       'field_link' => [
268         'title' => 'Google',
269         'uri' => 'http://www.google.com',
270       ],
271     ]);
272
273     // Update the link field.
274     $edit = [
275       'field_link[0][title]' => 'Guguel',
276       'field_link[0][uri]' => 'http://www.google.es',
277       'revision' => TRUE,
278     ];
279     $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
280
281     // Check differences between revisions.
282     $this->clickLink(t('Revisions'));
283     $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
284     $this->assertText('Link');
285     $this->assertText('Google');
286     $this->assertText('http://www.google.com');
287     $this->assertText('Guguel');
288     $this->assertText('http://www.google.es');
289   }
290
291   /**
292    * Tests the List plugin.
293    *
294    * @covers \Drupal\diff\Plugin\diff\Field\ListFieldBuilder
295    */
296   public function doTestListPlugin() {
297     // Add a list field to the article content type.
298     $field_name = 'field_list';
299     $field_storage = FieldStorageConfig::create([
300       'field_name' => $field_name,
301       'entity_type' => 'node',
302       'type' => 'list_string',
303       'cardinality' => 1,
304       'settings' => [
305         'allowed_values' => [
306           'value_a' => 'Value A',
307           'value_b' => 'Value B',
308         ],
309       ],
310     ]);
311     $field_storage->save();
312
313     FieldConfig::create([
314       'field_name' => $field_name,
315       'entity_type' => 'node',
316       'bundle' => 'article',
317       'required' => FALSE,
318       'label' => 'List',
319     ])->save();
320
321     $this->formDisplay->load('node.article.default')
322       ->setComponent($field_name, ['type' => 'options_select'])
323       ->save();
324     $this->viewDisplay->load('node.article.default')
325       ->setComponent($field_name, ['type' => 'list_default'])
326       ->save();
327
328     // Create an article, setting values on the lit field.
329     $node = $this->drupalCreateNode([
330       'type' => 'article',
331       'title' => 'Test article',
332       'field_list' => 'value_a',
333     ]);
334
335     // Update the list field.
336     $edit = [
337       'field_list' => 'value_b',
338       'revision' => TRUE,
339     ];
340     $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
341
342     // Check differences between revisions.
343     $this->clickLink(t('Revisions'));
344     $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
345     $this->assertText('List');
346     $this->assertText('value_a');
347     $this->assertText('value_b');
348   }
349
350   /**
351    * Tests the Text plugin.
352    *
353    * @covers \Drupal\diff\Plugin\diff\Field\TextFieldBuilder
354    */
355   public function doTestTextPlugin() {
356     // Add a text and a text long field to the Article content type.
357     $this->addArticleTextField('field_text', 'Text Field', 'string', 'string_textfield');
358     $this->addArticleTextField('field_text_long', 'Text Long Field', 'string_long', 'string_textarea');
359
360     // Create an article, setting values on both fields.
361     $node = $this->drupalCreateNode([
362       'type' => 'article',
363       'title' => 'Test article',
364       'field_text' => 'Foo',
365       'field_text_long' => 'Fighters',
366     ]);
367
368     // Edit the article and update these fields, creating a new revision.
369     $edit = [
370       'field_text[0][value]' => 'Bar',
371       'field_text_long[0][value]' => 'Fly',
372       'revision' => TRUE,
373     ];
374     $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
375
376     // Check differences between revisions.
377     $this->clickLink(t('Revisions'));
378     $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
379     $this->assertText('Text Field');
380     $this->assertText('Text Long Field');
381     $this->assertText('Foo');
382     $this->assertText('Fighters');
383     $this->assertText('Bar');
384     $this->assertText('Fly');
385   }
386
387   /**
388    * Tests the TextWithSummary plugin.
389    *
390    * @covers \Drupal\diff\Plugin\diff\Field\TextWithSummaryFieldBuilder
391    */
392   public function doTestTextWithSummaryPlugin() {
393     // Enable the comparison of the summary.
394     $config = \Drupal::configFactory()->getEditable('diff.plugins');
395     $settings['compare_summary'] = TRUE;
396     $config->set('fields.node.body.type', 'text_summary_field_diff_builder');
397     $config->set('fields.node.body.settings', $settings);
398     $config->save();
399
400     // Create an article, setting the body field.
401     $node = $this->drupalCreateNode([
402       'type' => 'article',
403       'title' => 'Test article',
404       'body' => [
405         'value' => 'Foo value',
406         'summary' => 'Foo summary',
407       ],
408     ]);
409
410     // Edit the article and update these fields, creating a new revision.
411     $edit = [
412       'body[0][value]' => 'Bar value',
413       'body[0][summary]' => 'Bar summary',
414       'revision' => TRUE,
415     ];
416     $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
417
418     // Check differences between revisions.
419     $this->clickLink(t('Revisions'));
420     $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
421     $this->assertText('Body');
422     $this->assertText('Foo value');
423     $this->assertText('Foo summary');
424     $this->assertText('Bar value');
425     $this->assertText('Bar summary');
426   }
427
428 }