PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php

https://gitlab.com/guillaumev/alkarama
PHP | 321 lines | 159 code | 50 blank | 112 comment | 6 complexity | 7416029baa71e75b7d0c67f049f6f12b MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\system\Kernel\Extension;
  3. use Drupal\Core\Extension\MissingDependencyException;
  4. use \Drupal\Core\Extension\ModuleUninstallValidatorException;
  5. use Drupal\entity_test\Entity\EntityTest;
  6. use Drupal\KernelTests\KernelTestBase;
  7. /**
  8. * Tests ModuleHandler functionality.
  9. *
  10. * @group Extension
  11. */
  12. class ModuleHandlerTest extends KernelTestBase {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public static $modules = ['system'];
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function setUp() {
  21. parent::setUp();
  22. // @todo ModuleInstaller calls system_rebuild_module_data which is part of
  23. // system.module, see https://www.drupal.org/node/2208429.
  24. include_once $this->root . '/core/modules/system/system.module';
  25. // Set up the state values so we know where to find the files when running
  26. // drupal_get_filename().
  27. // @todo Remove as part of https://www.drupal.org/node/2186491
  28. system_rebuild_module_data();
  29. }
  30. /**
  31. * The basic functionality of retrieving enabled modules.
  32. */
  33. function testModuleList() {
  34. $module_list = ['system'];
  35. $this->assertModuleList($module_list, 'Initial');
  36. // Try to install a new module.
  37. $this->moduleInstaller()->install(array('ban'));
  38. $module_list[] = 'ban';
  39. sort($module_list);
  40. $this->assertModuleList($module_list, 'After adding a module');
  41. // Try to mess with the module weights.
  42. module_set_weight('ban', 20);
  43. // Move ban to the end of the array.
  44. unset($module_list[array_search('ban', $module_list)]);
  45. $module_list[] = 'ban';
  46. $this->assertModuleList($module_list, 'After changing weights');
  47. // Test the fixed list feature.
  48. $fixed_list = array(
  49. 'system' => 'core/modules/system/system.module',
  50. 'menu' => 'core/modules/menu/menu.module',
  51. );
  52. $this->moduleHandler()->setModuleList($fixed_list);
  53. $new_module_list = array_combine(array_keys($fixed_list), array_keys($fixed_list));
  54. $this->assertModuleList($new_module_list, t('When using a fixed list'));
  55. }
  56. /**
  57. * Assert that the extension handler returns the expected values.
  58. *
  59. * @param array $expected_values
  60. * The expected values, sorted by weight and module name.
  61. * @param $condition
  62. */
  63. protected function assertModuleList(array $expected_values, $condition) {
  64. $expected_values = array_values(array_unique($expected_values));
  65. $enabled_modules = array_keys($this->container->get('module_handler')->getModuleList());
  66. $this->assertEqual($expected_values, $enabled_modules, format_string('@condition: extension handler returns correct results', array('@condition' => $condition)));
  67. }
  68. /**
  69. * Tests dependency resolution.
  70. *
  71. * Intentionally using fake dependencies added via hook_system_info_alter()
  72. * for modules that normally do not have any dependencies.
  73. *
  74. * To simplify things further, all of the manipulated modules are either
  75. * purely UI-facing or live at the "bottom" of all dependency chains.
  76. *
  77. * @see module_test_system_info_alter()
  78. * @see https://www.drupal.org/files/issues/dep.gv__0.png
  79. */
  80. function testDependencyResolution() {
  81. $this->enableModules(array('module_test'));
  82. $this->assertTrue($this->moduleHandler()->moduleExists('module_test'), 'Test module is enabled.');
  83. // Ensure that modules are not enabled.
  84. $this->assertFalse($this->moduleHandler()->moduleExists('color'), 'Color module is disabled.');
  85. $this->assertFalse($this->moduleHandler()->moduleExists('config'), 'Config module is disabled.');
  86. $this->assertFalse($this->moduleHandler()->moduleExists('help'), 'Help module is disabled.');
  87. // Create a missing fake dependency.
  88. // Color will depend on Config, which depends on a non-existing module Foo.
  89. // Nothing should be installed.
  90. \Drupal::state()->set('module_test.dependency', 'missing dependency');
  91. drupal_static_reset('system_rebuild_module_data');
  92. try {
  93. $result = $this->moduleInstaller()->install(array('color'));
  94. $this->fail(t('ModuleInstaller::install() throws an exception if dependencies are missing.'));
  95. }
  96. catch (MissingDependencyException $e) {
  97. $this->pass(t('ModuleInstaller::install() throws an exception if dependencies are missing.'));
  98. }
  99. $this->assertFalse($this->moduleHandler()->moduleExists('color'), 'ModuleInstaller::install() aborts if dependencies are missing.');
  100. // Fix the missing dependency.
  101. // Color module depends on Config. Config depends on Help module.
  102. \Drupal::state()->set('module_test.dependency', 'dependency');
  103. drupal_static_reset('system_rebuild_module_data');
  104. $result = $this->moduleInstaller()->install(array('color'));
  105. $this->assertTrue($result, 'ModuleInstaller::install() returns the correct value.');
  106. // Verify that the fake dependency chain was installed.
  107. $this->assertTrue($this->moduleHandler()->moduleExists('config') && $this->moduleHandler()->moduleExists('help'), 'Dependency chain was installed.');
  108. // Verify that the original module was installed.
  109. $this->assertTrue($this->moduleHandler()->moduleExists('color'), 'Module installation with dependencies succeeded.');
  110. // Verify that the modules were enabled in the correct order.
  111. $module_order = \Drupal::state()->get('module_test.install_order') ?: array();
  112. $this->assertEqual($module_order, array('help', 'config', 'color'));
  113. // Uninstall all three modules explicitly, but in the incorrect order,
  114. // and make sure that ModuleInstaller::uninstall() uninstalled them in the
  115. // correct sequence.
  116. $result = $this->moduleInstaller()->uninstall(array('config', 'help', 'color'));
  117. $this->assertTrue($result, 'ModuleInstaller::uninstall() returned TRUE.');
  118. foreach (array('color', 'config', 'help') as $module) {
  119. $this->assertEqual(drupal_get_installed_schema_version($module), SCHEMA_UNINSTALLED, "$module module was uninstalled.");
  120. }
  121. $uninstalled_modules = \Drupal::state()->get('module_test.uninstall_order') ?: array();
  122. $this->assertEqual($uninstalled_modules, array('color', 'config', 'help'), 'Modules were uninstalled in the correct order.');
  123. // Enable Color module again, which should enable both the Config module and
  124. // Help module. But, this time do it with Config module declaring a
  125. // dependency on a specific version of Help module in its info file. Make
  126. // sure that Drupal\Core\Extension\ModuleInstaller::install() still works.
  127. \Drupal::state()->set('module_test.dependency', 'version dependency');
  128. drupal_static_reset('system_rebuild_module_data');
  129. $result = $this->moduleInstaller()->install(array('color'));
  130. $this->assertTrue($result, 'ModuleInstaller::install() returns the correct value.');
  131. // Verify that the fake dependency chain was installed.
  132. $this->assertTrue($this->moduleHandler()->moduleExists('config') && $this->moduleHandler()->moduleExists('help'), 'Dependency chain was installed.');
  133. // Verify that the original module was installed.
  134. $this->assertTrue($this->moduleHandler()->moduleExists('color'), 'Module installation with version dependencies succeeded.');
  135. // Finally, verify that the modules were enabled in the correct order.
  136. $enable_order = \Drupal::state()->get('module_test.install_order') ?: array();
  137. $this->assertIdentical($enable_order, array('help', 'config', 'color'));
  138. }
  139. /**
  140. * Tests uninstalling a module that is a "dependency" of a profile.
  141. */
  142. function testUninstallProfileDependency() {
  143. $profile = 'minimal';
  144. $dependency = 'dblog';
  145. $this->setSetting('install_profile', $profile);
  146. // Prime the drupal_get_filename() static cache with the location of the
  147. // minimal profile as it is not the currently active profile and we don't
  148. // yet have any cached way to retrieve its location.
  149. // @todo Remove as part of https://www.drupal.org/node/2186491
  150. drupal_get_filename('profile', $profile, 'core/profiles/' . $profile . '/' . $profile . '.info.yml');
  151. $this->enableModules(array('module_test', $profile));
  152. drupal_static_reset('system_rebuild_module_data');
  153. $data = system_rebuild_module_data();
  154. $this->assertTrue(isset($data[$profile]->requires[$dependency]));
  155. $this->moduleInstaller()->install(array($dependency));
  156. $this->assertTrue($this->moduleHandler()->moduleExists($dependency));
  157. // Uninstall the profile module "dependency".
  158. $result = $this->moduleInstaller()->uninstall(array($dependency));
  159. $this->assertTrue($result, 'ModuleInstaller::uninstall() returns TRUE.');
  160. $this->assertFalse($this->moduleHandler()->moduleExists($dependency));
  161. $this->assertEqual(drupal_get_installed_schema_version($dependency), SCHEMA_UNINSTALLED, "$dependency module was uninstalled.");
  162. // Verify that the installation profile itself was not uninstalled.
  163. $uninstalled_modules = \Drupal::state()->get('module_test.uninstall_order') ?: array();
  164. $this->assertTrue(in_array($dependency, $uninstalled_modules), "$dependency module is in the list of uninstalled modules.");
  165. $this->assertFalse(in_array($profile, $uninstalled_modules), 'The installation profile is not in the list of uninstalled modules.');
  166. }
  167. /**
  168. * Tests uninstalling a module that has content.
  169. */
  170. function testUninstallContentDependency() {
  171. $this->enableModules(array('module_test', 'entity_test', 'text', 'user', 'help'));
  172. $this->assertTrue($this->moduleHandler()->moduleExists('entity_test'), 'Test module is enabled.');
  173. $this->assertTrue($this->moduleHandler()->moduleExists('module_test'), 'Test module is enabled.');
  174. $this->installSchema('user', 'users_data');
  175. $entity_types = \Drupal::entityManager()->getDefinitions();
  176. foreach ($entity_types as $entity_type) {
  177. if ('entity_test' == $entity_type->getProvider()) {
  178. $this->installEntitySchema($entity_type->id());
  179. }
  180. }
  181. // Create a fake dependency.
  182. // entity_test will depend on help. This way help can not be uninstalled
  183. // when there is test content preventing entity_test from being uninstalled.
  184. \Drupal::state()->set('module_test.dependency', 'dependency');
  185. drupal_static_reset('system_rebuild_module_data');
  186. // Create an entity so that the modules can not be disabled.
  187. $entity = EntityTest::create(array('name' => $this->randomString()));
  188. $entity->save();
  189. // Uninstalling entity_test is not possible when there is content.
  190. try {
  191. $message = 'ModuleInstaller::uninstall() throws ModuleUninstallValidatorException upon uninstalling a module which does not pass validation.';
  192. $this->moduleInstaller()->uninstall(array('entity_test'));
  193. $this->fail($message);
  194. }
  195. catch (ModuleUninstallValidatorException $e) {
  196. $this->pass(get_class($e) . ': ' . $e->getMessage());
  197. }
  198. // Uninstalling help needs entity_test to be un-installable.
  199. try {
  200. $message = 'ModuleInstaller::uninstall() throws ModuleUninstallValidatorException upon uninstalling a module which does not pass validation.';
  201. $this->moduleInstaller()->uninstall(array('help'));
  202. $this->fail($message);
  203. }
  204. catch (ModuleUninstallValidatorException $e) {
  205. $this->pass(get_class($e) . ': ' . $e->getMessage());
  206. }
  207. // Deleting the entity.
  208. $entity->delete();
  209. $result = $this->moduleInstaller()->uninstall(array('help'));
  210. $this->assertTrue($result, 'ModuleInstaller::uninstall() returns TRUE.');
  211. $this->assertEqual(drupal_get_installed_schema_version('entity_test'), SCHEMA_UNINSTALLED, "entity_test module was uninstalled.");
  212. }
  213. /**
  214. * Tests whether the correct module metadata is returned.
  215. */
  216. function testModuleMetaData() {
  217. // Generate the list of available modules.
  218. $modules = system_rebuild_module_data();
  219. // Check that the mtime field exists for the system module.
  220. $this->assertTrue(!empty($modules['system']->info['mtime']), 'The system.info.yml file modification time field is present.');
  221. // Use 0 if mtime isn't present, to avoid an array index notice.
  222. $test_mtime = !empty($modules['system']->info['mtime']) ? $modules['system']->info['mtime'] : 0;
  223. // Ensure the mtime field contains a number that is greater than zero.
  224. $this->assertTrue(is_numeric($test_mtime) && ($test_mtime > 0), 'The system.info.yml file modification time field contains a timestamp.');
  225. }
  226. /**
  227. * Tests whether module-provided stream wrappers are registered properly.
  228. */
  229. public function testModuleStreamWrappers() {
  230. // file_test.module provides (among others) a 'dummy' stream wrapper.
  231. // Verify that it is not registered yet to prevent false positives.
  232. $stream_wrappers = \Drupal::service('stream_wrapper_manager')->getWrappers();
  233. $this->assertFalse(isset($stream_wrappers['dummy']));
  234. $this->moduleInstaller()->install(['file_test']);
  235. // Verify that the stream wrapper is available even without calling
  236. // \Drupal::service('stream_wrapper_manager')->getWrappers() again.
  237. // If the stream wrapper is not available file_exists() will raise a notice.
  238. file_exists('dummy://');
  239. $stream_wrappers = \Drupal::service('stream_wrapper_manager')->getWrappers();
  240. $this->assertTrue(isset($stream_wrappers['dummy']));
  241. }
  242. /**
  243. * Tests whether the correct theme metadata is returned.
  244. */
  245. function testThemeMetaData() {
  246. // Generate the list of available themes.
  247. $themes = \Drupal::service('theme_handler')->rebuildThemeData();
  248. // Check that the mtime field exists for the bartik theme.
  249. $this->assertTrue(!empty($themes['bartik']->info['mtime']), 'The bartik.info.yml file modification time field is present.');
  250. // Use 0 if mtime isn't present, to avoid an array index notice.
  251. $test_mtime = !empty($themes['bartik']->info['mtime']) ? $themes['bartik']->info['mtime'] : 0;
  252. // Ensure the mtime field contains a number that is greater than zero.
  253. $this->assertTrue(is_numeric($test_mtime) && ($test_mtime > 0), 'The bartik.info.yml file modification time field contains a timestamp.');
  254. }
  255. /**
  256. * Returns the ModuleHandler.
  257. *
  258. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  259. */
  260. protected function moduleHandler() {
  261. return $this->container->get('module_handler');
  262. }
  263. /**
  264. * Returns the ModuleInstaller.
  265. *
  266. * @return \Drupal\Core\Extension\ModuleInstallerInterface
  267. */
  268. protected function moduleInstaller() {
  269. return $this->container->get('module_installer');
  270. }
  271. }