function EntityDisplayTest::testOnDependencyRemoval

Same name in other branches
  1. 8.9.x core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testOnDependencyRemoval()
  2. 10 core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testOnDependencyRemoval()
  3. 11.x core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testOnDependencyRemoval()

Tests \Drupal\Core\Entity\EntityDisplayBase::onDependencyRemoval().

File

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

Class

EntityDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\Tests\field_ui\Kernel

Code

public function testOnDependencyRemoval() {
    $this->enableModules([
        'field_plugins_test',
    ]);
    $field_name = 'test_field';
    // Create a field.
    $field_storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'entity_test',
        'type' => 'text',
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'entity_test',
    ]);
    $field->save();
    EntityViewDisplay::create([
        'targetEntityType' => 'entity_test',
        'bundle' => 'entity_test',
        'mode' => 'default',
    ])->setComponent($field_name, [
        'type' => 'field_plugins_test_text_formatter',
    ])
        ->save();
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    // Check the component exists and is of the correct type.
    $display = $display_repository->getViewDisplay('entity_test', 'entity_test');
    $this->assertEquals('field_plugins_test_text_formatter', $display->getComponent($field_name)['type']);
    // Removing the field_plugins_test module should change the component to use
    // the default formatter for test fields.
    \Drupal::service('config.manager')->uninstall('module', 'field_plugins_test');
    $display = $display_repository->getViewDisplay('entity_test', 'entity_test');
    $this->assertEquals('text_default', $display->getComponent($field_name)['type']);
    // Removing the text module should remove the field from the view display.
    \Drupal::service('config.manager')->uninstall('module', 'text');
    $display = $display_repository->getViewDisplay('entity_test', 'entity_test');
    $this->assertNull($display->getComponent($field_name));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.