3 namespace Drupal\KernelTests\Core\Extension;
5 use Drupal\Core\Database\Database;
6 use Drupal\KernelTests\KernelTestBase;
7 use Symfony\Component\Routing\Exception\RouteNotFoundException;
10 * Tests the ModuleInstaller class.
12 * @coversDefaultClass \Drupal\Core\Extension\ModuleInstaller
16 class ModuleInstallerTest extends KernelTestBase {
21 * The System module is required because system_rebuild_module_data() is used.
25 public static $modules = ['system'];
28 * Tests that routes are rebuilt during install and uninstall of modules.
33 public function testRouteRebuild() {
34 // Remove the routing table manually to ensure it can be created lazily
36 Database::getConnection()->schema()->dropTable('router');
38 $this->container->get('module_installer')->install(['router_test']);
39 $route = $this->container->get('router.route_provider')->getRouteByName('router_test.1');
40 $this->assertEquals('/router_test/test1', $route->getPath());
42 $this->container->get('module_installer')->uninstall(['router_test']);
43 $this->setExpectedException(RouteNotFoundException::class);
44 $this->container->get('router.route_provider')->getRouteByName('router_test.1');
48 * Tests config changes by hook_install() are saved for dependent modules.
52 public function testConfigChangeOnInstall() {
53 // Install the child module so the parent is installed automatically.
54 $this->container->get('module_installer')->install(['module_handler_test_multiple_child']);
55 $modules = $this->config('core.extension')->get('module');
57 $this->assertArrayHasKey('module_handler_test_multiple', $modules, 'Module module_handler_test_multiple is installed');
58 $this->assertArrayHasKey('module_handler_test_multiple_child', $modules, 'Module module_handler_test_multiple_child is installed');
59 $this->assertEquals(1, $modules['module_handler_test_multiple'], 'Weight of module_handler_test_multiple is set.');
60 $this->assertEquals(1, $modules['module_handler_test_multiple_child'], 'Weight of module_handler_test_multiple_child is set.');
64 * Tests cache bins defined by modules are removed when uninstalled.
66 * @covers ::removeCacheBins
68 public function testCacheBinCleanup() {
69 $schema = $this->container->get('database')->schema();
70 $table = 'cache_module_cachebin';
72 $module_installer = $this->container->get('module_installer');
73 $module_installer->install(['module_cachebin']);
76 /** @var \Drupal\Core\Cache\CacheBackendInterface $cache_bin */
77 $cache_bin = $this->container->get('module_cachebin.cache_bin');
78 $cache_bin->set('foo', 'bar');
80 // A database backend is used so there is a convenient way check whether the
81 // backend is uninstalled.
82 $this->assertTrue($schema->tableExists($table));
84 $module_installer->uninstall(['module_cachebin']);
85 $this->assertFalse($schema->tableExists($table));