function ConfigDependencyTest::testDependencyManagement

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php \Drupal\KernelTests\Core\Config\ConfigDependencyTest::testDependencyManagement()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php \Drupal\KernelTests\Core\Config\ConfigDependencyTest::testDependencyManagement()
  3. 10 core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php \Drupal\KernelTests\Core\Config\ConfigDependencyTest::testDependencyManagement()

Tests creating dependencies on configuration entities.

File

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

Class

ConfigDependencyTest
Tests for configuration dependencies.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testDependencyManagement() : void {
    
    /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
    $config_manager = \Drupal::service('config.manager');
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('config_test');
    // Test dependencies between modules.
    $entity1 = $storage->create([
        'id' => 'entity1',
        'dependencies' => [
            'enforced' => [
                'module' => [
                    'node',
                ],
            ],
        ],
    ]);
    $entity1->save();
    $dependents = $config_manager->findConfigEntityDependencies('module', [
        'node',
    ]);
    $this->assertTrue(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 has a dependency on the Node module.');
    $dependents = $config_manager->findConfigEntityDependencies('module', [
        'config_test',
    ]);
    $this->assertTrue(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 has a dependency on the config_test module.');
    $dependents = $config_manager->findConfigEntityDependencies('module', [
        'views',
    ]);
    $this->assertFalse(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 does not have a dependency on the Views module.');
    // Ensure that the provider of the config entity is not actually written to
    // the dependencies array.
    $raw_config = $this->config('config_test.dynamic.entity1');
    $root_module_dependencies = $raw_config->get('dependencies.module');
    $this->assertEmpty($root_module_dependencies, 'Node module is not written to the root dependencies array as it is enforced.');
    // 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' => [
                    $entity2->getConfigDependencyName(),
                ],
            ],
        ],
    ]);
    $entity3->save();
    $entity4 = $storage->create([
        'id' => 'entity4',
        'dependencies' => [
            'enforced' => [
                'config' => [
                    $entity3->getConfigDependencyName(),
                ],
            ],
        ],
    ]);
    $entity4->save();
    // Test getting $entity1's dependencies as configuration dependency objects.
    $dependents = $config_manager->findConfigEntityDependencies('config', [
        $entity1->getConfigDependencyName(),
    ]);
    $this->assertFalse(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 does not have a dependency on itself.');
    $this->assertTrue(isset($dependents['config_test.dynamic.entity2']), 'config_test.dynamic.entity2 has a dependency on config_test.dynamic.entity1.');
    $this->assertTrue(isset($dependents['config_test.dynamic.entity3']), 'config_test.dynamic.entity3 has a dependency on config_test.dynamic.entity1.');
    $this->assertTrue(isset($dependents['config_test.dynamic.entity4']), 'config_test.dynamic.entity4 has a dependency on config_test.dynamic.entity1.');
    // Test getting $entity2's dependencies as entities.
    $dependents = $config_manager->findConfigEntityDependenciesAsEntities('config', [
        $entity2->getConfigDependencyName(),
    ]);
    $dependent_ids = $this->getDependentIds($dependents);
    $this->assertNotContains('config_test:entity1', $dependent_ids, 'config_test.dynamic.entity1 does not have a dependency on config_test.dynamic.entity1.');
    $this->assertNotContains('config_test:entity2', $dependent_ids, 'config_test.dynamic.entity2 does not have a dependency on itself.');
    $this->assertContains('config_test:entity3', $dependent_ids, 'config_test.dynamic.entity3 has a dependency on config_test.dynamic.entity2.');
    $this->assertContains('config_test:entity4', $dependent_ids, 'config_test.dynamic.entity4 has a dependency on config_test.dynamic.entity2.');
    // Test getting node module's dependencies as configuration dependency
    // objects.
    $dependents = $config_manager->findConfigEntityDependencies('module', [
        'node',
    ]);
    $this->assertTrue(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 has a dependency on the Node module.');
    $this->assertTrue(isset($dependents['config_test.dynamic.entity2']), 'config_test.dynamic.entity2 has a dependency on the Node module.');
    $this->assertTrue(isset($dependents['config_test.dynamic.entity3']), 'config_test.dynamic.entity3 has a dependency on the Node module.');
    $this->assertTrue(isset($dependents['config_test.dynamic.entity4']), 'config_test.dynamic.entity4 has a dependency on the Node module.');
    // Test getting node module's dependencies as configuration dependency
    // objects after making $entity3 also dependent on node module but $entity1
    // no longer depend on node module.
    $entity1->setEnforcedDependencies([])
        ->save();
    $entity3->setEnforcedDependencies([
        'module' => [
            'node',
        ],
        'config' => [
            $entity2->getConfigDependencyName(),
        ],
    ])
        ->save();
    $dependents = $config_manager->findConfigEntityDependencies('module', [
        'node',
    ]);
    $this->assertFalse(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 does not have a dependency on the Node module.');
    $this->assertFalse(isset($dependents['config_test.dynamic.entity2']), 'config_test.dynamic.entity2 does not have a dependency on the Node module.');
    $this->assertTrue(isset($dependents['config_test.dynamic.entity3']), 'config_test.dynamic.entity3 has a dependency on the Node module.');
    $this->assertTrue(isset($dependents['config_test.dynamic.entity4']), 'config_test.dynamic.entity4 has a dependency on the Node module.');
    // Test dependency on a content entity.
    $entity_test = EntityTest::create([
        'name' => $this->randomString(),
        'type' => 'entity_test',
    ]);
    $entity_test->save();
    $entity2->setEnforcedDependencies([
        'config' => [
            $entity1->getConfigDependencyName(),
        ],
        'content' => [
            $entity_test->getConfigDependencyName(),
        ],
    ])
        ->save();
    $dependents = $config_manager->findConfigEntityDependencies('content', [
        $entity_test->getConfigDependencyName(),
    ]);
    $this->assertFalse(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 does not have a dependency on the content entity.');
    $this->assertTrue(isset($dependents['config_test.dynamic.entity2']), 'config_test.dynamic.entity2 has a dependency on the content entity.');
    $this->assertTrue(isset($dependents['config_test.dynamic.entity3']), 'config_test.dynamic.entity3 has a dependency on the content entity (via entity2).');
    $this->assertTrue(isset($dependents['config_test.dynamic.entity4']), 'config_test.dynamic.entity4 has a dependency on the content entity (via entity3).');
    // Create a configuration entity of a different type with the same ID as one
    // of the entities already created.
    $alt_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('config_query_test');
    $alt_storage->create([
        'id' => 'entity1',
        'dependencies' => [
            'enforced' => [
                'config' => [
                    $entity1->getConfigDependencyName(),
                ],
            ],
        ],
    ])
        ->save();
    $alt_storage->create([
        'id' => 'entity2',
        'dependencies' => [
            'enforced' => [
                'module' => [
                    'views',
                ],
            ],
        ],
    ])
        ->save();
    $dependents = $config_manager->findConfigEntityDependenciesAsEntities('config', [
        $entity1->getConfigDependencyName(),
    ]);
    $dependent_ids = $this->getDependentIds($dependents);
    $this->assertNotContains('config_test:entity1', $dependent_ids, 'config_test.dynamic.entity1 does not have a dependency on itself.');
    $this->assertContains('config_test:entity2', $dependent_ids, 'config_test.dynamic.entity2 has a dependency on config_test.dynamic.entity1.');
    $this->assertContains('config_test:entity3', $dependent_ids, 'config_test.dynamic.entity3 has a dependency on config_test.dynamic.entity1.');
    $this->assertContains('config_test:entity4', $dependent_ids, 'config_test.dynamic.entity4 has a dependency on config_test.dynamic.entity1.');
    $this->assertContains('config_query_test:entity1', $dependent_ids, 'config_query_test.dynamic.entity1 has a dependency on config_test.dynamic.entity1.');
    $this->assertNotContains('config_query_test:entity2', $dependent_ids, 'config_query_test.dynamic.entity2 does not have a dependency on config_test.dynamic.entity1.');
    $dependents = $config_manager->findConfigEntityDependenciesAsEntities('module', [
        'node',
        'views',
    ]);
    $dependent_ids = $this->getDependentIds($dependents);
    $this->assertNotContains('config_test:entity1', $dependent_ids, 'config_test.dynamic.entity1 does not have a dependency on Views or Node.');
    $this->assertNotContains('config_test:entity2', $dependent_ids, 'config_test.dynamic.entity2 does not have a dependency on Views or Node.');
    $this->assertContains('config_test:entity3', $dependent_ids, 'config_test.dynamic.entity3 has a dependency on Views or Node.');
    $this->assertContains('config_test:entity4', $dependent_ids, 'config_test.dynamic.entity4 has a dependency on Views or Node.');
    $this->assertNotContains('config_query_test:entity1', $dependent_ids, 'config_test.query.entity1 does not have a dependency on Views or Node.');
    $this->assertContains('config_query_test:entity2', $dependent_ids, 'config_test.query.entity2 has a dependency on Views or Node.');
    $dependents = $config_manager->findConfigEntityDependenciesAsEntities('module', [
        'config_test',
    ]);
    $dependent_ids = $this->getDependentIds($dependents);
    $this->assertContains('config_test:entity1', $dependent_ids, 'config_test.dynamic.entity1 has a dependency on config_test module.');
    $this->assertContains('config_test:entity2', $dependent_ids, 'config_test.dynamic.entity2 has a dependency on config_test module.');
    $this->assertContains('config_test:entity3', $dependent_ids, 'config_test.dynamic.entity3 has a dependency on config_test module.');
    $this->assertContains('config_test:entity4', $dependent_ids, 'config_test.dynamic.entity4 has a dependency on config_test module.');
    $this->assertContains('config_query_test:entity1', $dependent_ids, 'config_test.query.entity1 has a dependency on config_test module.');
    $this->assertContains('config_query_test:entity2', $dependent_ids, 'config_test.query.entity2 has a dependency on config_test module.');
    // Test the ability to find missing content dependencies.
    $missing_dependencies = $config_manager->findMissingContentDependencies();
    $this->assertEquals([], $missing_dependencies);
    $expected = [
        $entity_test->uuid() => [
            'entity_type' => 'entity_test',
            'bundle' => $entity_test->bundle(),
            'uuid' => $entity_test->uuid(),
        ],
    ];
    // Delete the content entity so that is it now missing.
    $entity_test->delete();
    $missing_dependencies = $config_manager->findMissingContentDependencies();
    $this->assertEquals($expected, $missing_dependencies);
    // Add a fake missing dependency to ensure multiple missing dependencies
    // work.
    $entity1->setEnforcedDependencies([
        'content' => [
            $entity_test->getConfigDependencyName(),
            'entity_test:bundle:uuid',
        ],
    ])
        ->save();
    $expected['uuid'] = [
        'entity_type' => 'entity_test',
        'bundle' => 'bundle',
        'uuid' => 'uuid',
    ];
    $missing_dependencies = $config_manager->findMissingContentDependencies();
    $this->assertEquals($expected, $missing_dependencies);
}

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