ConfigImporterMissingContentTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config;
  3. use Drupal\Core\Config\ConfigImporter;
  4. use Drupal\Core\Config\StorageComparer;
  5. use Drupal\entity_test\Entity\EntityTest;
  6. use Drupal\KernelTests\KernelTestBase;
  7. /**
  8. * Tests importing configuration which has missing content dependencies.
  9. *
  10. * @group config
  11. */
  12. class ConfigImporterMissingContentTest extends KernelTestBase {
  13. /**
  14. * Config Importer object used for testing.
  15. *
  16. * @var \Drupal\Core\Config\ConfigImporter
  17. */
  18. protected $configImporter;
  19. /**
  20. * Modules to enable.
  21. *
  22. * @var array
  23. */
  24. public static $modules = [
  25. 'system',
  26. 'user',
  27. 'entity_test',
  28. 'config_test',
  29. 'config_import_test',
  30. ];
  31. protected function setUp() {
  32. parent::setUp();
  33. $this->installSchema('system', 'sequences');
  34. $this->installEntitySchema('entity_test');
  35. $this->installEntitySchema('user');
  36. $this->installConfig(['system', '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']);
  41. $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
  42. // Set up the ConfigImporter object for testing.
  43. $storage_comparer = new StorageComparer(
  44. $this->container->get('config.storage.sync'),
  45. $this->container->get('config.storage')
  46. );
  47. $this->configImporter = new ConfigImporter(
  48. $storage_comparer->createChangelist(),
  49. $this->container->get('event_dispatcher'),
  50. $this->container->get('config.manager'),
  51. $this->container->get('lock'),
  52. $this->container->get('config.typed'),
  53. $this->container->get('module_handler'),
  54. $this->container->get('module_installer'),
  55. $this->container->get('theme_handler'),
  56. $this->container->get('string_translation'),
  57. $this->container->get('extension.list.module')
  58. );
  59. }
  60. /**
  61. * Tests the missing content event is fired.
  62. *
  63. * @see \Drupal\Core\Config\ConfigImporter::processMissingContent()
  64. * @see \Drupal\config_import_test\EventSubscriber
  65. */
  66. public function testMissingContent() {
  67. \Drupal::state()->set('config_import_test.config_import_missing_content', TRUE);
  68. // Update a configuration entity in the sync directory to have a dependency
  69. // on two content entities that do not exist.
  70. $storage = $this->container->get('config.storage');
  71. $sync = $this->container->get('config.storage.sync');
  72. $entity_one = EntityTest::create(['name' => 'one']);
  73. $entity_two = EntityTest::create(['name' => 'two']);
  74. $entity_three = EntityTest::create(['name' => 'three']);
  75. $dynamic_name = 'config_test.dynamic.dotted.default';
  76. $original_dynamic_data = $storage->read($dynamic_name);
  77. // Entity one will be resolved by
  78. // \Drupal\config_import_test\EventSubscriber::onConfigImporterMissingContentOne().
  79. $original_dynamic_data['dependencies']['content'][] = $entity_one->getConfigDependencyName();
  80. // Entity two will be resolved by
  81. // \Drupal\config_import_test\EventSubscriber::onConfigImporterMissingContentTwo().
  82. $original_dynamic_data['dependencies']['content'][] = $entity_two->getConfigDependencyName();
  83. // Entity three will be resolved by
  84. // \Drupal\Core\Config\Importer\FinalMissingContentSubscriber.
  85. $original_dynamic_data['dependencies']['content'][] = $entity_three->getConfigDependencyName();
  86. $sync->write($dynamic_name, $original_dynamic_data);
  87. // Import.
  88. $this->configImporter->reset()->import();
  89. $this->assertEqual([], $this->configImporter->getErrors(), 'There were no errors during the import.');
  90. $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.');
  91. $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.');
  92. $original_dynamic_data = $storage->read($dynamic_name);
  93. $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.');
  94. }
  95. }