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