3 namespace Drupal\KernelTests\Core\Config;
5 use Drupal\Core\Config\ConfigImporter;
6 use Drupal\Core\Config\StorageComparer;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\KernelTests\KernelTestBase;
11 * Tests importing configuration which has missing content dependencies.
15 class ConfigImporterMissingContentTest extends KernelTestBase {
18 * Config Importer object used for testing.
20 * @var \Drupal\Core\Config\ConfigImporter
22 protected $configImporter;
29 public static $modules = ['system', 'user', 'entity_test', 'config_test', 'config_import_test'];
31 protected function setUp() {
33 $this->installSchema('system', 'sequences');
34 $this->installEntitySchema('entity_test');
35 $this->installEntitySchema('user');
36 $this->installConfig(['config_test']);
37 // Installing config_test's default configuration pollutes the global
38 // variable being used for recording hook invocations by this test already,
39 // so it has to be cleared out manually.
40 unset($GLOBALS['hook_config_test']);
42 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
44 // Set up the ConfigImporter object for testing.
45 $storage_comparer = new StorageComparer(
46 $this->container->get('config.storage.sync'),
47 $this->container->get('config.storage'),
48 $this->container->get('config.manager')
50 $this->configImporter = new ConfigImporter(
51 $storage_comparer->createChangelist(),
52 $this->container->get('event_dispatcher'),
53 $this->container->get('config.manager'),
54 $this->container->get('lock'),
55 $this->container->get('config.typed'),
56 $this->container->get('module_handler'),
57 $this->container->get('module_installer'),
58 $this->container->get('theme_handler'),
59 $this->container->get('string_translation')
64 * Tests the missing content event is fired.
66 * @see \Drupal\Core\Config\ConfigImporter::processMissingContent()
67 * @see \Drupal\config_import_test\EventSubscriber
69 public function testMissingContent() {
70 \Drupal::state()->set('config_import_test.config_import_missing_content', TRUE);
72 // Update a configuration entity in the sync directory to have a dependency
73 // on two content entities that do not exist.
74 $storage = $this->container->get('config.storage');
75 $sync = $this->container->get('config.storage.sync');
76 $entity_one = EntityTest::create(['name' => 'one']);
77 $entity_two = EntityTest::create(['name' => 'two']);
78 $entity_three = EntityTest::create(['name' => 'three']);
79 $dynamic_name = 'config_test.dynamic.dotted.default';
80 $original_dynamic_data = $storage->read($dynamic_name);
81 // Entity one will be resolved by
82 // \Drupal\config_import_test\EventSubscriber::onConfigImporterMissingContentOne().
83 $original_dynamic_data['dependencies']['content'][] = $entity_one->getConfigDependencyName();
84 // Entity two will be resolved by
85 // \Drupal\config_import_test\EventSubscriber::onConfigImporterMissingContentTwo().
86 $original_dynamic_data['dependencies']['content'][] = $entity_two->getConfigDependencyName();
87 // Entity three will be resolved by
88 // \Drupal\Core\Config\Importer\FinalMissingContentSubscriber.
89 $original_dynamic_data['dependencies']['content'][] = $entity_three->getConfigDependencyName();
90 $sync->write($dynamic_name, $original_dynamic_data);
93 $this->configImporter->reset()->import();
94 $this->assertEqual([], $this->configImporter->getErrors(), 'There were no errors during the import.');
95 $this->assertEqual($entity_one->uuid(), \Drupal::state()->get('config_import_test.config_import_missing_content_one'), 'The missing content event is fired during configuration import.');
96 $this->assertEqual($entity_two->uuid(), \Drupal::state()->get('config_import_test.config_import_missing_content_two'), 'The missing content event is fired during configuration import.');
97 $original_dynamic_data = $storage->read($dynamic_name);
98 $this->assertEqual([$entity_one->getConfigDependencyName(), $entity_two->getConfigDependencyName(), $entity_three->getConfigDependencyName()], $original_dynamic_data['dependencies']['content'], 'The imported configuration entity has the missing content entity dependency.');