]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use PHPUnit\Framework\TestCase; | |
6 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | |
7 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | |
8 | use Wallabag\CoreBundle\Entity\Config; | |
9 | use Wallabag\CoreBundle\Helper\Redirect; | |
10 | use Wallabag\UserBundle\Entity\User; | |
11 | ||
12 | class RedirectTest extends TestCase | |
13 | { | |
14 | /** @var \PHPUnit_Framework_MockObject_MockObject */ | |
15 | private $routerMock; | |
16 | ||
17 | /** @var Redirect */ | |
18 | private $redirect; | |
19 | ||
20 | /** @var UsernamePasswordToken */ | |
21 | private $token; | |
22 | ||
23 | public function setUp() | |
24 | { | |
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 | ||
56 | $this->redirect = new Redirect($this->routerMock, $tokenStorage); | |
57 | } | |
58 | ||
59 | public function testRedirectToNullWithFallback() | |
60 | { | |
61 | $redirectUrl = $this->redirect->to(null, 'fallback'); | |
62 | ||
63 | $this->assertSame('fallback', $redirectUrl); | |
64 | } | |
65 | ||
66 | public function testRedirectToNullWithoutFallback() | |
67 | { | |
68 | $redirectUrl = $this->redirect->to(null); | |
69 | ||
70 | $this->assertSame($this->routerMock->generate('homepage'), $redirectUrl); | |
71 | } | |
72 | ||
73 | public function testRedirectToValidUrl() | |
74 | { | |
75 | $redirectUrl = $this->redirect->to('/unread/list'); | |
76 | ||
77 | $this->assertSame('/unread/list', $redirectUrl); | |
78 | } | |
79 | ||
80 | public function testWithNotLoggedUser() | |
81 | { | |
82 | $redirect = new Redirect($this->routerMock, new TokenStorage()); | |
83 | $redirectUrl = $redirect->to('/unread/list'); | |
84 | ||
85 | $this->assertSame('/unread/list', $redirectUrl); | |
86 | } | |
87 | ||
88 | public function testUserForRedirectToHomepage() | |
89 | { | |
90 | $this->token->getUser()->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE); | |
91 | ||
92 | $redirectUrl = $this->redirect->to('/unread/list'); | |
93 | ||
94 | $this->assertSame($this->routerMock->generate('homepage'), $redirectUrl); | |
95 | } | |
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 | ||
103 | $this->assertSame('/unread/list', $redirectUrl); | |
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 | ||
112 | $this->assertSame('fallback', $redirectUrl); | |
113 | } | |
114 | } |