PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/system/tests/src/Functional/Module/DependencyTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 277 lines | 158 code | 38 blank | 81 comment | 0 complexity | 8c23fd6fa798ae8be56812ff5def865e MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\system\Functional\Module;
  3. use Drupal\Component\Serialization\Yaml;
  4. use Drupal\Component\Utility\Unicode;
  5. /**
  6. * Enable module without dependency enabled.
  7. *
  8. * @group Module
  9. */
  10. class DependencyTest extends ModuleTestBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. protected $defaultTheme = 'stark';
  15. /**
  16. * Checks functionality of project namespaces for dependencies.
  17. */
  18. public function testProjectNamespaceForDependencies() {
  19. $edit = [
  20. 'modules[filter][enable]' => TRUE,
  21. ];
  22. $this->drupalGet('admin/modules');
  23. $this->submitForm($edit, 'Install');
  24. // Enable module with project namespace to ensure nothing breaks.
  25. $edit = [
  26. 'modules[system_project_namespace_test][enable]' => TRUE,
  27. ];
  28. $this->drupalGet('admin/modules');
  29. $this->submitForm($edit, 'Install');
  30. $this->assertModules(['system_project_namespace_test'], TRUE);
  31. }
  32. /**
  33. * Attempts to enable the Content Translation module without Language enabled.
  34. */
  35. public function testEnableWithoutDependency() {
  36. // Attempt to enable Content Translation without Language enabled.
  37. $edit = [];
  38. $edit['modules[content_translation][enable]'] = 'content_translation';
  39. $this->drupalGet('admin/modules');
  40. $this->submitForm($edit, 'Install');
  41. $this->assertSession()->pageTextContains('Some required modules must be enabled');
  42. $this->assertModules(['content_translation', 'language'], FALSE);
  43. // Assert that the language module config was not installed.
  44. $this->assertNoModuleConfig('language');
  45. $this->submitForm([], 'Continue');
  46. $this->assertSession()->pageTextContains('2 modules have been enabled: Content Translation, Language.');
  47. $this->assertModules(['content_translation', 'language'], TRUE);
  48. // Assert that the language YAML files were created.
  49. $storage = $this->container->get('config.storage');
  50. $this->assertNotEmpty($storage->listAll('language.entity.'), 'Language config entity files exist.');
  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->assertSession()->pageTextContains(Unicode::ucfirst('_missing_dependency') . ' (missing)');
  60. $this->assertSession()->elementTextEquals('xpath', '//tr[@data-drupal-selector="edit-modules-system-dependencies-test"]//span[@class="admin-missing"]', 'missing');
  61. $this->assertSession()->checkboxNotChecked('modules[system_dependencies_test][enable]');
  62. }
  63. /**
  64. * Tests enabling a module that depends on an incompatible version of a
  65. * 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->assertSession()->pageTextContains('System incompatible module version test (>2.0) (incompatible with version 1.0)');
  72. $this->assertSession()->elementTextEquals('xpath', '//tr[@data-drupal-selector="edit-modules-system-incompatible-module-version-dependencies-test"]//span[@class="admin-missing"]', 'incompatible with');
  73. $this->assertSession()->fieldDisabled('modules[system_incompatible_module_version_dependencies_test][enable]');
  74. }
  75. /**
  76. * Tests enabling a module that depends on a module with an incompatible core version.
  77. */
  78. public function testIncompatibleCoreVersionDependency() {
  79. // Test that the system_incompatible_core_version_dependencies_test is
  80. // marked as having an incompatible dependency.
  81. $this->drupalGet('admin/modules');
  82. $this->assertSession()->pageTextContains('System core incompatible semver test (incompatible with this version of Drupal core)');
  83. $this->assertSession()->elementTextEquals('xpath', '//tr[@data-drupal-selector="edit-modules-system-incompatible-core-version-dependencies-test"]//span[@class="admin-missing"]', 'incompatible with');
  84. $this->assertSession()->fieldDisabled('modules[system_incompatible_core_version_dependencies_test][enable]');
  85. }
  86. /**
  87. * Tests visiting admin/modules when a module outside of core has no version.
  88. */
  89. public function testNoVersionInfo() {
  90. // Create a module for testing. We set core_version_requirement to '*' for
  91. // the test so that it does not need to be updated between major versions.
  92. $info = [
  93. 'type' => 'module',
  94. 'core_version_requirement' => '*',
  95. 'name' => 'System no module version dependency test',
  96. ];
  97. $path = $this->siteDirectory . '/modules/system_no_module_version_dependency_test';
  98. mkdir($path, 0777, TRUE);
  99. file_put_contents("$path/system_no_module_version_dependency_test.info.yml", Yaml::encode($info));
  100. $info = [
  101. 'type' => 'module',
  102. 'core_version_requirement' => '*',
  103. 'name' => 'System no module version test',
  104. 'dependencies' => ['system_no_module_version_dependency_test'],
  105. ];
  106. $path = $this->siteDirectory . '/modules/system_no_module_version_test';
  107. mkdir($path, 0777, TRUE);
  108. file_put_contents("$path/system_no_module_version_test.info.yml", Yaml::encode($info));
  109. $this->drupalGet('admin/modules');
  110. $this->assertSession()->pageTextContains('System no module version dependency test');
  111. $this->assertSession()->pageTextContains('System no module version test');
  112. // Ensure the modules can actually be installed.
  113. $edit['modules[system_no_module_version_test][enable]'] = 'system_no_module_version_test';
  114. $edit['modules[system_no_module_version_dependency_test][enable]'] = 'system_no_module_version_dependency_test';
  115. $this->drupalGet('admin/modules');
  116. $this->submitForm($edit, 'Install');
  117. $this->assertSession()->pageTextContains('2 modules have been enabled: System no module version dependency test, System no module version test.');
  118. // Ensure status report is working.
  119. $this->drupalLogin($this->createUser(['administer site configuration']));
  120. $this->drupalGet('admin/reports/status');
  121. $this->assertSession()->statusCodeEquals(200);
  122. }
  123. /**
  124. * Tests failing PHP version requirements.
  125. */
  126. public function testIncompatiblePhpVersionDependency() {
  127. $this->drupalGet('admin/modules');
  128. $this->assertSession()->pageTextContains('This module requires PHP version 6502.* and is incompatible with PHP version ' . phpversion() . '.');
  129. $this->assertSession()->fieldDisabled('modules[system_incompatible_php_version_test][enable]');
  130. }
  131. /**
  132. * Tests enabling modules with different core version specifications.
  133. */
  134. public function testCoreCompatibility() {
  135. $assert_session = $this->assertSession();
  136. // Test incompatible 'core_version_requirement'.
  137. $this->drupalGet('admin/modules');
  138. $assert_session->fieldDisabled('modules[system_core_incompatible_semver_test][enable]');
  139. // Test compatible 'core_version_requirement' and compatible 'core'.
  140. $this->drupalGet('admin/modules');
  141. $assert_session->fieldEnabled('modules[common_test][enable]');
  142. $assert_session->fieldEnabled('modules[system_core_semver_test][enable]');
  143. // Ensure the modules can actually be installed.
  144. $edit['modules[common_test][enable]'] = 'common_test';
  145. $edit['modules[system_core_semver_test][enable]'] = 'system_core_semver_test';
  146. $this->drupalGet('admin/modules');
  147. $this->submitForm($edit, 'Install');
  148. $this->assertModules(['common_test', 'system_core_semver_test'], TRUE);
  149. }
  150. /**
  151. * Tests enabling a module that depends on a module which fails hook_requirements().
  152. */
  153. public function testEnableRequirementsFailureDependency() {
  154. \Drupal::service('module_installer')->install(['comment']);
  155. $this->assertModules(['requirements1_test'], FALSE);
  156. $this->assertModules(['requirements2_test'], FALSE);
  157. // Attempt to install both modules at the same time.
  158. $edit = [];
  159. $edit['modules[requirements1_test][enable]'] = 'requirements1_test';
  160. $edit['modules[requirements2_test][enable]'] = 'requirements2_test';
  161. $this->drupalGet('admin/modules');
  162. $this->submitForm($edit, 'Install');
  163. // Makes sure the modules were NOT installed.
  164. $this->assertSession()->pageTextContains('Requirements 1 Test failed requirements');
  165. $this->assertModules(['requirements1_test'], FALSE);
  166. $this->assertModules(['requirements2_test'], FALSE);
  167. // Makes sure that already enabled modules the failing modules depend on
  168. // were not disabled.
  169. $this->assertModules(['comment'], TRUE);
  170. }
  171. /**
  172. * Tests that module dependencies are enabled in the correct order in the UI.
  173. *
  174. * Dependencies should be enabled before their dependents.
  175. */
  176. public function testModuleEnableOrder() {
  177. \Drupal::service('module_installer')->install(['module_test'], FALSE);
  178. $this->resetAll();
  179. $this->assertModules(['module_test'], TRUE);
  180. \Drupal::state()->set('module_test.dependency', 'dependency');
  181. // module_test creates a dependency chain:
  182. // - color depends on config
  183. // - config depends on help
  184. $expected_order = ['help', 'config', 'color'];
  185. // Enable the modules through the UI, verifying that the dependency chain
  186. // is correct.
  187. $edit = [];
  188. $edit['modules[color][enable]'] = 'color';
  189. $this->drupalGet('admin/modules');
  190. $this->submitForm($edit, 'Install');
  191. $this->assertModules(['color'], FALSE);
  192. // Note that dependencies are sorted alphabetically in the confirmation
  193. // message.
  194. $this->assertSession()->pageTextContains('You must enable the Configuration Manager, Help modules to install Color.');
  195. $edit['modules[config][enable]'] = 'config';
  196. $edit['modules[help][enable]'] = 'help';
  197. $this->drupalGet('admin/modules');
  198. $this->submitForm($edit, 'Install');
  199. $this->assertModules(['color', 'config', 'help'], TRUE);
  200. // Check the actual order which is saved by module_test_modules_enabled().
  201. $module_order = \Drupal::state()->get('module_test.install_order', []);
  202. $this->assertSame($expected_order, $module_order);
  203. }
  204. /**
  205. * Tests attempting to uninstall a module that has installed dependents.
  206. */
  207. public function testUninstallDependents() {
  208. // Enable the forum module.
  209. $edit = ['modules[forum][enable]' => 'forum'];
  210. $this->drupalGet('admin/modules');
  211. $this->submitForm($edit, 'Install');
  212. $this->submitForm([], 'Continue');
  213. $this->assertModules(['forum'], TRUE);
  214. // Check that the comment module cannot be uninstalled.
  215. $this->drupalGet('admin/modules/uninstall');
  216. $this->assertSession()->fieldDisabled('uninstall[comment]');
  217. // Delete any forum terms.
  218. $vid = $this->config('forum.settings')->get('vocabulary');
  219. // Ensure taxonomy has been loaded into the test-runner after forum was
  220. // enabled.
  221. \Drupal::moduleHandler()->load('taxonomy');
  222. $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
  223. $terms = $storage->loadByProperties(['vid' => $vid]);
  224. $storage->delete($terms);
  225. // Uninstall the forum module, and check that taxonomy now can also be
  226. // uninstalled.
  227. $edit = ['uninstall[forum]' => 'forum'];
  228. $this->drupalGet('admin/modules/uninstall');
  229. $this->submitForm($edit, 'Uninstall');
  230. $this->submitForm([], 'Uninstall');
  231. $this->assertSession()->pageTextContains('The selected modules have been uninstalled.');
  232. // Uninstall comment module.
  233. $edit = ['uninstall[comment]' => 'comment'];
  234. $this->drupalGet('admin/modules/uninstall');
  235. $this->submitForm($edit, 'Uninstall');
  236. $this->submitForm([], 'Uninstall');
  237. $this->assertSession()->pageTextContains('The selected modules have been uninstalled.');
  238. }
  239. }