You are here

public function EntityDisplayTest::testDeleteField 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::testDeleteField()
  2. 10 core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testDeleteField()

Tests deleting field.

File

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

Class

EntityDisplayTest
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 teaser entity display.
  EntityViewMode::create([
    'id' => 'entity_test.teaser',
    'targetEntityType' => 'entity_test',
  ])
    ->save();
  EntityViewDisplay::create([
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
  ])
    ->setComponent($field_name)
    ->save();
  EntityViewDisplay::create([
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'teaser',
  ])
    ->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
    ->getViewDisplay('entity_test', 'entity_test');
  $this
    ->assertNotEmpty($display
    ->getComponent($field_name));
  $display = $display_repository
    ->getViewDisplay('entity_test', 'entity_test', 'teaser');
  $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
    ->getViewDisplay('entity_test', 'entity_test');
  $this
    ->assertNull($display
    ->getComponent($field_name));
  $display = $display_repository
    ->getViewDisplay('entity_test', 'entity_test', 'teaser');
  $this
    ->assertNull($display
    ->getComponent($field_name));
}