Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / field_ui / tests / src / Kernel / EntityDisplayTest.php
1 <?php
2
3 namespace Drupal\Tests\field_ui\Kernel;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\Database\Database;
8 use Drupal\Core\Entity\Display\EntityDisplayInterface;
9 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
10 use Drupal\Core\Entity\Entity\EntityFormDisplay;
11 use Drupal\Core\Entity\Entity\EntityViewDisplay;
12 use Drupal\Core\Entity\Entity\EntityViewMode;
13 use Drupal\field\Entity\FieldConfig;
14 use Drupal\field\Entity\FieldStorageConfig;
15 use Drupal\node\Entity\NodeType;
16 use Drupal\KernelTests\KernelTestBase;
17 use Drupal\user\Entity\Role;
18
19 /**
20  * Tests the entity display configuration entities.
21  *
22  * @group field_ui
23  */
24 class EntityDisplayTest extends KernelTestBase {
25
26   /**
27    * Modules to install.
28    *
29    * @var string[]
30    */
31   public static $modules = ['field_ui', 'field', 'entity_test', 'user', 'text', 'field_test', 'node', 'system'];
32
33   protected function setUp() {
34     parent::setUp();
35     $this->installEntitySchema('node');
36     $this->installEntitySchema('user');
37     $this->installConfig(['field', 'node', 'user']);
38   }
39
40   /**
41    * Tests basic CRUD operations on entity display objects.
42    */
43   public function testEntityDisplayCRUD() {
44     $display = EntityViewDisplay::create([
45       'targetEntityType' => 'entity_test',
46       'bundle' => 'entity_test',
47       'mode' => 'default',
48     ]);
49
50     $expected = [];
51
52     // Check that providing no 'weight' results in the highest current weight
53     // being assigned. The 'name' field's formatter has weight -5, therefore
54     // these follow.
55     $expected['component_1'] = ['weight' => -4, 'settings' => [], 'third_party_settings' => []];
56     $expected['component_2'] = ['weight' => -3, 'settings' => [], 'third_party_settings' => []];
57     $display->setComponent('component_1');
58     $display->setComponent('component_2');
59     $this->assertEqual($display->getComponent('component_1'), $expected['component_1']);
60     $this->assertEqual($display->getComponent('component_2'), $expected['component_2']);
61
62     // Check that arbitrary options are correctly stored.
63     $expected['component_3'] = ['weight' => 10, 'third_party_settings' => ['field_test' => ['foo' => 'bar']], 'settings' => []];
64     $display->setComponent('component_3', $expected['component_3']);
65     $this->assertEqual($display->getComponent('component_3'), $expected['component_3']);
66
67     // Check that the display can be properly saved and read back.
68     $display->save();
69     $display = EntityViewDisplay::load($display->id());
70     foreach (['component_1', 'component_2', 'component_3'] as $name) {
71       $expected[$name]['region'] = 'content';
72       $this->assertEqual($display->getComponent($name), $expected[$name]);
73     }
74
75     // Ensure that third party settings were added to the config entity.
76     // These are added by entity_test_entity_presave() implemented in
77     // entity_test module.
78     $this->assertEqual('bar', $display->getThirdPartySetting('entity_test', 'foo'), 'Third party settings were added to the entity view display.');
79
80     // Check that getComponents() returns options for all components.
81     $expected['name'] = [
82       'label' => 'hidden',
83       'type' => 'string',
84       'weight' => -5,
85       'settings' => [
86         'link_to_entity' => FALSE,
87       ],
88       'third_party_settings' => [],
89       'region' => 'content',
90     ];
91     $this->assertEqual($display->getComponents(), $expected);
92
93     // Check that a component can be removed.
94     $display->removeComponent('component_3');
95     $this->assertNULL($display->getComponent('component_3'));
96
97     // Check that the removal is correctly persisted.
98     $display->save();
99     $display = EntityViewDisplay::load($display->id());
100     $this->assertNULL($display->getComponent('component_3'));
101
102     // Check that createCopy() creates a new component that can be correctly
103     // saved.
104     EntityViewMode::create(['id' => $display->getTargetEntityTypeId() . '.other_view_mode', 'targetEntityType' => $display->getTargetEntityTypeId()])->save();
105     $new_display = $display->createCopy('other_view_mode');
106     $new_display->save();
107     $new_display = EntityViewDisplay::load($new_display->id());
108     $dependencies = $new_display->calculateDependencies()->getDependencies();
109     $this->assertEqual(['config' => ['core.entity_view_mode.entity_test.other_view_mode'], 'module' => ['entity_test']], $dependencies);
110     $this->assertEqual($new_display->getTargetEntityTypeId(), $display->getTargetEntityTypeId());
111     $this->assertEqual($new_display->getTargetBundle(), $display->getTargetBundle());
112     $this->assertEqual($new_display->getMode(), 'other_view_mode');
113     $this->assertEqual($new_display->getComponents(), $display->getComponents());
114   }
115
116   /**
117    * Test sorting of components by name on basic CRUD operations
118    */
119   public function testEntityDisplayCRUDSort() {
120     $display = EntityViewDisplay::create([
121       'targetEntityType' => 'entity_test',
122       'bundle' => 'entity_test',
123       'mode' => 'default',
124     ]);
125     $display->setComponent('component_3');
126     $display->setComponent('component_1');
127     $display->setComponent('component_2');
128     $display->save();
129     $components = array_keys($display->getComponents());
130     // The name field is not configurable so will be added automatically.
131     $expected = [0 => 'component_1', 1 => 'component_2', 2 => 'component_3', 'name'];
132     $this->assertIdentical($components, $expected);
133   }
134
135   /**
136    * Tests entity_get_display().
137    */
138   public function testEntityGetDisplay() {
139     // Check that entity_get_display() returns a fresh object when no
140     // configuration entry exists.
141     $display = entity_get_display('entity_test', 'entity_test', 'default');
142     $this->assertTrue($display->isNew());
143
144     // Add some components and save the display.
145     $display->setComponent('component_1', ['weight' => 10, 'settings' => []])
146       ->save();
147
148     // Check that entity_get_display() returns the correct object.
149     $display = entity_get_display('entity_test', 'entity_test', 'default');
150     $this->assertFalse($display->isNew());
151     $this->assertEqual($display->id(), 'entity_test.entity_test.default');
152     $this->assertEqual($display->getComponent('component_1'), ['weight' => 10, 'settings' => [], 'third_party_settings' => [], 'region' => 'content']);
153   }
154
155   /**
156    * Tests the behavior of a field component within an entity display object.
157    */
158   public function testExtraFieldComponent() {
159     entity_test_create_bundle('bundle_with_extra_fields');
160     $display = EntityViewDisplay::create([
161       'targetEntityType' => 'entity_test',
162       'bundle' => 'bundle_with_extra_fields',
163       'mode' => 'default',
164     ]);
165
166     // Check that the default visibility taken into account for extra fields
167     // unknown in the display.
168     $this->assertEqual(
169       $display->getComponent('display_extra_field'),
170       [
171         'weight' => 5,
172         'region' => 'content',
173         'settings' => [],
174         'third_party_settings' => [],
175       ]
176     );
177     $this->assertNull($display->getComponent('display_extra_field_hidden'));
178
179     // Check that setting explicit options overrides the defaults.
180     $display->removeComponent('display_extra_field');
181     $display->setComponent('display_extra_field_hidden', ['weight' => 10]);
182     $this->assertNull($display->getComponent('display_extra_field'));
183     $this->assertEqual($display->getComponent('display_extra_field_hidden'), ['weight' => 10, 'settings' => [], 'third_party_settings' => []]);
184   }
185
186   /**
187    * Tests the behavior of an extra field component with initial invalid values.
188    */
189   public function testExtraFieldComponentInitialInvalidConfig() {
190     entity_test_create_bundle('bundle_with_extra_fields');
191     $display = EntityViewDisplay::create([
192       'targetEntityType' => 'entity_test',
193       'bundle' => 'bundle_with_extra_fields',
194       'mode' => 'default',
195       // Add the extra field to the initial config, without a 'type'.
196       'content' => [
197         'display_extra_field' => [
198           'weight' => 5,
199         ],
200       ],
201     ]);
202
203     // Check that the default visibility taken into account for extra fields
204     // unknown in the display that were included in the initial config.
205     $this->assertEqual($display->getComponent('display_extra_field'), ['weight' => 5, 'region' => 'content']);
206     $this->assertNull($display->getComponent('display_extra_field_hidden'));
207
208     // Check that setting explicit options overrides the defaults.
209     $display->removeComponent('display_extra_field');
210     $display->setComponent('display_extra_field_hidden', ['weight' => 10]);
211     $this->assertNull($display->getComponent('display_extra_field'));
212     $this->assertEqual($display->getComponent('display_extra_field_hidden'), ['weight' => 10, 'settings' => [], 'third_party_settings' => []]);
213   }
214
215   /**
216    * Tests the behavior of a field component within an entity display object.
217    */
218   public function testFieldComponent() {
219     $field_name = 'test_field';
220     // Create a field storage and a field.
221     $field_storage = FieldStorageConfig::create([
222       'field_name' => $field_name,
223       'entity_type' => 'entity_test',
224       'type' => 'test_field',
225     ]);
226     $field_storage->save();
227     $field = FieldConfig::create([
228       'field_storage' => $field_storage,
229       'bundle' => 'entity_test',
230     ]);
231     $field->save();
232
233     $display = EntityViewDisplay::create([
234       'targetEntityType' => 'entity_test',
235       'bundle' => 'entity_test',
236       'mode' => 'default',
237     ]);
238
239     // Check that providing no options results in default values being used.
240     $display->setComponent($field_name);
241     $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_storage->getType());
242     $default_formatter = $field_type_info['default_formatter'];
243     $formatter_settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings($default_formatter);
244     $expected = [
245       'weight' => -4,
246       'label' => 'above',
247       'type' => $default_formatter,
248       'settings' => $formatter_settings,
249       'third_party_settings' => [],
250     ];
251     $this->assertEqual($display->getComponent($field_name), $expected);
252
253     // Check that the getFormatter() method returns the correct formatter plugin.
254     $formatter = $display->getRenderer($field_name);
255     $this->assertEqual($formatter->getPluginId(), $default_formatter);
256     $this->assertEqual($formatter->getSettings(), $formatter_settings);
257
258     // Check that the formatter is statically persisted, by assigning an
259     // arbitrary property and reading it back.
260     $random_value = $this->randomString();
261     $formatter->randomValue = $random_value;
262     $formatter = $display->getRenderer($field_name);
263     $this->assertEqual($formatter->randomValue, $random_value);
264
265     // Check that changing the definition creates a new formatter.
266     $display->setComponent($field_name, [
267       'type' => 'field_test_multiple',
268     ]);
269     $formatter = $display->getRenderer($field_name);
270     $this->assertEqual($formatter->getPluginId(), 'field_test_multiple');
271     $this->assertFalse(isset($formatter->randomValue));
272
273     // Check that the display has dependencies on the field and the module that
274     // provides the formatter.
275     $dependencies = $display->calculateDependencies()->getDependencies();
276     $this->assertEqual(['config' => ['field.field.entity_test.entity_test.test_field'], 'module' => ['entity_test', 'field_test']], $dependencies);
277   }
278
279   /**
280    * Tests the behavior of a field component for a base field.
281    */
282   public function testBaseFieldComponent() {
283     $display = EntityViewDisplay::create([
284       'targetEntityType' => 'entity_test_base_field_display',
285       'bundle' => 'entity_test_base_field_display',
286       'mode' => 'default',
287     ]);
288
289     // Check that default options are correctly filled in.
290     $formatter_settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('text_default');
291     $expected = [
292       'test_no_display' => NULL,
293       'test_display_configurable' => [
294         'label' => 'above',
295         'type' => 'text_default',
296         'settings' => $formatter_settings,
297         'third_party_settings' => [],
298         'weight' => 10,
299         'region' => 'content',
300       ],
301       'test_display_non_configurable' => [
302         'label' => 'above',
303         'type' => 'text_default',
304         'settings' => $formatter_settings,
305         'third_party_settings' => [],
306         'weight' => 11,
307         'region' => 'content',
308       ],
309     ];
310     foreach ($expected as $field_name => $options) {
311       $this->assertEqual($display->getComponent($field_name), $options);
312     }
313
314     // Check that saving the display only writes data for fields whose display
315     // is configurable.
316     $display->save();
317     $config = $this->config('core.entity_view_display.' . $display->id());
318     $data = $config->get();
319     $this->assertFalse(isset($data['content']['test_no_display']));
320     $this->assertFalse(isset($data['hidden']['test_no_display']));
321     $this->assertEqual($data['content']['test_display_configurable'], $expected['test_display_configurable']);
322     $this->assertFalse(isset($data['content']['test_display_non_configurable']));
323     $this->assertFalse(isset($data['hidden']['test_display_non_configurable']));
324
325     // Check that defaults are correctly filled when loading the display.
326     $display = EntityViewDisplay::load($display->id());
327     foreach ($expected as $field_name => $options) {
328       $this->assertEqual($display->getComponent($field_name), $options);
329     }
330
331     // Check that data manually written for fields whose display is not
332     // configurable is discarded when loading the display.
333     $data['content']['test_display_non_configurable'] = $expected['test_display_non_configurable'];
334     $data['content']['test_display_non_configurable']['weight']++;
335     $config->setData($data)->save();
336     $display = EntityViewDisplay::load($display->id());
337     foreach ($expected as $field_name => $options) {
338       $this->assertEqual($display->getComponent($field_name), $options);
339     }
340   }
341
342   /**
343    * Tests deleting a bundle.
344    */
345   public function testDeleteBundle() {
346     // Create a node bundle, display and form display object.
347     $type = NodeType::create(['type' => 'article']);
348     $type->save();
349     node_add_body_field($type);
350     entity_get_display('node', 'article', 'default')->save();
351     entity_get_form_display('node', 'article', 'default')->save();
352
353     // Delete the bundle.
354     $type->delete();
355     $display = EntityViewDisplay::load('node.article.default');
356     $this->assertFalse((bool) $display);
357     $form_display = EntityFormDisplay::load('node.article.default');
358     $this->assertFalse((bool) $form_display);
359   }
360
361   /**
362    * Tests deleting field.
363    */
364   public function testDeleteField() {
365     $field_name = 'test_field';
366     // Create a field storage and a field.
367     $field_storage = FieldStorageConfig::create([
368       'field_name' => $field_name,
369       'entity_type' => 'entity_test',
370       'type' => 'test_field',
371     ]);
372     $field_storage->save();
373     $field = FieldConfig::create([
374       'field_storage' => $field_storage,
375       'bundle' => 'entity_test',
376     ]);
377     $field->save();
378
379     // Create default and teaser entity display.
380     EntityViewMode::create(['id' => 'entity_test.teaser', 'targetEntityType' => 'entity_test'])->save();
381     EntityViewDisplay::create([
382       'targetEntityType' => 'entity_test',
383       'bundle' => 'entity_test',
384       'mode' => 'default',
385     ])->setComponent($field_name)->save();
386     EntityViewDisplay::create([
387       'targetEntityType' => 'entity_test',
388       'bundle' => 'entity_test',
389       'mode' => 'teaser',
390     ])->setComponent($field_name)->save();
391
392     // Check the component exists.
393     $display = entity_get_display('entity_test', 'entity_test', 'default');
394     $this->assertTrue($display->getComponent($field_name));
395     $display = entity_get_display('entity_test', 'entity_test', 'teaser');
396     $this->assertTrue($display->getComponent($field_name));
397
398     // Delete the field.
399     $field->delete();
400
401     // Check that the component has been removed from the entity displays.
402     $display = entity_get_display('entity_test', 'entity_test', 'default');
403     $this->assertFalse($display->getComponent($field_name));
404     $display = entity_get_display('entity_test', 'entity_test', 'teaser');
405     $this->assertFalse($display->getComponent($field_name));
406   }
407
408   /**
409    * Tests \Drupal\Core\Entity\EntityDisplayBase::onDependencyRemoval().
410    */
411   public function testOnDependencyRemoval() {
412     $this->enableModules(['field_plugins_test']);
413
414     $field_name = 'test_field';
415     // Create a field.
416     $field_storage = FieldStorageConfig::create([
417       'field_name' => $field_name,
418       'entity_type' => 'entity_test',
419       'type' => 'text',
420     ]);
421     $field_storage->save();
422     $field = FieldConfig::create([
423       'field_storage' => $field_storage,
424       'bundle' => 'entity_test',
425     ]);
426     $field->save();
427
428     EntityViewDisplay::create([
429       'targetEntityType' => 'entity_test',
430       'bundle' => 'entity_test',
431       'mode' => 'default',
432     ])->setComponent($field_name, ['type' => 'field_plugins_test_text_formatter'])->save();
433
434     // Check the component exists and is of the correct type.
435     $display = entity_get_display('entity_test', 'entity_test', 'default');
436     $this->assertEqual($display->getComponent($field_name)['type'], 'field_plugins_test_text_formatter');
437
438     // Removing the field_plugins_test module should change the component to use
439     // the default formatter for test fields.
440     \Drupal::service('config.manager')->uninstall('module', 'field_plugins_test');
441     $display = entity_get_display('entity_test', 'entity_test', 'default');
442     $this->assertEqual($display->getComponent($field_name)['type'], 'text_default');
443
444     // Removing the text module should remove the field from the view display.
445     \Drupal::service('config.manager')->uninstall('module', 'text');
446     $display = entity_get_display('entity_test', 'entity_test', 'default');
447     $this->assertFalse($display->getComponent($field_name));
448   }
449
450   /**
451    * Ensure that entity view display changes invalidates cache tags.
452    */
453   public function testEntityDisplayInvalidateCacheTags() {
454     $cache = \Drupal::cache();
455     $cache->set('cid', 'kittens', Cache::PERMANENT, ['config:entity_view_display_list']);
456     $display = EntityViewDisplay::create([
457       'targetEntityType' => 'entity_test',
458       'bundle' => 'entity_test',
459       'mode' => 'default',
460     ]);
461     $display->setComponent('kitten');
462     $display->save();
463     $this->assertFalse($cache->get('cid'));
464   }
465
466   /**
467    * Test getDisplayModeOptions().
468    */
469   public function testGetDisplayModeOptions() {
470     NodeType::create(['type' => 'article'])->save();
471
472     EntityViewDisplay::create([
473       'targetEntityType' => 'node',
474       'bundle' => 'article',
475       'mode' => 'default',
476     ])->setStatus(TRUE)->save();
477
478     $display_teaser = EntityViewDisplay::create([
479       'targetEntityType' => 'node',
480       'bundle' => 'article',
481       'mode' => 'teaser',
482     ]);
483     $display_teaser->save();
484
485     EntityFormDisplay::create([
486       'targetEntityType' => 'user',
487       'bundle' => 'user',
488       'mode' => 'default',
489     ])->setStatus(TRUE)->save();
490
491     $form_display_teaser = EntityFormDisplay::create([
492       'targetEntityType' => 'user',
493       'bundle' => 'user',
494       'mode' => 'register',
495     ]);
496     $form_display_teaser->save();
497
498     // Test getViewModeOptionsByBundle().
499     $view_modes = \Drupal::entityManager()->getViewModeOptionsByBundle('node', 'article');
500     $this->assertEqual($view_modes, ['default' => 'Default']);
501     $display_teaser->setStatus(TRUE)->save();
502     $view_modes = \Drupal::entityManager()->getViewModeOptionsByBundle('node', 'article');
503     $this->assertEqual($view_modes, ['default' => 'Default', 'teaser' => 'Teaser']);
504
505     // Test getFormModeOptionsByBundle().
506     $form_modes = \Drupal::entityManager()->getFormModeOptionsByBundle('user', 'user');
507     $this->assertEqual($form_modes, ['default' => 'Default']);
508     $form_display_teaser->setStatus(TRUE)->save();
509     $form_modes = \Drupal::entityManager()->getFormModeOptionsByBundle('user', 'user');
510     $this->assertEqual($form_modes, ['default' => 'Default', 'register' => 'Register']);
511   }
512
513   /**
514    * Tests components dependencies additions.
515    */
516   public function testComponentDependencies() {
517     $this->enableModules(['dblog', 'color']);
518     $this->installSchema('dblog', ['watchdog']);
519     $this->installEntitySchema('user');
520     /** @var \Drupal\user\RoleInterface[] $roles */
521     $roles = [];
522     // Create two arbitrary user roles.
523     for ($i = 0; $i < 2; $i++) {
524       $roles[$i] = Role::create([
525         'id' => mb_strtolower($this->randomMachineName()),
526         'label' => $this->randomString(),
527       ]);
528       $roles[$i]->save();
529     }
530
531     // Create a field of type 'test_field' attached to 'entity_test'.
532     $field_name = mb_strtolower($this->randomMachineName());
533     FieldStorageConfig::create([
534       'field_name' => $field_name,
535       'entity_type' => 'entity_test',
536       'type' => 'test_field',
537     ])->save();
538     FieldConfig::create([
539       'field_name' => $field_name,
540       'entity_type' => 'entity_test',
541       'bundle' => 'entity_test',
542     ])->save();
543
544     // Create a new form display without components.
545     /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
546     $form_display = EntityFormDisplay::create([
547       'targetEntityType' => 'entity_test',
548       'bundle' => 'entity_test',
549       'mode' => 'default',
550     ]);
551     $form_display->save();
552
553     $dependencies = ['user.role.' . $roles[0]->id(), 'user.role.' . $roles[1]->id()];
554
555     // The config object should not depend on none of the two $roles.
556     $this->assertNoDependency('config', $dependencies[0], $form_display);
557     $this->assertNoDependency('config', $dependencies[1], $form_display);
558
559     // Add a widget of type 'test_field_widget'.
560     $component = [
561       'type' => 'test_field_widget',
562       'settings' => [
563         'test_widget_setting' => $this->randomString(),
564         'role' => $roles[0]->id(),
565         'role2' => $roles[1]->id(),
566       ],
567       'third_party_settings' => [
568         'color' => ['foo' => 'bar'],
569       ],
570     ];
571     $form_display->setComponent($field_name, $component);
572     $form_display->save();
573
574     // Now, the form display should depend on both user roles $roles.
575     $this->assertDependency('config', $dependencies[0], $form_display);
576     $this->assertDependency('config', $dependencies[1], $form_display);
577     // The form display should depend on 'color' module.
578     $this->assertDependency('module', 'color', $form_display);
579
580     // Delete the first user role entity.
581     $roles[0]->delete();
582
583     // Reload the form display.
584     $form_display = EntityFormDisplay::load($form_display->id());
585     // The display exists.
586     $this->assertFalse(empty($form_display));
587     // The form display should not depend on $role[0] anymore.
588     $this->assertNoDependency('config', $dependencies[0], $form_display);
589     // The form display should depend on 'anonymous' user role.
590     $this->assertDependency('config', 'user.role.anonymous', $form_display);
591     // The form display should depend on 'color' module.
592     $this->assertDependency('module', 'color', $form_display);
593
594     // Manually trigger the removal of configuration belonging to the module
595     // because KernelTestBase::disableModules() is not aware of this.
596     $this->container->get('config.manager')->uninstall('module', 'color');
597     // Uninstall 'color' module.
598     $this->disableModules(['color']);
599
600     // Reload the form display.
601     $form_display = EntityFormDisplay::load($form_display->id());
602     // The display exists.
603     $this->assertFalse(empty($form_display));
604     // The component is still enabled.
605     $this->assertNotNull($form_display->getComponent($field_name));
606     // The form display should not depend on 'color' module anymore.
607     $this->assertNoDependency('module', 'color', $form_display);
608
609     // Delete the 2nd user role entity.
610     $roles[1]->delete();
611
612     // Reload the form display.
613     $form_display = EntityFormDisplay::load($form_display->id());
614     // The display exists.
615     $this->assertFalse(empty($form_display));
616     // The component has been disabled.
617     $this->assertNull($form_display->getComponent($field_name));
618     $this->assertTrue($form_display->get('hidden')[$field_name]);
619     // The correct warning message has been logged.
620     $arguments = ['@display' => (string) t('Entity form display'), '@id' => $form_display->id(), '@name' => $field_name];
621     $logged = (bool) Database::getConnection()->select('watchdog', 'w')
622       ->fields('w', ['wid'])
623       ->condition('type', 'system')
624       ->condition('message', "@display '@id': Component '@name' was disabled because its settings depend on removed dependencies.")
625       ->condition('variables', serialize($arguments))
626       ->execute()
627       ->fetchAll();
628     $this->assertTrue($logged);
629   }
630
631   /**
632    * Asserts that $key is a $type type dependency of $display config entity.
633    *
634    * @param string $type
635    *   The dependency type: 'config', 'content', 'module' or 'theme'.
636    * @param string $key
637    *   The string to be checked.
638    * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $display
639    *   The entity display object to get dependencies from.
640    *
641    * @return bool
642    *   TRUE if the assertion succeeded, FALSE otherwise.
643    */
644   protected function assertDependency($type, $key, EntityDisplayInterface $display) {
645     return $this->assertDependencyHelper(TRUE, $type, $key, $display);
646   }
647
648   /**
649    * Asserts that $key is not a $type type dependency of $display config entity.
650    *
651    * @param string $type
652    *   The dependency type: 'config', 'content', 'module' or 'theme'.
653    * @param string $key
654    *   The string to be checked.
655    * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $display
656    *   The entity display object to get dependencies from.
657    *
658    * @return bool
659    *   TRUE if the assertion succeeded, FALSE otherwise.
660    */
661   protected function assertNoDependency($type, $key, EntityDisplayInterface $display) {
662     return $this->assertDependencyHelper(FALSE, $type, $key, $display);
663   }
664
665   /**
666    * Provides a helper for dependency assertions.
667    *
668    * @param bool $assertion
669    *   Assertion: positive or negative.
670    * @param string $type
671    *   The dependency type: 'config', 'content', 'module' or 'theme'.
672    * @param string $key
673    *   The string to be checked.
674    * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $display
675    *   The entity display object to get dependencies from.
676    *
677    * @return bool
678    *   TRUE if the assertion succeeded, FALSE otherwise.
679    */
680   protected function assertDependencyHelper($assertion, $type, $key, EntityDisplayInterface $display) {
681     $all_dependencies = $display->getDependencies();
682     $dependencies = !empty($all_dependencies[$type]) ? $all_dependencies[$type] : [];
683     $context = $display instanceof EntityViewDisplayInterface ? 'View' : 'Form';
684     $value = $assertion ? in_array($key, $dependencies) : !in_array($key, $dependencies);
685     $args = ['@context' => $context, '@id' => $display->id(), '@type' => $type, '@key' => $key];
686     $message = $assertion ? new FormattableMarkup("@context display '@id' depends on @type '@key'.", $args) : new FormattableMarkup("@context display '@id' do not depend on @type '@key'.", $args);
687     return $this->assert($value, $message);
688   }
689
690 }