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