Skip to content

Commit

Permalink
Fix EZP-26963: IO Url prefix not working with a host
Browse files Browse the repository at this point in the history
  • Loading branch information
lolautruche committed Mar 13, 2017
1 parent 4775441 commit 91c9d4e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 11 deletions.
14 changes: 10 additions & 4 deletions eZ/Bundle/EzPublishIOBundle/EventListener/StreamFileListener.php
Expand Up @@ -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;
}

Expand All @@ -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);
}
}
Expand Up @@ -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);
}
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 91c9d4e

Please sign in to comment.