From b191e0d1ade05c6221ee910f9605dad6ef17d6cc Mon Sep 17 00:00:00 2001 From: Cees Geene Date: Thu, 21 Jan 2016 10:52:18 +0100 Subject: [PATCH 1/5] Update RedirectRequestSubscriber.php --- src/EventSubscriber/RedirectRequestSubscriber.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/EventSubscriber/RedirectRequestSubscriber.php b/src/EventSubscriber/RedirectRequestSubscriber.php index fbeb863..1aa20e9 100644 --- a/src/EventSubscriber/RedirectRequestSubscriber.php +++ b/src/EventSubscriber/RedirectRequestSubscriber.php @@ -119,7 +119,8 @@ public function __construct(RedirectRepository $redirect_repository, LanguageMan * The event to process. */ public function onKernelRequestCheckRedirect(GetResponseEvent $event) { - $request = $event->getRequest(); + // Get a clone of the request as it might be altered during inbound processing. + $request = clone $event->getRequest(); if (!$this->checker->canRedirect($request)) { return; From 5dbe5f13c1bfc07bbeb47a499f2e14ee724342ab Mon Sep 17 00:00:00 2001 From: Cees Geene Date: Thu, 21 Jan 2016 11:00:31 +0100 Subject: [PATCH 2/5] Update RedirectRequestSubscriber.php --- src/EventSubscriber/RedirectRequestSubscriber.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/EventSubscriber/RedirectRequestSubscriber.php b/src/EventSubscriber/RedirectRequestSubscriber.php index 1aa20e9..cacd951 100644 --- a/src/EventSubscriber/RedirectRequestSubscriber.php +++ b/src/EventSubscriber/RedirectRequestSubscriber.php @@ -119,7 +119,12 @@ public function __construct(RedirectRepository $redirect_repository, LanguageMan * The event to process. */ public function onKernelRequestCheckRedirect(GetResponseEvent $event) { - // Get a clone of the request as it might be altered during inbound processing. + // Get a clone of the request. During inbound processing the request + // can be altered. Allowing this here can lead to unexpected behavior. + // For example the path_processor.files inbound processor provided by + // the system module alters both the path and the request; only the + // changes to the request will be propagated, while the change to the + // path will be lost. $request = clone $event->getRequest(); if (!$this->checker->canRedirect($request)) { From a9cad9ab11d46e839644b1fe6b71a8c2b62a4412 Mon Sep 17 00:00:00 2001 From: Cees Geene Date: Thu, 21 Jan 2016 11:28:39 +0100 Subject: [PATCH 3/5] Allow request mock to be cloned --- tests/src/Unit/RedirectRequestSubscriberTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/src/Unit/RedirectRequestSubscriberTest.php b/tests/src/Unit/RedirectRequestSubscriberTest.php index 3a72e7a..1db5dc2 100644 --- a/tests/src/Unit/RedirectRequestSubscriberTest.php +++ b/tests/src/Unit/RedirectRequestSubscriberTest.php @@ -273,7 +273,12 @@ protected function getGetResponseEventStub($path_info, $query_string) { ->with('GET') ->will($this->returnValue(TRUE)); + $request->query = new ParameterBag(); $request->attributes = new ParameterBag(); + $request->cookies = new ParameterBag(); + $request->files = new FileBag(); + $request->server = new ServerBag(); + $request->headers = new HeaderBag(); $http_kernel = $this->getMockBuilder('\Symfony\Component\HttpKernel\HttpKernelInterface') ->getMock(); From 9e65ad8cb2a0b858fb8832df36add9f88114dbd0 Mon Sep 17 00:00:00 2001 From: Cees Geene Date: Thu, 21 Jan 2016 11:44:47 +0100 Subject: [PATCH 4/5] Missing imports --- tests/src/Unit/RedirectRequestSubscriberTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/src/Unit/RedirectRequestSubscriberTest.php b/tests/src/Unit/RedirectRequestSubscriberTest.php index 1db5dc2..a0e3aeb 100644 --- a/tests/src/Unit/RedirectRequestSubscriberTest.php +++ b/tests/src/Unit/RedirectRequestSubscriberTest.php @@ -11,9 +11,12 @@ use Drupal\redirect\EventSubscriber\RedirectRequestSubscriber; use Drupal\Tests\UnitTestCase; use PHPUnit_Framework_MockObject_MockObject; +use Symfony\Component\HttpFoundation\FileBag; +use Symfony\Component\HttpFoundation\HeaderBag; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\ServerBag; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\PostResponseEvent; From 0328ba7323f626d55d5a35c45ff1b246208f20c5 Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher Date: Mon, 1 Feb 2016 21:51:18 +0100 Subject: [PATCH 5/5] Simplify unit test by using a real request object --- .../Unit/RedirectRequestSubscriberTest.php | 30 ++----------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/tests/src/Unit/RedirectRequestSubscriberTest.php b/tests/src/Unit/RedirectRequestSubscriberTest.php index a0e3aeb..d9b886a 100644 --- a/tests/src/Unit/RedirectRequestSubscriberTest.php +++ b/tests/src/Unit/RedirectRequestSubscriberTest.php @@ -11,12 +11,9 @@ use Drupal\redirect\EventSubscriber\RedirectRequestSubscriber; use Drupal\Tests\UnitTestCase; use PHPUnit_Framework_MockObject_MockObject; -use Symfony\Component\HttpFoundation\FileBag; -use Symfony\Component\HttpFoundation\HeaderBag; -use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpFoundation\ServerBag; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\PostResponseEvent; @@ -258,30 +255,7 @@ protected function getPostResponseEvent($headers = array()) { * @return GetResponseEvent */ protected function getGetResponseEventStub($path_info, $query_string) { - - $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request') - ->disableOriginalConstructor() - ->getMock(); - $request->expects($this->any()) - ->method('getQueryString') - ->will($this->returnValue($query_string)); - $request->expects($this->any()) - ->method('getPathInfo') - ->will($this->returnValue($path_info)); - $request->expects($this->any()) - ->method('getScriptName') - ->will($this->returnValue('index.php')); - $request->expects($this->any()) - ->method('isMethod') - ->with('GET') - ->will($this->returnValue(TRUE)); - - $request->query = new ParameterBag(); - $request->attributes = new ParameterBag(); - $request->cookies = new ParameterBag(); - $request->files = new FileBag(); - $request->server = new ServerBag(); - $request->headers = new HeaderBag(); + $request = Request::create($path_info . '?' . $query_string, 'GET', [], [], [], ['SCRIPT_NAME' => 'index.php']); $http_kernel = $this->getMockBuilder('\Symfony\Component\HttpKernel\HttpKernelInterface') ->getMock();