b5cb96b8cc4754a577cdbed51a8c909abc16cb14
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / DrupalKernel / DrupalKernelTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\DrupalKernel;
4
5 use Drupal\Core\DrupalKernel;
6 use Drupal\KernelTests\KernelTestBase;
7 use Symfony\Component\HttpFoundation\Request;
8
9 /**
10  * Tests DIC compilation to disk.
11  *
12  * @group DrupalKernel
13  */
14 class DrupalKernelTest extends KernelTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   protected function setUp() {
20     // DrupalKernel relies on global $config_directories and requires those
21     // directories to exist. Therefore, create the directories, but do not
22     // invoke KernelTestBase::setUp(), since that would set up further
23     // environment aspects, which would distort this test, because it tests
24     // the DrupalKernel (re-)building itself.
25     $this->root = static::getDrupalRoot();
26     $this->bootEnvironment();
27   }
28
29   /**
30    * Build a kernel for testings.
31    *
32    * Because the bootstrap is in DrupalKernel::boot and that involved loading
33    * settings from the filesystem we need to go to extra lengths to build a kernel
34    * for testing.
35    *
36    * @param \Symfony\Component\HttpFoundation\Request $request
37    *   A request object to use in booting the kernel.
38    * @param array $modules_enabled
39    *   A list of modules to enable on the kernel.
40    *
41    * @return \Drupal\Core\DrupalKernel
42    *   New kernel for testing.
43    */
44   protected function getTestKernel(Request $request, array $modules_enabled = NULL) {
45     // Manually create kernel to avoid replacing settings.
46     $class_loader = require $this->root . '/autoload.php';
47     $kernel = DrupalKernel::createFromRequest($request, $class_loader, 'testing');
48     $this->setSetting('container_yamls', []);
49     $this->setSetting('hash_salt', $this->databasePrefix);
50     if (isset($modules_enabled)) {
51       $kernel->updateModules($modules_enabled);
52     }
53     $kernel->boot();
54
55     return $kernel;
56   }
57
58   /**
59    * Tests DIC compilation.
60    */
61   public function testCompileDIC() {
62     // @todo: write a memory based storage backend for testing.
63     $modules_enabled = [
64       'system' => 'system',
65       'user' => 'user',
66     ];
67
68     $request = Request::createFromGlobals();
69     $this->getTestKernel($request, $modules_enabled);
70
71     // Instantiate it a second time and we should get the compiled Container
72     // class.
73     $kernel = $this->getTestKernel($request);
74     $container = $kernel->getContainer();
75     $refClass = new \ReflectionClass($container);
76     $is_compiled_container = !$refClass->isSubclassOf('Symfony\Component\DependencyInjection\ContainerBuilder');
77     $this->assertTrue($is_compiled_container);
78     // Verify that the list of modules is the same for the initial and the
79     // compiled container.
80     $module_list = array_keys($container->get('module_handler')->getModuleList());
81     $this->assertEqual(array_values($modules_enabled), $module_list);
82
83     // Get the container another time, simulating a "production" environment.
84     $container = $this->getTestKernel($request, NULL)
85       ->getContainer();
86
87     $refClass = new \ReflectionClass($container);
88     $is_compiled_container = !$refClass->isSubclassOf('Symfony\Component\DependencyInjection\ContainerBuilder');
89     $this->assertTrue($is_compiled_container);
90
91     // Verify that the list of modules is the same for the initial and the
92     // compiled container.
93     $module_list = array_keys($container->get('module_handler')->getModuleList());
94     $this->assertEqual(array_values($modules_enabled), $module_list);
95
96     // Test that our synthetic services are there.
97     $class_loader = $container->get('class_loader');
98     $refClass = new \ReflectionClass($class_loader);
99     $this->assertTrue($refClass->hasMethod('loadClass'), 'Container has a class loader');
100
101     // We make this assertion here purely to show that the new container below
102     // is functioning correctly, i.e. we get a brand new ContainerBuilder
103     // which has the required new services, after changing the list of enabled
104     // modules.
105     $this->assertFalse($container->has('service_provider_test_class'));
106
107     // Add another module so that we can test that the new module's bundle is
108     // registered to the new container.
109     $modules_enabled['service_provider_test'] = 'service_provider_test';
110     $this->getTestKernel($request, $modules_enabled);
111
112     // Instantiate it a second time and we should not get a ContainerBuilder
113     // class because we are loading the container definition from cache.
114     $kernel = $this->getTestKernel($request, $modules_enabled);
115     $container = $kernel->getContainer();
116
117     $refClass = new \ReflectionClass($container);
118     $is_container_builder = $refClass->isSubclassOf('Symfony\Component\DependencyInjection\ContainerBuilder');
119     $this->assertFalse($is_container_builder, 'Container is not a builder');
120
121     // Assert that the new module's bundle was registered to the new container.
122     $this->assertTrue($container->has('service_provider_test_class'), 'Container has test service');
123
124     // Test that our synthetic services are there.
125     $class_loader = $container->get('class_loader');
126     $refClass = new \ReflectionClass($class_loader);
127     $this->assertTrue($refClass->hasMethod('loadClass'), 'Container has a class loader');
128
129     // Check that the location of the new module is registered.
130     $modules = $container->getParameter('container.modules');
131     $this->assertEqual($modules['service_provider_test'], [
132       'type' => 'module',
133       'pathname' => drupal_get_filename('module', 'service_provider_test'),
134       'filename' => NULL,
135     ]);
136
137     // Check that the container itself is not among the persist IDs because it
138     // does not make sense to persist the container itself.
139     $persist_ids = $container->getParameter('persist_ids');
140     $this->assertSame(FALSE, array_search('service_container', $persist_ids));
141   }
142
143   /**
144    * Tests repeated loading of compiled DIC with different environment.
145    */
146   public function testRepeatedBootWithDifferentEnvironment() {
147     $request = Request::createFromGlobals();
148     $class_loader = require $this->root . '/autoload.php';
149
150     $environments = [
151       'testing1',
152       'testing1',
153       'testing2',
154       'testing2',
155     ];
156
157     foreach ($environments as $environment) {
158       $kernel = DrupalKernel::createFromRequest($request, $class_loader, $environment);
159       $this->setSetting('container_yamls', []);
160       $this->setSetting('hash_salt', $this->databasePrefix);
161       $kernel->boot();
162     }
163
164     $this->pass('Repeatedly loaded compiled DIC with different environment');
165   }
166
167   /**
168    * Tests setting of site path after kernel boot.
169    */
170   public function testPreventChangeOfSitePath() {
171     // @todo: write a memory based storage backend for testing.
172     $modules_enabled = [
173       'system' => 'system',
174       'user' => 'user',
175     ];
176
177     $request = Request::createFromGlobals();
178     $kernel = $this->getTestKernel($request, $modules_enabled);
179     $pass = FALSE;
180     try {
181       $kernel->setSitePath('/dev/null');
182     }
183     catch (\LogicException $e) {
184       $pass = TRUE;
185     }
186     $this->assertTrue($pass, 'Throws LogicException if DrupalKernel::setSitePath() is called after boot');
187
188     // Ensure no LogicException if DrupalKernel::setSitePath() is called with
189     // identical path after boot.
190     $path = $kernel->getSitePath();
191     $kernel->setSitePath($path);
192   }
193
194 }