You are here

public function EntityFormDisplayTest::testDeleteField in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityFormDisplayTest::testDeleteField()

Tests deleting field.

File

core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php, line 194

Class

EntityFormDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\Tests\field_ui\Kernel

Code

public function testDeleteField() {
  $field_name = 'test_field';

  // Create a field storage and a field.
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'test_field',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
  ]);
  $field
    ->save();

  // Create default and compact entity display.
  EntityFormMode::create([
    'id' => 'entity_test.compact',
    'targetEntityType' => 'entity_test',
  ])
    ->save();
  EntityFormDisplay::create([
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
  ])
    ->setComponent($field_name)
    ->save();
  EntityFormDisplay::create([
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'compact',
  ])
    ->setComponent($field_name)
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');

  // Check the component exists.
  $display = $display_repository
    ->getFormDisplay('entity_test', 'entity_test');
  $this
    ->assertNotEmpty($display
    ->getComponent($field_name));
  $display = $display_repository
    ->getFormDisplay('entity_test', 'entity_test', 'compact');
  $this
    ->assertNotEmpty($display
    ->getComponent($field_name));

  // Delete the field.
  $field
    ->delete();

  // Check that the component has been removed from the entity displays.
  $display = $display_repository
    ->getFormDisplay('entity_test', 'entity_test');
  $this
    ->assertNull($display
    ->getComponent($field_name));
  $display = $display_repository
    ->getFormDisplay('entity_test', 'entity_test', 'compact');
  $this
    ->assertNull($display
    ->getComponent($field_name));
}