diff --git a/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php b/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php index b646dd5..8708e36 100644 --- a/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php +++ b/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php @@ -157,10 +157,23 @@ public function getListeners($event_name = NULL) { * {@inheritdoc} */ public function getListenerPriority($eventName, $listener) { - if (isset($this->listeners[$eventName])) { - foreach ($this->listeners[$eventName] as $priority => $listeners) { - if (($key = array_search($listener, $listeners, TRUE)) !== FALSE) { - return $priority; + if (!isset($this->listeners[$eventName])) { + return; + } + foreach ($this->listeners[$eventName] as $priority => $listeners) { + if (FALSE !== ($key = array_search(['callable' => $listener], $listeners, TRUE))) { + return $priority; + } + } + // Resolve service definitions if the listener has not been found so far. + foreach ($this->listeners[$eventName] as $priority => &$definitions) { + foreach ($definitions as $key => &$definition) { + + if (!isset($definition['callable'])) { + $definition['callable'] = [$this->container->get($definition['service'][0]), $definition['service'][1]]; + if ($definition['callable'] === $listener) { + return $priority; + } } } } diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index 78b913d..1c8068b 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -1078,9 +1078,9 @@ protected function getServicesToPersist(ContainerInterface $container) { */ protected function persistServices(ContainerInterface $container, array $persist) { foreach ($persist as $id => $object) { - // Do not override services already set() on the new container and never - // set the service container itself. - if (!$container->initialized($id) && $id !== 'service_container') { + // Do not override services already set() on the new container, for + // example 'service_container'. + if (!$container->initialized($id)) { $container->set($id, $object); } } @@ -1227,7 +1227,8 @@ protected function compileContainer() { // be automatically reinstantiated. Also include services tagged to persist. $persist_ids = array(); foreach ($container->getDefinitions() as $id => $definition) { - if ($definition->isSynthetic() || $definition->getTag('persist')) { + // It does not make sense to persist the container itself, exclude it. + if ($id !== 'service_container' && ($definition->isSynthetic() || $definition->getTag('persist'))) { $persist_ids[] = $id; } } diff --git a/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php b/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php index 8271c59..c2290bc 100644 --- a/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php +++ b/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php @@ -5,6 +5,7 @@ use Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraint; use Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator; use Drupal\Tests\UnitTestCase; +use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * Tests the LinkAccessConstraintValidator validator. @@ -29,7 +30,7 @@ class LinkAccessConstraintValidatorTest extends UnitTestCase { * @dataProvider providerValidate */ public function testValidate($value, $user, $valid) { - $context = $this->getMock('Symfony\Component\Validator\Context\ExecutionContextInterface'); + $context = $this->getMock(ExecutionContextInterface::class); if ($valid) { $context->expects($this->never()) diff --git a/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidatorTest.php b/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidatorTest.php index a326b57..0de416f 100644 --- a/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidatorTest.php +++ b/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidatorTest.php @@ -7,6 +7,7 @@ use Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraint; use Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator; use Drupal\Tests\UnitTestCase; +use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * @coversDefaultClass \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator @@ -19,7 +20,7 @@ class LinkExternalProtocolsConstraintValidatorTest extends UnitTestCase { * @dataProvider providerValidate */ public function testValidate($value, $valid) { - $context = $this->getMock('Symfony\Component\Validator\Context\ExecutionContextInterface'); + $context = $this->getMock(ExecutionContextInterface::class); if ($valid) { $context->expects($this->never()) @@ -77,7 +78,7 @@ public function testValidateWithMalformedUri() { ->method('getUrl') ->willThrowException(new \InvalidArgumentException()); - $context = $this->getMock('Symfony\Component\Validator\Context\ExecutionContextInterface'); + $context = $this->getMock(ExecutionContextInterface::class); $context->expects($this->never()) ->method('addViolation'); @@ -97,7 +98,7 @@ public function testValidateIgnoresInternalUrls() { ->method('getUrl') ->willReturn(Url::fromRoute('example.test')); - $context = $this->getMock('Symfony\Component\Validator\Context\ExecutionContextInterface'); + $context = $this->getMock(ExecutionContextInterface::class); $context->expects($this->never()) ->method('addViolation'); diff --git a/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkNotExistingInternalConstraintValidatorTest.php b/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkNotExistingInternalConstraintValidatorTest.php index 2f0f7b5..41ecd4a 100644 --- a/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkNotExistingInternalConstraintValidatorTest.php +++ b/core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkNotExistingInternalConstraintValidatorTest.php @@ -7,6 +7,7 @@ use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator; use Drupal\Tests\UnitTestCase; use Symfony\Component\Routing\Exception\RouteNotFoundException; +use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * @coversDefaultClass \Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator @@ -19,7 +20,7 @@ class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase { * @dataProvider providerValidate */ public function testValidate($value, $valid) { - $context = $this->getMock('Symfony\Component\Validator\Context\ExecutionContextInterface'); + $context = $this->getMock(ExecutionContextInterface::class); if ($valid) { $context->expects($this->never()) @@ -94,7 +95,7 @@ public function testValidateWithMalformedUri() { ->method('getUrl') ->willThrowException(new \InvalidArgumentException()); - $context = $this->getMock('Symfony\Component\Validator\Context\ExecutionContextInterface'); + $context = $this->getMock(ExecutionContextInterface::class); $context->expects($this->never()) ->method('addViolation'); diff --git a/core/modules/system/src/Tests/DrupalKernel/DrupalKernelTest.php b/core/modules/system/src/Tests/DrupalKernel/DrupalKernelTest.php index c412d99..754b587 100644 --- a/core/modules/system/src/Tests/DrupalKernel/DrupalKernelTest.php +++ b/core/modules/system/src/Tests/DrupalKernel/DrupalKernelTest.php @@ -146,6 +146,11 @@ public function testCompileDIC() { 'pathname' => drupal_get_filename('module', 'service_provider_test'), 'filename' => NULL, )); + + // Check that the container itself is not among the presist IDs because it + // does not make sense to persist the container itself. + $persist_ids = $container->getParameter('persist_ids'); + $this->assertFalse(array_search('service_container', $persist_ids)); } /** diff --git a/core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidatorTest.php b/core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidatorTest.php index 0370461..69edf3b 100644 --- a/core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidatorTest.php +++ b/core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidatorTest.php @@ -5,6 +5,7 @@ use Drupal\Tests\UnitTestCase; use Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraint; use Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator; +use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * @coversDefaultClass \Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator @@ -47,7 +48,7 @@ public function testValidate($items, $expected_violation, $name = FALSE) { // If a violation is expected, then the context's addViolation method will // be called, otherwise it should not be called. - $context = $this->getMock('Symfony\Component\Validator\Context\ExecutionContextInterface'); + $context = $this->getMock(ExecutionContextInterface::class); if ($expected_violation) { $context->expects($this->once()) diff --git a/core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php b/core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php index 48f7455..ea0685a 100644 --- a/core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php +++ b/core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php @@ -173,10 +173,24 @@ public function testRemoveService() $this->assertTrue($otherService->preFooInvoked); } - public function testGetListenerPriority() + public function testGetListenerPriorityWithServices() { - // Override the parent test as our implementation doesn't define - // getListenerPriority(). + $container = new ContainerBuilder(); + $container->register('listener_service', TestEventListener::class); + + $listeners = array( + 'test_event' => array( + 5 => array( + array('service' => array('listener_service', 'preFoo')), + ), + ), + ); + + $dispatcher = new ContainerAwareEventDispatcher($container, $listeners); + $listenerService = $container->get('listener_service'); + $actualPriority = $dispatcher->getListenerPriority('test_event', [$listenerService, 'preFoo']); + + $this->assertSame(5, $actualPriority); } } diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/CustomPageExceptionHtmlSubscriberTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/CustomPageExceptionHtmlSubscriberTest.php index 5d3993d..81e7cc6 100644 --- a/core/tests/Drupal/Tests/Core/EventSubscriber/CustomPageExceptionHtmlSubscriberTest.php +++ b/core/tests/Drupal/Tests/Core/EventSubscriber/CustomPageExceptionHtmlSubscriberTest.php @@ -14,6 +14,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\Routing\RequestContext; /** * @coversDefaultClass \Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber @@ -127,6 +128,12 @@ protected function tearDown() { public function testHandleWithPostRequest() { $request = Request::create('/test', 'POST', array('name' => 'druplicon', 'pass' => '12345')); + $request_context = new RequestContext(); + $request_context->fromRequest($request); + $this->accessUnawareRouter->expects($this->any()) + ->method('getContext') + ->willReturn($request_context); + $this->kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) { return new HtmlResponse($request->getMethod()); })); @@ -148,6 +155,12 @@ public function testHandleWithGetRequest() { $request = Request::create('/test', 'GET', array('name' => 'druplicon', 'pass' => '12345')); $request->attributes->set(AccessAwareRouterInterface::ACCESS_RESULT, AccessResult::forbidden()->addCacheTags(['druplicon'])); + $request_context = new RequestContext(); + $request_context->fromRequest($request); + $this->accessUnawareRouter->expects($this->any()) + ->method('getContext') + ->willReturn($request_context); + $this->kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) { return new Response($request->getMethod() . ' ' . UrlHelper::buildQuery($request->query->all())); }));