From 44898fe980c5239c2927cd0f9251d616c2723950 Mon Sep 17 00:00:00 2001 From: webchick Date: Thu, 11 Oct 2012 08:42:27 -0700 Subject: [PATCH] Issue #1786990 by effulgentsia, tim.plunkett, andypost, sun: Fixed [Module]Bundle is registered too late in WebTestBase::setUp(). --- .../lib/Drupal/simpletest/TestBase.php | 30 +++++++++++++++++++ .../lib/Drupal/simpletest/WebTestBase.php | 18 ++--------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index c61015bd30c..b31d8a0a408 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -9,6 +9,7 @@ namespace Drupal\simpletest; use Drupal\Core\Database\Database; use Drupal\Core\Database\ConnectionNotDefinedException; +use Drupal\Core\DrupalKernel; use ReflectionMethod; use ReflectionObject; use Exception; @@ -828,6 +829,35 @@ abstract class TestBase { $this->setupEnvironment = TRUE; } + /** + * Rebuild drupal_container(). + * + * @todo Fix http://drupal.org/node/1708692 so that module enable/disable + * changes are immediately reflected in drupal_container(). 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() { + // DrupalKernel expects to merge a fresh bootstrap container, not remerge + // what is left over from a prior container build. + drupal_container(NULL, TRUE); + // 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(); + // The DrupalKernel does not update the container in drupal_container(), but + // replaces it with a new object. We therefore need to replace the minimal + // boostrap container that has been set up by TestBase::prepareEnvironment(). + $this->container = drupal_container(); + } + /** * Deletes created files, database tables, and reverts all environment changes. * diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index 24bf1f251dc..400d8851ea6 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -684,6 +684,7 @@ abstract class WebTestBase extends TestBase { // Execute the non-interactive installer. require_once DRUPAL_ROOT . '/core/includes/install.core.inc'; install_drupal($settings); + $this->rebuildContainer(); // Restore the original Simpletest batch. $batch = &batch_get(); @@ -716,24 +717,9 @@ abstract class WebTestBase extends TestBase { if ($modules) { $success = module_enable($modules, TRUE); $this->assertTrue($success, t('Enabled modules: %modules', array('%modules' => implode(', ', $modules)))); + $this->rebuildContainer(); } - // 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(); - // The DrupalKernel does not update the container in drupal_container(), but - // replaces it with a new object. We therefore need to replace the minimal - // boostrap container that has been set up by TestBase::prepareEnvironment(). - $this->container = drupal_container(); - // Reset/rebuild all data structures after enabling the modules. $this->resetAll();