]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Redirect to homepage if referer is null
authorNicolas Lœuillet <nicolas@loeuillet.org>
Fri, 15 Apr 2016 05:58:01 +0000 (07:58 +0200)
committerNicolas Lœuillet <nicolas@loeuillet.org>
Fri, 15 Apr 2016 05:58:01 +0000 (07:58 +0200)
Fix #1924

src/Wallabag/CoreBundle/Controller/EntryController.php
src/Wallabag/CoreBundle/Controller/TagController.php
src/Wallabag/CoreBundle/Helper/Redirect.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Resources/config/services.yml
src/Wallabag/CoreBundle/Tests/Helper/RedirectTest.php [new file with mode: 0644]

index 17b72bd1c56e632c8d5c4bc7bfdfd4a88f0064c0..9443ae821d63fef7e1d61a832d07c064d9e5d73c 100644 (file)
@@ -344,7 +344,9 @@ class EntryController extends Controller
             $message
         );
 
-        return $this->redirect($request->headers->get('referer'));
+        $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
+
+        return $this->redirect($redirectUrl);
     }
 
     /**
@@ -374,7 +376,9 @@ class EntryController extends Controller
             $message
         );
 
-        return $this->redirect($request->headers->get('referer'));
+        $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
+
+        return $this->redirect($redirectUrl);
     }
 
     /**
@@ -408,7 +412,11 @@ class EntryController extends Controller
         );
 
         // don't redirect user to the deleted entry
-        return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage'));
+        $to = ($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage'));
+
+        $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
+
+        return $this->redirect($redirectUrl);
     }
 
     /**
index e8e9ecbee0893f2620e3191ff9ea562e67b93a85..16d14d7974059aa9fa9d9e2966315cad7f3845ee 100644 (file)
@@ -65,7 +65,9 @@ class TagController extends Controller
         }
         $em->flush();
 
-        return $this->redirect($request->headers->get('referer'));
+        $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
+
+        return $this->redirect($redirectUrl);
     }
 
     /**
diff --git a/src/Wallabag/CoreBundle/Helper/Redirect.php b/src/Wallabag/CoreBundle/Helper/Redirect.php
new file mode 100644 (file)
index 0000000..0921c3f
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+namespace Wallabag\CoreBundle\Helper;
+
+use Symfony\Component\Routing\Router;
+
+class Redirect
+{
+    private $router;
+
+    public function __construct(Router $router)
+    {
+        $this->router = $router;
+    }
+
+    /**
+     * @param string $url      URL to redirect
+     * @param string $fallback Fallback URL if $url is null
+     *
+     * @return string
+     */
+    public function to($url, $fallback = '')
+    {
+        $returnUrl = $url;
+
+        if (null === $url) {
+            if ('' !== $fallback) {
+                $returnUrl = $fallback;
+            } else {
+                $returnUrl = $this->router->generate('homepage');
+            }
+        }
+
+        return $returnUrl;
+    }
+}
index 6dc1f1d7f5808d2345dacc6731bb4441ec46e80a..f8835198caff7d55ffa12a9a5bf1bf64aef0be8f 100644 (file)
@@ -114,3 +114,8 @@ services:
         class: Wallabag\CoreBundle\Operator\Doctrine\Matches
         tags:
             - { name: rulerz.operator, executor: rulerz.executor.doctrine, operator: matches, inline: true }
+
+    wallabag_core.helper.redirect:
+        class: Wallabag\CoreBundle\Helper\Redirect
+        arguments:
+            - "@router"
diff --git a/src/Wallabag/CoreBundle/Tests/Helper/RedirectTest.php b/src/Wallabag/CoreBundle/Tests/Helper/RedirectTest.php
new file mode 100644 (file)
index 0000000..da19cf5
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+namespace Wallabag\CoreBundle\Tests\Helper;
+
+use Wallabag\CoreBundle\Helper\Redirect;
+
+class RedirectTest extends \PHPUnit_Framework_TestCase
+{
+    /** @var \Symfony\Component\Routing\Router */
+    private $routerMock;
+
+    /** @var Redirect */
+    private $redirect;
+
+    public function setUp()
+    {
+        $this->routerMock = $this->getRouterMock();
+        $this->redirect = new Redirect($this->routerMock);
+    }
+
+    public function testRedirectToNullWithFallback()
+    {
+        $redirectUrl = $this->redirect->to(null, 'fallback');
+
+        $this->assertEquals('fallback', $redirectUrl);
+    }
+
+    public function testRedirectToNullWithoutFallback()
+    {
+        $redirectUrl = $this->redirect->to(null);
+
+        $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl);
+    }
+
+    public function testRedirectToValidUrl()
+    {
+        $redirectUrl = $this->redirect->to('/unread/list');
+
+        $this->assertEquals('/unread/list', $redirectUrl);
+    }
+
+    private function getRouterMock()
+    {
+        return $this->getMockBuilder('Symfony\Component\Routing\Router')
+            ->setMethods(['generate'])
+            ->disableOriginalConstructor()
+            ->getMock();
+    }
+}