3 namespace Drupal\Tests\Core\DrupalKernel {
5 use Drupal\Core\DrupalKernel;
6 use Drupal\Tests\UnitTestCase;
7 use org\bovigo\vfs\vfsStream;
8 use Symfony\Component\ClassLoader\ApcClassLoader;
9 use Symfony\Component\HttpFoundation\Request;
12 * @coversDefaultClass \Drupal\Core\DrupalKernel
15 class DrupalKernelTest extends UnitTestCase {
18 * Tests hostname validation with settings.
20 * @covers ::setupTrustedHosts
21 * @dataProvider providerTestTrustedHosts
23 public function testTrustedHosts($host, $server_name, $message, $expected = FALSE) {
24 $request = new Request();
26 $trusted_host_patterns = [
34 $request->headers->set('HOST', $host);
37 $request->server->set('SERVER_NAME', $server_name);
39 $method = new \ReflectionMethod('Drupal\Core\DrupalKernel', 'setupTrustedHosts');
40 $method->setAccessible(TRUE);
41 $valid_host = $method->invoke(NULL, $request, $trusted_host_patterns);
43 $this->assertSame($expected, $valid_host, $message);
45 // Reset the trusted hosts because it is statically stored on the request.
46 $method->invoke(NULL, $request, []);
47 // Reset the request factory because it is statically stored on the request.
48 Request::setFactory(NULL);
52 * Tests the reregistration of autoloaders if APCu available.
54 * This test runs in a separate process since it registers class loaders and
55 * results in statics being set.
57 * @runInSeparateProcess
58 * @preserveGlobalState disabled
59 * @requires function apcu_fetch
60 * @covers ::initializeSettings
62 public function testInitializeSettings() {
63 $request = new Request();
64 $classloader = new fakeAutoloader();
66 // Create a kernel suitable for testing.
67 $kernel = $this->getMockBuilder(DrupalKernel::class)
68 ->disableOriginalConstructor()
69 ->setMethods(['do_not_mock_any_methods'])
71 $classloader_property = new \ReflectionProperty($kernel, 'classLoader');
72 $classloader_property->setAccessible(TRUE);
73 $classloader_property->setValue($kernel, $classloader);
74 $method = new \ReflectionMethod($kernel, 'initializeSettings');
75 $method->setAccessible(TRUE);
77 // Prepend another autoloader to simulate Drush's autoloader.
78 $fake_drush_autoloader = function () {
81 spl_autoload_register($fake_drush_autoloader, TRUE, TRUE);
83 // Before calling DrupalKernel::initializeSettings() the first autoloader
84 // is the fake Drush autoloader.
85 $this->assertSame($fake_drush_autoloader, spl_autoload_functions()[0]);
87 // Call DrupalKernel::initializeSettings() to simulate part of a Drupal
88 // bootstrap. During the include of autoload.php Composer would prepend
89 // Drupal's autoloader and then this method should not result in Drush's
90 // autoloader becoming the first autoloader even if it swaps out
91 // Composer's autoloader for an optimised one.
92 $method->invoke($kernel, $request);
94 $autoloaders = spl_autoload_functions();
95 // The first autoloader should be the APCu based autoloader.
96 $this->assertInstanceOf(ApcClassLoader::class, $autoloaders[0][0]);
97 // The second autoloader should be the original autoloader the kernel was
99 $this->assertSame($classloader, $autoloaders[1][0]);
100 // The third autoloader should be Drush's autoloader.
101 $this->assertSame($fake_drush_autoloader, $autoloaders[2]);
103 // Reset the request factory because it is statically stored on the
105 Request::setFactory(NULL);
109 * Provides test data for testTrustedHosts().
111 public function providerTestTrustedHosts() {
114 // Tests canonical URL.
118 'canonical URL is trusted',
122 // Tests missing hostname for HTTP/1.0 compatibility where the Host
123 // header is optional.
124 $data[] = [NULL, 'www.example.com', 'empty Host is valid', TRUE];
126 // Tests the additional patterns from the settings.
130 'host from settings is trusted',
134 'subdomain.example.com',
136 'host from settings is trusted',
142 'host from settings is trusted',
148 'host from settings is trusted',
156 'unspecified host is untrusted',
164 * Tests site path finding.
166 * This test is run in a separate process since it defines DRUPAL_ROOT. This
167 * stops any possible pollution of other tests.
169 * @covers ::findSitePath
170 * @runInSeparateProcess
172 public function testFindSitePath() {
173 $vfs_root = vfsStream::setup('drupal_root');
174 $sites_php = <<<'EOD'
176 $sites['8888.www.example.org'] = 'example';
179 // Create the expected directory structure.
180 vfsStream::create(['sites' => [
181 'sites.php' => $sites_php,
183 'settings.php' => 'test'
187 $request = new Request();
188 $request->server->set('SERVER_NAME', 'www.example.org');
189 $request->server->set('SERVER_PORT', '8888');
190 $request->server->set('SCRIPT_NAME', '/index.php');
191 $this->assertEquals('sites/example', DrupalKernel::findSitePath($request, TRUE, $vfs_root->url('drupal_root')));
192 $this->assertEquals('sites/example', DrupalKernel::findSitePath($request, FALSE, $vfs_root->url('drupal_root')));
198 * A fake autoloader for testing
200 class fakeAutoloader {
203 * Registers this instance as an autoloader.
205 * @param bool $prepend
206 * Whether to prepend the autoloader or not
208 public function register($prepend = FALSE) {
209 spl_autoload_register([$this, 'loadClass'], TRUE, $prepend);
213 * Unregisters this instance as an autoloader.
215 public function unregister() {
216 spl_autoload_unregister([$this, 'loadClass']);
220 * Loads the given class or interface.
223 * This class never loads.
225 public function loadClass() {
230 * Finds a file by class name while caching lookups to APC.
233 * This class never finds.
235 public function findFile() {
244 if (!function_exists('drupal_valid_test_ua')) {
245 function drupal_valid_test_ua($new_prefix = NULL) {