Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Functional / Module / DependencyTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Module;
4
5 use Drupal\Component\Utility\Unicode;
6
7 /**
8  * Enable module without dependency enabled.
9  *
10  * @group Module
11  */
12 class DependencyTest extends ModuleTestBase {
13
14   /**
15    * Checks functionality of project namespaces for dependencies.
16    */
17   public function testProjectNamespaceForDependencies() {
18     $edit = [
19       'modules[filter][enable]' => TRUE,
20     ];
21     $this->drupalPostForm('admin/modules', $edit, t('Install'));
22     // Enable module with project namespace to ensure nothing breaks.
23     $edit = [
24       'modules[system_project_namespace_test][enable]' => TRUE,
25     ];
26     $this->drupalPostForm('admin/modules', $edit, t('Install'));
27     $this->assertModules(['system_project_namespace_test'], TRUE);
28   }
29
30   /**
31    * Attempts to enable the Content Translation module without Language enabled.
32    */
33   public function testEnableWithoutDependency() {
34     // Attempt to enable Content Translation without Language enabled.
35     $edit = [];
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.');
39
40     $this->assertModules(['content_translation', 'language'], FALSE);
41
42     // Assert that the language tables weren't enabled.
43     $this->assertTableCount('language', FALSE);
44
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);
48
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.');
52   }
53
54   /**
55    * Attempts to enable a module with a missing dependency.
56    */
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.');
64   }
65
66   /**
67    * Tests enabling a module that depends on an incompatible version of a module.
68    */
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)',
75       '@version' => '1.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.');
79   }
80
81   /**
82    * Tests enabling a module that depends on a module with an incompatible core version.
83    */
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.');
93   }
94
95   /**
96    * Tests failing PHP version requirements.
97    */
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.');
103   }
104
105   /**
106    * Tests enabling a module that depends on a module which fails hook_requirements().
107    */
108   public function testEnableRequirementsFailureDependency() {
109     \Drupal::service('module_installer')->install(['comment']);
110
111     $this->assertModules(['requirements1_test'], FALSE);
112     $this->assertModules(['requirements2_test'], FALSE);
113
114     // Attempt to install both modules at the same time.
115     $edit = [];
116     $edit['modules[requirements1_test][enable]'] = 'requirements1_test';
117     $edit['modules[requirements2_test][enable]'] = 'requirements2_test';
118     $this->drupalPostForm('admin/modules', $edit, t('Install'));
119
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);
124
125     // Makes sure that already enabled modules the failing modules depend on
126     // were not disabled.
127     $this->assertModules(['comment'], TRUE);
128   }
129
130   /**
131    * Tests that module dependencies are enabled in the correct order in the UI.
132    *
133    * Dependencies should be enabled before their dependents.
134    */
135   public function testModuleEnableOrder() {
136     \Drupal::service('module_installer')->install(['module_test'], FALSE);
137     $this->resetAll();
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'];
144
145     // Enable the modules through the UI, verifying that the dependency chain
146     // is correct.
147     $edit = [];
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
152     // message.
153     $this->assertText(t('You must enable the Configuration Manager, Help modules to install Color.'));
154
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);
159
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);
163   }
164
165   /**
166    * Tests attempting to uninstall a module that has installed dependents.
167    */
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);
174
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.');
179
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
183     // enabled.
184     \Drupal::moduleHandler()->load('taxonomy');
185     $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]);
186     foreach ($terms as $term) {
187       $term->delete();
188     }
189     // Uninstall the forum module, and check that taxonomy now can also be
190     // uninstalled.
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.');
195
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.');
201   }
202
203 }