3 namespace Drupal\system\Tests\Module;
4 use Drupal\Component\Utility\Unicode;
7 * Enable module without dependency enabled.
11 class DependencyTest extends ModuleTestBase {
13 * Checks functionality of project namespaces for dependencies.
15 public function testProjectNamespaceForDependencies() {
17 'modules[filter][enable]' => TRUE,
19 $this->drupalPostForm('admin/modules', $edit, t('Install'));
20 // Enable module with project namespace to ensure nothing breaks.
22 'modules[system_project_namespace_test][enable]' => TRUE,
24 $this->drupalPostForm('admin/modules', $edit, t('Install'));
25 $this->assertModules(['system_project_namespace_test'], TRUE);
29 * Attempts to enable the Content Translation module without Language enabled.
31 public function testEnableWithoutDependency() {
32 // Attempt to enable Content Translation without Language enabled.
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.');
38 $this->assertModules(['content_translation', 'language'], FALSE);
40 // Assert that the language tables weren't enabled.
41 $this->assertTableCount('language', FALSE);
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);
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.');
53 * Attempts to enable a module with a missing dependency.
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.');
65 * Tests enabling a module that depends on an incompatible version of a module.
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)',
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.');
80 * Tests enabling a module that depends on a module with an incompatible core version.
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.');
94 * Tests enabling a module that depends on a module which fails hook_requirements().
96 public function testEnableRequirementsFailureDependency() {
97 \Drupal::service('module_installer')->install(['comment']);
99 $this->assertModules(['requirements1_test'], FALSE);
100 $this->assertModules(['requirements2_test'], FALSE);
102 // Attempt to install both modules at the same time.
104 $edit['modules[requirements1_test][enable]'] = 'requirements1_test';
105 $edit['modules[requirements2_test][enable]'] = 'requirements2_test';
106 $this->drupalPostForm('admin/modules', $edit, t('Install'));
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);
113 // Makes sure that already enabled modules the failing modules depend on
114 // were not disabled.
115 $this->assertModules(['comment'], TRUE);
119 * Tests that module dependencies are enabled in the correct order in the UI.
121 * Dependencies should be enabled before their dependents.
123 public function testModuleEnableOrder() {
124 \Drupal::service('module_installer')->install(['module_test'], FALSE);
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'];
133 // Enable the modules through the UI, verifying that the dependency chain
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
141 $this->assertText(t('You must enable the Configuration Manager, Help modules to install Color.'));
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);
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);
154 * Tests attempting to uninstall a module that has installed dependents.
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);
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.');
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
172 \Drupal::moduleHandler()->load('taxonomy');
173 $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]);
174 foreach ($terms as $term) {
177 // Uninstall the forum module, and check that taxonomy now can also be
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.');
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.');