From 91c9d4e8df8044b99851713a09f582502deb501b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81ro=CC=82me=20Vieilledent?= Date: Wed, 8 Feb 2017 12:04:48 +0100 Subject: [PATCH] Fix EZP-26963: IO Url prefix not working with a host > https://jira.ez.no/browse/EZP-26963 This patch makes it possible to define an `url_prefix` with a host [as documented](https://doc.ez.no/display/EZP/Binary+files+URL+handling#BinaryfilesURLhandling-Usingastaticserverforimages). --- .../EventListener/StreamFileListener.php | 14 ++-- .../EventListener/StreamFileListenerTest.php | 66 +++++++++++++++++-- 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/eZ/Bundle/EzPublishIOBundle/EventListener/StreamFileListener.php b/eZ/Bundle/EzPublishIOBundle/EventListener/StreamFileListener.php index d2ece17fac4..e23198e0975 100644 --- a/eZ/Bundle/EzPublishIOBundle/EventListener/StreamFileListener.php +++ b/eZ/Bundle/EzPublishIOBundle/EventListener/StreamFileListener.php @@ -51,9 +51,15 @@ public function onKernelRequest(GetResponseEvent $event) return; } - $uri = $event->getRequest()->attributes->get('semanticPathinfo'); + $request = $event->getRequest(); + $urlPrefix = $this->configResolver->getParameter('io.url_prefix'); + if (strpos($urlPrefix, '://') !== false) { + $uri = $request->getSchemeAndHttpHost() . $request->getPathInfo(); + } else { + $uri = $request->attributes->get('semanticPathinfo'); + } - if (!$this->isIoUri($uri)) { + if (!$this->isIoUri($uri, $urlPrefix)) { return; } @@ -77,8 +83,8 @@ public function onKernelRequest(GetResponseEvent $event) * * @return bool */ - private function isIoUri($uri) + private function isIoUri($uri, $urlPrefix) { - return (strpos(ltrim($uri, '/'), $this->configResolver->getParameter('io.url_prefix')) === 0); + return (strpos(ltrim($uri, '/'), $urlPrefix) === 0); } } diff --git a/eZ/Bundle/EzPublishIOBundle/Tests/EventListener/StreamFileListenerTest.php b/eZ/Bundle/EzPublishIOBundle/Tests/EventListener/StreamFileListenerTest.php index 66d0b3c5790..625cb4501c2 100644 --- a/eZ/Bundle/EzPublishIOBundle/Tests/EventListener/StreamFileListenerTest.php +++ b/eZ/Bundle/EzPublishIOBundle/Tests/EventListener/StreamFileListenerTest.php @@ -35,11 +35,6 @@ public function setUp() $this->ioServiceMock = $this->getMock('eZ\Publish\Core\IO\IOServiceInterface'); $this->configResolverMock = $this->getMock('eZ\Publish\Core\MVC\ConfigResolverInterface'); - $this->configResolverMock - ->expects($this->any()) - ->method('getParameter') - ->with('io.url_prefix') - ->will($this->returnValue($this->ioUriPrefix)); $this->eventListener = new StreamFileListener($this->ioServiceMock, $this->configResolverMock); } @@ -49,6 +44,26 @@ public function testDoesNotRespondToNonIoUri() $request = $this->createRequest('/Not-an-image'); $event = $this->createEvent($request); + $this->configureIoUrlPrefix('var/test/storage'); + $this->ioServiceMock + ->expects($this->never()) + ->method('loadBinaryFileByUri'); + + $this->eventListener->onKernelRequest($event); + + self::assertNull($event->getResponse()); + } + + public function testDoesNotRespondToNoIoRequest() + { + $request = $this->createRequest('/Not-an-image', 'bar.fr'); + $event = $this->createEvent($request); + + $this->configureIoUrlPrefix('http://foo.com/var/test/storage'); + $this->ioServiceMock + ->expects($this->never()) + ->method('loadBinaryFileByUri'); + $this->eventListener->onKernelRequest($event); self::assertNull($event->getResponse()); @@ -57,6 +72,7 @@ public function testDoesNotRespondToNonIoUri() public function testRespondsToIoUri() { $uri = '/var/test/storage/images/image.png'; + $this->configureIoUrlPrefix(ltrim($uri, '/')); $request = $this->createRequest($uri); $event = $this->createEvent($request); @@ -78,12 +94,48 @@ public function testRespondsToIoUri() ); } + public function testRespondsToIoRequest() + { + $uri = '/var/test/storage/images/image.png'; + $host = 'phoenix-rises.fm'; + $urlPrefix = "http://$host/var/test/storage"; + $this->configureIoUrlPrefix($urlPrefix); + $request = $this->createRequest($uri, $host); + + $event = $this->createEvent($request); + + $binaryFile = new BinaryFile(array('mtime' => new DateTime())); + + $this->ioServiceMock + ->expects($this->once()) + ->method('loadBinaryFileByUri') + ->with(sprintf('http://%s%s', $host, $uri)) + ->will($this->returnValue($binaryFile)); + + $this->eventListener->onKernelRequest($event); + + self::assertTrue($event->hasResponse()); + self::assertEquals( + new BinaryStreamResponse($binaryFile, $this->ioServiceMock), + $event->getResponse() + ); + } + + private function configureIoUrlPrefix($urlPrefix) + { + $this->configResolverMock + ->expects($this->any()) + ->method('getParameter') + ->with('io.url_prefix') + ->willReturn($urlPrefix); + } + /** * @return Request */ - protected function createRequest($semanticPath) + protected function createRequest($semanticPath, $host = 'localhost') { - $request = new Request(); + $request = Request::create(sprintf('http://%s%s', $host, $semanticPath)); $request->attributes->set('semanticPathinfo', $semanticPath); return $request;