View source
<?php
namespace Drupal\Tests\Core\DrupalKernel;
use Drupal\Core\DrupalKernel;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
use Symfony\Component\HttpFoundation\Request;
class DrupalKernelTest extends UnitTestCase {
public function testTrustedHosts($host, $server_name, $message, $expected = FALSE) {
$request = new Request();
$trusted_host_patterns = [
'^example\\.com$',
'^.+\\.example\\.com$',
'^example\\.org',
'^.+\\.example\\.org',
];
if (!empty($host)) {
$request->headers
->set('HOST', $host);
}
$request->server
->set('SERVER_NAME', $server_name);
$method = new \ReflectionMethod('Drupal\\Core\\DrupalKernel', 'setupTrustedHosts');
$method
->setAccessible(TRUE);
$valid_host = $method
->invoke(NULL, $request, $trusted_host_patterns);
$this
->assertSame($expected, $valid_host, $message);
$method
->invoke(NULL, $request, []);
Request::setFactory(NULL);
}
public function providerTestTrustedHosts() {
$data = [];
$data[] = [
'www.example.com',
'www.example.com',
'canonical URL is trusted',
TRUE,
];
$data[] = [
NULL,
'www.example.com',
'empty Host is valid',
TRUE,
];
$data[] = [
'example.com',
'www.example.com',
'host from settings is trusted',
TRUE,
];
$data[] = [
'subdomain.example.com',
'www.example.com',
'host from settings is trusted',
TRUE,
];
$data[] = [
'www.example.org',
'www.example.com',
'host from settings is trusted',
TRUE,
];
$data[] = [
'example.org',
'www.example.com',
'host from settings is trusted',
TRUE,
];
$data[] = [
'www.blackhat.com',
'www.example.com',
'unspecified host is untrusted',
FALSE,
];
return $data;
}
public function testFindSitePath() {
$vfs_root = vfsStream::setup('drupal_root');
$sites_php = <<<'EOD'
<?php
$sites['8888.www.example.org'] = 'example';
EOD;
vfsStream::create([
'sites' => [
'sites.php' => $sites_php,
'example' => [
'settings.php' => 'test',
],
],
]);
$request = new Request();
$request->server
->set('SERVER_NAME', 'www.example.org');
$request->server
->set('SERVER_PORT', '8888');
$request->server
->set('SCRIPT_NAME', '/index.php');
$this
->assertEquals('sites/example', DrupalKernel::findSitePath($request, TRUE, $vfs_root
->url('drupal_root')));
$this
->assertEquals('sites/example', DrupalKernel::findSitePath($request, FALSE, $vfs_root
->url('drupal_root')));
}
}
class FakeAutoloader {
public function register($prepend = FALSE) {
spl_autoload_register([
$this,
'loadClass',
], TRUE, $prepend);
}
public function unregister() {
spl_autoload_unregister([
$this,
'loadClass',
]);
}
public function loadClass() {
return NULL;
}
public function findFile() {
return NULL;
}
}