Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / config / tests / src / Functional / ConfigInstallWebTest.php
1 <?php
2
3 namespace Drupal\Tests\config\Functional;
4
5 use Drupal\Core\Config\PreExistingConfigException;
6 use Drupal\Core\Config\StorageInterface;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\Tests\BrowserTestBase;
9
10 /**
11  * Tests installation and removal of configuration objects in install, disable
12  * and uninstall functionality.
13  *
14  * @group config
15  */
16 class ConfigInstallWebTest extends BrowserTestBase {
17
18   /**
19    * The admin user used in this test.
20    */
21   protected $adminUser;
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
29     $this->adminUser = $this->drupalCreateUser(['administer modules', 'administer themes', 'administer site configuration']);
30
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']);
34   }
35
36   /**
37    * Tests module re-installation.
38    */
39   public function testIntegrationModuleReinstallation() {
40     $default_config = 'config_integration_test.settings';
41     $default_configuration_entity = 'config_test.dynamic.config_integration_test';
42
43     // Install the config_test module we're integrating with.
44     \Drupal::service('module_installer')->install(['config_test']);
45
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);
51
52     // Install the integration module.
53     \Drupal::service('module_installer')->install(['config_integration_test']);
54     $this->resetAll();
55
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');
65
66     // Customize both configuration objects.
67     $config_static->set('foo', 'customized setting')->save();
68     $config_entity->set('label', 'Customized integration config label')->save();
69
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();
75
76     // Disable and uninstall the integration module.
77     $this->container->get('module_installer')->uninstall(['config_integration_test']);
78
79     // Verify the integration module's config was uninstalled.
80     $config_static = $this->config($default_config);
81     $this->assertIdentical($config_static->isNew(), TRUE);
82
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');
87
88     // Reinstall the integration module.
89     try {
90       \Drupal::service('module_installer')->install(['config_integration_test']);
91       $this->fail('Expected PreExistingConfigException not thrown.');
92     }
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');
97     }
98
99     // Delete the configuration entity so that the install will work.
100     $config_entity->delete();
101     \Drupal::service('module_installer')->install(['config_integration_test']);
102
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');
109
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');
114   }
115
116   /**
117    * Tests pre-existing configuration detection.
118    */
119   public function testPreExistingConfigInstall() {
120     $this->drupalLogin($this->adminUser);
121
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.');
128
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'));
132
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
135     // dependency.
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.');
140
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')
149       ->save();
150
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.');
153
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.');
162
163     // Test installing a theme through the API that has existing configuration.
164     try {
165       \Drupal::service('theme_handler')->install(['config_clash_test_theme']);
166       $this->fail('Expected PreExistingConfigException not thrown.');
167     }
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');
172     }
173   }
174
175   /**
176    * Tests unmet dependencies detection.
177    */
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>');
185
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>');
189
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.');
194   }
195
196   /**
197    * Tests config_requirements().
198    */
199   public function testConfigModuleRequirements() {
200     $this->drupalLogin($this->adminUser);
201     $this->drupalPostForm('admin/modules', ['modules[config][enable]' => TRUE], t('Install'));
202
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]));
207   }
208
209 }