function ConfigDependencyTest::testDependencyOrder

Tests that config dependency ordering.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php, line 647

Class

ConfigDependencyTest
Tests for configuration dependencies.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testDependencyOrder() : void {
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('config_test');
    // Test dependencies between modules.
    $entity1 = $storage->create([
        'id' => 'entity1',
    ]);
    $entity1->save();
    // Create additional entities to test dependencies on config entities.
    $entity2 = $storage->create([
        'id' => 'entity2',
        'dependencies' => [
            'enforced' => [
                'config' => [
                    $entity1->getConfigDependencyName(),
                ],
            ],
        ],
    ]);
    $entity2->save();
    $entity3 = $storage->create([
        'id' => 'entity3',
        'dependencies' => [
            'enforced' => [
                'config' => [
                    $entity1->getConfigDependencyName(),
                ],
            ],
        ],
    ]);
    $entity3->save();
    // Include a role entity to test ordering when dependencies have multiple
    // entity types.
    $role = Role::create([
        'id' => 'test_role',
        'label' => 'Test role',
        // This adds an implicit dependency on $entity 2, and hence also $entity1,
        // to the role.
'permissions' => [
            "permission with {$entity2->getConfigDependencyName()} dependency",
        ],
    ]);
    $role->save();
    $entity4 = $storage->create([
        'id' => 'entity4',
        'dependencies' => [
            'enforced' => [
                'config' => [
                    // Add dependencies to $entity3 and the role so that the $entity4
                    // should be last to be processed when handling dependency removal.
                    // The role should be processed after $entity1 and $entity2, but
                    // before $entity4.
$entity3->getConfigDependencyName(),
                    $role->getConfigDependencyName(),
                ],
            ],
        ],
    ]);
    $entity4->save();
    // Create scenario where entity1 is deleted, but all the config_test
    // entities depending on entity1 are fixed instead of being deleted. This
    // means that entity2 is not deleted, so the role should not lose the
    // permission depending on entity2.
    \Drupal::state()->set('config_test.fix_dependencies', [
        'config_test.dynamic.entity1',
    ]);
    $entity1->delete();
    $role = Role::load('test_role');
    $this->assertNotNull($role);
    $this->assertTrue($role->hasPermission("permission with {$entity2->getConfigDependencyName()} dependency"));
}

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