ModuleInstallerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Drupal\KernelTests\Core\Extension;
  3. use Drupal\Core\Database\Database;
  4. use Drupal\Core\Extension\MissingDependencyException;
  5. use Drupal\KernelTests\KernelTestBase;
  6. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  7. /**
  8. * Tests the ModuleInstaller class.
  9. *
  10. * @coversDefaultClass \Drupal\Core\Extension\ModuleInstaller
  11. *
  12. * @group Extension
  13. */
  14. class ModuleInstallerTest extends KernelTestBase {
  15. /**
  16. * Tests that routes are rebuilt during install and uninstall of modules.
  17. *
  18. * @covers ::install
  19. * @covers ::uninstall
  20. */
  21. public function testRouteRebuild() {
  22. // Remove the routing table manually to ensure it can be created lazily
  23. // properly.
  24. Database::getConnection()->schema()->dropTable('router');
  25. $this->container->get('module_installer')->install(['router_test']);
  26. $route = $this->container->get('router.route_provider')->getRouteByName('router_test.1');
  27. $this->assertEquals('/router_test/test1', $route->getPath());
  28. $this->container->get('module_installer')->uninstall(['router_test']);
  29. $this->expectException(RouteNotFoundException::class);
  30. $this->container->get('router.route_provider')->getRouteByName('router_test.1');
  31. }
  32. /**
  33. * Tests config changes by hook_install() are saved for dependent modules.
  34. *
  35. * @covers ::install
  36. */
  37. public function testConfigChangeOnInstall() {
  38. // Install the child module so the parent is installed automatically.
  39. $this->container->get('module_installer')->install(['module_handler_test_multiple_child']);
  40. $modules = $this->config('core.extension')->get('module');
  41. $this->assertArrayHasKey('module_handler_test_multiple', $modules, 'Module module_handler_test_multiple is installed');
  42. $this->assertArrayHasKey('module_handler_test_multiple_child', $modules, 'Module module_handler_test_multiple_child is installed');
  43. $this->assertEquals(1, $modules['module_handler_test_multiple'], 'Weight of module_handler_test_multiple is set.');
  44. $this->assertEquals(1, $modules['module_handler_test_multiple_child'], 'Weight of module_handler_test_multiple_child is set.');
  45. }
  46. /**
  47. * Tests cache bins defined by modules are removed when uninstalled.
  48. *
  49. * @covers ::removeCacheBins
  50. */
  51. public function testCacheBinCleanup() {
  52. $schema = $this->container->get('database')->schema();
  53. $table = 'cache_module_cachebin';
  54. $module_installer = $this->container->get('module_installer');
  55. $module_installer->install(['module_cachebin']);
  56. // Prime the bin.
  57. /** @var \Drupal\Core\Cache\CacheBackendInterface $cache_bin */
  58. $cache_bin = $this->container->get('module_cachebin.cache_bin');
  59. $cache_bin->set('foo', 'bar');
  60. // A database backend is used so there is a convenient way check whether the
  61. // backend is uninstalled.
  62. $this->assertTrue($schema->tableExists($table));
  63. $module_installer->uninstall(['module_cachebin']);
  64. $this->assertFalse($schema->tableExists($table));
  65. }
  66. /**
  67. * Ensure that rebuilding the container in hook_install() works.
  68. */
  69. public function testKernelRebuildDuringHookInstall() {
  70. \Drupal::state()->set('module_test_install:rebuild_container', TRUE);
  71. $module_installer = $this->container->get('module_installer');
  72. $this->assertTrue($module_installer->install(['module_test']));
  73. }
  74. /**
  75. * Tests install with a module with an invalid core version constraint.
  76. *
  77. * @dataProvider providerTestInvalidCoreInstall
  78. * @covers ::install
  79. */
  80. public function testInvalidCoreInstall($module_name, $install_dependencies) {
  81. $this->expectException(MissingDependencyException::class);
  82. $this->expectExceptionMessage("Unable to install modules: module '$module_name' is incompatible with this version of Drupal core.");
  83. $this->container->get('module_installer')->install([$module_name], $install_dependencies);
  84. }
  85. /**
  86. * Dataprovider for testInvalidCoreInstall().
  87. */
  88. public function providerTestInvalidCoreInstall() {
  89. return [
  90. 'no dependencies system_incompatible_core_version_test_1x' => [
  91. 'system_incompatible_core_version_test_1x',
  92. FALSE,
  93. ],
  94. 'install_dependencies system_incompatible_core_version_test_1x' => [
  95. 'system_incompatible_core_version_test_1x',
  96. TRUE,
  97. ],
  98. 'no dependencies system_core_incompatible_semver_test' => [
  99. 'system_core_incompatible_semver_test',
  100. FALSE,
  101. ],
  102. 'install_dependencies system_core_incompatible_semver_test' => [
  103. 'system_core_incompatible_semver_test',
  104. TRUE,
  105. ],
  106. ];
  107. }
  108. /**
  109. * Tests install with a dependency with an invalid core version constraint.
  110. *
  111. * @covers ::install
  112. */
  113. public function testDependencyInvalidCoreInstall() {
  114. $this->expectException(MissingDependencyException::class);
  115. $this->expectExceptionMessage("Unable to install modules: module 'system_incompatible_core_version_dependencies_test'. Its dependency module 'system_incompatible_core_version_test' is incompatible with this version of Drupal core.");
  116. $this->container->get('module_installer')->install(['system_incompatible_core_version_dependencies_test']);
  117. }
  118. /**
  119. * Tests no dependencies install with a dependency with invalid core.
  120. *
  121. * @covers ::install
  122. */
  123. public function testDependencyInvalidCoreInstallNoDependencies() {
  124. $this->assertTrue($this->container->get('module_installer')->install(['system_incompatible_core_version_dependencies_test'], FALSE));
  125. }
  126. }