aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2018-10-13 09:24:39 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2018-10-13 09:39:00 +0200
commit4d4147b228ac90f329fd2d40dd4fb60cb980328a (patch)
treebb18d67c8ec03bf8abd24dc6ef2138c3f81f6fa5
parentbe417ef23685e17a239b1d192a0e9b9f484f1bfe (diff)
downloadwallabag-4d4147b228ac90f329fd2d40dd4fb60cb980328a.tar.gz
wallabag-4d4147b228ac90f329fd2d40dd4fb60cb980328a.tar.zst
wallabag-4d4147b228ac90f329fd2d40dd4fb60cb980328a.zip
Ensure language is valid
- Do not override locale if user has choosen a locale from the login screen. - Add some tests about locale url
-rw-r--r--src/Wallabag/CoreBundle/Controller/ConfigController.php9
-rw-r--r--src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php8
-rw-r--r--src/Wallabag/UserBundle/Resources/views/layout.html.twig4
-rw-r--r--tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php35
-rw-r--r--tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php23
-rw-r--r--tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php6
6 files changed, 76 insertions, 9 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php
index 99576fbb..be6feb7c 100644
--- a/src/Wallabag/CoreBundle/Controller/ConfigController.php
+++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php
@@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
8use Symfony\Component\HttpFoundation\Request; 8use Symfony\Component\HttpFoundation\Request;
9use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 9use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
10use Symfony\Component\Routing\Annotation\Route; 10use Symfony\Component\Routing\Annotation\Route;
11use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint;
11use Wallabag\CoreBundle\Entity\Config; 12use Wallabag\CoreBundle\Entity\Config;
12use Wallabag\CoreBundle\Entity\TaggingRule; 13use Wallabag\CoreBundle\Entity\TaggingRule;
13use Wallabag\CoreBundle\Form\Type\ChangePasswordType; 14use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
@@ -341,11 +342,13 @@ class ConfigController extends Controller
341 */ 342 */
342 public function setLocaleAction(Request $request, $language = null) 343 public function setLocaleAction(Request $request, $language = null)
343 { 344 {
344 if (null !== $language) { 345 $errors = $this->get('validator')->validate($language, (new LocaleConstraint()));
345 $this->get('session')->set('_locale', $language); 346
347 if (0 === \count($errors)) {
348 $request->getSession()->set('_locale', $language);
346 } 349 }
347 350
348 return $this->redirect($request->headers->get('referer')); 351 return $this->redirect($request->headers->get('referer', $this->generateUrl('homepage')));
349 } 352 }
350 353
351 /** 354 /**
diff --git a/src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php b/src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php
index 367cdfb0..dc1db5c7 100644
--- a/src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php
+++ b/src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php
@@ -6,8 +6,10 @@ use Symfony\Component\HttpFoundation\Session\Session;
6use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; 6use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
7 7
8/** 8/**
9 * Stores the locale of the user in the session after the 9 * Stores the locale of the user in the session after the login.
10 * login. This can be used by the LocaleListener afterwards. 10 * If no locale are defined (if user doesn't change it from the login screen), override it with the user's config one.
11 *
12 * This can be used by the LocaleListener afterwards.
11 * 13 *
12 * @see http://symfony.com/doc/master/cookbook/session/locale_sticky_session.html 14 * @see http://symfony.com/doc/master/cookbook/session/locale_sticky_session.html
13 */ 15 */
@@ -30,7 +32,7 @@ class UserLocaleListener
30 { 32 {
31 $user = $event->getAuthenticationToken()->getUser(); 33 $user = $event->getAuthenticationToken()->getUser();
32 34
33 if (null !== $user->getConfig()->getLanguage()) { 35 if (null !== $user->getConfig()->getLanguage() && null === $this->session->get('_locale')) {
34 $this->session->set('_locale', $user->getConfig()->getLanguage()); 36 $this->session->set('_locale', $user->getConfig()->getLanguage());
35 } 37 }
36 } 38 }
diff --git a/src/Wallabag/UserBundle/Resources/views/layout.html.twig b/src/Wallabag/UserBundle/Resources/views/layout.html.twig
index 6934c686..b53f8746 100644
--- a/src/Wallabag/UserBundle/Resources/views/layout.html.twig
+++ b/src/Wallabag/UserBundle/Resources/views/layout.html.twig
@@ -16,8 +16,8 @@
16 {% endblock fos_user_content %} 16 {% endblock fos_user_content %}
17 </div> 17 </div>
18 <div class="center"> 18 <div class="center">
19 <a href="{{ path('changeLocale', {'language': 'de'}) }}">Deutsch</a> 19 <a href="{{ path('changeLocale', {'language': 'de'}) }}">Deutsch</a>
20 <a href="{{ path('changeLocale', {'language': 'en'}) }}">English</a> 20 <a href="{{ path('changeLocale', {'language': 'en'}) }}">English</a>
21 <a href="{{ path('changeLocale', {'language': 'fr'}) }}">Français</a> 21 <a href="{{ path('changeLocale', {'language': 'fr'}) }}">Français</a>
22 </div> 22 </div>
23 </div> 23 </div>
diff --git a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
index d709f4eb..cf9f1e97 100644
--- a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
@@ -965,4 +965,39 @@ class ConfigControllerTest extends WallabagCoreTestCase
965 965
966 $client->request('GET', '/config/view-mode'); 966 $client->request('GET', '/config/view-mode');
967 } 967 }
968
969 public function testChangeLocaleWithoutReferer()
970 {
971 $client = $this->getClient();
972
973 $client->request('GET', '/locale/de');
974 $client->followRedirect();
975
976 $this->assertSame('de', $client->getRequest()->getLocale());
977 $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
978 }
979
980 public function testChangeLocaleWithReferer()
981 {
982 $client = $this->getClient();
983
984 $client->request('GET', '/login');
985 $client->request('GET', '/locale/de');
986 $client->followRedirect();
987
988 $this->assertSame('de', $client->getRequest()->getLocale());
989 $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
990 }
991
992 public function testChangeLocaleToBadLocale()
993 {
994 $client = $this->getClient();
995
996 $client->request('GET', '/login');
997 $client->request('GET', '/locale/yuyuyuyu');
998 $client->followRedirect();
999
1000 $this->assertNotSame('yuyuyuyu', $client->getRequest()->getLocale());
1001 $this->assertNotSame('yuyuyuyu', $client->getContainer()->get('session')->get('_locale'));
1002 }
968} 1003}
diff --git a/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php b/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php
index 93edfde8..ff0a9602 100644
--- a/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php
+++ b/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php
@@ -56,4 +56,27 @@ class UserLocaleListenerTest extends TestCase
56 56
57 $this->assertNull($session->get('_locale')); 57 $this->assertNull($session->get('_locale'));
58 } 58 }
59
60 public function testWithLanguageFromSession()
61 {
62 $session = new Session(new MockArraySessionStorage());
63 $listener = new UserLocaleListener($session);
64 $session->set('_locale', 'de');
65
66 $user = new User();
67 $user->setEnabled(true);
68
69 $config = new Config($user);
70 $config->setLanguage('fr');
71
72 $user->setConfig($config);
73
74 $userToken = new UsernamePasswordToken($user, '', 'test');
75 $request = Request::create('/');
76 $event = new InteractiveLoginEvent($request, $userToken);
77
78 $listener->onInteractiveLogin($event);
79
80 $this->assertSame('de', $session->get('_locale'));
81 }
59} 82}
diff --git a/tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php b/tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php
index 2b540fdf..c13bfbea 100644
--- a/tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php
+++ b/tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php
@@ -8,6 +8,8 @@ use PHPUnit\Framework\TestCase;
8use Symfony\Component\EventDispatcher\EventDispatcher; 8use Symfony\Component\EventDispatcher\EventDispatcher;
9use Symfony\Component\HttpFoundation\Request; 9use Symfony\Component\HttpFoundation\Request;
10use Symfony\Component\HttpFoundation\Response; 10use Symfony\Component\HttpFoundation\Response;
11use Symfony\Component\HttpFoundation\Session\Session;
12use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
11use Wallabag\CoreBundle\Entity\Config; 13use Wallabag\CoreBundle\Entity\Config;
12use Wallabag\UserBundle\Entity\User; 14use Wallabag\UserBundle\Entity\User;
13use Wallabag\UserBundle\EventListener\CreateConfigListener; 15use Wallabag\UserBundle\EventListener\CreateConfigListener;
@@ -22,6 +24,7 @@ class CreateConfigListenerTest extends TestCase
22 24
23 protected function setUp() 25 protected function setUp()
24 { 26 {
27 $session = new Session(new MockArraySessionStorage());
25 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') 28 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
26 ->disableOriginalConstructor() 29 ->disableOriginalConstructor()
27 ->getMock(); 30 ->getMock();
@@ -34,7 +37,8 @@ class CreateConfigListenerTest extends TestCase
34 'fr', 37 'fr',
35 1, 38 1,
36 1, 39 1,
37 1 40 1,
41 $session
38 ); 42 );
39 43
40 $this->dispatcher = new EventDispatcher(); 44 $this->dispatcher = new EventDispatcher();