You are here

public function EntityDisplayTest::testComponentDependencies in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testComponentDependencies()

Tests components dependencies additions.

File

core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php, line 536

Class

EntityDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\Tests\field_ui\Kernel

Code

public function testComponentDependencies() {
  $this
    ->enableModules([
    'dblog',
    'color',
  ]);
  $this
    ->installSchema('dblog', [
    'watchdog',
  ]);
  $this
    ->installEntitySchema('user');

  /** @var \Drupal\user\RoleInterface[] $roles */
  $roles = [];

  // Create two arbitrary user roles.
  for ($i = 0; $i < 2; $i++) {
    $roles[$i] = Role::create([
      'id' => mb_strtolower($this
        ->randomMachineName()),
      'label' => $this
        ->randomString(),
    ]);
    $roles[$i]
      ->save();
  }

  // Create a field of type 'test_field' attached to 'entity_test'.
  $field_name = mb_strtolower($this
    ->randomMachineName());
  FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'test_field',
  ])
    ->save();
  FieldConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  ])
    ->save();

  // Create a new form display without components.

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
  $form_display = EntityFormDisplay::create([
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
  ]);
  $form_display
    ->save();
  $dependencies = [
    'user.role.' . $roles[0]
      ->id(),
    'user.role.' . $roles[1]
      ->id(),
  ];

  // The config object should not depend on none of the two $roles.
  $this
    ->assertNoDependency('config', $dependencies[0], $form_display);
  $this
    ->assertNoDependency('config', $dependencies[1], $form_display);

  // Add a widget of type 'test_field_widget'.
  $component = [
    'type' => 'test_field_widget',
    'settings' => [
      'test_widget_setting' => $this
        ->randomString(),
      'role' => $roles[0]
        ->id(),
      'role2' => $roles[1]
        ->id(),
    ],
    'third_party_settings' => [
      'color' => [
        'foo' => 'bar',
      ],
    ],
  ];
  $form_display
    ->setComponent($field_name, $component);
  $form_display
    ->save();

  // Now, the form display should depend on both user roles $roles.
  $this
    ->assertDependency('config', $dependencies[0], $form_display);
  $this
    ->assertDependency('config', $dependencies[1], $form_display);

  // The form display should depend on 'color' module.
  $this
    ->assertDependency('module', 'color', $form_display);

  // Delete the first user role entity.
  $roles[0]
    ->delete();

  // Reload the form display.
  $form_display = EntityFormDisplay::load($form_display
    ->id());

  // The display exists.
  $this
    ->assertFalse(empty($form_display));

  // The form display should not depend on $role[0] anymore.
  $this
    ->assertNoDependency('config', $dependencies[0], $form_display);

  // The form display should depend on 'anonymous' user role.
  $this
    ->assertDependency('config', 'user.role.anonymous', $form_display);

  // The form display should depend on 'color' module.
  $this
    ->assertDependency('module', 'color', $form_display);

  // Manually trigger the removal of configuration belonging to the module
  // because KernelTestBase::disableModules() is not aware of this.
  $this->container
    ->get('config.manager')
    ->uninstall('module', 'color');

  // Uninstall 'color' module.
  $this
    ->disableModules([
    'color',
  ]);

  // Reload the form display.
  $form_display = EntityFormDisplay::load($form_display
    ->id());

  // The display exists.
  $this
    ->assertFalse(empty($form_display));

  // The component is still enabled.
  $this
    ->assertNotNull($form_display
    ->getComponent($field_name));

  // The form display should not depend on 'color' module anymore.
  $this
    ->assertNoDependency('module', 'color', $form_display);

  // Delete the 2nd user role entity.
  $roles[1]
    ->delete();

  // Reload the form display.
  $form_display = EntityFormDisplay::load($form_display
    ->id());

  // The display exists.
  $this
    ->assertFalse(empty($form_display));

  // The component has been disabled.
  $this
    ->assertNull($form_display
    ->getComponent($field_name));
  $this
    ->assertTrue($form_display
    ->get('hidden')[$field_name]);

  // The correct warning message has been logged.
  $arguments = [
    '@display' => (string) t('Entity form display'),
    '@id' => $form_display
      ->id(),
    '@name' => $field_name,
  ];
  $variables = Database::getConnection()
    ->select('watchdog', 'w')
    ->fields('w', [
    'variables',
  ])
    ->condition('type', 'system')
    ->condition('message', "@display '@id': Component '@name' was disabled because its settings depend on removed dependencies.")
    ->execute()
    ->fetchField();
  $this
    ->assertEquals($arguments, unserialize($variables));
}