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;
12 * Tests the Diff module plugins.
16 class DiffPluginVariousTest extends DiffPluginTestBase {
25 public static $modules = [
30 * Tests the module plugins.
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();
45 * @param string $field_name
46 * The machine field name.
47 * @param string $label
49 * @param string $field_type
51 * @param string $widget_type
54 protected function addArticleTextField($field_name, $label, $field_type, $widget_type) {
56 $field_storage = FieldStorageConfig::create([
57 'field_name' => $field_name,
58 'entity_type' => 'node',
59 'type' => $field_type,
61 $field_storage->save();
63 'field_storage' => $field_storage,
64 'bundle' => 'article',
67 $this->formDisplay->load('node.article.default')
68 ->setComponent($field_name, ['type' => $widget_type])
70 $this->viewDisplay->load('node.article.default')
71 ->setComponent($field_name)
76 * Tests the comment plugin.
78 * @covers \Drupal\diff\Plugin\diff\Field\CommentFieldBuilder
80 public function doTestCommentPlugin() {
81 // Add the comment field to articles.
82 $this->addDefaultCommentField('node', 'article');
84 // Create an article with comments enabled..
85 $title = 'Sample article';
87 'title[0][value]' => $title,
88 'body[0][value]' => '<p>Revision 1</p>',
89 'comment[0][status]' => CommentItemInterface::OPEN,
91 $this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
92 $node = $this->drupalGetNodeByTitle($title);
94 // Edit the article and close its comments.
96 'comment[0][status]' => CommentItemInterface::CLOSED,
99 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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.');
110 * Tests the Core plugin.
112 * @covers \Drupal\diff\Plugin\diff\Field\CoreFieldBuilder
114 public function doTestCorePlugin() {
115 // Add an email field (supported by the Diff core plugin) to the Article
117 $field_name = 'field_email';
118 $field_storage = FieldStorageConfig::create([
119 'field_name' => $field_name,
120 'entity_type' => 'node',
123 $field_storage->save();
124 FieldConfig::create([
125 'field_storage' => $field_storage,
126 'bundle' => 'article',
130 // Add the email field to the article form.
131 $this->formDisplay->load('node.article.default')
132 ->setComponent($field_name, ['type' => 'email_default'])
135 // Add the email field to the default display.
136 $this->viewDisplay->load('node.article.default')
137 ->setComponent($field_name, ['type' => 'basic_string'])
140 // Create an article with an email.
141 $node = $this->drupalCreateNode([
143 'field_email' => 'foo@example.com',
146 // Edit the article and change the email.
148 'field_email[0][value]' => 'bar@example.com',
151 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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');
162 * Tests the Core plugin with a timestamp field.
164 * @covers \Drupal\diff\Plugin\diff\Field\CoreFieldBuilder
166 public function doTestCorePluginTimestampField() {
167 // Add a timestamp field (supported by the Diff core plugin) to the Article
169 $field_name = 'field_timestamp';
170 $field_storage = FieldStorageConfig::create([
171 'field_name' => $field_name,
172 'entity_type' => 'node',
173 'type' => 'timestamp',
175 $field_storage->save();
176 FieldConfig::create([
177 'field_storage' => $field_storage,
178 'bundle' => 'article',
179 'label' => 'Timestamp test',
182 // Add the timestamp field to the article form.
183 $this->formDisplay->load('node.article.default')
184 ->setComponent($field_name, ['type' => 'datetime_timestamp'])
187 // Add the timestamp field to the default display.
188 $this->viewDisplay->load('node.article.default')
189 ->setComponent($field_name, ['type' => 'timestamp'])
192 $old_timestamp = '321321321';
193 $new_timestamp = '123123123';
195 // Create an article with an timestamp.
196 $this->drupalCreateNode([
197 'title' => 'timestamp_test',
199 'field_timestamp' => $old_timestamp,
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);
208 // Compare the revisions.
209 $this->drupalGet('node/' . $node->id() . '/revisions');
210 $this->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
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));
220 * Tests the Link plugin.
222 * @covers \Drupal\diff\Plugin\diff\Field\LinkFieldBuilder
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',
232 $field_storage->save();
233 FieldConfig::create([
234 'field_storage' => $field_storage,
235 'bundle' => 'article',
238 'title' => DRUPAL_OPTIONAL,
239 'link_type' => LinkItemInterface::LINK_GENERIC,
242 $this->formDisplay->load('node.article.default')
243 ->setComponent($field_name, [
244 'type' => 'link_default',
246 'placeholder_url' => 'http://example.com',
250 $this->viewDisplay->load('node.article.default')
251 ->setComponent($field_name, ['type' => 'link'])
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);
261 // Create an article, setting values on the link field.
262 $node = $this->drupalCreateNode([
264 'title' => 'Test article',
267 'uri' => 'http://www.google.com',
271 // Update the link field.
273 'field_link[0][title]' => 'Guguel',
274 'field_link[0][uri]' => 'http://www.google.es',
277 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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');
290 * Tests the List plugin.
292 * @covers \Drupal\diff\Plugin\diff\Field\ListFieldBuilder
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',
303 'allowed_values' => [
304 'value_a' => 'Value A',
305 'value_b' => 'Value B',
309 $field_storage->save();
311 FieldConfig::create([
312 'field_name' => $field_name,
313 'entity_type' => 'node',
314 'bundle' => 'article',
319 $this->formDisplay->load('node.article.default')
320 ->setComponent($field_name, ['type' => 'options_select'])
322 $this->viewDisplay->load('node.article.default')
323 ->setComponent($field_name, ['type' => 'list_default'])
326 // Create an article, setting values on the lit field.
327 $node = $this->drupalCreateNode([
329 'title' => 'Test article',
330 'field_list' => 'value_a',
333 // Update the list field.
335 'field_list' => 'value_b',
338 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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');
349 * Tests the Text plugin.
351 * @covers \Drupal\diff\Plugin\diff\Field\TextFieldBuilder
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');
358 // Create an article, setting values on both fields.
359 $node = $this->drupalCreateNode([
361 'title' => 'Test article',
362 'field_text' => 'Foo',
363 'field_text_long' => 'Fighters',
366 // Edit the article and update these fields, creating a new revision.
368 'field_text[0][value]' => 'Bar',
369 'field_text_long[0][value]' => 'Fly',
372 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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');
386 * Tests the TextWithSummary plugin.
388 * @covers \Drupal\diff\Plugin\diff\Field\TextWithSummaryFieldBuilder
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);
398 // Create an article, setting the body field.
399 $node = $this->drupalCreateNode([
401 'title' => 'Test article',
403 'value' => 'Foo value',
404 'summary' => 'Foo summary',
408 // Edit the article and update these fields, creating a new revision.
410 'body[0][value]' => 'Bar value',
411 'body[0][summary]' => 'Bar summary',
414 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
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');