a99676072b576c40ad571a241ec4d85b65b3a2f3
[yaffs-website] / web / core / modules / config / tests / src / Functional / ConfigOtherModuleTest.php
1 <?php
2
3 namespace Drupal\Tests\config\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests default configuration provided by a module that does not own it.
9  *
10  * @group config
11  */
12 class ConfigOtherModuleTest extends BrowserTestBase {
13
14   /**
15    * Tests enabling the provider of the default configuration first.
16    */
17   public function testInstallOtherModuleFirst() {
18     $this->installModule('config_other_module_config_test');
19
20     // Check that the config entity doesn't exist before the config_test module
21     // is enabled. We cannot use the entity system because the config_test
22     // entity type does not exist.
23     $config = $this->config('config_test.dynamic.other_module_test');
24     $this->assertTrue($config->isNew(), 'Default configuration for other modules is not installed if that module is not enabled.');
25
26     // Install the module that provides the entity type. This installs the
27     // default configuration.
28     $this->installModule('config_test');
29     $this->assertTrue(entity_load('config_test', 'other_module_test', TRUE), 'Default configuration has been installed.');
30
31     // Uninstall the module that provides the entity type. This will remove the
32     // default configuration.
33     $this->uninstallModule('config_test');
34     $config = $this->config('config_test.dynamic.other_module_test');
35     $this->assertTrue($config->isNew(), 'Default configuration for other modules is removed when the config entity provider is disabled.');
36
37     // Install the module that provides the entity type again. This installs the
38     // default configuration.
39     $this->installModule('config_test');
40     $other_module_config_entity = entity_load('config_test', 'other_module_test', TRUE);
41     $this->assertTrue($other_module_config_entity, "Default configuration has been recreated.");
42
43     // Update the default configuration to test that the changes are preserved
44     // if the module that provides the default configuration is uninstalled.
45     $other_module_config_entity->set('style', "The piano ain't got no wrong notes.");
46     $other_module_config_entity->save();
47
48     // Uninstall the module that provides the default configuration.
49     $this->uninstallModule('config_other_module_config_test');
50     $this->assertTrue(entity_load('config_test', 'other_module_test', TRUE), 'Default configuration for other modules is not removed when the module that provides it is uninstalled.');
51
52     // Default configuration provided by config_test should still exist.
53     $this->assertTrue(entity_load('config_test', 'dotted.default', TRUE), 'The configuration is not deleted.');
54
55     // Re-enable module to test that pre-existing optional configuration does
56     // not throw an error.
57     $this->installModule('config_other_module_config_test');
58     $this->assertTrue(\Drupal::moduleHandler()->moduleExists('config_other_module_config_test'), 'The config_other_module_config_test module is installed.');
59
60     // Ensure that optional configuration with unmet dependencies is only
61     // installed once all the dependencies are met.
62     $this->assertNull(entity_load('config_test', 'other_module_test_unmet', TRUE), 'The optional configuration config_test.dynamic.other_module_test_unmet whose dependencies are not met is not created.');
63     $this->assertNull(entity_load('config_test', 'other_module_test_optional_entity_unmet', TRUE), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are not met is not created.');
64     $this->installModule('config_test_language');
65     $this->installModule('config_install_dependency_test');
66     $this->assertTrue(entity_load('config_test', 'other_module_test_unmet', TRUE), 'The optional configuration config_test.dynamic.other_module_test_unmet whose dependencies are met is now created.');
67     // Although the following configuration entity's are now met it is not
68     // installed because it does not have a direct dependency on the
69     // config_install_dependency_test module.
70     $this->assertNull(entity_load('config_test', 'other_module_test_optional_entity_unmet', TRUE), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are met is not created.');
71   }
72
73   /**
74    * Tests enabling the provider of the config entity type first.
75    */
76   public function testInstallConfigEntityModuleFirst() {
77     $this->installModule('config_test');
78     $this->assertFalse(entity_load('config_test', 'other_module_test', TRUE), 'Default configuration provided by config_other_module_config_test does not exist.');
79
80     $this->installModule('config_other_module_config_test');
81     $this->assertTrue(entity_load('config_test', 'other_module_test', TRUE), 'Default configuration provided by config_other_module_config_test has been installed.');
82   }
83
84   /**
85    * Tests uninstalling Node module removes views which are dependent on it.
86    */
87   public function testUninstall() {
88     $this->installModule('views');
89     $storage = $this->container->get('entity_type.manager')->getStorage('view');
90     $storage->resetCache(['frontpage']);
91     $this->assertTrue($storage->load('frontpage') === NULL, 'After installing Views, frontpage view which is dependant on the Node and Views modules does not exist.');
92     $this->installModule('node');
93     $storage->resetCache(['frontpage']);
94     $this->assertTrue($storage->load('frontpage') !== NULL, 'After installing Node, frontpage view which is dependant on the Node and Views modules exists.');
95     $this->uninstallModule('node');
96     $storage = $this->container->get('entity_type.manager')->getStorage('view');
97     $storage->resetCache(['frontpage']);
98     $this->assertTrue($storage->load('frontpage') === NULL, 'After uninstalling Node, frontpage view which is dependant on the Node and Views modules does not exist.');
99   }
100
101   /**
102    * Installs a module.
103    *
104    * @param string $module
105    *   The module name.
106    */
107   protected function installModule($module) {
108     $this->container->get('module_installer')->install([$module]);
109     $this->container = \Drupal::getContainer();
110   }
111
112   /**
113    * Uninstalls a module.
114    *
115    * @param string $module
116    *   The module name.
117    */
118   protected function uninstallModule($module) {
119     $this->container->get('module_installer')->uninstall([$module]);
120     $this->container = \Drupal::getContainer();
121   }
122
123 }