aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/Wallabag/CoreBundle/Controller/EntryController.php14
-rw-r--r--src/Wallabag/CoreBundle/Controller/TagController.php4
-rw-r--r--src/Wallabag/CoreBundle/Helper/Redirect.php37
-rw-r--r--src/Wallabag/CoreBundle/Resources/config/services.yml5
-rw-r--r--src/Wallabag/CoreBundle/Tests/Helper/RedirectTest.php55
5 files changed, 111 insertions, 4 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php
index 17b72bd1..69dfd4b1 100644
--- a/src/Wallabag/CoreBundle/Controller/EntryController.php
+++ b/src/Wallabag/CoreBundle/Controller/EntryController.php
@@ -344,7 +344,9 @@ class EntryController extends Controller
344 $message 344 $message
345 ); 345 );
346 346
347 return $this->redirect($request->headers->get('referer')); 347 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
348
349 return $this->redirect($redirectUrl);
348 } 350 }
349 351
350 /** 352 /**
@@ -374,7 +376,9 @@ class EntryController extends Controller
374 $message 376 $message
375 ); 377 );
376 378
377 return $this->redirect($request->headers->get('referer')); 379 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
380
381 return $this->redirect($redirectUrl);
378 } 382 }
379 383
380 /** 384 /**
@@ -408,7 +412,11 @@ class EntryController extends Controller
408 ); 412 );
409 413
410 // don't redirect user to the deleted entry 414 // don't redirect user to the deleted entry
411 return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage')); 415 $to = ($url !== $request->headers->get('referer') ? $request->headers->get('referer') : null);
416
417 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
418
419 return $this->redirect($redirectUrl);
412 } 420 }
413 421
414 /** 422 /**
diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php
index e8e9ecbe..16d14d79 100644
--- a/src/Wallabag/CoreBundle/Controller/TagController.php
+++ b/src/Wallabag/CoreBundle/Controller/TagController.php
@@ -65,7 +65,9 @@ class TagController extends Controller
65 } 65 }
66 $em->flush(); 66 $em->flush();
67 67
68 return $this->redirect($request->headers->get('referer')); 68 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
69
70 return $this->redirect($redirectUrl);
69 } 71 }
70 72
71 /** 73 /**
diff --git a/src/Wallabag/CoreBundle/Helper/Redirect.php b/src/Wallabag/CoreBundle/Helper/Redirect.php
new file mode 100644
index 00000000..c14c79d1
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Helper/Redirect.php
@@ -0,0 +1,37 @@
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use Symfony\Component\Routing\Router;
6
7/**
8 * Manage redirections to avoid redirecting to empty routes.
9 */
10class Redirect
11{
12 private $router;
13
14 public function __construct(Router $router)
15 {
16 $this->router = $router;
17 }
18
19 /**
20 * @param string $url URL to redirect
21 * @param string $fallback Fallback URL if $url is null
22 *
23 * @return string
24 */
25 public function to($url, $fallback = '')
26 {
27 if (null !== $url) {
28 return $url;
29 }
30
31 if ('' === $fallback) {
32 return $this->router->generate('homepage');
33 }
34
35 return $fallback;
36 }
37}
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml
index 6dc1f1d7..f8835198 100644
--- a/src/Wallabag/CoreBundle/Resources/config/services.yml
+++ b/src/Wallabag/CoreBundle/Resources/config/services.yml
@@ -114,3 +114,8 @@ services:
114 class: Wallabag\CoreBundle\Operator\Doctrine\Matches 114 class: Wallabag\CoreBundle\Operator\Doctrine\Matches
115 tags: 115 tags:
116 - { name: rulerz.operator, executor: rulerz.executor.doctrine, operator: matches, inline: true } 116 - { name: rulerz.operator, executor: rulerz.executor.doctrine, operator: matches, inline: true }
117
118 wallabag_core.helper.redirect:
119 class: Wallabag\CoreBundle\Helper\Redirect
120 arguments:
121 - "@router"
diff --git a/src/Wallabag/CoreBundle/Tests/Helper/RedirectTest.php b/src/Wallabag/CoreBundle/Tests/Helper/RedirectTest.php
new file mode 100644
index 00000000..f4aecc80
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Tests/Helper/RedirectTest.php
@@ -0,0 +1,55 @@
1<?php
2
3namespace Wallabag\CoreBundle\Tests\Helper;
4
5use Wallabag\CoreBundle\Helper\Redirect;
6
7class RedirectTest extends \PHPUnit_Framework_TestCase
8{
9 /** @var \PHPUnit_Framework_MockObject_MockObject */
10 private $routerMock;
11
12 /** @var Redirect */
13 private $redirect;
14
15 public function setUp()
16 {
17 $this->routerMock = $this->getRouterMock();
18 $this->redirect = new Redirect($this->routerMock);
19 }
20
21 public function testRedirectToNullWithFallback()
22 {
23 $redirectUrl = $this->redirect->to(null, 'fallback');
24
25 $this->assertEquals('fallback', $redirectUrl);
26 }
27
28 public function testRedirectToNullWithoutFallback()
29 {
30 $redirectUrl = $this->redirect->to(null);
31
32 $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl);
33 }
34
35 public function testRedirectToValidUrl()
36 {
37 $redirectUrl = $this->redirect->to('/unread/list');
38
39 $this->assertEquals('/unread/list', $redirectUrl);
40 }
41
42 private function getRouterMock()
43 {
44 $mock = $this->getMockBuilder('Symfony\Component\Routing\Router')
45 ->disableOriginalConstructor()
46 ->getMock();
47
48 $mock->expects($this->any())
49 ->method('generate')
50 ->with('homepage')
51 ->willReturn('homepage');
52
53 return $mock;
54 }
55}