function EntityDisplayTest::testComponentDependencies

Same name and namespace in other branches
  1. 8.9.x core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testComponentDependencies()
  2. 10 core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testComponentDependencies()
  3. 11.x 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 531

Class

EntityDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\Tests\field_ui\Kernel

Code

public function testComponentDependencies() {
    $this->enableModules([
        'dblog',
        'help',
    ]);
    $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' => [
            'help' => [
                '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 'help' module.
    $this->assertDependency('module', 'help', $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->assertNotEmpty($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 'help' module.
    $this->assertDependency('module', 'help', $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', 'help');
    // Uninstall 'help' module.
    $this->disableModules([
        'help',
    ]);
    // Reload the form display.
    $form_display = EntityFormDisplay::load($form_display->id());
    // The display exists.
    $this->assertNotEmpty($form_display);
    // The component is still enabled.
    $this->assertNotNull($form_display->getComponent($field_name));
    // The form display should not depend on 'help' module anymore.
    $this->assertNoDependency('module', 'help', $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->assertNotEmpty($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' => '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));
}

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