3 namespace Drupal\diff\Tests;
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;
13 * Tests the Diff module plugins.
17 class DiffPluginVariousTest extends DiffPluginTestBase {
20 use CoreVersionUiTestTrait;
27 public static $modules = [
32 * Tests the module plugins.
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();
47 * @param string $field_name
48 * The machine field name.
49 * @param string $label
51 * @param string $field_type
53 * @param string $widget_type
56 protected function addArticleTextField($field_name, $label, $field_type, $widget_type) {
58 $field_storage = FieldStorageConfig::create([
59 'field_name' => $field_name,
60 'entity_type' => 'node',
61 'type' => $field_type,
63 $field_storage->save();
65 'field_storage' => $field_storage,
66 'bundle' => 'article',
69 $this->formDisplay->load('node.article.default')
70 ->setComponent($field_name, ['type' => $widget_type])
72 $this->viewDisplay->load('node.article.default')
73 ->setComponent($field_name)
78 * Tests the comment plugin.
80 * @covers \Drupal\diff\Plugin\diff\Field\CommentFieldBuilder
82 public function doTestCommentPlugin() {
83 // Add the comment field to articles.
84 $this->addDefaultCommentField('node', 'article');
86 // Create an article with comments enabled..
87 $title = 'Sample article';
89 'title[0][value]' => $title,
90 'body[0][value]' => '<p>Revision 1</p>',
91 'comment[0][status]' => CommentItemInterface::OPEN,
93 $this->drupalPostNodeForm('node/add/article', $edit, t('Save and publish'));
94 $node = $this->drupalGetNodeByTitle($title);
96 // Edit the article and close its comments.
98 'comment[0][status]' => CommentItemInterface::CLOSED,
101 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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.');
112 * Tests the Core plugin.
114 * @covers \Drupal\diff\Plugin\diff\Field\CoreFieldBuilder
116 public function doTestCorePlugin() {
117 // Add an email field (supported by the Diff core plugin) to the Article
119 $field_name = 'field_email';
120 $field_storage = FieldStorageConfig::create([
121 'field_name' => $field_name,
122 'entity_type' => 'node',
125 $field_storage->save();
126 FieldConfig::create([
127 'field_storage' => $field_storage,
128 'bundle' => 'article',
132 // Add the email field to the article form.
133 $this->formDisplay->load('node.article.default')
134 ->setComponent($field_name, ['type' => 'email_default'])
137 // Add the email field to the default display.
138 $this->viewDisplay->load('node.article.default')
139 ->setComponent($field_name, ['type' => 'basic_string'])
142 // Create an article with an email.
143 $node = $this->drupalCreateNode([
145 'field_email' => 'foo@example.com',
148 // Edit the article and change the email.
150 'field_email[0][value]' => 'bar@example.com',
153 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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');
164 * Tests the Core plugin with a timestamp field.
166 * @covers \Drupal\diff\Plugin\diff\Field\CoreFieldBuilder
168 public function doTestCorePluginTimestampField() {
169 // Add a timestamp field (supported by the Diff core plugin) to the Article
171 $field_name = 'field_timestamp';
172 $field_storage = FieldStorageConfig::create([
173 'field_name' => $field_name,
174 'entity_type' => 'node',
175 'type' => 'timestamp',
177 $field_storage->save();
178 FieldConfig::create([
179 'field_storage' => $field_storage,
180 'bundle' => 'article',
181 'label' => 'Timestamp test',
184 // Add the timestamp field to the article form.
185 $this->formDisplay->load('node.article.default')
186 ->setComponent($field_name, ['type' => 'datetime_timestamp'])
189 // Add the timestamp field to the default display.
190 $this->viewDisplay->load('node.article.default')
191 ->setComponent($field_name, ['type' => 'timestamp'])
194 $old_timestamp = '321321321';
195 $new_timestamp = '123123123';
197 // Create an article with an timestamp.
198 $this->drupalCreateNode([
199 'title' => 'timestamp_test',
201 'field_timestamp' => $old_timestamp,
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);
210 // Compare the revisions.
211 $this->drupalGet('node/' . $node->id() . '/revisions');
212 $this->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
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));
222 * Tests the Link plugin.
224 * @covers \Drupal\diff\Plugin\diff\Field\LinkFieldBuilder
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',
234 $field_storage->save();
235 FieldConfig::create([
236 'field_storage' => $field_storage,
237 'bundle' => 'article',
240 'title' => DRUPAL_OPTIONAL,
241 'link_type' => LinkItemInterface::LINK_GENERIC,
244 $this->formDisplay->load('node.article.default')
245 ->setComponent($field_name, [
246 'type' => 'link_default',
248 'placeholder_url' => 'http://example.com',
252 $this->viewDisplay->load('node.article.default')
253 ->setComponent($field_name, ['type' => 'link'])
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);
263 // Create an article, setting values on the link field.
264 $node = $this->drupalCreateNode([
266 'title' => 'Test article',
269 'uri' => 'http://www.google.com',
273 // Update the link field.
275 'field_link[0][title]' => 'Guguel',
276 'field_link[0][uri]' => 'http://www.google.es',
279 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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');
292 * Tests the List plugin.
294 * @covers \Drupal\diff\Plugin\diff\Field\ListFieldBuilder
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',
305 'allowed_values' => [
306 'value_a' => 'Value A',
307 'value_b' => 'Value B',
311 $field_storage->save();
313 FieldConfig::create([
314 'field_name' => $field_name,
315 'entity_type' => 'node',
316 'bundle' => 'article',
321 $this->formDisplay->load('node.article.default')
322 ->setComponent($field_name, ['type' => 'options_select'])
324 $this->viewDisplay->load('node.article.default')
325 ->setComponent($field_name, ['type' => 'list_default'])
328 // Create an article, setting values on the lit field.
329 $node = $this->drupalCreateNode([
331 'title' => 'Test article',
332 'field_list' => 'value_a',
335 // Update the list field.
337 'field_list' => 'value_b',
340 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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');
351 * Tests the Text plugin.
353 * @covers \Drupal\diff\Plugin\diff\Field\TextFieldBuilder
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');
360 // Create an article, setting values on both fields.
361 $node = $this->drupalCreateNode([
363 'title' => 'Test article',
364 'field_text' => 'Foo',
365 'field_text_long' => 'Fighters',
368 // Edit the article and update these fields, creating a new revision.
370 'field_text[0][value]' => 'Bar',
371 'field_text_long[0][value]' => 'Fly',
374 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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');
388 * Tests the TextWithSummary plugin.
390 * @covers \Drupal\diff\Plugin\diff\Field\TextWithSummaryFieldBuilder
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);
400 // Create an article, setting the body field.
401 $node = $this->drupalCreateNode([
403 'title' => 'Test article',
405 'value' => 'Foo value',
406 'summary' => 'Foo summary',
410 // Edit the article and update these fields, creating a new revision.
412 'body[0][value]' => 'Bar value',
413 'body[0][summary]' => 'Bar summary',
416 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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');