3 namespace Drupal\Tests\config\Functional;
5 use Drupal\Core\Config\StorageComparer;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\shortcut\Entity\Shortcut;
8 use Drupal\taxonomy\Entity\Term;
9 use Drupal\Tests\SchemaCheckTestTrait;
10 use Drupal\Tests\system\Functional\Module\ModuleTestBase;
13 * Tests the largest configuration import possible with all available modules.
17 class ConfigImportAllTest extends ModuleTestBase {
19 use SchemaCheckTestTrait;
22 * A user with the 'synchronize configuration' permission.
24 * @var \Drupal\user\UserInterface
29 * The profile to install as a basis for testing.
31 * Using the standard profile as this has a lot of additional configuration.
35 protected $profile = 'standard';
37 protected function setUp() {
40 $this->webUser = $this->drupalCreateUser(['synchronize configuration']);
41 $this->drupalLogin($this->webUser);
45 * Tests that a fixed set of modules can be installed and uninstalled.
47 public function testInstallUninstall() {
49 // Get a list of modules to enable.
50 $all_modules = system_rebuild_module_data();
51 $all_modules = array_filter($all_modules, function ($module) {
52 // Filter contrib, hidden, already enabled modules and modules in the
54 if ($module->origin !== 'core' || !empty($module->info['hidden']) || $module->status == TRUE || $module->info['package'] == 'Testing') {
60 // Install every module possible.
61 \Drupal::service('module_installer')->install(array_keys($all_modules));
63 $this->assertModules(array_keys($all_modules), TRUE);
64 foreach ($all_modules as $module => $info) {
65 $this->assertModuleConfig($module);
66 $this->assertModuleTablesExist($module);
69 // Export active config to sync.
70 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
75 // Delete every field on the site so all modules can be uninstalled. For
76 // example, if a comment field exists then module becomes required and can
77 // not be uninstalled.
79 $field_storages = \Drupal::entityManager()->getStorage('field_storage_config')->loadMultiple();
80 \Drupal::entityManager()->getStorage('field_storage_config')->delete($field_storages);
82 field_purge_batch(1000);
85 $terms = Term::loadMultiple();
86 entity_delete_multiple('taxonomy_term', array_keys($terms));
88 // Delete all filter formats.
89 $filters = FilterFormat::loadMultiple();
90 entity_delete_multiple('filter_format', array_keys($filters));
92 // Delete any shortcuts so the shortcut module can be uninstalled.
93 $shortcuts = Shortcut::loadMultiple();
94 entity_delete_multiple('shortcut', array_keys($shortcuts));
97 $all_modules = system_rebuild_module_data();
99 // Ensure that only core required modules and the install profile can not be uninstalled.
100 $validation_reasons = \Drupal::service('module_installer')->validateUninstall(array_keys($all_modules));
101 $this->assertEqual(['standard', 'system', 'user'], array_keys($validation_reasons));
103 $modules_to_uninstall = array_filter($all_modules, function ($module) use ($validation_reasons) {
104 // Filter required and not enabled modules.
105 if (!empty($module->info['required']) || $module->status == FALSE) {
111 // Can not uninstall config and use admin/config/development/configuration!
112 unset($modules_to_uninstall['config']);
114 $this->assertTrue(isset($modules_to_uninstall['comment']), 'The comment module will be disabled');
115 $this->assertTrue(isset($modules_to_uninstall['file']), 'The File module will be disabled');
116 $this->assertTrue(isset($modules_to_uninstall['editor']), 'The Editor module will be disabled');
118 // Uninstall all modules that can be uninstalled.
119 \Drupal::service('module_installer')->uninstall(array_keys($modules_to_uninstall));
121 $this->assertModules(array_keys($modules_to_uninstall), FALSE);
122 foreach ($modules_to_uninstall as $module => $info) {
123 $this->assertNoModuleConfig($module);
124 $this->assertModuleTablesDoNotExist($module);
127 // Import the configuration thereby re-installing all the modules.
128 $this->drupalPostForm('admin/config/development/configuration', [], t('Import all'));
129 // Modules have been installed that have services.
130 $this->rebuildContainer();
132 // Check that there are no errors.
133 $this->assertIdentical($this->configImporter()->getErrors(), []);
135 // Check that all modules that were uninstalled are now reinstalled.
136 $this->assertModules(array_keys($modules_to_uninstall), TRUE);
137 foreach ($modules_to_uninstall as $module => $info) {
138 $this->assertModuleConfig($module);
139 $this->assertModuleTablesExist($module);
142 // Ensure that we have no configuration changes to import.
143 $storage_comparer = new StorageComparer(
144 $this->container->get('config.storage.sync'),
145 $this->container->get('config.storage'),
146 $this->container->get('config.manager')
148 $this->assertIdentical($storage_comparer->createChangelist()->getChangelist(), $storage_comparer->getEmptyChangelist());
150 // Now we have all configuration imported, test all of them for schema
151 // conformance. Ensures all imported default configuration is valid when
152 // all modules are enabled.
153 $names = $this->container->get('config.storage')->listAll();
154 /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */
155 $typed_config = $this->container->get('config.typed');
156 foreach ($names as $name) {
157 $config = $this->config($name);
158 $this->assertConfigSchema($typed_config, $name, $config->get());