View source
<?php
namespace Drupal\Tests\config\Functional;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Tests\BrowserTestBase;
class ConfigDependencyWebTest extends BrowserTestBase {
const MAX_ID_LENGTH = ConfigEntityStorage::MAX_ID_LENGTH;
protected static $modules = [
'config_test',
];
protected $defaultTheme = 'stark';
public function testConfigDependencyDeleteFormTrait() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer site configuration',
]));
$storage = $this->container
->get('entity_type.manager')
->getStorage('config_test');
$entity1 = $storage
->create([
'id' => 'entity1',
'label' => 'Entity One',
]);
$entity1
->save();
$entity2 = $storage
->create([
'id' => 'entity2',
'dependencies' => [
'enforced' => [
'config' => [
$entity1
->getConfigDependencyName(),
],
],
],
]);
$entity2
->save();
$this
->drupalGet($entity2
->toUrl('delete-form'));
$this
->assertSession()
->pageTextNotContains('Configuration updates');
$this
->assertSession()
->pageTextNotContains('Configuration deletions');
$this
->drupalGet($entity1
->toUrl('delete-form'));
$this
->assertSession()
->pageTextNotContains('Configuration updates');
$this
->assertSession()
->pageTextContains('Configuration deletions');
$this
->assertSession()
->pageTextContains($entity2
->id());
$this
->drupalGet($entity1
->toUrl('delete-form'));
$this
->submitForm([], 'Delete');
$storage
->resetCache();
$this
->assertEmpty($storage
->loadMultiple([
$entity1
->id(),
$entity2
->id(),
]), 'Test entities deleted');
$entity1 = $storage
->create([
'id' => 'entity1',
]);
$entity1
->save();
\Drupal::state()
->set('config_test.fix_dependencies', [
$entity1
->getConfigDependencyName(),
]);
$entity2 = $storage
->create([
'id' => 'entity2',
'label' => 'Entity Two',
'dependencies' => [
'enforced' => [
'config' => [
$entity1
->getConfigDependencyName(),
],
],
],
]);
$entity2
->save();
$entity3 = $storage
->create([
'id' => 'entity3',
'dependencies' => [
'enforced' => [
'config' => [
$entity2
->getConfigDependencyName(),
],
],
],
]);
$entity3
->save();
$this
->drupalGet($entity1
->toUrl('delete-form'));
$this
->assertSession()
->pageTextContains('Configuration updates');
$this
->assertSession()
->pageTextNotContains('Configuration deletions');
$this
->assertSession()
->pageTextNotContains($entity2
->id());
$this
->assertSession()
->pageTextContains($entity2
->label());
$this
->assertSession()
->pageTextNotContains($entity3
->id());
$this
->drupalGet($entity1
->toUrl('delete-form'));
$this
->submitForm([], 'Delete');
$storage
->resetCache();
$this
->assertNull($storage
->load('entity1'), 'Test entity 1 deleted');
$entity2 = $storage
->load('entity2');
$this
->assertNotEmpty($entity2, 'Entity 2 not deleted');
$this
->assertEquals([], $entity2
->calculateDependencies()
->getDependencies()['config'], 'Entity 2 dependencies updated to remove dependency on Entity1.');
$entity3 = $storage
->load('entity3');
$this
->assertNotEmpty($entity3, 'Entity 3 not deleted');
$this
->assertEquals([
$entity2
->getConfigDependencyName(),
], $entity3
->calculateDependencies()
->getDependencies()['config'], 'Entity 3 still depends on Entity 2.');
}
}