From 39a3c14f59a8da8038674942ee7e02b3142ce279 Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Fri, 4 Aug 2017 22:54:09 +0200 Subject: [PATCH] Allow to configure the HTTP response code for redirects --- Controller/ImagineController.php | 23 +++++--- DependencyInjection/Configuration.php | 1 + DependencyInjection/LiipImagineExtension.php | 1 + Resources/config/imagine.xml | 1 + Tests/Controller/ImagineControllerTest.php | 55 ++++++++++++++++++- .../LiipImagineExtensionTest.php | 1 + .../Controller/ImagineControllerTest.php | 10 ++-- Tests/Functional/app/config/config.yml | 4 +- 8 files changed, 82 insertions(+), 14 deletions(-) diff --git a/Controller/ImagineController.php b/Controller/ImagineController.php index 6fc81fe06..8cbb6251d 100644 --- a/Controller/ImagineController.php +++ b/Controller/ImagineController.php @@ -52,23 +52,32 @@ class ImagineController protected $logger; /** - * @param DataManager $dataManager - * @param FilterManager $filterManager - * @param CacheManager $cacheManager - * @param SignerInterface $signer + * @var int + */ + protected $redirectResponseCode; + + /** + * @param DataManager $dataManager + * @param FilterManager $filterManager + * @param CacheManager $cacheManager + * @param SignerInterface $signer + * @param LoggerInterface|null $logger + * @param int $redirectResponseCode */ public function __construct( DataManager $dataManager, FilterManager $filterManager, CacheManager $cacheManager, SignerInterface $signer, - LoggerInterface $logger = null + LoggerInterface $logger = null, + $redirectResponseCode = 301 ) { $this->dataManager = $dataManager; $this->filterManager = $filterManager; $this->cacheManager = $cacheManager; $this->signer = $signer; $this->logger = $logger; + $this->redirectResponseCode = $redirectResponseCode; } /** @@ -109,7 +118,7 @@ public function filterAction(Request $request, $path, $filter) ); } - return new RedirectResponse($this->cacheManager->resolve($path, $filter, $resolver), 301); + return new RedirectResponse($this->cacheManager->resolve($path, $filter, $resolver), $this->redirectResponseCode); } catch (NonExistingFilterException $e) { $message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $path, $e->getMessage()); @@ -177,7 +186,7 @@ public function filterRuntimeAction(Request $request, $hash, $path, $filter) $resolver ); - return new RedirectResponse($this->cacheManager->resolve($rcPath, $filter, $resolver), 301); + return new RedirectResponse($this->cacheManager->resolve($rcPath, $filter, $resolver), $this->redirectResponseCode); } catch (NonExistingFilterException $e) { $message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $hash.'/'.$path, $e->getMessage()); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index e5577dda4..e9e27f9b3 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -124,6 +124,7 @@ public function getConfigTreeBuilder() ->children() ->scalarNode('filter_action')->defaultValue('liip_imagine.controller:filterAction')->end() ->scalarNode('filter_runtime_action')->defaultValue('liip_imagine.controller:filterRuntimeAction')->end() + ->scalarNode('redirect_response_code')->defaultValue(301)->end() ->end() ->end() ->arrayNode('filter_sets') diff --git a/DependencyInjection/LiipImagineExtension.php b/DependencyInjection/LiipImagineExtension.php index 7f406c253..0e3e22f08 100644 --- a/DependencyInjection/LiipImagineExtension.php +++ b/DependencyInjection/LiipImagineExtension.php @@ -95,6 +95,7 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('liip_imagine.controller.filter_action', $config['controller']['filter_action']); $container->setParameter('liip_imagine.controller.filter_runtime_action', $config['controller']['filter_runtime_action']); + $container->setParameter('liip_imagine.controller.redirect_response_code', $config['controller']['redirect_response_code']); $resources = $container->hasParameter('twig.form.resources') ? $container->getParameter('twig.form.resources') : array(); $resources[] = 'LiipImagineBundle:Form:form_div_layout.html.twig'; diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index 060dc6bb3..ad0ab774e 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -134,6 +134,7 @@ + %liip_imagine.controller.redirect_response_code% diff --git a/Tests/Controller/ImagineControllerTest.php b/Tests/Controller/ImagineControllerTest.php index d93da8486..3057bcb24 100644 --- a/Tests/Controller/ImagineControllerTest.php +++ b/Tests/Controller/ImagineControllerTest.php @@ -13,6 +13,7 @@ use Liip\ImagineBundle\Controller\ImagineController; use Liip\ImagineBundle\Tests\AbstractTest; +use Symfony\Component\HttpFoundation\Request; /** * @covers \Liip\ImagineBundle\Controller\ImagineController @@ -26,7 +27,59 @@ public function testConstruction() $this->createFilterManagerMock(), $this->createCacheManagerMock(), $this->createSignerInterfaceMock(), - $this->createLoggerInterfaceMock() + $this->createLoggerInterfaceMock(), + 301 ); } + + public function testRedirectCodeIsConfigurable() + { + $redirectResponseCode = 307; + $path = '/foo'; + $filter = 'filter'; + $binary = $this->createObjectMock('\Liip\ImagineBundle\Model\Binary'); + $hash = 'hash'; + + $dataManager = $this->createDataManagerMock(); + $dataManager + ->method('find') + ->with($filter, $path) + ->willReturn($binary); + + $filterManager = $this->createFilterManagerMock(); + $filterManager + ->method('applyFilter') + ->with($binary, $filter) + ->willReturn($binary); + + $cacheManager = $this->createCacheManagerMock(); + $cacheManager + ->method('resolve') + ->willReturn($path, $filter) + ->willReturn('/target'); + + $signer = $this->createSignerInterfaceMock(); + $signer + ->expects($this->once()) + ->method('check') + ->with($hash, $path, array()) + ->willReturn(true); + + $controller = new ImagineController( + $dataManager, + $filterManager, + $cacheManager, + $signer, + $this->createLoggerInterfaceMock(), + $redirectResponseCode + ); + + $response = $controller->filterAction(new Request(), $path, $filter); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); + $this->assertSame($redirectResponseCode, $response->getStatusCode()); + + $response = $controller->filterRuntimeAction(new Request(), $hash, $path, $filter); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); + $this->assertSame($redirectResponseCode, $response->getStatusCode()); + } } diff --git a/Tests/DependencyInjection/LiipImagineExtensionTest.php b/Tests/DependencyInjection/LiipImagineExtensionTest.php index 5b8492ffc..9d7034d1c 100644 --- a/Tests/DependencyInjection/LiipImagineExtensionTest.php +++ b/Tests/DependencyInjection/LiipImagineExtensionTest.php @@ -57,6 +57,7 @@ public function testLoadWithDefaults() new Reference('liip_imagine.cache.manager'), new Reference('liip_imagine.cache.signer'), new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE), + '%liip_imagine.controller.redirect_response_code%' ) ); } diff --git a/Tests/Functional/Controller/ImagineControllerTest.php b/Tests/Functional/Controller/ImagineControllerTest.php index 85d1d9c4e..14301a439 100644 --- a/Tests/Functional/Controller/ImagineControllerTest.php +++ b/Tests/Functional/Controller/ImagineControllerTest.php @@ -37,7 +37,7 @@ public function testShouldResolvePopulatingCacheFirst() $response = $this->client->getResponse(); $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); - $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals(302, $response->getStatusCode()); $this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $response->getTargetUrl()); $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); @@ -55,7 +55,7 @@ public function testShouldResolveFromCache() $response = $this->client->getResponse(); $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); - $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals(302, $response->getStatusCode()); $this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $response->getTargetUrl()); $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); @@ -131,7 +131,7 @@ public function testShouldResolveWithCustomFiltersPopulatingCacheFirst() $response = $this->client->getResponse(); $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); - $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals(302, $response->getStatusCode()); $this->assertEquals('http://localhost/media/cache/'.$expectedCachePath, $response->getTargetUrl()); $this->assertFileExists($this->cacheRoot.'/'.$expectedCachePath); @@ -166,7 +166,7 @@ public function testShouldResolveWithCustomFiltersFromCache() $response = $this->client->getResponse(); $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); - $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals(302, $response->getStatusCode()); $this->assertEquals('http://localhost/media/cache'.'/'.$expectedCachePath, $response->getTargetUrl()); $this->assertFileExists($this->cacheRoot.'/'.$expectedCachePath); @@ -186,7 +186,7 @@ public function testShouldResolvePathWithSpecialCharactersAndWhiteSpaces() $response = $this->client->getResponse(); $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); - $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals(302, $response->getStatusCode()); $this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/foo bar.jpeg', $response->getTargetUrl()); $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/foo bar.jpeg'); diff --git a/Tests/Functional/app/config/config.yml b/Tests/Functional/app/config/config.yml index 2c7ebbfb7..41b99282a 100644 --- a/Tests/Functional/app/config/config.yml +++ b/Tests/Functional/app/config/config.yml @@ -84,4 +84,6 @@ liip_imagine: filters: thumbnail: { size: [223, 223], mode: inset } -... + + controller: + redirect_response_code: 302