Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Extension / ModuleInstallerTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Extension;
4
5 use Drupal\Core\Database\Database;
6 use Drupal\KernelTests\KernelTestBase;
7 use Symfony\Component\Routing\Exception\RouteNotFoundException;
8
9 /**
10  * Tests the ModuleInstaller class.
11  *
12  * @coversDefaultClass \Drupal\Core\Extension\ModuleInstaller
13  *
14  * @group Extension
15  */
16 class ModuleInstallerTest extends KernelTestBase {
17
18   /**
19    * Modules to install.
20    *
21    * The System module is required because system_rebuild_module_data() is used.
22    *
23    * @var array
24    */
25   public static $modules = ['system'];
26
27   /**
28    * Tests that routes are rebuilt during install and uninstall of modules.
29    *
30    * @covers ::install
31    * @covers ::uninstall
32    */
33   public function testRouteRebuild() {
34     // Remove the routing table manually to ensure it can be created lazily
35     // properly.
36     Database::getConnection()->schema()->dropTable('router');
37
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());
41
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');
45   }
46
47   /**
48    * Tests config changes by hook_install() are saved for dependent modules.
49    *
50    * @covers ::install
51    */
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');
56
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.');
61   }
62
63   /**
64    * Tests cache bins defined by modules are removed when uninstalled.
65    *
66    * @covers ::removeCacheBins
67    */
68   public function testCacheBinCleanup() {
69     $schema = $this->container->get('database')->schema();
70     $table = 'cache_module_cachebin';
71
72     $module_installer = $this->container->get('module_installer');
73     $module_installer->install(['module_cachebin']);
74
75     // Prime the bin.
76     /** @var \Drupal\Core\Cache\CacheBackendInterface $cache_bin */
77     $cache_bin = $this->container->get('module_cachebin.cache_bin');
78     $cache_bin->set('foo', 'bar');
79
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));
83
84     $module_installer->uninstall(['module_cachebin']);
85     $this->assertFalse($schema->tableExists($table));
86   }
87
88 }