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