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