3 namespace Drupal\config\Tests;
5 use Drupal\Core\Config\PreExistingConfigException;
6 use Drupal\Core\Config\StorageInterface;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\simpletest\WebTestBase;
11 * Tests installation and removal of configuration objects in install, disable
12 * and uninstall functionality.
16 class ConfigInstallWebTest extends WebTestBase {
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']);
55 // Verify that default module config exists.
56 \Drupal::configFactory()->reset($default_config);
57 \Drupal::configFactory()->reset($default_configuration_entity);
58 $config_static = $this->config($default_config);
59 $this->assertIdentical($config_static->isNew(), FALSE);
60 $this->assertIdentical($config_static->get('foo'), 'default setting');
61 $config_entity = $this->config($default_configuration_entity);
62 $this->assertIdentical($config_entity->isNew(), FALSE);
63 $this->assertIdentical($config_entity->get('label'), 'Default integration config label');
65 // Customize both configuration objects.
66 $config_static->set('foo', 'customized setting')->save();
67 $config_entity->set('label', 'Customized integration config label')->save();
69 // @todo FIXME: Setting config keys WITHOUT SAVING retains the changed config
70 // object in memory. Every new call to $this->config() MUST revert in-memory changes
71 // that haven't been saved!
72 // In other words: This test passes even without this reset, but it shouldn't.
73 $this->container->get('config.factory')->reset();
75 // Disable and uninstall the integration module.
76 $this->container->get('module_installer')->uninstall(['config_integration_test']);
78 // Verify the integration module's config was uninstalled.
79 $config_static = $this->config($default_config);
80 $this->assertIdentical($config_static->isNew(), TRUE);
82 // Verify the integration config still exists.
83 $config_entity = $this->config($default_configuration_entity);
84 $this->assertIdentical($config_entity->isNew(), FALSE);
85 $this->assertIdentical($config_entity->get('label'), 'Customized integration config label');
87 // Reinstall the integration module.
89 \Drupal::service('module_installer')->install(['config_integration_test']);
90 $this->fail('Expected PreExistingConfigException not thrown.');
92 catch (PreExistingConfigException $e) {
93 $this->assertEqual($e->getExtension(), 'config_integration_test');
94 $this->assertEqual($e->getConfigObjects(), [StorageInterface::DEFAULT_COLLECTION => ['config_test.dynamic.config_integration_test']]);
95 $this->assertEqual($e->getMessage(), 'Configuration objects (config_test.dynamic.config_integration_test) provided by config_integration_test already exist in active configuration');
98 // Delete the configuration entity so that the install will work.
99 $config_entity->delete();
100 \Drupal::service('module_installer')->install(['config_integration_test']);
102 // Verify the integration module's config was re-installed.
103 \Drupal::configFactory()->reset($default_config);
104 \Drupal::configFactory()->reset($default_configuration_entity);
105 $config_static = $this->config($default_config);
106 $this->assertIdentical($config_static->isNew(), FALSE);
107 $this->assertIdentical($config_static->get('foo'), 'default setting');
109 // Verify the integration config is using the default.
110 $config_entity = \Drupal::config($default_configuration_entity);
111 $this->assertIdentical($config_entity->isNew(), FALSE);
112 $this->assertIdentical($config_entity->get('label'), 'Default integration config label');
116 * Tests pre-existing configuration detection.
118 public function testPreExistingConfigInstall() {
119 $this->drupalLogin($this->adminUser);
121 // Try to install config_install_fail_test and config_test. Doing this
122 // will install the config_test module first because it is a dependency of
123 // config_install_fail_test.
124 // @see \Drupal\system\Form\ModulesListForm::submitForm()
125 $this->drupalPostForm('admin/modules', ['modules[config_test][enable]' => TRUE, 'modules[config_install_fail_test][enable]' => TRUE], t('Install'));
126 $this->assertRaw('Unable to install Configuration install fail test, <em class="placeholder">config_test.dynamic.dotted.default</em> already exists in active configuration.');
128 // Uninstall the config_test module to test the confirm form.
129 $this->drupalPostForm('admin/modules/uninstall', ['uninstall[config_test]' => TRUE], t('Uninstall'));
130 $this->drupalPostForm(NULL, [], t('Uninstall'));
132 // Try to install config_install_fail_test without selecting config_test.
133 // The user is shown a confirm form because the config_test module is a
135 // @see \Drupal\system\Form\ModulesListConfirmForm::submitForm()
136 $this->drupalPostForm('admin/modules', ['modules[config_install_fail_test][enable]' => TRUE], t('Install'));
137 $this->drupalPostForm(NULL, [], t('Continue'));
138 $this->assertRaw('Unable to install Configuration install fail test, <em class="placeholder">config_test.dynamic.dotted.default</em> already exists in active configuration.');
140 // Test that collection configuration clashes during a module install are
141 // reported correctly.
142 \Drupal::service('module_installer')->install(['language']);
143 $this->rebuildContainer();
144 ConfigurableLanguage::createFromLangcode('fr')->save();
145 \Drupal::languageManager()
146 ->getLanguageConfigOverride('fr', 'config_test.dynamic.dotted.default')
147 ->set('label', 'Je suis Charlie')
150 $this->drupalPostForm('admin/modules', ['modules[config_install_fail_test][enable]' => TRUE], t('Install'));
151 $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.');
153 // Test installing a theme through the UI that has existing configuration.
154 // This relies on the fact the config_test has been installed and created
155 // the config_test.dynamic.dotted.default configuration and the translation
156 // override created still exists.
157 $this->drupalGet('admin/appearance');
158 $url = $this->xpath("//a[contains(@href,'config_clash_test_theme') and contains(@href,'/install?')]/@href")[0];
159 $this->drupalGet($this->getAbsoluteUrl($url));
160 $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.');
162 // Test installing a theme through the API that has existing configuration.
164 \Drupal::service('theme_handler')->install(['config_clash_test_theme']);
165 $this->fail('Expected PreExistingConfigException not thrown.');
167 catch (PreExistingConfigException $e) {
168 $this->assertEqual($e->getExtension(), 'config_clash_test_theme');
169 $this->assertEqual($e->getConfigObjects(), [StorageInterface::DEFAULT_COLLECTION => ['config_test.dynamic.dotted.default'], 'language.fr' => ['config_test.dynamic.dotted.default']]);
170 $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');
175 * Tests unmet dependencies detection.
177 public function testUnmetDependenciesInstall() {
178 $this->drupalLogin($this->adminUser);
179 // We need to install separately since config_install_dependency_test does
180 // not depend on config_test and order is important.
181 $this->drupalPostForm('admin/modules', ['modules[config_test][enable]' => TRUE], t('Install'));
182 $this->drupalPostForm('admin/modules', ['modules[config_install_dependency_test][enable]' => TRUE], t('Install'));
183 $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>');
185 $this->drupalPostForm('admin/modules', ['modules[config_test_language][enable]' => TRUE], t('Install'));
186 $this->drupalPostForm('admin/modules', ['modules[config_install_dependency_test][enable]' => TRUE], t('Install'));
187 $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>');
189 $this->drupalPostForm('admin/modules', ['modules[config_other_module_config_test][enable]' => TRUE], t('Install'));
190 $this->drupalPostForm('admin/modules', ['modules[config_install_dependency_test][enable]' => TRUE], t('Install'));
191 $this->rebuildContainer();
192 $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.');
196 * Tests config_requirements().
198 public function testConfigModuleRequirements() {
199 $this->drupalLogin($this->adminUser);
200 $this->drupalPostForm('admin/modules', ['modules[config][enable]' => TRUE], t('Install'));
202 $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
203 file_unmanaged_delete_recursive($directory);
204 $this->drupalGet('/admin/reports/status');
205 $this->assertRaw(t('The directory %directory does not exist.', ['%directory' => $directory]));
207 file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
208 \Drupal::service('file_system')->chmod($directory, 0555);
209 $this->drupalGet('/admin/reports/status');
210 $this->assertRaw(t('The directory %directory is not writable.', ['%directory' => $directory]));