db backup prior to drupal security update
[yaffs-website] / web / core / modules / system / src / Tests / Module / DependencyTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Module;
4 use Drupal\Component\Utility\Unicode;
5
6 /**
7  * Enable module without dependency enabled.
8  *
9  * @group Module
10  */
11 class DependencyTest extends ModuleTestBase {
12   /**
13    * Checks functionality of project namespaces for dependencies.
14    */
15   public function testProjectNamespaceForDependencies() {
16     $edit = [
17       'modules[filter][enable]' => TRUE,
18     ];
19     $this->drupalPostForm('admin/modules', $edit, t('Install'));
20     // Enable module with project namespace to ensure nothing breaks.
21     $edit = [
22       'modules[system_project_namespace_test][enable]' => TRUE,
23     ];
24     $this->drupalPostForm('admin/modules', $edit, t('Install'));
25     $this->assertModules(['system_project_namespace_test'], TRUE);
26   }
27
28   /**
29    * Attempts to enable the Content Translation module without Language enabled.
30    */
31   public function testEnableWithoutDependency() {
32     // Attempt to enable Content Translation without Language enabled.
33     $edit = [];
34     $edit['modules[content_translation][enable]'] = 'content_translation';
35     $this->drupalPostForm('admin/modules', $edit, t('Install'));
36     $this->assertText(t('Some required modules must be enabled'), 'Dependency required.');
37
38     $this->assertModules(['content_translation', 'language'], FALSE);
39
40     // Assert that the language tables weren't enabled.
41     $this->assertTableCount('language', FALSE);
42
43     $this->drupalPostForm(NULL, NULL, t('Continue'));
44     $this->assertText(t('2 modules have been enabled: Content Translation, Language.'), 'Modules status has been updated.');
45     $this->assertModules(['content_translation', 'language'], TRUE);
46
47     // Assert that the language YAML files were created.
48     $storage = $this->container->get('config.storage');
49     $this->assertTrue(count($storage->listAll('language.entity.')) > 0, 'Language config entity files exist.');
50   }
51
52   /**
53    * Attempts to enable a module with a missing dependency.
54    */
55   public function testMissingModules() {
56     // Test that the system_dependencies_test module is marked
57     // as missing a dependency.
58     $this->drupalGet('admin/modules');
59     $this->assertRaw(t('@module (<span class="admin-missing">missing</span>)', ['@module' => Unicode::ucfirst('_missing_dependency')]), 'A module with missing dependencies is marked as such.');
60     $checkbox = $this->xpath('//input[@type="checkbox" and @disabled="disabled" and @name="modules[system_dependencies_test][enable]"]');
61     $this->assert(count($checkbox) == 1, 'Checkbox for the module is disabled.');
62   }
63
64   /**
65    * Tests enabling a module that depends on an incompatible version of a module.
66    */
67   public function testIncompatibleModuleVersionDependency() {
68     // Test that the system_incompatible_module_version_dependencies_test is
69     // marked as having an incompatible dependency.
70     $this->drupalGet('admin/modules');
71     $this->assertRaw(t('@module (<span class="admin-missing">incompatible with</span> version @version)', [
72       '@module' => 'System incompatible module version test (>2.0)',
73       '@version' => '1.0',
74     ]), 'A module that depends on an incompatible version of a module is marked as such.');
75     $checkbox = $this->xpath('//input[@type="checkbox" and @disabled="disabled" and @name="modules[system_incompatible_module_version_dependencies_test][enable]"]');
76     $this->assert(count($checkbox) == 1, 'Checkbox for the module is disabled.');
77   }
78
79   /**
80    * Tests enabling a module that depends on a module with an incompatible core version.
81    */
82   public function testIncompatibleCoreVersionDependency() {
83     // Test that the system_incompatible_core_version_dependencies_test is
84     // marked as having an incompatible dependency.
85     $this->drupalGet('admin/modules');
86     $this->assertRaw(t('@module (<span class="admin-missing">incompatible with</span> this version of Drupal core)', [
87       '@module' => 'System incompatible core version test',
88     ]), 'A module that depends on a module with an incompatible core version is marked as such.');
89     $checkbox = $this->xpath('//input[@type="checkbox" and @disabled="disabled" and @name="modules[system_incompatible_core_version_dependencies_test][enable]"]');
90     $this->assert(count($checkbox) == 1, 'Checkbox for the module is disabled.');
91   }
92
93   /**
94    * Tests enabling a module that depends on a module which fails hook_requirements().
95    */
96   public function testEnableRequirementsFailureDependency() {
97     \Drupal::service('module_installer')->install(['comment']);
98
99     $this->assertModules(['requirements1_test'], FALSE);
100     $this->assertModules(['requirements2_test'], FALSE);
101
102     // Attempt to install both modules at the same time.
103     $edit = [];
104     $edit['modules[requirements1_test][enable]'] = 'requirements1_test';
105     $edit['modules[requirements2_test][enable]'] = 'requirements2_test';
106     $this->drupalPostForm('admin/modules', $edit, t('Install'));
107
108     // Makes sure the modules were NOT installed.
109     $this->assertText(t('Requirements 1 Test failed requirements'), 'Modules status has been updated.');
110     $this->assertModules(['requirements1_test'], FALSE);
111     $this->assertModules(['requirements2_test'], FALSE);
112
113     // Makes sure that already enabled modules the failing modules depend on
114     // were not disabled.
115     $this->assertModules(['comment'], TRUE);
116   }
117
118   /**
119    * Tests that module dependencies are enabled in the correct order in the UI.
120    *
121    * Dependencies should be enabled before their dependents.
122    */
123   public function testModuleEnableOrder() {
124     \Drupal::service('module_installer')->install(['module_test'], FALSE);
125     $this->resetAll();
126     $this->assertModules(['module_test'], TRUE);
127     \Drupal::state()->set('module_test.dependency', 'dependency');
128     // module_test creates a dependency chain:
129     // - color depends on config
130     // - config depends on help
131     $expected_order = ['help', 'config', 'color'];
132
133     // Enable the modules through the UI, verifying that the dependency chain
134     // is correct.
135     $edit = [];
136     $edit['modules[color][enable]'] = 'color';
137     $this->drupalPostForm('admin/modules', $edit, t('Install'));
138     $this->assertModules(['color'], FALSE);
139     // Note that dependencies are sorted alphabetically in the confirmation
140     // message.
141     $this->assertText(t('You must enable the Configuration Manager, Help modules to install Color.'));
142
143     $edit['modules[config][enable]'] = 'config';
144     $edit['modules[help][enable]'] = 'help';
145     $this->drupalPostForm('admin/modules', $edit, t('Install'));
146     $this->assertModules(['color', 'config', 'help'], TRUE);
147
148     // Check the actual order which is saved by module_test_modules_enabled().
149     $module_order = \Drupal::state()->get('module_test.install_order') ?: [];
150     $this->assertIdentical($module_order, $expected_order);
151   }
152
153   /**
154    * Tests attempting to uninstall a module that has installed dependents.
155    */
156   public function testUninstallDependents() {
157     // Enable the forum module.
158     $edit = ['modules[forum][enable]' => 'forum'];
159     $this->drupalPostForm('admin/modules', $edit, t('Install'));
160     $this->drupalPostForm(NULL, [], t('Continue'));
161     $this->assertModules(['forum'], TRUE);
162
163     // Check that the comment module cannot be uninstalled.
164     $this->drupalGet('admin/modules/uninstall');
165     $checkbox = $this->xpath('//input[@type="checkbox" and @name="uninstall[comment]" and @disabled="disabled"]');
166     $this->assert(count($checkbox) == 1, 'Checkbox for uninstalling the comment module is disabled.');
167
168     // Delete any forum terms.
169     $vid = $this->config('forum.settings')->get('vocabulary');
170     // Ensure taxonomy has been loaded into the test-runner after forum was
171     // enabled.
172     \Drupal::moduleHandler()->load('taxonomy');
173     $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]);
174     foreach ($terms as $term) {
175       $term->delete();
176     }
177     // Uninstall the forum module, and check that taxonomy now can also be
178     // uninstalled.
179     $edit = ['uninstall[forum]' => 'forum'];
180     $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
181     $this->drupalPostForm(NULL, NULL, t('Uninstall'));
182     $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
183
184     // Uninstall comment module.
185     $edit = ['uninstall[comment]' => 'comment'];
186     $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
187     $this->drupalPostForm(NULL, NULL, t('Uninstall'));
188     $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
189   }
190
191 }