View source
<?php
namespace Drupal\Tests\config\Functional;
use Drupal\config_test\Entity\ConfigTest;
use Drupal\Core\Config\PreExistingConfigException;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\Site\Settings;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
class ConfigInstallWebTest extends BrowserTestBase {
protected $adminUser;
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer modules',
'administer themes',
'administer site configuration',
]);
unset($GLOBALS['hook_config_test']);
}
public function testIntegrationModuleReinstallation() {
$default_config = 'config_integration_test.settings';
$default_configuration_entity = 'config_test.dynamic.config_integration_test';
\Drupal::service('module_installer')
->install([
'config_test',
]);
$config_static = $this
->config($default_config);
$this
->assertIdentical($config_static
->isNew(), TRUE);
$config_entity = $this
->config($default_configuration_entity);
$this
->assertIdentical($config_entity
->isNew(), TRUE);
\Drupal::service('module_installer')
->install([
'config_integration_test',
]);
$this
->resetAll();
\Drupal::configFactory()
->reset($default_config);
\Drupal::configFactory()
->reset($default_configuration_entity);
$config_static = $this
->config($default_config);
$this
->assertIdentical($config_static
->isNew(), FALSE);
$this
->assertIdentical($config_static
->get('foo'), 'default setting');
$config_entity = $this
->config($default_configuration_entity);
$this
->assertIdentical($config_entity
->isNew(), FALSE);
$this
->assertIdentical($config_entity
->get('label'), 'Default integration config label');
$config_static
->set('foo', 'customized setting')
->save();
$config_entity
->set('label', 'Customized integration config label')
->save();
$this->container
->get('config.factory')
->reset();
$this->container
->get('module_installer')
->uninstall([
'config_integration_test',
]);
$config_static = $this
->config($default_config);
$this
->assertIdentical($config_static
->isNew(), TRUE);
$config_entity = $this
->config($default_configuration_entity);
$this
->assertIdentical($config_entity
->isNew(), FALSE);
$this
->assertIdentical($config_entity
->get('label'), 'Customized integration config label');
try {
\Drupal::service('module_installer')
->install([
'config_integration_test',
]);
$this
->fail('Expected PreExistingConfigException not thrown.');
} catch (PreExistingConfigException $e) {
$this
->assertEquals('config_integration_test', $e
->getExtension());
$this
->assertEquals([
StorageInterface::DEFAULT_COLLECTION => [
'config_test.dynamic.config_integration_test',
],
], $e
->getConfigObjects());
$this
->assertEquals('Configuration objects (config_test.dynamic.config_integration_test) provided by config_integration_test already exist in active configuration', $e
->getMessage());
}
$config_entity
->delete();
\Drupal::service('module_installer')
->install([
'config_integration_test',
]);
\Drupal::configFactory()
->reset($default_config);
\Drupal::configFactory()
->reset($default_configuration_entity);
$config_static = $this
->config($default_config);
$this
->assertIdentical($config_static
->isNew(), FALSE);
$this
->assertIdentical($config_static
->get('foo'), 'default setting');
$config_entity = \Drupal::config($default_configuration_entity);
$this
->assertIdentical($config_entity
->isNew(), FALSE);
$this
->assertIdentical($config_entity
->get('label'), 'Default integration config label');
}
public function testPreExistingConfigInstall() {
$this
->drupalLogin($this->adminUser);
$this
->drupalPostForm('admin/modules', [
'modules[config_test][enable]' => TRUE,
'modules[config_install_fail_test][enable]' => TRUE,
], t('Install'));
$this
->assertRaw('Unable to install Configuration install fail test, <em class="placeholder">config_test.dynamic.dotted.default</em> already exists in active configuration.');
$this
->drupalPostForm('admin/modules/uninstall', [
'uninstall[config_test]' => TRUE,
], t('Uninstall'));
$this
->drupalPostForm(NULL, [], t('Uninstall'));
$this
->drupalPostForm('admin/modules', [
'modules[config_install_fail_test][enable]' => TRUE,
], t('Install'));
$this
->drupalPostForm(NULL, [], t('Continue'));
$this
->assertRaw('Unable to install Configuration install fail test, <em class="placeholder">config_test.dynamic.dotted.default</em> already exists in active configuration.');
\Drupal::service('module_installer')
->install([
'language',
]);
$this
->rebuildContainer();
ConfigurableLanguage::createFromLangcode('fr')
->save();
\Drupal::languageManager()
->getLanguageConfigOverride('fr', 'config_test.dynamic.dotted.default')
->set('label', 'Je suis Charlie')
->save();
$this
->drupalPostForm('admin/modules', [
'modules[config_install_fail_test][enable]' => TRUE,
], t('Install'));
$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.');
$this
->drupalGet('admin/appearance');
$url = $this
->xpath("//a[contains(@href,'config_clash_test_theme') and contains(@href,'/install?')]/@href")[0];
$this
->drupalGet($this
->getAbsoluteUrl($url
->getText()));
$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.');
try {
\Drupal::service('theme_installer')
->install([
'config_clash_test_theme',
]);
$this
->fail('Expected PreExistingConfigException not thrown.');
} catch (PreExistingConfigException $e) {
$this
->assertEquals('config_clash_test_theme', $e
->getExtension());
$this
->assertEquals([
StorageInterface::DEFAULT_COLLECTION => [
'config_test.dynamic.dotted.default',
],
'language.fr' => [
'config_test.dynamic.dotted.default',
],
], $e
->getConfigObjects());
$this
->assertEquals('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', $e
->getMessage());
}
}
public function testUnmetDependenciesInstall() {
$this
->drupalLogin($this->adminUser);
$this
->drupalPostForm('admin/modules', [
'modules[config_test][enable]' => TRUE,
], t('Install'));
$this
->drupalPostForm('admin/modules', [
'modules[config_install_dependency_test][enable]' => TRUE,
], t('Install'));
$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>');
$this
->drupalPostForm('admin/modules', [
'modules[config_test_language][enable]' => TRUE,
], t('Install'));
$this
->drupalPostForm('admin/modules', [
'modules[config_install_dependency_test][enable]' => TRUE,
], t('Install'));
$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>');
$this
->drupalPostForm('admin/modules', [
'modules[config_other_module_config_test][enable]' => TRUE,
], t('Install'));
$this
->drupalPostForm('admin/modules', [
'modules[config_install_dependency_test][enable]' => TRUE,
], t('Install'));
$this
->rebuildContainer();
$this
->assertInstanceOf(ConfigTest::class, \Drupal::entityTypeManager()
->getStorage('config_test')
->load('other_module_test_with_dependency'));
}
public function testConfigModuleRequirements() {
$this
->drupalLogin($this->adminUser);
$this
->drupalPostForm('admin/modules', [
'modules[config][enable]' => TRUE,
], t('Install'));
$directory = Settings::get('config_sync_directory');
try {
\Drupal::service('file_system')
->deleteRecursive($directory);
} catch (FileException $e) {
}
$this
->drupalGet('/admin/reports/status');
$this
->assertRaw(t('The directory %directory does not exist.', [
'%directory' => $directory,
]));
}
}