3 namespace Drupal\Tests\system\Functional\Module;
5 use Drupal\Component\Utility\Unicode;
8 * Enable module without dependency enabled.
12 class DependencyTest extends ModuleTestBase {
15 * Checks functionality of project namespaces for dependencies.
17 public function testProjectNamespaceForDependencies() {
19 'modules[filter][enable]' => TRUE,
21 $this->drupalPostForm('admin/modules', $edit, t('Install'));
22 // Enable module with project namespace to ensure nothing breaks.
24 'modules[system_project_namespace_test][enable]' => TRUE,
26 $this->drupalPostForm('admin/modules', $edit, t('Install'));
27 $this->assertModules(['system_project_namespace_test'], TRUE);
31 * Attempts to enable the Content Translation module without Language enabled.
33 public function testEnableWithoutDependency() {
34 // Attempt to enable Content Translation without Language enabled.
36 $edit['modules[content_translation][enable]'] = 'content_translation';
37 $this->drupalPostForm('admin/modules', $edit, t('Install'));
38 $this->assertText(t('Some required modules must be enabled'), 'Dependency required.');
40 $this->assertModules(['content_translation', 'language'], FALSE);
42 // Assert that the language tables weren't enabled.
43 $this->assertTableCount('language', FALSE);
45 $this->drupalPostForm(NULL, NULL, t('Continue'));
46 $this->assertText(t('2 modules have been enabled: Content Translation, Language.'), 'Modules status has been updated.');
47 $this->assertModules(['content_translation', 'language'], TRUE);
49 // Assert that the language YAML files were created.
50 $storage = $this->container->get('config.storage');
51 $this->assertTrue(count($storage->listAll('language.entity.')) > 0, 'Language config entity files exist.');
55 * Attempts to enable a module with a missing dependency.
57 public function testMissingModules() {
58 // Test that the system_dependencies_test module is marked
59 // as missing a dependency.
60 $this->drupalGet('admin/modules');
61 $this->assertRaw(t('@module (<span class="admin-missing">missing</span>)', ['@module' => Unicode::ucfirst('_missing_dependency')]), 'A module with missing dependencies is marked as such.');
62 $checkbox = $this->xpath('//input[@type="checkbox" and @disabled="disabled" and @name="modules[system_dependencies_test][enable]"]');
63 $this->assert(count($checkbox) == 1, 'Checkbox for the module is disabled.');
67 * Tests enabling a module that depends on an incompatible version of a module.
69 public function testIncompatibleModuleVersionDependency() {
70 // Test that the system_incompatible_module_version_dependencies_test is
71 // marked as having an incompatible dependency.
72 $this->drupalGet('admin/modules');
73 $this->assertRaw(t('@module (<span class="admin-missing">incompatible with</span> version @version)', [
74 '@module' => 'System incompatible module version test (>2.0)',
76 ]), 'A module that depends on an incompatible version of a module is marked as such.');
77 $checkbox = $this->xpath('//input[@type="checkbox" and @disabled="disabled" and @name="modules[system_incompatible_module_version_dependencies_test][enable]"]');
78 $this->assert(count($checkbox) == 1, 'Checkbox for the module is disabled.');
82 * Tests enabling a module that depends on a module with an incompatible core version.
84 public function testIncompatibleCoreVersionDependency() {
85 // Test that the system_incompatible_core_version_dependencies_test is
86 // marked as having an incompatible dependency.
87 $this->drupalGet('admin/modules');
88 $this->assertRaw(t('@module (<span class="admin-missing">incompatible with</span> this version of Drupal core)', [
89 '@module' => 'System incompatible core version test',
90 ]), 'A module that depends on a module with an incompatible core version is marked as such.');
91 $checkbox = $this->xpath('//input[@type="checkbox" and @disabled="disabled" and @name="modules[system_incompatible_core_version_dependencies_test][enable]"]');
92 $this->assert(count($checkbox) == 1, 'Checkbox for the module is disabled.');
96 * Tests failing PHP version requirements.
98 public function testIncompatiblePhpVersionDependency() {
99 $this->drupalGet('admin/modules');
100 $this->assertRaw('This module requires PHP version 6502.* and is incompatible with PHP version ' . phpversion() . '.', 'User is informed when the PHP dependency requirement of a module is not met.');
101 $checkbox = $this->xpath('//input[@type="checkbox" and @disabled="disabled" and @name="modules[system_incompatible_php_version_test][enable]"]');
102 $this->assert(count($checkbox) == 1, 'Checkbox for the module is disabled.');
106 * Tests enabling a module that depends on a module which fails hook_requirements().
108 public function testEnableRequirementsFailureDependency() {
109 \Drupal::service('module_installer')->install(['comment']);
111 $this->assertModules(['requirements1_test'], FALSE);
112 $this->assertModules(['requirements2_test'], FALSE);
114 // Attempt to install both modules at the same time.
116 $edit['modules[requirements1_test][enable]'] = 'requirements1_test';
117 $edit['modules[requirements2_test][enable]'] = 'requirements2_test';
118 $this->drupalPostForm('admin/modules', $edit, t('Install'));
120 // Makes sure the modules were NOT installed.
121 $this->assertText(t('Requirements 1 Test failed requirements'), 'Modules status has been updated.');
122 $this->assertModules(['requirements1_test'], FALSE);
123 $this->assertModules(['requirements2_test'], FALSE);
125 // Makes sure that already enabled modules the failing modules depend on
126 // were not disabled.
127 $this->assertModules(['comment'], TRUE);
131 * Tests that module dependencies are enabled in the correct order in the UI.
133 * Dependencies should be enabled before their dependents.
135 public function testModuleEnableOrder() {
136 \Drupal::service('module_installer')->install(['module_test'], FALSE);
138 $this->assertModules(['module_test'], TRUE);
139 \Drupal::state()->set('module_test.dependency', 'dependency');
140 // module_test creates a dependency chain:
141 // - color depends on config
142 // - config depends on help
143 $expected_order = ['help', 'config', 'color'];
145 // Enable the modules through the UI, verifying that the dependency chain
148 $edit['modules[color][enable]'] = 'color';
149 $this->drupalPostForm('admin/modules', $edit, t('Install'));
150 $this->assertModules(['color'], FALSE);
151 // Note that dependencies are sorted alphabetically in the confirmation
153 $this->assertText(t('You must enable the Configuration Manager, Help modules to install Color.'));
155 $edit['modules[config][enable]'] = 'config';
156 $edit['modules[help][enable]'] = 'help';
157 $this->drupalPostForm('admin/modules', $edit, t('Install'));
158 $this->assertModules(['color', 'config', 'help'], TRUE);
160 // Check the actual order which is saved by module_test_modules_enabled().
161 $module_order = \Drupal::state()->get('module_test.install_order') ?: [];
162 $this->assertIdentical($module_order, $expected_order);
166 * Tests attempting to uninstall a module that has installed dependents.
168 public function testUninstallDependents() {
169 // Enable the forum module.
170 $edit = ['modules[forum][enable]' => 'forum'];
171 $this->drupalPostForm('admin/modules', $edit, t('Install'));
172 $this->drupalPostForm(NULL, [], t('Continue'));
173 $this->assertModules(['forum'], TRUE);
175 // Check that the comment module cannot be uninstalled.
176 $this->drupalGet('admin/modules/uninstall');
177 $checkbox = $this->xpath('//input[@type="checkbox" and @name="uninstall[comment]" and @disabled="disabled"]');
178 $this->assert(count($checkbox) == 1, 'Checkbox for uninstalling the comment module is disabled.');
180 // Delete any forum terms.
181 $vid = $this->config('forum.settings')->get('vocabulary');
182 // Ensure taxonomy has been loaded into the test-runner after forum was
184 \Drupal::moduleHandler()->load('taxonomy');
185 $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]);
186 foreach ($terms as $term) {
189 // Uninstall the forum module, and check that taxonomy now can also be
191 $edit = ['uninstall[forum]' => 'forum'];
192 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
193 $this->drupalPostForm(NULL, NULL, t('Uninstall'));
194 $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
196 // Uninstall comment module.
197 $edit = ['uninstall[comment]' => 'comment'];
198 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
199 $this->drupalPostForm(NULL, NULL, t('Uninstall'));
200 $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');