wallabag/tests/Helper/RedirectTest.php

119 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2024-02-19 01:30:12 +01:00
namespace Tests\Wallabag\Helper;
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;
2017-12-16 22:17:42 +01:00
class RedirectTest extends TestCase
{
2023-08-08 03:27:21 +02:00
/** @var Router&MockObject */
private $routerMock;
/** @var Redirect */
private $redirect;
2022-11-23 15:51:33 +01:00
/** @var User */
private $user;
/** @var UsernamePasswordToken */
private $token;
2020-12-08 09:17:10 +01:00
protected function setUp(): void
{
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')
->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);
$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 09:52:38 +02:00
$this->assertSame('/unread/list', $redirectUrl);
}
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
$this->assertSame('/', $redirectUrl);
2016-11-10 15:50:10 +01:00
}
public function testUserForRedirectWithIgnoreActionMarkAsRead()
{
2022-11-23 15:51:33 +01:00
$this->user->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
$redirectUrl = $this->redirect->to('/unread/list', true);
2017-07-01 09:52:38 +02:00
$this->assertSame('/unread/list', $redirectUrl);
}
public function testUserForRedirectNullWithIgnoreActionMarkAsRead()
{
2022-11-23 15:51:33 +01:00
$this->user->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
$redirectUrl = $this->redirect->to(null, true);
$this->assertSame('/', $redirectUrl);
}
}