]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Added tests
authorNicolas LÅ“uillet <nicolas@loeuillet.org>
Mon, 7 Nov 2016 09:26:05 +0000 (10:26 +0100)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Wed, 16 Nov 2016 22:07:34 +0000 (23:07 +0100)
src/Wallabag/CoreBundle/Helper/Redirect.php
tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
tests/Wallabag/CoreBundle/Helper/RedirectTest.php

index 59172db40110d628a223a750af6a88fc718ded36..82792aec37eaba2b68b4f82326ee9548be6f429c 100644 (file)
@@ -3,7 +3,7 @@
 namespace Wallabag\CoreBundle\Helper;
 
 use Symfony\Component\Routing\Router;
-use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
+use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
 use Wallabag\CoreBundle\Entity\Config;
 
 /**
@@ -12,12 +12,13 @@ use Wallabag\CoreBundle\Entity\Config;
 class Redirect
 {
     private $router;
+    private $tokenStorage;
     private $actionMarkAsRead;
 
-    public function __construct(Router $router, TokenStorage $token)
+    public function __construct(Router $router, TokenStorageInterface $tokenStorage)
     {
         $this->router = $router;
-        $this->actionMarkAsRead = $token->getToken()->getUser()->getConfig()->getActionMarkAsRead();
+        $this->tokenStorage = $tokenStorage;
     }
 
     /**
@@ -28,7 +29,13 @@ class Redirect
      */
     public function to($url, $fallback = '')
     {
-        if (Config::REDIRECT_TO_HOMEPAGE === $this->actionMarkAsRead) {
+        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
+
+        if (null === $user || !is_object($user)) {
+            return $url;
+        }
+
+        if (Config::REDIRECT_TO_HOMEPAGE === $user->getConfig()->getActionMarkAsRead()) {
             return $this->router->generate('homepage');
         }
 
index 4ab06dbfffb0700b3b17bccba0d2937952fb918f..bf4e0543f87fdf1d6adcf5a9b6c86f91435d335d 100644 (file)
@@ -3,6 +3,7 @@
 namespace Tests\Wallabag\CoreBundle\Controller;
 
 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
+use Wallabag\CoreBundle\Entity\Config;
 use Wallabag\CoreBundle\Entity\Entry;
 
 class EntryControllerTest extends WallabagCoreTestCase
@@ -896,4 +897,68 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
     }
+
+    public function testRedirectToHomepage()
+    {
+        $this->logInAs('empty');
+        $client = $this->getClient();
+
+        $em = $client->getContainer()->get('doctrine.orm.entity_manager');
+        $user = $em
+            ->getRepository('WallabagUserBundle:User')
+            ->find($this->getLoggedInUserId());
+
+        if (!$user) {
+            $this->markTestSkipped('No user found in db.');
+        }
+
+        // Redirect to homepage
+        $config = $user->getConfig();
+        $config->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
+        $em->persist($config);
+        $em->flush();
+
+        $content = $client->getContainer()
+            ->get('doctrine.orm.entity_manager')
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
+
+        $client->request('GET', '/view/'.$content->getId());
+        $client->request('GET', '/archive/'.$content->getId());
+
+        $this->assertEquals(302, $client->getResponse()->getStatusCode());
+        $this->assertEquals('/', $client->getResponse()->headers->get('location'));
+    }
+
+    public function testRedirectToCurrentPage()
+    {
+        $this->logInAs('empty');
+        $client = $this->getClient();
+
+        $em = $client->getContainer()->get('doctrine.orm.entity_manager');
+        $user = $em
+            ->getRepository('WallabagUserBundle:User')
+            ->find($this->getLoggedInUserId());
+
+        if (!$user) {
+            $this->markTestSkipped('No user found in db.');
+        }
+
+        // Redirect to current page
+        $config = $user->getConfig();
+        $config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE);
+        $em->persist($config);
+        $em->flush();
+
+        $content = $client->getContainer()
+            ->get('doctrine.orm.entity_manager')
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
+
+        $client->request('GET', '/view/'.$content->getId());
+        $client->request('GET', '/archive/'.$content->getId());
+
+        $this->assertEquals(302, $client->getResponse()->getStatusCode());
+        $this->assertContains('/view/'.$content->getId(), $client->getResponse()->headers->get('location'));
+    }
 }
index 825e8d53f2cb651cee70bfb1f2c9c2329a6b102b..a2d6a5245e9b20a7d556bf6e6f50bb67441d27d1 100644 (file)
@@ -25,14 +25,14 @@ class RedirectTest extends \PHPUnit_Framework_TestCase
     {
         $redirectUrl = $this->redirect->to(null, 'fallback');
 
-        $this->assertEquals('fallback', $redirectUrl);
+        $this->assertEquals(null, $redirectUrl);
     }
 
     public function testRedirectToNullWithoutFallback()
     {
         $redirectUrl = $this->redirect->to(null);
 
-        $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl);
+        $this->assertEquals(null, $redirectUrl);
     }
 
     public function testRedirectToValidUrl()