DefaultConfigTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Drupal\KernelTests\Config;
  3. use Drupal\Core\Config\Entity\ConfigEntityDependency;
  4. use Drupal\Core\Config\FileStorage;
  5. use Drupal\Core\Config\InstallStorage;
  6. use Drupal\Core\Config\StorageInterface;
  7. use Drupal\KernelTests\AssertConfigTrait;
  8. use Drupal\KernelTests\FileSystemModuleDiscoveryDataProviderTrait;
  9. use Drupal\KernelTests\KernelTestBase;
  10. /**
  11. * Tests that the installed config matches the default config.
  12. *
  13. * @group Config
  14. */
  15. class DefaultConfigTest extends KernelTestBase {
  16. use AssertConfigTrait;
  17. use FileSystemModuleDiscoveryDataProviderTrait;
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected static $timeLimit = 500;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static $modules = ['system', 'user', 'path_alias'];
  26. /**
  27. * The following config entries are changed on module install.
  28. *
  29. * Comparing them does not make sense.
  30. *
  31. * @todo Figure out why simpletest.settings is not installed.
  32. *
  33. * @var array
  34. */
  35. public static $skippedConfig = [
  36. 'locale.settings' => ['path: '],
  37. 'syslog.settings' => ['facility: '],
  38. 'simpletest.settings' => TRUE,
  39. ];
  40. /**
  41. * Tests if installed config is equal to the exported config.
  42. *
  43. * @dataProvider coreModuleListDataProvider
  44. */
  45. public function testModuleConfig($module) {
  46. // System and user are required in order to be able to install some of the
  47. // other modules. Therefore they are put into static::$modules, which though
  48. // doesn't install config files, so import those config files explicitly. Do
  49. // this for all tests in case optional configuration depends on it.
  50. $this->installConfig(['system', 'user']);
  51. $module_path = drupal_get_path('module', $module) . '/';
  52. /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
  53. $module_installer = $this->container->get('module_installer');
  54. $module_config_storage = new FileStorage($module_path . InstallStorage::CONFIG_INSTALL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION);
  55. $optional_config_storage = new FileStorage($module_path . InstallStorage::CONFIG_OPTIONAL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION);
  56. if (empty($optional_config_storage->listAll()) && empty($module_config_storage->listAll())) {
  57. $this->markTestSkipped("$module has no configuration to test");
  58. }
  59. // Work out any additional modules and themes that need installing to create
  60. // an optional config.
  61. $modules_to_install = [$module];
  62. $themes_to_install = [];
  63. foreach ($optional_config_storage->listAll() as $config_name) {
  64. $data = $optional_config_storage->read($config_name);
  65. $dependency = new ConfigEntityDependency($config_name, $data);
  66. $modules_to_install = array_merge($modules_to_install, $dependency->getDependencies('module'));
  67. $themes_to_install = array_merge($themes_to_install, $dependency->getDependencies('theme'));
  68. }
  69. // Remove core because that cannot be installed.
  70. $modules_to_install = array_diff(array_unique($modules_to_install), ['core']);
  71. $module_installer->install($modules_to_install);
  72. $this->container->get('theme_installer')->install(array_unique($themes_to_install));
  73. // Test configuration in the module's config/install directory.
  74. $this->doTestsOnConfigStorage($module_config_storage, $module);
  75. // Test configuration in the module's config/optional directory.
  76. $this->doTestsOnConfigStorage($optional_config_storage, $module);
  77. }
  78. /**
  79. * Tests that default config matches the installed config.
  80. *
  81. * @param \Drupal\Core\Config\StorageInterface $default_config_storage
  82. * The default config storage to test.
  83. */
  84. protected function doTestsOnConfigStorage(StorageInterface $default_config_storage, $module) {
  85. /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
  86. $config_manager = $this->container->get('config.manager');
  87. // Just connect directly to the config table so we don't need to worry about
  88. // the cache layer.
  89. $active_config_storage = $this->container->get('config.storage');
  90. foreach ($default_config_storage->listAll() as $config_name) {
  91. if ($active_config_storage->exists($config_name)) {
  92. // If it is a config entity re-save it. This ensures that any
  93. // recalculation of dependencies does not cause config change.
  94. if ($entity_type = $config_manager->getEntityTypeIdByName($config_name)) {
  95. $entity_storage = $config_manager
  96. ->getEntityTypeManager()
  97. ->getStorage($entity_type);
  98. $id = $entity_storage->getIDFromConfigName($config_name, $entity_storage->getEntityType()
  99. ->getConfigPrefix());
  100. $entity_storage->load($id)->calculateDependencies()->save();
  101. }
  102. $result = $config_manager->diff($default_config_storage, $active_config_storage, $config_name);
  103. // ::assertConfigDiff will throw an exception if the configuration is
  104. // different.
  105. $this->assertNull($this->assertConfigDiff($result, $config_name, static::$skippedConfig));
  106. }
  107. else {
  108. $info = $this->container->get('extension.list.module')->getExtensionInfo($module);
  109. if (!isset($info['package']) || $info['package'] !== 'Core (Experimental)') {
  110. $this->fail("$config_name provided by $module does not exist after installing all dependencies");
  111. }
  112. }
  113. }
  114. }
  115. }