Issue #2252967 by sun: RebuildContainer() is not limited to web tests (WebTestBase).

8.0.x
Nathaniel Catchpole 2014-04-30 11:02:21 +01:00
parent b73b86b8aa
commit bd248aa5a9
3 changed files with 48 additions and 56 deletions

View File

@ -107,11 +107,10 @@ class FieldImportDeleteUninstallTest extends FieldUnitTestBase {
$steps = $this->configImporter()->initialize();
$this->assertIdentical($steps[0], array('\Drupal\field\ConfigImporterFieldPurger', 'process'), 'The additional process configuration synchronization step has been added.');
$this->configImporter()->import();
// This will purge all the data, delete the field and uninstall the
// Telephone module.
$this->rebuildContainer();
$this->configImporter()->import();
$this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone'));
$this->assertFalse(entity_load_by_uuid('field_config', $field_uuid), 'The test field has been deleted by the configuration synchronization');
$deleted_fields = \Drupal::state()->get('field.field.deleted') ?: array();
@ -169,11 +168,10 @@ class FieldImportDeleteUninstallTest extends FieldUnitTestBase {
$steps = $this->configImporter()->initialize();
$this->assertIdentical($steps[0], array('\Drupal\field\ConfigImporterFieldPurger', 'process'), 'The additional process configuration synchronization step has been added.');
$this->configImporter()->import();
// This will purge all the data, delete the field and uninstall the
// Telephone module.
$this->rebuildContainer();
$this->configImporter()->import();
$this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone'));
$deleted_fields = \Drupal::state()->get('field.field.deleted') ?: array();
$this->assertFalse(isset($deleted_fields[$field_uuid]), 'Field has been completed removed from the system.');

View File

@ -1123,54 +1123,6 @@ abstract class TestBase {
drupal_set_time_limit($this->timeLimit);
}
/**
* Rebuild \Drupal::getContainer().
*
* Use this to build a new kernel and service container. For example, when the
* list of enabled modules is changed via the internal browser, in which case
* the test process still contains an old kernel and service container with an
* old module list.
*
* @see TestBase::prepareEnvironment()
* @see TestBase::restoreEnvironment()
*
* @todo Fix http://drupal.org/node/1708692 so that module enable/disable
* changes are immediately reflected in \Drupal::getContainer(). Until then,
* tests can invoke this workaround when requiring services from newly
* enabled modules to be immediately available in the same request.
*/
protected function rebuildContainer($environment = 'testing') {
// Preserve the request object after the container rebuild.
$request = \Drupal::request();
// When called from InstallerTestBase, the current container is the minimal
// container from TestBase::prepareEnvironment(), which does not contain a
// request stack.
if (\Drupal::getContainer()->initialized('request_stack')) {
$request_stack = \Drupal::service('request_stack');
}
$this->kernel = new DrupalKernel($environment, drupal_classloader(), FALSE);
$this->kernel->boot();
// DrupalKernel replaces the container in \Drupal::getContainer() with a
// different object, so we need to replace the instance on this test class.
$this->container = \Drupal::getContainer();
// The current user is set in TestBase::prepareEnvironment().
$this->container->set('request', $request);
if (isset($request_stack)) {
$this->container->set('request_stack', $request_stack);
}
else {
$this->container->get('request_stack')->push($request);
}
$this->container->get('current_user')->setAccount(\Drupal::currentUser());
// The request context is normally set by the router_listener from within
// its KernelEvents::REQUEST listener. In the simpletest parent site this
// event is not fired, therefore it is necessary to updated the request
// context manually here.
$this->container->get('router.request_context')->fromRequest($request);
}
/**
* Performs cleanup tasks after each individual test method has been run.
*/

View File

@ -1019,10 +1019,52 @@ abstract class WebTestBase extends TestBase {
}
/**
* Overrides \Drupal\simpletest\TestBase::rebuildContainer().
* Rebuilds \Drupal::getContainer().
*
* Use this to build a new kernel and service container. For example, when the
* list of enabled modules is changed via the internal browser, in which case
* the test process still contains an old kernel and service container with an
* old module list.
*
* @see TestBase::prepareEnvironment()
* @see TestBase::restoreEnvironment()
*
* @todo Fix http://drupal.org/node/1708692 so that module enable/disable
* changes are immediately reflected in \Drupal::getContainer(). Until then,
* tests can invoke this workaround when requiring services from newly
* enabled modules to be immediately available in the same request.
*/
protected function rebuildContainer($environment = 'prod') {
parent::rebuildContainer($environment);
// Preserve the request object after the container rebuild.
$request = \Drupal::request();
// When called from InstallerTestBase, the current container is the minimal
// container from TestBase::prepareEnvironment(), which does not contain a
// request stack.
if (\Drupal::getContainer()->initialized('request_stack')) {
$request_stack = \Drupal::service('request_stack');
}
$this->kernel = new DrupalKernel($environment, drupal_classloader(), FALSE);
$this->kernel->boot();
// DrupalKernel replaces the container in \Drupal::getContainer() with a
// different object, so we need to replace the instance on this test class.
$this->container = \Drupal::getContainer();
// The current user is set in TestBase::prepareEnvironment().
$this->container->set('request', $request);
if (isset($request_stack)) {
$this->container->set('request_stack', $request_stack);
}
else {
$this->container->get('request_stack')->push($request);
}
$this->container->get('current_user')->setAccount(\Drupal::currentUser());
// The request context is normally set by the router_listener from within
// its KernelEvents::REQUEST listener. In the simpletest parent site this
// event is not fired, therefore it is necessary to updated the request
// context manually here.
$this->container->get('router.request_context')->fromRequest($request);
// Make sure the url generator has a request object, otherwise calls to
// $this->drupalGet() will fail.
$this->prepareRequestForGenerator();