ConfigOtherModuleTest.php in Drupal 9
File
core/modules/config/tests/src/Functional/ConfigOtherModuleTest.php
View source
<?php
namespace Drupal\Tests\config\Functional;
use Drupal\Tests\BrowserTestBase;
class ConfigOtherModuleTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public function testInstallOtherModuleFirst() {
$this
->installModule('config_other_module_config_test');
$config = $this
->config('config_test.dynamic.other_module_test');
$this
->assertTrue($config
->isNew(), 'Default configuration for other modules is not installed if that module is not enabled.');
$this
->installModule('config_test');
$this
->assertNotEmpty($this
->getStorage()
->load('other_module_test'), 'Default configuration has been installed.');
$this
->uninstallModule('config_test');
$config = $this
->config('config_test.dynamic.other_module_test');
$this
->assertTrue($config
->isNew(), 'Default configuration for other modules is removed when the config entity provider is disabled.');
$this
->installModule('config_test');
$other_module_config_entity = $this
->getStorage()
->load('other_module_test');
$this
->assertNotEmpty($other_module_config_entity, "Default configuration has been recreated.");
$other_module_config_entity
->set('style', "The piano ain't got no wrong notes.");
$other_module_config_entity
->save();
$this
->uninstallModule('config_other_module_config_test');
$this
->assertNotEmpty($this
->getStorage()
->load('other_module_test'), 'Default configuration for other modules is not removed when the module that provides it is uninstalled.');
$this
->assertNotEmpty($this
->getStorage()
->load('dotted.default'), 'The configuration is not deleted.');
$this
->installModule('config_other_module_config_test');
$this
->assertTrue(\Drupal::moduleHandler()
->moduleExists('config_other_module_config_test'), 'The config_other_module_config_test module is installed.');
$this
->assertNull($this
->getStorage()
->load('other_module_test_unmet'), 'The optional configuration config_test.dynamic.other_module_test_unmet whose dependencies are not met is not created.');
$this
->assertNull($this
->getStorage()
->load('other_module_test_optional_entity_unmet'), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are not met is not created.');
$this
->installModule('config_test_language');
$this
->assertNull($this
->getStorage()
->load('other_module_test_optional_entity_unmet'), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are met is not created.');
$this
->installModule('config_install_dependency_test');
$this
->assertNotEmpty($this
->getStorage()
->load('other_module_test_unmet'), 'The optional configuration config_test.dynamic.other_module_test_unmet whose dependencies are met is now created.');
$this
->assertNotEmpty($this
->getStorage()
->load('other_module_test_optional_entity_unmet2'), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet2 whose dependencies are met is now created.');
$entity = $this
->getStorage()
->load('other_module_test_optional_entity_unmet');
$this
->assertNotEmpty($entity, 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are met is created.');
$entity
->delete();
$this
->installModule('config');
$this
->assertNull($this
->getStorage()
->load('other_module_test_optional_entity_unmet'), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are met is not installed when an unrelated module is installed.');
}
public function testInstallConfigEntityModuleFirst() {
$this
->installModule('config_test');
$this
->assertNull($this
->getStorage()
->load('other_module_test'), 'Default configuration provided by config_other_module_config_test does not exist.');
$this
->installModule('config_other_module_config_test');
$this
->assertNotEmpty($this
->getStorage()
->load('other_module_test'), 'Default configuration provided by config_other_module_config_test has been installed.');
}
public function testUninstall() {
$this
->installModule('views');
$this
->assertNull($this
->getStorage('view')
->load('frontpage'), 'After installing Views, frontpage view which is dependant on the Node and Views modules does not exist.');
$this
->installModule('node');
$this
->assertNotNull($this
->getStorage('view')
->load('frontpage'), 'After installing Node, frontpage view which is dependant on the Node and Views modules exists.');
$this
->uninstallModule('node');
$this
->assertNull($this
->getStorage('view')
->load('frontpage'), 'After uninstalling Node, frontpage view which is dependant on the Node and Views modules does not exist.');
}
protected function installModule($module) {
$this->container
->get('module_installer')
->install([
$module,
]);
$this->container = \Drupal::getContainer();
}
protected function uninstallModule($module) {
$this->container
->get('module_installer')
->uninstall([
$module,
]);
$this->container = \Drupal::getContainer();
}
protected function getStorage($entity_type_id = 'config_test') {
return \Drupal::entityTypeManager()
->getStorage($entity_type_id);
}
}