diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index 0a991e2..81e7cfc 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -719,17 +719,7 @@ public function updateModules(array $module_list, array $module_filenames = arra * {@inheritdoc} */ public function getServiceIdMapping() { - if (isset($this->container)) { - foreach ($this->container->getServiceIds() as $service_id) { - if ($this->container->initialized($service_id)) { - $service = $this->container->get($service_id); - // @TODO remove after https://www.drupal.org/node/2536012 - if (is_object($service)) { - $this->serviceIdMapping[spl_object_hash($service)] = $service_id; - } - } - } - } + $this->collectServiceIdMapping(); return $this->serviceIdMapping; } @@ -778,8 +768,8 @@ protected function initializeContainer() { if ($this->container->initialized('current_user')) { $current_user_id = $this->container->get('current_user')->id(); } - // Save the current hashes. - $this->getServiceIdMapping(); + // Save the current services. + $this->collectServiceIdMapping(); // If there is a session, close and save it. if ($this->container->initialized('session')) { @@ -1473,4 +1463,22 @@ protected function addServiceFiles($service_yamls) { } return FALSE; } + + /** + * Collect a mapping between service to ids. + */ + protected function collectServiceIdMapping() { + if (isset($this->container)) { + foreach ($this->container->getServiceIds() as $service_id) { + if ($this->container->initialized($service_id)) { + $service = $this->container->get($service_id); + // @TODO remove after https://www.drupal.org/node/2536012 + if (is_object($service)) { + $this->serviceIdMapping[spl_object_hash($service)] = $service_id; + } + } + } + } + } + } diff --git a/core/lib/Drupal/Core/Test/TestKernel.php b/core/lib/Drupal/Core/Test/TestKernel.php index 55294ca..c6bab31 100644 --- a/core/lib/Drupal/Core/Test/TestKernel.php +++ b/core/lib/Drupal/Core/Test/TestKernel.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Test; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DrupalKernel; /** @@ -30,4 +31,23 @@ public function __construct($environment, $class_loader, $allow_dumping = TRUE) parent::__construct($environment, $class_loader, $allow_dumping); } + /** + * Sets a container with a kernel service on the Drupal class. + * + * @return \Drupal\Core\DependencyInjection\IntrospectableContainerInterface + * A container with the kernel service set. + */ + public static function setContainerWithKernel() { + $container = new ContainerBuilder(); + $kernel = new DrupalKernel('test', NULL); + // Objects of the same type will have access to each others private and + // protected members even though they are not the same instances. This is + // because the implementation specific details are already known when + // inside those objects. + $kernel->container = $container; + $container->set('kernel', $kernel); + \Drupal::setContainer($container); + return $container; + } + } diff --git a/core/modules/views_ui/tests/src/Unit/ViewUIObjectTest.php b/core/modules/views_ui/tests/src/Unit/ViewUIObjectTest.php index cb26b58..c1d90c2 100644 --- a/core/modules/views_ui/tests/src/Unit/ViewUIObjectTest.php +++ b/core/modules/views_ui/tests/src/Unit/ViewUIObjectTest.php @@ -8,11 +8,10 @@ namespace Drupal\Tests\views_ui\Unit; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Test\TestKernel; use Drupal\Tests\UnitTestCase; use Drupal\views\Entity\View; -use Drupal\views\ViewExecutable; use Drupal\views_ui\ViewUI; -use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerBuilder; /** @@ -121,8 +120,7 @@ public function testIsLocked() { */ public function testSerialization() { // Set a container so the DependencySerializationTrait has it. - $container = new ContainerBuilder(); - \Drupal::setContainer($container); + TestKernel::setContainerWithKernel(); $storage = new View([], 'view'); $executable = $this->getMockBuilder('Drupal\views\ViewExecutable') diff --git a/core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php b/core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php index bd7fe01..aa783f1 100644 --- a/core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php +++ b/core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php @@ -9,6 +9,7 @@ use Drupal\Core\DependencyInjection\Container; use Drupal\Core\DependencyInjection\DependencySerializationTrait; +use Drupal\Core\Test\TestKernel; use Drupal\Tests\UnitTestCase; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -26,10 +27,9 @@ class DependencySerializationTest extends UnitTestCase { public function testSerialization() { // Create a pseudo service and dependency injected object. $service = new \stdClass(); - $container = new Container(); + $container = TestKernel::setContainerWithKernel(); $container->set('test_service', $service); $container->set('service_container', $container); - \Drupal::setContainer($container); $dependencySerialization = new DependencySerializationTestDummy($service); $dependencySerialization->setContainer($container); diff --git a/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php b/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php index e2bd250..622a883 100644 --- a/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php +++ b/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php @@ -9,6 +9,7 @@ use Drupal\Core\DependencyInjection\Container; use Drupal\Core\DrupalKernel; + use Drupal\Core\Test\TestKernel; use Drupal\Tests\Core\DependencyInjection\Fixture\BarClass; use Drupal\Tests\UnitTestCase; use org\bovigo\vfs\vfsStream; @@ -145,14 +146,10 @@ public function testFindSitePath() { * @covers ::getServiceIdMapping */ public function testGetServiceIdMapping() { - $container = new Container(); $service = new BarClass(); + $container = TestKernel::setContainerWithKernel(); $container->set('bar', $service); - $kernel = new DrupalKernel('test', NULL); - $r = new \ReflectionProperty('Drupal\Core\DrupalKernel', 'container'); - $r->setAccessible(TRUE); - $r->setValue($kernel, $container); - $this->assertEquals($kernel->getServiceIdMapping()[spl_object_hash($service)], 'bar'); + $this->assertEquals($container->get('kernel')->getServiceIdMapping()[spl_object_hash($service)], 'bar'); } }