]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Ensure language is valid 3216/head
authorJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 13 Oct 2018 07:24:39 +0000 (09:24 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 13 Oct 2018 07:39:00 +0000 (09:39 +0200)
- Do not override locale if user has choosen a locale from the login screen.
- Add some tests about locale url

src/Wallabag/CoreBundle/Controller/ConfigController.php
src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php
src/Wallabag/UserBundle/Resources/views/layout.html.twig
tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php
tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php

index 99576fbb93f1fb5277c9ce36077bf55684dd42ba..be6feb7cdd21b441229e72a26813f85e37b69c53 100644 (file)
@@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
 use Symfony\Component\Routing\Annotation\Route;
+use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint;
 use Wallabag\CoreBundle\Entity\Config;
 use Wallabag\CoreBundle\Entity\TaggingRule;
 use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
@@ -341,11 +342,13 @@ class ConfigController extends Controller
      */
     public function setLocaleAction(Request $request, $language = null)
     {
-        if (null !== $language) {
-            $this->get('session')->set('_locale', $language);
+        $errors = $this->get('validator')->validate($language, (new LocaleConstraint()));
+
+        if (0 === \count($errors)) {
+            $request->getSession()->set('_locale', $language);
         }
 
-        return $this->redirect($request->headers->get('referer'));
+        return $this->redirect($request->headers->get('referer', $this->generateUrl('homepage')));
     }
 
     /**
index 367cdfb00b46be2f49c928d2c94a8ce49ee8a01c..dc1db5c7a009d03cbb78b54f577a4514e8bb008d 100644 (file)
@@ -6,8 +6,10 @@ use Symfony\Component\HttpFoundation\Session\Session;
 use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
 
 /**
- * Stores the locale of the user in the session after the
- * login. This can be used by the LocaleListener afterwards.
+ * Stores the locale of the user in the session after the login.
+ * If no locale are defined (if user doesn't change it from the login screen), override it with the user's config one.
+ *
+ * This can be used by the LocaleListener afterwards.
  *
  * @see http://symfony.com/doc/master/cookbook/session/locale_sticky_session.html
  */
@@ -30,7 +32,7 @@ class UserLocaleListener
     {
         $user = $event->getAuthenticationToken()->getUser();
 
-        if (null !== $user->getConfig()->getLanguage()) {
+        if (null !== $user->getConfig()->getLanguage() && null === $this->session->get('_locale')) {
             $this->session->set('_locale', $user->getConfig()->getLanguage());
         }
     }
index 6934c6868dbed25b441ccef10e229c319886b41a..b53f87465d59946a7edc910ad574365ccd1a3b2d 100644 (file)
@@ -16,8 +16,8 @@
             {% endblock fos_user_content %}
         </div>
         <div class="center">
-            <a href="{{ path('changeLocale', {'language': 'de'}) }}">Deutsch</a>
-            <a href="{{ path('changeLocale', {'language': 'en'}) }}">English</a>
+            <a href="{{ path('changeLocale', {'language': 'de'}) }}">Deutsch</a> –
+            <a href="{{ path('changeLocale', {'language': 'en'}) }}">English</a> –
             <a href="{{ path('changeLocale', {'language': 'fr'}) }}">Français</a>
         </div>
     </div>
index d709f4ebd2439596acfe7ea461204317737f56e9..cf9f1e97dc2e64203a0e2335a3e0c5fe70c4a451 100644 (file)
@@ -965,4 +965,39 @@ class ConfigControllerTest extends WallabagCoreTestCase
 
         $client->request('GET', '/config/view-mode');
     }
+
+    public function testChangeLocaleWithoutReferer()
+    {
+        $client = $this->getClient();
+
+        $client->request('GET', '/locale/de');
+        $client->followRedirect();
+
+        $this->assertSame('de', $client->getRequest()->getLocale());
+        $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
+    }
+
+    public function testChangeLocaleWithReferer()
+    {
+        $client = $this->getClient();
+
+        $client->request('GET', '/login');
+        $client->request('GET', '/locale/de');
+        $client->followRedirect();
+
+        $this->assertSame('de', $client->getRequest()->getLocale());
+        $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
+    }
+
+    public function testChangeLocaleToBadLocale()
+    {
+        $client = $this->getClient();
+
+        $client->request('GET', '/login');
+        $client->request('GET', '/locale/yuyuyuyu');
+        $client->followRedirect();
+
+        $this->assertNotSame('yuyuyuyu', $client->getRequest()->getLocale());
+        $this->assertNotSame('yuyuyuyu', $client->getContainer()->get('session')->get('_locale'));
+    }
 }
index 93edfde8c8421977dfa018ef4aae29ac96967f57..ff0a9602b73b8c9fe5c2630042df96b0600b4930 100644 (file)
@@ -56,4 +56,27 @@ class UserLocaleListenerTest extends TestCase
 
         $this->assertNull($session->get('_locale'));
     }
+
+    public function testWithLanguageFromSession()
+    {
+        $session = new Session(new MockArraySessionStorage());
+        $listener = new UserLocaleListener($session);
+        $session->set('_locale', 'de');
+
+        $user = new User();
+        $user->setEnabled(true);
+
+        $config = new Config($user);
+        $config->setLanguage('fr');
+
+        $user->setConfig($config);
+
+        $userToken = new UsernamePasswordToken($user, '', 'test');
+        $request = Request::create('/');
+        $event = new InteractiveLoginEvent($request, $userToken);
+
+        $listener->onInteractiveLogin($event);
+
+        $this->assertSame('de', $session->get('_locale'));
+    }
 }
index 2b540fdfeb334c20bb64953a340ec1e2c3db6f24..c13bfbea9b6ae01a929f73282907c23aa024809d 100644 (file)
@@ -8,6 +8,8 @@ use PHPUnit\Framework\TestCase;
 use Symfony\Component\EventDispatcher\EventDispatcher;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\Session\Session;
+use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
 use Wallabag\CoreBundle\Entity\Config;
 use Wallabag\UserBundle\Entity\User;
 use Wallabag\UserBundle\EventListener\CreateConfigListener;
@@ -22,6 +24,7 @@ class CreateConfigListenerTest extends TestCase
 
     protected function setUp()
     {
+        $session = new Session(new MockArraySessionStorage());
         $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
             ->disableOriginalConstructor()
             ->getMock();
@@ -34,7 +37,8 @@ class CreateConfigListenerTest extends TestCase
             'fr',
             1,
             1,
-            1
+            1,
+            $session
         );
 
         $this->dispatcher = new EventDispatcher();