Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EZP-26963: IO Url prefix not working with a host #1907

Merged
merged 2 commits into from Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 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,7 +83,7 @@ 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;
}
Expand Down
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