ConfigInstallTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config;
  3. use Drupal\Core\Config\InstallStorage;
  4. use Drupal\Core\Config\PreExistingConfigException;
  5. use Drupal\Core\Config\UnmetDependenciesException;
  6. use Drupal\KernelTests\KernelTestBase;
  7. use PHPUnit\Framework\Error\Warning;
  8. /**
  9. * Tests installation of configuration objects in installation functionality.
  10. *
  11. * @group config
  12. * @see \Drupal\Core\Config\ConfigInstaller
  13. */
  14. class ConfigInstallTest extends KernelTestBase {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static $modules = ['system'];
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUp() {
  23. parent::setUp();
  24. // Ensure the global variable being asserted by this test does not exist;
  25. // a previous test executed in this request/process might have set it.
  26. unset($GLOBALS['hook_config_test']);
  27. }
  28. /**
  29. * Tests module installation.
  30. */
  31. public function testModuleInstallation() {
  32. $default_config = 'config_test.system';
  33. $default_configuration_entity = 'config_test.dynamic.dotted.default';
  34. // Verify that default module config does not exist before installation yet.
  35. $config = $this->config($default_config);
  36. $this->assertIdentical($config->isNew(), TRUE);
  37. $config = $this->config($default_configuration_entity);
  38. $this->assertIdentical($config->isNew(), TRUE);
  39. // Ensure that schema provided by modules that are not installed is not
  40. // available.
  41. $this->assertFalse(\Drupal::service('config.typed')->hasConfigSchema('config_schema_test.someschema'), 'Configuration schema for config_schema_test.someschema does not exist.');
  42. // Install the test module.
  43. $this->installModules(['config_test']);
  44. // Verify that default module config exists.
  45. \Drupal::configFactory()->reset($default_config);
  46. \Drupal::configFactory()->reset($default_configuration_entity);
  47. $config = $this->config($default_config);
  48. $this->assertIdentical($config->isNew(), FALSE);
  49. $config = $this->config($default_configuration_entity);
  50. $this->assertIdentical($config->isNew(), FALSE);
  51. // Verify that config_test API hooks were invoked for the dynamic default
  52. // configuration entity.
  53. $this->assertFalse(isset($GLOBALS['hook_config_test']['load']));
  54. $this->assertTrue(isset($GLOBALS['hook_config_test']['presave']));
  55. $this->assertTrue(isset($GLOBALS['hook_config_test']['insert']));
  56. $this->assertFalse(isset($GLOBALS['hook_config_test']['update']));
  57. $this->assertFalse(isset($GLOBALS['hook_config_test']['predelete']));
  58. $this->assertFalse(isset($GLOBALS['hook_config_test']['delete']));
  59. // Install the schema test module.
  60. $this->enableModules(['config_schema_test']);
  61. $this->installConfig(['config_schema_test']);
  62. // After module installation the new schema should exist.
  63. $this->assertTrue(\Drupal::service('config.typed')->hasConfigSchema('config_schema_test.someschema'), 'Configuration schema for config_schema_test.someschema exists.');
  64. // Test that uninstalling configuration removes configuration schema.
  65. $this->config('core.extension')->set('module', [])->save();
  66. \Drupal::service('config.manager')->uninstall('module', 'config_test');
  67. $this->assertFalse(\Drupal::service('config.typed')->hasConfigSchema('config_schema_test.someschema'), 'Configuration schema for config_schema_test.someschema does not exist.');
  68. }
  69. /**
  70. * Tests that collections are ignored if the event does not return anything.
  71. */
  72. public function testCollectionInstallationNoCollections() {
  73. // Install the test module.
  74. $this->enableModules(['config_collection_install_test']);
  75. $this->installConfig(['config_collection_install_test']);
  76. /** @var \Drupal\Core\Config\StorageInterface $active_storage */
  77. $active_storage = \Drupal::service('config.storage');
  78. $this->assertEqual([], $active_storage->getAllCollectionNames());
  79. }
  80. /**
  81. * Tests config objects in collections are installed as expected.
  82. */
  83. public function testCollectionInstallationCollections() {
  84. $collections = [
  85. 'another_collection',
  86. 'collection.test1',
  87. 'collection.test2',
  88. ];
  89. // Set the event listener to return three possible collections.
  90. // @see \Drupal\config_collection_install_test\EventSubscriber
  91. \Drupal::state()->set('config_collection_install_test.collection_names', $collections);
  92. // Install the test module.
  93. $this->enableModules(['config_collection_install_test']);
  94. $this->installConfig(['config_collection_install_test']);
  95. /** @var \Drupal\Core\Config\StorageInterface $active_storage */
  96. $active_storage = \Drupal::service('config.storage');
  97. $this->assertEqual($collections, $active_storage->getAllCollectionNames());
  98. foreach ($collections as $collection) {
  99. $collection_storage = $active_storage->createCollection($collection);
  100. $data = $collection_storage->read('config_collection_install_test.test');
  101. $this->assertEqual($collection, $data['collection']);
  102. }
  103. // Tests that clashing configuration in collections is detected.
  104. try {
  105. \Drupal::service('module_installer')->install(['config_collection_clash_install_test']);
  106. $this->fail('Expected PreExistingConfigException not thrown.');
  107. }
  108. catch (PreExistingConfigException $e) {
  109. $this->assertEqual($e->getExtension(), 'config_collection_clash_install_test');
  110. $this->assertEqual($e->getConfigObjects(), [
  111. 'another_collection' => ['config_collection_install_test.test'],
  112. 'collection.test1' => ['config_collection_install_test.test'],
  113. 'collection.test2' => ['config_collection_install_test.test'],
  114. ]);
  115. $this->assertEqual($e->getMessage(), 'Configuration objects (another_collection/config_collection_install_test.test, collection/test1/config_collection_install_test.test, collection/test2/config_collection_install_test.test) provided by config_collection_clash_install_test already exist in active configuration');
  116. }
  117. // Test that the we can use the config installer to install all the
  118. // available default configuration in a particular collection for enabled
  119. // extensions.
  120. \Drupal::service('config.installer')->installCollectionDefaultConfig('entity');
  121. // The 'entity' collection will not exist because the 'config_test' module
  122. // is not enabled.
  123. $this->assertEqual($collections, $active_storage->getAllCollectionNames());
  124. // Enable the 'config_test' module and try again.
  125. $this->enableModules(['config_test']);
  126. \Drupal::service('config.installer')->installCollectionDefaultConfig('entity');
  127. $collections[] = 'entity';
  128. $this->assertEqual($collections, $active_storage->getAllCollectionNames());
  129. $collection_storage = $active_storage->createCollection('entity');
  130. $data = $collection_storage->read('config_test.dynamic.dotted.default');
  131. $this->assertSame(['label' => 'entity'], $data);
  132. // Test that the config manager uninstalls configuration from collections
  133. // as expected.
  134. \Drupal::service('config.manager')->uninstall('module', 'config_collection_install_test');
  135. $this->assertEqual(['entity'], $active_storage->getAllCollectionNames());
  136. \Drupal::service('config.manager')->uninstall('module', 'config_test');
  137. $this->assertEqual([], $active_storage->getAllCollectionNames());
  138. }
  139. /**
  140. * Tests collections which do not support config entities install correctly.
  141. *
  142. * Config entity detection during config installation is done by matching
  143. * config name prefixes. If a collection provides a configuration with a
  144. * matching name but does not support config entities it should be created
  145. * using simple configuration.
  146. */
  147. public function testCollectionInstallationCollectionConfigEntity() {
  148. $collections = [
  149. 'entity',
  150. ];
  151. \Drupal::state()->set('config_collection_install_test.collection_names', $collections);
  152. // Install the test module.
  153. $this->installModules(['config_test', 'config_collection_install_test']);
  154. /** @var \Drupal\Core\Config\StorageInterface $active_storage */
  155. $active_storage = \Drupal::service('config.storage');
  156. $this->assertEqual($collections, $active_storage->getAllCollectionNames());
  157. $collection_storage = $active_storage->createCollection('entity');
  158. // The config_test.dynamic.dotted.default configuration object saved in the
  159. // active store should be a configuration entity complete with UUID. Because
  160. // the entity collection does not support configuration entities the
  161. // configuration object stored there with the same name should only contain
  162. // a label.
  163. $name = 'config_test.dynamic.dotted.default';
  164. $data = $active_storage->read($name);
  165. $this->assertTrue(isset($data['uuid']));
  166. $data = $collection_storage->read($name);
  167. $this->assertSame(['label' => 'entity'], $data);
  168. }
  169. /**
  170. * Tests the configuration with unmet dependencies is not installed.
  171. */
  172. public function testDependencyChecking() {
  173. $this->installModules(['config_test']);
  174. try {
  175. $this->installModules(['config_install_dependency_test']);
  176. $this->fail('Expected UnmetDependenciesException not thrown.');
  177. }
  178. catch (UnmetDependenciesException $e) {
  179. $this->assertEqual($e->getExtension(), 'config_install_dependency_test');
  180. $this->assertEqual($e->getConfigObjects(), ['config_test.dynamic.other_module_test_with_dependency' => ['config_other_module_config_test', 'config_test.dynamic.dotted.english']]);
  181. $this->assertEqual($e->getMessage(), 'Configuration objects provided by <em class="placeholder">config_install_dependency_test</em> have unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test, config_test.dynamic.dotted.english)</em>');
  182. }
  183. try {
  184. $this->installModules(['config_install_double_dependency_test']);
  185. $this->fail('Expected UnmetDependenciesException not thrown.');
  186. }
  187. catch (UnmetDependenciesException $e) {
  188. $this->assertEquals('config_install_double_dependency_test', $e->getExtension());
  189. $this->assertEquals(['config_test.dynamic.other_module_test_with_dependency' => ['config_other_module_config_test', 'config_test.dynamic.dotted.english']], $e->getConfigObjects());
  190. $this->assertEquals('Configuration objects provided by <em class="placeholder">config_install_double_dependency_test</em> have unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test, config_test.dynamic.dotted.english)</em>', $e->getMessage());
  191. }
  192. $this->installModules(['config_test_language']);
  193. try {
  194. $this->installModules(['config_install_dependency_test']);
  195. $this->fail('Expected UnmetDependenciesException not thrown.');
  196. }
  197. catch (UnmetDependenciesException $e) {
  198. $this->assertEqual($e->getExtension(), 'config_install_dependency_test');
  199. $this->assertEqual($e->getConfigObjects(), ['config_test.dynamic.other_module_test_with_dependency' => ['config_other_module_config_test']]);
  200. $this->assertEqual($e->getMessage(), 'Configuration objects provided by <em class="placeholder">config_install_dependency_test</em> have unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test)</em>');
  201. }
  202. $this->installModules(['config_other_module_config_test']);
  203. $this->installModules(['config_install_dependency_test']);
  204. $entity = \Drupal::entityTypeManager()->getStorage('config_test')->load('other_module_test_with_dependency');
  205. $this->assertNotEmpty($entity, 'The config_test.dynamic.other_module_test_with_dependency configuration has been created during install.');
  206. // Ensure that dependencies can be added during module installation by
  207. // hooks.
  208. $this->assertSame('config_install_dependency_test', $entity->getDependencies()['module'][0]);
  209. }
  210. /**
  211. * Tests imported configuration entities with and without language information.
  212. */
  213. public function testLanguage() {
  214. $this->installModules(['config_test_language']);
  215. // Test imported configuration with implicit language code.
  216. $storage = new InstallStorage();
  217. $data = $storage->read('config_test.dynamic.dotted.english');
  218. $this->assertTrue(!isset($data['langcode']));
  219. $this->assertEqual(
  220. $this->config('config_test.dynamic.dotted.english')->get('langcode'),
  221. 'en'
  222. );
  223. // Test imported configuration with explicit language code.
  224. $data = $storage->read('config_test.dynamic.dotted.french');
  225. $this->assertEqual($data['langcode'], 'fr');
  226. $this->assertEqual(
  227. $this->config('config_test.dynamic.dotted.french')->get('langcode'),
  228. 'fr'
  229. );
  230. }
  231. /**
  232. * Tests installing configuration where the filename and ID do not match.
  233. */
  234. public function testIdMisMatch() {
  235. $this->expectException(Warning::class);
  236. $this->expectExceptionMessage('The configuration name "config_test.dynamic.no_id_match" does not match the ID "does_not_match"');
  237. $this->installModules(['config_test_id_mismatch']);
  238. }
  239. /**
  240. * Installs a module.
  241. *
  242. * @param array $modules
  243. * The module names.
  244. */
  245. protected function installModules(array $modules) {
  246. $this->container->get('module_installer')->install($modules);
  247. $this->container = \Drupal::getContainer();
  248. }
  249. }