View source
<?php
namespace Drupal\system\Tests\DrupalKernel;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Drupal\simpletest\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
class DrupalKernelTest extends KernelTestBase {
protected function setUp() {
$this
->prepareConfigDirectories();
$this
->settingsSet('php_storage', array(
'service_container' => array(
'bin' => 'service_container',
'class' => 'Drupal\\Component\\PhpStorage\\MTimeProtectedFileStorage',
'directory' => DRUPAL_ROOT . '/' . $this->publicFilesDirectory . '/php',
'secret' => Settings::getHashSalt(),
),
));
}
protected function prepareConfigDirectories() {
\Drupal::setContainer($this->originalContainer);
parent::prepareConfigDirectories();
\Drupal::unsetContainer();
}
protected function getTestKernel(Request $request, array $modules_enabled = NULL) {
$class_loader = (require DRUPAL_ROOT . '/autoload.php');
$kernel = DrupalKernel::createFromRequest($request, $class_loader, 'testing');
$this
->settingsSet('container_yamls', []);
$this
->settingsSet('hash_salt', $this->databasePrefix);
if (isset($modules_enabled)) {
$kernel
->updateModules($modules_enabled);
}
$kernel
->boot();
return $kernel;
}
public function testCompileDIC() {
$modules_enabled = array(
'system' => 'system',
'user' => 'user',
);
$request = Request::createFromGlobals();
$this
->getTestKernel($request, $modules_enabled);
$kernel = $this
->getTestKernel($request);
$container = $kernel
->getContainer();
$refClass = new \ReflectionClass($container);
$is_compiled_container = !$refClass
->isSubclassOf('Symfony\\Component\\DependencyInjection\\ContainerBuilder');
$this
->assertTrue($is_compiled_container);
$module_list = array_keys($container
->get('module_handler')
->getModuleList());
$this
->assertEqual(array_values($modules_enabled), $module_list);
$container = $this
->getTestKernel($request, NULL)
->getContainer();
$refClass = new \ReflectionClass($container);
$is_compiled_container = !$refClass
->isSubclassOf('Symfony\\Component\\DependencyInjection\\ContainerBuilder');
$this
->assertTrue($is_compiled_container);
$module_list = array_keys($container
->get('module_handler')
->getModuleList());
$this
->assertEqual(array_values($modules_enabled), $module_list);
$class_loader = $container
->get('class_loader');
$refClass = new \ReflectionClass($class_loader);
$this
->assertTrue($refClass
->hasMethod('loadClass'), 'Container has a class loader');
$this
->assertFalse($container
->has('service_provider_test_class'));
$modules_enabled['service_provider_test'] = 'service_provider_test';
$this
->getTestKernel($request, $modules_enabled);
$kernel = $this
->getTestKernel($request, $modules_enabled);
$container = $kernel
->getContainer();
$refClass = new \ReflectionClass($container);
$is_container_builder = $refClass
->isSubclassOf('Symfony\\Component\\DependencyInjection\\ContainerBuilder');
$this
->assertFalse($is_container_builder, 'Container is not a builder');
$this
->assertTrue($container
->has('service_provider_test_class'), 'Container has test service');
$class_loader = $container
->get('class_loader');
$refClass = new \ReflectionClass($class_loader);
$this
->assertTrue($refClass
->hasMethod('loadClass'), 'Container has a class loader');
$modules = $container
->getParameter('container.modules');
$this
->assertEqual($modules['service_provider_test'], array(
'type' => 'module',
'pathname' => drupal_get_filename('module', 'service_provider_test'),
'filename' => NULL,
));
}
public function testRepeatedBootWithDifferentEnvironment() {
$request = Request::createFromGlobals();
$class_loader = (require DRUPAL_ROOT . '/autoload.php');
$environments = [
'testing1',
'testing1',
'testing2',
'testing2',
];
foreach ($environments as $environment) {
$kernel = DrupalKernel::createFromRequest($request, $class_loader, $environment);
$this
->settingsSet('container_yamls', []);
$this
->settingsSet('hash_salt', $this->databasePrefix);
$kernel
->boot();
}
$this
->pass('Repeatedly loaded compiled DIC with different environment');
}
public function testPreventChangeOfSitePath() {
$modules_enabled = array(
'system' => 'system',
'user' => 'user',
);
$request = Request::createFromGlobals();
$kernel = $this
->getTestKernel($request, $modules_enabled);
$pass = FALSE;
try {
$kernel
->setSitePath('/dev/null');
} catch (\LogicException $e) {
$pass = TRUE;
}
$this
->assertTrue($pass, 'Throws LogicException if DrupalKernel::setSitePath() is called after boot');
}
}