3 namespace Drupal\Tests\config\Functional;
5 use Drupal\Core\Config\PreExistingConfigException;
6 use Drupal\Core\Config\StorageInterface;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests installation and removal of configuration objects in install, disable
12 * and uninstall functionality.
16 class ConfigInstallWebTest extends BrowserTestBase {
19 * The admin user used in this test.
26 protected function setUp() {
29 $this->adminUser = $this->drupalCreateUser(['administer modules', 'administer themes', 'administer site configuration']);
31 // Ensure the global variable being asserted by this test does not exist;
32 // a previous test executed in this request/process might have set it.
33 unset($GLOBALS['hook_config_test']);
37 * Tests module re-installation.
39 public function testIntegrationModuleReinstallation() {
40 $default_config = 'config_integration_test.settings';
41 $default_configuration_entity = 'config_test.dynamic.config_integration_test';
43 // Install the config_test module we're integrating with.
44 \Drupal::service('module_installer')->install(['config_test']);
46 // Verify the configuration does not exist prior to installation.
47 $config_static = $this->config($default_config);
48 $this->assertIdentical($config_static->isNew(), TRUE);
49 $config_entity = $this->config($default_configuration_entity);
50 $this->assertIdentical($config_entity->isNew(), TRUE);
52 // Install the integration module.
53 \Drupal::service('module_installer')->install(['config_integration_test']);
56 // Verify that default module config exists.
57 \Drupal::configFactory()->reset($default_config);
58 \Drupal::configFactory()->reset($default_configuration_entity);
59 $config_static = $this->config($default_config);
60 $this->assertIdentical($config_static->isNew(), FALSE);
61 $this->assertIdentical($config_static->get('foo'), 'default setting');
62 $config_entity = $this->config($default_configuration_entity);
63 $this->assertIdentical($config_entity->isNew(), FALSE);
64 $this->assertIdentical($config_entity->get('label'), 'Default integration config label');
66 // Customize both configuration objects.
67 $config_static->set('foo', 'customized setting')->save();
68 $config_entity->set('label', 'Customized integration config label')->save();
70 // @todo FIXME: Setting config keys WITHOUT SAVING retains the changed config
71 // object in memory. Every new call to $this->config() MUST revert in-memory changes
72 // that haven't been saved!
73 // In other words: This test passes even without this reset, but it shouldn't.
74 $this->container->get('config.factory')->reset();
76 // Disable and uninstall the integration module.
77 $this->container->get('module_installer')->uninstall(['config_integration_test']);
79 // Verify the integration module's config was uninstalled.
80 $config_static = $this->config($default_config);
81 $this->assertIdentical($config_static->isNew(), TRUE);
83 // Verify the integration config still exists.
84 $config_entity = $this->config($default_configuration_entity);
85 $this->assertIdentical($config_entity->isNew(), FALSE);
86 $this->assertIdentical($config_entity->get('label'), 'Customized integration config label');
88 // Reinstall the integration module.
90 \Drupal::service('module_installer')->install(['config_integration_test']);
91 $this->fail('Expected PreExistingConfigException not thrown.');
93 catch (PreExistingConfigException $e) {
94 $this->assertEqual($e->getExtension(), 'config_integration_test');
95 $this->assertEqual($e->getConfigObjects(), [StorageInterface::DEFAULT_COLLECTION => ['config_test.dynamic.config_integration_test']]);
96 $this->assertEqual($e->getMessage(), 'Configuration objects (config_test.dynamic.config_integration_test) provided by config_integration_test already exist in active configuration');
99 // Delete the configuration entity so that the install will work.
100 $config_entity->delete();
101 \Drupal::service('module_installer')->install(['config_integration_test']);
103 // Verify the integration module's config was re-installed.
104 \Drupal::configFactory()->reset($default_config);
105 \Drupal::configFactory()->reset($default_configuration_entity);
106 $config_static = $this->config($default_config);
107 $this->assertIdentical($config_static->isNew(), FALSE);
108 $this->assertIdentical($config_static->get('foo'), 'default setting');
110 // Verify the integration config is using the default.
111 $config_entity = \Drupal::config($default_configuration_entity);
112 $this->assertIdentical($config_entity->isNew(), FALSE);
113 $this->assertIdentical($config_entity->get('label'), 'Default integration config label');
117 * Tests pre-existing configuration detection.
119 public function testPreExistingConfigInstall() {
120 $this->drupalLogin($this->adminUser);
122 // Try to install config_install_fail_test and config_test. Doing this
123 // will install the config_test module first because it is a dependency of
124 // config_install_fail_test.
125 // @see \Drupal\system\Form\ModulesListForm::submitForm()
126 $this->drupalPostForm('admin/modules', ['modules[config_test][enable]' => TRUE, 'modules[config_install_fail_test][enable]' => TRUE], t('Install'));
127 $this->assertRaw('Unable to install Configuration install fail test, <em class="placeholder">config_test.dynamic.dotted.default</em> already exists in active configuration.');
129 // Uninstall the config_test module to test the confirm form.
130 $this->drupalPostForm('admin/modules/uninstall', ['uninstall[config_test]' => TRUE], t('Uninstall'));
131 $this->drupalPostForm(NULL, [], t('Uninstall'));
133 // Try to install config_install_fail_test without selecting config_test.
134 // The user is shown a confirm form because the config_test module is a
136 // @see \Drupal\system\Form\ModulesListConfirmForm::submitForm()
137 $this->drupalPostForm('admin/modules', ['modules[config_install_fail_test][enable]' => TRUE], t('Install'));
138 $this->drupalPostForm(NULL, [], t('Continue'));
139 $this->assertRaw('Unable to install Configuration install fail test, <em class="placeholder">config_test.dynamic.dotted.default</em> already exists in active configuration.');
141 // Test that collection configuration clashes during a module install are
142 // reported correctly.
143 \Drupal::service('module_installer')->install(['language']);
144 $this->rebuildContainer();
145 ConfigurableLanguage::createFromLangcode('fr')->save();
146 \Drupal::languageManager()
147 ->getLanguageConfigOverride('fr', 'config_test.dynamic.dotted.default')
148 ->set('label', 'Je suis Charlie')
151 $this->drupalPostForm('admin/modules', ['modules[config_install_fail_test][enable]' => TRUE], t('Install'));
152 $this->assertRaw('Unable to install Configuration install fail test, <em class="placeholder">config_test.dynamic.dotted.default, language/fr/config_test.dynamic.dotted.default</em> already exist in active configuration.');
154 // Test installing a theme through the UI that has existing configuration.
155 // This relies on the fact the config_test has been installed and created
156 // the config_test.dynamic.dotted.default configuration and the translation
157 // override created still exists.
158 $this->drupalGet('admin/appearance');
159 $url = $this->xpath("//a[contains(@href,'config_clash_test_theme') and contains(@href,'/install?')]/@href")[0];
160 $this->drupalGet($this->getAbsoluteUrl($url->getText()));
161 $this->assertRaw('Unable to install config_clash_test_theme, <em class="placeholder">config_test.dynamic.dotted.default, language/fr/config_test.dynamic.dotted.default</em> already exist in active configuration.');
163 // Test installing a theme through the API that has existing configuration.
165 \Drupal::service('theme_handler')->install(['config_clash_test_theme']);
166 $this->fail('Expected PreExistingConfigException not thrown.');
168 catch (PreExistingConfigException $e) {
169 $this->assertEqual($e->getExtension(), 'config_clash_test_theme');
170 $this->assertEqual($e->getConfigObjects(), [StorageInterface::DEFAULT_COLLECTION => ['config_test.dynamic.dotted.default'], 'language.fr' => ['config_test.dynamic.dotted.default']]);
171 $this->assertEqual($e->getMessage(), 'Configuration objects (config_test.dynamic.dotted.default, language/fr/config_test.dynamic.dotted.default) provided by config_clash_test_theme already exist in active configuration');
176 * Tests unmet dependencies detection.
178 public function testUnmetDependenciesInstall() {
179 $this->drupalLogin($this->adminUser);
180 // We need to install separately since config_install_dependency_test does
181 // not depend on config_test and order is important.
182 $this->drupalPostForm('admin/modules', ['modules[config_test][enable]' => TRUE], t('Install'));
183 $this->drupalPostForm('admin/modules', ['modules[config_install_dependency_test][enable]' => TRUE], t('Install'));
184 $this->assertRaw('Unable to install <em class="placeholder">Config install dependency test</em> due to unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test, config_test.dynamic.dotted.english)</em>');
186 $this->drupalPostForm('admin/modules', ['modules[config_test_language][enable]' => TRUE], t('Install'));
187 $this->drupalPostForm('admin/modules', ['modules[config_install_dependency_test][enable]' => TRUE], t('Install'));
188 $this->assertRaw('Unable to install <em class="placeholder">Config install dependency test</em> due to unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test)</em>');
190 $this->drupalPostForm('admin/modules', ['modules[config_other_module_config_test][enable]' => TRUE], t('Install'));
191 $this->drupalPostForm('admin/modules', ['modules[config_install_dependency_test][enable]' => TRUE], t('Install'));
192 $this->rebuildContainer();
193 $this->assertTrue(entity_load('config_test', 'other_module_test_with_dependency'), 'The config_test.dynamic.other_module_test_with_dependency configuration has been created during install.');
197 * Tests config_requirements().
199 public function testConfigModuleRequirements() {
200 $this->drupalLogin($this->adminUser);
201 $this->drupalPostForm('admin/modules', ['modules[config][enable]' => TRUE], t('Install'));
203 $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
204 file_unmanaged_delete_recursive($directory);
205 $this->drupalGet('/admin/reports/status');
206 $this->assertRaw(t('The directory %directory does not exist.', ['%directory' => $directory]));