3 namespace Drupal\Tests\field_ui\Kernel;
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;
20 * Tests the entity display configuration entities.
24 class EntityDisplayTest extends KernelTestBase {
31 public static $modules = ['field_ui', 'field', 'entity_test', 'user', 'text', 'field_test', 'node', 'system'];
33 protected function setUp() {
35 $this->installEntitySchema('node');
36 $this->installEntitySchema('user');
37 $this->installConfig(['field', 'node', 'user']);
41 * Tests basic CRUD operations on entity display objects.
43 public function testEntityDisplayCRUD() {
44 $display = EntityViewDisplay::create([
45 'targetEntityType' => 'entity_test',
46 'bundle' => 'entity_test',
52 // Check that providing no 'weight' results in the highest current weight
53 // being assigned. The 'name' field's formatter has weight -5, therefore
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']);
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']);
67 // Check that the display can be properly saved and read back.
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]);
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.');
80 // Check that getComponents() returns options for all components.
86 'link_to_entity' => FALSE,
88 'third_party_settings' => [],
89 'region' => 'content',
91 $this->assertEqual($display->getComponents(), $expected);
93 // Check that a component can be removed.
94 $display->removeComponent('component_3');
95 $this->assertNULL($display->getComponent('component_3'));
97 // Check that the removal is correctly persisted.
99 $display = EntityViewDisplay::load($display->id());
100 $this->assertNULL($display->getComponent('component_3'));
102 // Check that createCopy() creates a new component that can be correctly
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());
117 * Test sorting of components by name on basic CRUD operations
119 public function testEntityDisplayCRUDSort() {
120 $display = EntityViewDisplay::create([
121 'targetEntityType' => 'entity_test',
122 'bundle' => 'entity_test',
125 $display->setComponent('component_3');
126 $display->setComponent('component_1');
127 $display->setComponent('component_2');
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);
136 * Tests entity_get_display().
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());
144 // Add some components and save the display.
145 $display->setComponent('component_1', ['weight' => 10, 'settings' => []])
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']);
156 * Tests the behavior of a field component within an entity display object.
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',
166 // Check that the default visibility taken into account for extra fields
167 // unknown in the display.
169 $display->getComponent('display_extra_field'),
172 'region' => 'content',
174 'third_party_settings' => [],
177 $this->assertNull($display->getComponent('display_extra_field_hidden'));
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' => []]);
187 * Tests the behavior of an extra field component with initial invalid values.
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',
195 // Add the extra field to the initial config, without a 'type'.
197 'display_extra_field' => [
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'));
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' => []]);
216 * Tests the behavior of a field component within an entity display object.
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',
226 $field_storage->save();
227 $field = FieldConfig::create([
228 'field_storage' => $field_storage,
229 'bundle' => 'entity_test',
233 $display = EntityViewDisplay::create([
234 'targetEntityType' => 'entity_test',
235 'bundle' => 'entity_test',
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);
247 'type' => $default_formatter,
248 'settings' => $formatter_settings,
249 'third_party_settings' => [],
251 $this->assertEqual($display->getComponent($field_name), $expected);
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);
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);
265 // Check that changing the definition creates a new formatter.
266 $display->setComponent($field_name, [
267 'type' => 'field_test_multiple',
269 $formatter = $display->getRenderer($field_name);
270 $this->assertEqual($formatter->getPluginId(), 'field_test_multiple');
271 $this->assertFalse(isset($formatter->randomValue));
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);
280 * Tests the behavior of a field component for a base field.
282 public function testBaseFieldComponent() {
283 $display = EntityViewDisplay::create([
284 'targetEntityType' => 'entity_test_base_field_display',
285 'bundle' => 'entity_test_base_field_display',
289 // Check that default options are correctly filled in.
290 $formatter_settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('text_default');
292 'test_no_display' => NULL,
293 'test_display_configurable' => [
295 'type' => 'text_default',
296 'settings' => $formatter_settings,
297 'third_party_settings' => [],
299 'region' => 'content',
301 'test_display_non_configurable' => [
303 'type' => 'text_default',
304 'settings' => $formatter_settings,
305 'third_party_settings' => [],
307 'region' => 'content',
310 foreach ($expected as $field_name => $options) {
311 $this->assertEqual($display->getComponent($field_name), $options);
314 // Check that saving the display only writes data for fields whose display
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']));
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);
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);
343 * Tests deleting a bundle.
345 public function testDeleteBundle() {
346 // Create a node bundle, display and form display object.
347 $type = NodeType::create(['type' => 'article']);
349 node_add_body_field($type);
350 entity_get_display('node', 'article', 'default')->save();
351 entity_get_form_display('node', 'article', 'default')->save();
353 // Delete the bundle.
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);
362 * Tests deleting field.
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',
372 $field_storage->save();
373 $field = FieldConfig::create([
374 'field_storage' => $field_storage,
375 'bundle' => 'entity_test',
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',
385 ])->setComponent($field_name)->save();
386 EntityViewDisplay::create([
387 'targetEntityType' => 'entity_test',
388 'bundle' => 'entity_test',
390 ])->setComponent($field_name)->save();
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));
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));
409 * Tests \Drupal\Core\Entity\EntityDisplayBase::onDependencyRemoval().
411 public function testOnDependencyRemoval() {
412 $this->enableModules(['field_plugins_test']);
414 $field_name = 'test_field';
416 $field_storage = FieldStorageConfig::create([
417 'field_name' => $field_name,
418 'entity_type' => 'entity_test',
421 $field_storage->save();
422 $field = FieldConfig::create([
423 'field_storage' => $field_storage,
424 'bundle' => 'entity_test',
428 EntityViewDisplay::create([
429 'targetEntityType' => 'entity_test',
430 'bundle' => 'entity_test',
432 ])->setComponent($field_name, ['type' => 'field_plugins_test_text_formatter'])->save();
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');
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');
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));
451 * Ensure that entity view display changes invalidates cache tags.
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',
461 $display->setComponent('kitten');
463 $this->assertFalse($cache->get('cid'));
467 * Test getDisplayModeOptions().
469 public function testGetDisplayModeOptions() {
470 NodeType::create(['type' => 'article'])->save();
472 EntityViewDisplay::create([
473 'targetEntityType' => 'node',
474 'bundle' => 'article',
476 ])->setStatus(TRUE)->save();
478 $display_teaser = EntityViewDisplay::create([
479 'targetEntityType' => 'node',
480 'bundle' => 'article',
483 $display_teaser->save();
485 EntityFormDisplay::create([
486 'targetEntityType' => 'user',
489 ])->setStatus(TRUE)->save();
491 $form_display_teaser = EntityFormDisplay::create([
492 'targetEntityType' => 'user',
494 'mode' => 'register',
496 $form_display_teaser->save();
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']);
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']);
514 * Tests components dependencies additions.
516 public function testComponentDependencies() {
517 $this->enableModules(['dblog', 'color']);
518 $this->installSchema('dblog', ['watchdog']);
519 $this->installEntitySchema('user');
520 /** @var \Drupal\user\RoleInterface[] $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(),
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',
538 FieldConfig::create([
539 'field_name' => $field_name,
540 'entity_type' => 'entity_test',
541 'bundle' => 'entity_test',
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',
551 $form_display->save();
553 $dependencies = ['user.role.' . $roles[0]->id(), 'user.role.' . $roles[1]->id()];
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);
559 // Add a widget of type 'test_field_widget'.
561 'type' => 'test_field_widget',
563 'test_widget_setting' => $this->randomString(),
564 'role' => $roles[0]->id(),
565 'role2' => $roles[1]->id(),
567 'third_party_settings' => [
568 'color' => ['foo' => 'bar'],
571 $form_display->setComponent($field_name, $component);
572 $form_display->save();
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);
580 // Delete the first user role entity.
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);
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']);
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);
609 // Delete the 2nd user role entity.
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))
628 $this->assertTrue($logged);
632 * Asserts that $key is a $type type dependency of $display config entity.
634 * @param string $type
635 * The dependency type: 'config', 'content', 'module' or 'theme'.
637 * The string to be checked.
638 * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $display
639 * The entity display object to get dependencies from.
642 * TRUE if the assertion succeeded, FALSE otherwise.
644 protected function assertDependency($type, $key, EntityDisplayInterface $display) {
645 return $this->assertDependencyHelper(TRUE, $type, $key, $display);
649 * Asserts that $key is not a $type type dependency of $display config entity.
651 * @param string $type
652 * The dependency type: 'config', 'content', 'module' or 'theme'.
654 * The string to be checked.
655 * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $display
656 * The entity display object to get dependencies from.
659 * TRUE if the assertion succeeded, FALSE otherwise.
661 protected function assertNoDependency($type, $key, EntityDisplayInterface $display) {
662 return $this->assertDependencyHelper(FALSE, $type, $key, $display);
666 * Provides a helper for dependency assertions.
668 * @param bool $assertion
669 * Assertion: positive or negative.
670 * @param string $type
671 * The dependency type: 'config', 'content', 'module' or 'theme'.
673 * The string to be checked.
674 * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $display
675 * The entity display object to get dependencies from.
678 * TRUE if the assertion succeeded, FALSE otherwise.
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);