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