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

redirect_fallback #191

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions Controller/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;

/**
* Theme controller.
Expand All @@ -40,18 +41,32 @@ class ThemeController
*/
protected $cookieOptions;

/**
* @var RouterInterface
*/
protected $router;

/**
* @var string|null
*/
protected $defaultRoute;

/**
* Theme controller construct.
*
* @param ActiveTheme $activeTheme active theme instance
* @param array $themes Available themes
* @param array|null $cookieOptions The options of the cookie we look for the theme to set
* @param RouterInterface $router
* @param string|null $defaultRoute
*/
public function __construct(ActiveTheme $activeTheme, array $themes, array $cookieOptions = null)
public function __construct(ActiveTheme $activeTheme, array $themes, array $cookieOptions = null, RouterInterface $router, $defaultRoute = null)
Copy link
Contributor

@maximgubar maximgubar Jun 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lsmith77 it looks like a BC break, shall we announce it somehow, or refactor ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point .. we could actually add a setter for this which is only used if the feature is enabled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or allow the argument to be null and in that case fall back to the old logic

{
$this->activeTheme = $activeTheme;
$this->themes = $themes;
$this->cookieOptions = $cookieOptions;
$this->router = $router;
$this->defaultRoute = $defaultRoute;
}

/**
Expand All @@ -73,7 +88,15 @@ public function switchAction(Request $request)

$this->activeTheme->setName($theme);

$url = $request->headers->get('Referer', '/');

if ($this->defaultRoute) {
$redirect = $this->router->generate($this->defaultRoute);
} else {
$redirect = '/';
}

$url = $request->headers->get('Referer', $redirect);

$response = new RedirectResponse($url);

if (!empty($this->cookieOptions)) {
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function getConfigTreeBuilder()
->prototype('scalar')->end()
->end()
->scalarNode('active_theme')->defaultNull()->end()
->scalarNode('redirect_fallback')->defaultNull()->end()
->arrayNode('path_patterns')
->addDefaultsIfNotSet()
->children()
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/LiipThemeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration(new Configuration(), $configs);

foreach (array('themes', 'active_theme', 'path_patterns', 'cache_warming') as $key) {
foreach (array('themes', 'active_theme', 'path_patterns', 'cache_warming', 'redirect_fallback') as $key) {
$container->setParameter($this->getAlias().'.'.$key, $config[$key]);
}

Expand Down
2 changes: 2 additions & 0 deletions Resources/config/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<argument type="service" id="liip_theme.active_theme" />
<argument>%liip_theme.themes%</argument>
<argument>%liip_theme.cookie%</argument>
<argument type="service" id="router"/>
<argument>%liip_theme.redirect_fallback%</argument>
</service>
</services>
</container>
68 changes: 33 additions & 35 deletions Tests/UseCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,39 +68,22 @@ protected function getEventMock($request, $response, $type = 'Symfony\Component\
return $event;
}

protected function getMockRequest($theme, $cookieReturnValue = 'cookie', $userAgent = 'autodetect')
/**
* @return \Symfony\Bundle\FrameworkBundle\Routing\Router
*/
protected function getRouterMock()
{
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
->disableOriginalConstructor()
->getMock();
$router = $this->getMockBuilder('Symfony\Component\Routing\Router')
->disableOriginalConstructor()
->setMethods(['generate', 'supports', 'exists'])
->getMock();

$request->expects($this->any())
->method('get')
->will($this->returnValue($theme));
$request->cookies = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')
->disableOriginalConstructor()
->getMock();
$request->cookies->expects($this->any())
->method('get')
->will($this->returnValue($cookieReturnValue));
$request->headers = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')
->disableOriginalConstructor()
->getMock();
$request->headers->expects($this->any())
->method('get')
->will($this->returnValue('/'));
$request->headers->expects($this->any())
->method('get')
->will($this->returnValue($cookieReturnValue));
$router->expects($this->any())
->method('generate')
->with('test_route')
->will($this->returnValue('/test_route'));

$request->server = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')
->disableOriginalConstructor()
->getMock();
$request->server->expects($this->any())
->method('get')
->will($this->returnValue($userAgent));

return $request;
return $router;
}

private function getCookieValueFromResponse($response)
Expand Down Expand Up @@ -128,14 +111,17 @@ public function testThemeAction($config, $assertion, $hasAlreadyACookie = true)
}
$response = new \Symfony\Component\HttpFoundation\Response();
$request = new \Symfony\Component\HttpFoundation\Request();

if ($hasAlreadyACookie) {
$request = $this->getMockRequest('cookie');
$request->query->set('theme', 'cookie');
$request->cookies->set('cookieName', 'cookie');
$request->server->set('HTTP_USER_AGENT', 'autodetect');
}

$router = $this->getRouterMock();

$controller = false;
if ($config['load_controllers']) {
$controller = new ThemeController($activeTheme, $config['themes'], $config['cookie']);
$controller = new ThemeController($activeTheme, $config['themes'], $config['cookie'], $router, $config['redirect_fallback']);
}

$listener = new ThemeRequestListener($activeTheme, $config['cookie'], $device);
Expand All @@ -145,10 +131,13 @@ public function testThemeAction($config, $assertion, $hasAlreadyACookie = true)
$this->assertEquals($activeTheme->getName(), $assertion['themeAfterKernelRequest']);

if ($controller) {
$request->query->set('theme', $assertion['themeAfterController']);

$response = $controller->switchAction(
$this->getMockRequest($assertion['themeAfterController'])
$request
);
$this->assertCookieValue($response, $assertion['themeAfterController']);
$this->assertEquals($response->getTargetUrl(), $assertion['redirect']);
}

// onResponse will not set the cookie if the controller modified it
Expand Down Expand Up @@ -178,6 +167,7 @@ public function dataProvider()
// all-in Controller wins over Cookie and Autodetection
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'test_route',
'active_theme' => 'default',
'autodetect_theme' => true,
'load_controllers' => true,
Expand All @@ -187,13 +177,15 @@ public function dataProvider()
'themeAfterKernelRequest' => 'cookie',
'themeAfterController' => 'controller',
'themeAfterKernelResponse' => 'controller',
'redirect' => '/test_route',
),
true,
),
// autodetect if no cookie exists, but at the end controller wins
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'test_route',
'active_theme' => 'default',
'autodetect_theme' => true,
'load_controllers' => true,
Expand All @@ -203,13 +195,15 @@ public function dataProvider()
'themeAfterKernelRequest' => 'autodetect',
'themeAfterController' => 'controller',
'themeAfterKernelResponse' => 'controller',
'redirect' => '/test_route',
),
false,
),
// no cookie pre-esist, so set autodect value into cookie if the controller is not called
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'test_route',
'active_theme' => 'default',
'autodetect_theme' => true,
'load_controllers' => false,
Expand All @@ -219,13 +213,15 @@ public function dataProvider()
'themeAfterKernelRequest' => 'autodetect',
'themeAfterController' => 'autodetect',
'themeAfterKernelResponse' => 'autodetect',
'redirect' => '/test_route',
),
false,
),
// a cookie don't pre-esist, autodetection is disabled, controller is not called, set the cookie
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'test_route',
'active_theme' => 'default',
'autodetect_theme' => false,
'load_controllers' => true,
Expand All @@ -235,13 +231,15 @@ public function dataProvider()
'themeAfterKernelRequest' => 'default',
'themeAfterController' => 'controller',
'themeAfterKernelResponse' => 'controller',
'redirect' => '/test_route',
),
false,
),
// a cookie pre-esist, autodetection is disabled, controller is not called, set the cookie
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'test_route',
'active_theme' => 'default',
'autodetect_theme' => false,
'load_controllers' => true,
Expand All @@ -251,10 +249,10 @@ public function dataProvider()
'themeAfterKernelRequest' => 'cookie',
'themeAfterController' => 'controller',
'themeAfterKernelResponse' => 'controller',
'redirect' => '/test_route',
),
true,
),

);
}
}