wallabag/tests/Helper/RedirectTest.php

119 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2024-02-19 00:30:12 +00:00
namespace Tests\Wallabag\Helper;
2023-08-08 01:27:21 +00:00
use PHPUnit\Framework\MockObject\MockObject;
2017-12-16 21:17:42 +00:00
use PHPUnit\Framework\TestCase;
2022-09-01 18:54:56 +00:00
use Symfony\Component\Routing\Router;
2017-07-01 07:52:38 +00:00
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2024-02-19 00:30:12 +00:00
use Wallabag\Entity\Config;
use Wallabag\Entity\User;
use Wallabag\Helper\Redirect;
2017-12-16 21:17:42 +00:00
class RedirectTest extends TestCase
{
2023-08-08 01:27:21 +00:00
/** @var Router&MockObject */
private $routerMock;
/** @var Redirect */
private $redirect;
2022-11-23 14:51:33 +00:00
/** @var User */
private $user;
/** @var UsernamePasswordToken */
private $token;
2020-12-08 08:17:10 +00:00
protected function setUp(): void
{
2022-09-01 18:54:56 +00:00
$this->routerMock = $this->getMockBuilder(Router::class)
2016-11-16 22:07:00 +00:00
->disableOriginalConstructor()
->getMock();
$this->routerMock->expects($this->any())
->method('generate')
->with('homepage')
->willReturn('/');
2016-11-16 22:07:00 +00:00
2022-11-23 14:51:33 +00:00
$this->user = new User();
$this->user->setName('youpi');
$this->user->setEmail('youpi@youpi.org');
$this->user->setUsername('youpi');
$this->user->setPlainPassword('youpi');
$this->user->setEnabled(true);
$this->user->addRole('ROLE_SUPER_ADMIN');
2016-11-16 22:07:00 +00:00
2022-11-23 14:51:33 +00:00
$config = new Config($this->user);
2016-11-16 22:07:00 +00:00
$config->setItemsPerPage(30);
2019-09-19 12:23:06 +00:00
$config->setReadingSpeed(200);
2016-11-16 22:07:00 +00:00
$config->setLanguage('en');
$config->setPocketConsumerKey('xxxxx');
$config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE);
2022-11-23 14:51:33 +00:00
$this->user->setConfig($config);
2016-11-16 22:07:00 +00:00
2022-11-23 14:51:33 +00:00
$this->token = new UsernamePasswordToken($this->user, 'password', 'key');
2016-11-16 22:07:00 +00:00
$tokenStorage = new TokenStorage();
$tokenStorage->setToken($this->token);
$this->redirect = new Redirect($this->routerMock, $tokenStorage);
}
public function testRedirectToNull()
{
$redirectUrl = $this->redirect->to(null);
$this->assertSame('/', $redirectUrl);
}
public function testRedirectToValidUrl()
{
$redirectUrl = $this->redirect->to('/unread/list');
2017-07-01 07:52:38 +00:00
$this->assertSame('/unread/list', $redirectUrl);
}
public function testRedirectToAbsoluteUrl()
{
$redirectUrl = $this->redirect->to('https://www.google.com/');
$this->assertSame('/', $redirectUrl);
}
2016-11-16 22:07:00 +00:00
public function testWithNotLoggedUser()
2016-11-10 14:50:10 +00:00
{
2016-11-16 22:07:00 +00:00
$redirect = new Redirect($this->routerMock, new TokenStorage());
$redirectUrl = $redirect->to('/unread/list');
2016-11-10 14:50:10 +00:00
2017-07-01 07:52:38 +00:00
$this->assertSame('/unread/list', $redirectUrl);
2016-11-10 14:50:10 +00:00
}
2016-11-16 22:07:00 +00:00
public function testUserForRedirectToHomepage()
2016-11-10 14:50:10 +00:00
{
2022-11-23 14:51:33 +00:00
$this->user->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
2016-11-10 14:50:10 +00:00
2016-11-16 22:07:00 +00:00
$redirectUrl = $this->redirect->to('/unread/list');
2016-11-10 14:50:10 +00:00
$this->assertSame('/', $redirectUrl);
2016-11-10 14:50:10 +00:00
}
public function testUserForRedirectWithIgnoreActionMarkAsRead()
{
2022-11-23 14:51:33 +00:00
$this->user->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
$redirectUrl = $this->redirect->to('/unread/list', true);
2017-07-01 07:52:38 +00:00
$this->assertSame('/unread/list', $redirectUrl);
}
public function testUserForRedirectNullWithIgnoreActionMarkAsRead()
{
2022-11-23 14:51:33 +00:00
$this->user->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
$redirectUrl = $this->redirect->to(null, true);
$this->assertSame('/', $redirectUrl);
}
}