ConfigImporterMissingContentTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = ['system', 'user', 'entity_test', 'config_test', 'config_import_test'];
  25. protected function setUp() {
  26. parent::setUp();
  27. $this->installSchema('system', 'sequences');
  28. $this->installEntitySchema('entity_test');
  29. $this->installEntitySchema('user');
  30. $this->installConfig(['config_test']);
  31. // Installing config_test's default configuration pollutes the global
  32. // variable being used for recording hook invocations by this test already,
  33. // so it has to be cleared out manually.
  34. unset($GLOBALS['hook_config_test']);
  35. $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
  36. // Set up the ConfigImporter object for testing.
  37. $storage_comparer = new StorageComparer(
  38. $this->container->get('config.storage.sync'),
  39. $this->container->get('config.storage'),
  40. $this->container->get('config.manager')
  41. );
  42. $this->configImporter = new ConfigImporter(
  43. $storage_comparer->createChangelist(),
  44. $this->container->get('event_dispatcher'),
  45. $this->container->get('config.manager'),
  46. $this->container->get('lock'),
  47. $this->container->get('config.typed'),
  48. $this->container->get('module_handler'),
  49. $this->container->get('module_installer'),
  50. $this->container->get('theme_handler'),
  51. $this->container->get('string_translation')
  52. );
  53. }
  54. /**
  55. * Tests the missing content event is fired.
  56. *
  57. * @see \Drupal\Core\Config\ConfigImporter::processMissingContent()
  58. * @see \Drupal\config_import_test\EventSubscriber
  59. */
  60. public function testMissingContent() {
  61. \Drupal::state()->set('config_import_test.config_import_missing_content', TRUE);
  62. // Update a configuration entity in the sync directory to have a dependency
  63. // on two content entities that do not exist.
  64. $storage = $this->container->get('config.storage');
  65. $sync = $this->container->get('config.storage.sync');
  66. $entity_one = EntityTest::create(['name' => 'one']);
  67. $entity_two = EntityTest::create(['name' => 'two']);
  68. $entity_three = EntityTest::create(['name' => 'three']);
  69. $dynamic_name = 'config_test.dynamic.dotted.default';
  70. $original_dynamic_data = $storage->read($dynamic_name);
  71. // Entity one will be resolved by
  72. // \Drupal\config_import_test\EventSubscriber::onConfigImporterMissingContentOne().
  73. $original_dynamic_data['dependencies']['content'][] = $entity_one->getConfigDependencyName();
  74. // Entity two will be resolved by
  75. // \Drupal\config_import_test\EventSubscriber::onConfigImporterMissingContentTwo().
  76. $original_dynamic_data['dependencies']['content'][] = $entity_two->getConfigDependencyName();
  77. // Entity three will be resolved by
  78. // \Drupal\Core\Config\Importer\FinalMissingContentSubscriber.
  79. $original_dynamic_data['dependencies']['content'][] = $entity_three->getConfigDependencyName();
  80. $sync->write($dynamic_name, $original_dynamic_data);
  81. // Import.
  82. $this->configImporter->reset()->import();
  83. $this->assertEqual([], $this->configImporter->getErrors(), 'There were no errors during the import.');
  84. $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.');
  85. $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.');
  86. $original_dynamic_data = $storage->read($dynamic_name);
  87. $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.');
  88. }
  89. }