3 namespace Tests\Wallabag\CoreBundle\Helper
;
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
;
12 class RedirectTest
extends TestCase
14 /** @var \PHPUnit_Framework_MockObject_MockObject */
20 public function setUp()
22 $this->routerMock
= $this->getMockBuilder('Symfony\Component\Routing\Router')
23 ->disableOriginalConstructor()
26 $this->routerMock
->expects($this->any())
29 ->willReturn('homepage');
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');
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
);
47 $user->setConfig($config);
49 $this->token
= new UsernamePasswordToken($user, 'password', 'key');
50 $tokenStorage = new TokenStorage();
51 $tokenStorage->setToken($this->token
);
53 $this->redirect
= new Redirect($this->routerMock
, $tokenStorage);
56 public function testRedirectToNullWithFallback()
58 $redirectUrl = $this->redirect
->to(null, 'fallback');
60 $this->assertSame('fallback', $redirectUrl);
63 public function testRedirectToNullWithoutFallback()
65 $redirectUrl = $this->redirect
->to(null);
67 $this->assertSame($this->routerMock
->generate('homepage'), $redirectUrl);
70 public function testRedirectToValidUrl()
72 $redirectUrl = $this->redirect
->to('/unread/list');
74 $this->assertSame('/unread/list', $redirectUrl);
77 public function testWithNotLoggedUser()
79 $redirect = new Redirect($this->routerMock
, new TokenStorage());
80 $redirectUrl = $redirect->to('/unread/list');
82 $this->assertSame('/unread/list', $redirectUrl);
85 public function testUserForRedirectToHomepage()
87 $this->token
->getUser()->getConfig()->setActionMarkAsRead(Config
::REDIRECT_TO_HOMEPAGE
);
89 $redirectUrl = $this->redirect
->to('/unread/list');
91 $this->assertSame($this->routerMock
->generate('homepage'), $redirectUrl);
94 public function testUserForRedirectWithIgnoreActionMarkAsRead()
96 $this->token
->getUser()->getConfig()->setActionMarkAsRead(Config
::REDIRECT_TO_HOMEPAGE
);
98 $redirectUrl = $this->redirect
->to('/unread/list', '', true);
100 $this->assertSame('/unread/list', $redirectUrl);
103 public function testUserForRedirectNullWithFallbackWithIgnoreActionMarkAsRead()
105 $this->token
->getUser()->getConfig()->setActionMarkAsRead(Config
::REDIRECT_TO_HOMEPAGE
);
107 $redirectUrl = $this->redirect
->to(null, 'fallback', true);
109 $this->assertSame('fallback', $redirectUrl);