Commit 7a95a78e authored by webchick's avatar webchick

Issue #1727538 by katbailey, chx, alexpott: WebTest tests should have access to the correct DIC.

parent 6cdb69e1
......@@ -2632,7 +2632,7 @@ function language($type, $reset = FALSE) {
// Reset the language manager's cache and our own.
if ($reset) {
if (drupal_container()->has('language_manager')) {
if (drupal_container()->isScopeActive('request')) {
drupal_container()->get('language_manager')->reset($type);
}
if (!isset($type)) {
......
......@@ -370,7 +370,7 @@ function current_path() {
// @todo Remove the check for whether the request service exists and the
// fallback code below, once the path alias logic has been figured out in
// http://drupal.org/node/1269742.
if (drupal_container()->has('request')) {
if (drupal_container()->isScopeActive('request')) {
return drupal_container()->get('request')->attributes->get('system_path');
}
// If we are outside the request scope, fall back to using the path stored in
......
......@@ -7,6 +7,7 @@
namespace Drupal\simpletest;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\ConnectionNotDefinedException;
use PDO;
......@@ -143,6 +144,11 @@ abstract class WebTestBase extends TestBase {
*/
protected $redirect_count;
/**
* The kernel used in this test.
*/
protected $kernel;
/**
* Constructor for Drupal\simpletest\WebTestBase.
*/
......@@ -659,6 +665,18 @@ protected function setUp() {
module_enable(array($this->profile), FALSE);
}
// Create a new DrupalKernel for testing purposes, now that all required
// modules have been enabled. This also stores a new dependency injection
// container in drupal_container(). Drupal\simpletest\TestBase::tearDown()
// restores the original container.
// @see Drupal\Core\DrupalKernel::initializeContainer()
$this->kernel = new DrupalKernel('testing', FALSE);
// Booting the kernel is necessary to initialize the new DIC. While
// normally the kernel gets booted on demand in
// Symfony\Component\HttpKernel\handle(), this kernel needs manual booting
// as it is not used to handle a request.
$this->kernel->boot();
// Reset/rebuild all data structures after enabling the modules.
$this->resetAll();
......@@ -769,6 +787,10 @@ protected function tearDown() {
if (!$this->setupDatabasePrefix) {
return FALSE;
}
// Destroy the testing kernel.
if (isset($this->kernel)) {
$this->kernel->shutdown();
}
// Remove all prefixed tables.
$connection_info = Database::getConnectionInfo('default');
$tables = db_find_tables($connection_info['default']['prefix']['default'] . '%');
......
......@@ -33,12 +33,7 @@ public static function getInfo() {
* Test that services provided by module bundles get registered to the DIC.
*/
function testBundleRegistration() {
// The page callback at /bundle_test checks
// drupal_container()->has('bundle_test_class')
// and if this returns TRUE it outputs a message to this effect. We just
// need to check that the message appears on the page.
$this->drupalGet('bundle_test');
$this->assertText(t('The service with id bundle_test_class is available in the DIC'), t('The bundle_test_class service has been registered to the DIC'));
$this->assertTrue(drupal_container()->has('bundle_test_class'), t('The bundle_test_class service has been registered to the DIC'));
// The event subscriber method in the test class calls drupal_set_message with
// a message saying it has fired. This will fire on every page request so it
// should show up on the front page.
......
......@@ -2205,7 +2205,7 @@ function system_add_module_assets() {
* Implements hook_custom_theme().
*/
function system_custom_theme() {
if (drupal_container()->has('request')) {
if (drupal_container()->isScopeActive('request')) {
$request = drupal_container()->get('request');
$path = $request->attributes->get('system_path');
if (user_access('view the administration theme') && path_is_admin($path)) {
......
<?php
/**
* Implements hook_menu().
*/
function bundle_test_menu() {
$items['bundle_test'] = array(
'type' => MENU_CALLBACK,
'title' => t('Bundle test callback'),
'page callback' => 'bundle_test_callback',
'access callback' => TRUE,
);
return $items;
}
/**
* Simple callback for testing that the bundle_test_class service exists in the
* DIC.
*/
function bundle_test_callback() {
if (drupal_container()->has('bundle_test_class')) {
return t('The service with id bundle_test_class is available in the DIC');
}
return t('Service not found');
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment