]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Helper/RedirectTest.php
Use namespaced PHPUnit classes
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / RedirectTest.php
CommitLineData
af497a64
NL
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle\Helper;
af497a64 4
bd91bd5c 5use PHPUnit\Framework\TestCase;
f808b016
JB
6use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
7use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
00bf45b6 8use Wallabag\CoreBundle\Entity\Config;
af497a64 9use Wallabag\CoreBundle\Helper\Redirect;
f808b016 10use Wallabag\UserBundle\Entity\User;
af497a64 11
bd91bd5c 12class RedirectTest extends TestCase
af497a64 13{
4086e078 14 /** @var \PHPUnit_Framework_MockObject_MockObject */
af497a64
NL
15 private $routerMock;
16
17 /** @var Redirect */
18 private $redirect;
19
20 public function setUp()
21 {
00bf45b6
JB
22 $this->routerMock = $this->getMockBuilder('Symfony\Component\Routing\Router')
23 ->disableOriginalConstructor()
24 ->getMock();
25
26 $this->routerMock->expects($this->any())
27 ->method('generate')
28 ->with('homepage')
29 ->willReturn('homepage');
30
31 $user = new User();
32 $user->setName('youpi');
33 $user->setEmail('youpi@youpi.org');
34 $user->setUsername('youpi');
35 $user->setPlainPassword('youpi');
36 $user->setEnabled(true);
37 $user->addRole('ROLE_SUPER_ADMIN');
38
39 $config = new Config($user);
40 $config->setTheme('material');
41 $config->setItemsPerPage(30);
42 $config->setReadingSpeed(1);
43 $config->setLanguage('en');
44 $config->setPocketConsumerKey('xxxxx');
45 $config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE);
46
47 $user->setConfig($config);
48
49 $this->token = new UsernamePasswordToken($user, 'password', 'key');
50 $tokenStorage = new TokenStorage();
51 $tokenStorage->setToken($this->token);
52
a42f38d9 53 $this->redirect = new Redirect($this->routerMock, $tokenStorage);
af497a64
NL
54 }
55
56 public function testRedirectToNullWithFallback()
57 {
58 $redirectUrl = $this->redirect->to(null, 'fallback');
59
f808b016 60 $this->assertSame('fallback', $redirectUrl);
af497a64
NL
61 }
62
63 public function testRedirectToNullWithoutFallback()
64 {
65 $redirectUrl = $this->redirect->to(null);
66
f808b016 67 $this->assertSame($this->routerMock->generate('homepage'), $redirectUrl);
af497a64
NL
68 }
69
70 public function testRedirectToValidUrl()
71 {
72 $redirectUrl = $this->redirect->to('/unread/list');
73
f808b016 74 $this->assertSame('/unread/list', $redirectUrl);
af497a64
NL
75 }
76
00bf45b6 77 public function testWithNotLoggedUser()
54fd55fd 78 {
00bf45b6
JB
79 $redirect = new Redirect($this->routerMock, new TokenStorage());
80 $redirectUrl = $redirect->to('/unread/list');
54fd55fd 81
f808b016 82 $this->assertSame('/unread/list', $redirectUrl);
54fd55fd
NL
83 }
84
00bf45b6 85 public function testUserForRedirectToHomepage()
54fd55fd 86 {
00bf45b6 87 $this->token->getUser()->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
54fd55fd 88
00bf45b6 89 $redirectUrl = $this->redirect->to('/unread/list');
54fd55fd 90
f808b016 91 $this->assertSame($this->routerMock->generate('homepage'), $redirectUrl);
54fd55fd 92 }
5dbf3f23
KD
93
94 public function testUserForRedirectWithIgnoreActionMarkAsRead()
95 {
96 $this->token->getUser()->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
97
98 $redirectUrl = $this->redirect->to('/unread/list', '', true);
99
f808b016 100 $this->assertSame('/unread/list', $redirectUrl);
5dbf3f23
KD
101 }
102
103 public function testUserForRedirectNullWithFallbackWithIgnoreActionMarkAsRead()
104 {
105 $this->token->getUser()->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
106
107 $redirectUrl = $this->redirect->to(null, 'fallback', true);
108
f808b016 109 $this->assertSame('fallback', $redirectUrl);
5dbf3f23 110 }
af497a64 111}