Process session filters through Slim controllers
Including: - visibility - links per page - untagged only
This commit is contained in:
parent
893f5159c6
commit
af290059d1
9 changed files with 667 additions and 52 deletions
81
application/front/controllers/SessionFilterController.php
Normal file
81
application/front/controllers/SessionFilterController.php
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shaarli\Front\Controller;
|
||||||
|
|
||||||
|
use Shaarli\Bookmark\BookmarkFilter;
|
||||||
|
use Shaarli\Security\SessionManager;
|
||||||
|
use Slim\Http\Request;
|
||||||
|
use Slim\Http\Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class SessionFilterController
|
||||||
|
*
|
||||||
|
* Slim controller used to handle filters stored in the user session, such as visibility, links per page, etc.
|
||||||
|
*
|
||||||
|
* @package Shaarli\Front\Controller
|
||||||
|
*/
|
||||||
|
class SessionFilterController extends ShaarliController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* GET /links-per-page: set the number of bookmarks to display per page in homepage
|
||||||
|
*/
|
||||||
|
public function linksPerPage(Request $request, Response $response): Response
|
||||||
|
{
|
||||||
|
$linksPerPage = $request->getParam('nb') ?? null;
|
||||||
|
if (null === $linksPerPage || false === is_numeric($linksPerPage)) {
|
||||||
|
$linksPerPage = $this->container->conf->get('general.links_per_page', 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->container->sessionManager->setSessionParameter(
|
||||||
|
SessionManager::KEY_LINKS_PER_PAGE,
|
||||||
|
abs(intval($linksPerPage))
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->redirectFromReferer($response, ['linksperpage'], ['nb']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /visibility: allows to display only public or only private bookmarks in linklist
|
||||||
|
*/
|
||||||
|
public function visibility(Request $request, Response $response, array $args): Response
|
||||||
|
{
|
||||||
|
if (false === $this->container->loginManager->isLoggedIn()) {
|
||||||
|
return $this->redirectFromReferer($response, ['visibility']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$newVisibility = $args['visibility'] ?? null;
|
||||||
|
if (false === in_array($newVisibility, [BookmarkFilter::$PRIVATE, BookmarkFilter::$PUBLIC], true)) {
|
||||||
|
$newVisibility = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$currentVisibility = $this->container->sessionManager->getSessionParameter(SessionManager::KEY_VISIBILITY);
|
||||||
|
|
||||||
|
// Visibility not set or not already expected value, set expected value, otherwise reset it
|
||||||
|
if ($newVisibility !== null && (null === $currentVisibility || $currentVisibility !== $newVisibility)) {
|
||||||
|
// See only public bookmarks
|
||||||
|
$this->container->sessionManager->setSessionParameter(
|
||||||
|
SessionManager::KEY_VISIBILITY,
|
||||||
|
$newVisibility
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$this->container->sessionManager->deleteSessionParameter(SessionManager::KEY_VISIBILITY);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->redirectFromReferer($response, ['visibility']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /untagged-only: allows to display only bookmarks without any tag
|
||||||
|
*/
|
||||||
|
public function untaggedOnly(Request $request, Response $response): Response
|
||||||
|
{
|
||||||
|
$this->container->sessionManager->setSessionParameter(
|
||||||
|
SessionManager::KEY_UNTAGGED_ONLY,
|
||||||
|
empty($this->container->sessionManager->getSessionParameter(SessionManager::KEY_UNTAGGED_ONLY))
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->redirectFromReferer($response, ['untaggedonly', 'untagged-only']);
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
use Shaarli\Bookmark\BookmarkFilter;
|
use Shaarli\Bookmark\BookmarkFilter;
|
||||||
use Shaarli\Container\ShaarliContainer;
|
use Shaarli\Container\ShaarliContainer;
|
||||||
|
use Slim\Http\Response;
|
||||||
|
|
||||||
abstract class ShaarliController
|
abstract class ShaarliController
|
||||||
{
|
{
|
||||||
|
@ -80,4 +81,46 @@ protected function executeDefaultHooks(string $template): void
|
||||||
$this->assignView('plugins_' . $name, $plugin_data);
|
$this->assignView('plugins_' . $name, $plugin_data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a redirection to the previous page, based on the HTTP_REFERER.
|
||||||
|
* It fails back to the home page.
|
||||||
|
*
|
||||||
|
* @param array $loopTerms Terms to remove from path and query string to prevent direction loop.
|
||||||
|
* @param array $clearParams List of parameter to remove from the query string of the referrer.
|
||||||
|
*/
|
||||||
|
protected function redirectFromReferer(Response $response, array $loopTerms = [], array $clearParams = []): Response
|
||||||
|
{
|
||||||
|
$defaultPath = './';
|
||||||
|
$referer = $this->container->environment['HTTP_REFERER'] ?? null;
|
||||||
|
|
||||||
|
if (null !== $referer) {
|
||||||
|
$currentUrl = parse_url($referer);
|
||||||
|
parse_str($currentUrl['query'] ?? '', $params);
|
||||||
|
$path = $currentUrl['path'] ?? $defaultPath;
|
||||||
|
} else {
|
||||||
|
$params = [];
|
||||||
|
$path = $defaultPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent redirection loop
|
||||||
|
if (isset($currentUrl)) {
|
||||||
|
foreach ($clearParams as $value) {
|
||||||
|
unset($params[$value]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$checkQuery = implode('', array_keys($params));
|
||||||
|
foreach ($loopTerms as $value) {
|
||||||
|
if (strpos($path . $checkQuery, $value) !== false) {
|
||||||
|
$params = [];
|
||||||
|
$path = $defaultPath;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$queryString = count($params) > 0 ? '?'. http_build_query($params) : '';
|
||||||
|
|
||||||
|
return $response->withRedirect($path . $queryString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,10 @@
|
||||||
*/
|
*/
|
||||||
class SessionManager
|
class SessionManager
|
||||||
{
|
{
|
||||||
|
public const KEY_LINKS_PER_PAGE = 'LINKS_PER_PAGE';
|
||||||
|
public const KEY_VISIBILITY = 'visibility';
|
||||||
|
public const KEY_UNTAGGED_ONLY = 'untaggedonly';
|
||||||
|
|
||||||
/** @var int Session expiration timeout, in seconds */
|
/** @var int Session expiration timeout, in seconds */
|
||||||
public static $SHORT_TIMEOUT = 3600; // 1 hour
|
public static $SHORT_TIMEOUT = 3600; // 1 hour
|
||||||
|
|
||||||
|
@ -212,4 +216,33 @@ public function getSessionParameter(string $key, $default = null)
|
||||||
{
|
{
|
||||||
return $this->session[$key] ?? $default;
|
return $this->session[$key] ?? $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a variable in user session.
|
||||||
|
*
|
||||||
|
* @param string $key Session key
|
||||||
|
* @param mixed $value Session value to store
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setSessionParameter(string $key, $value): self
|
||||||
|
{
|
||||||
|
$this->session[$key] = $value;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a variable in user session.
|
||||||
|
*
|
||||||
|
* @param string $key Session key
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function deleteSessionParameter(string $key): self
|
||||||
|
{
|
||||||
|
unset($this->session[$key]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
57
index.php
57
index.php
|
@ -457,57 +457,19 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
|
||||||
|
|
||||||
// -------- User wants to change the number of bookmarks per page (linksperpage=...)
|
// -------- User wants to change the number of bookmarks per page (linksperpage=...)
|
||||||
if (isset($_GET['linksperpage'])) {
|
if (isset($_GET['linksperpage'])) {
|
||||||
if (is_numeric($_GET['linksperpage'])) {
|
header('Location: ./links-per-page?nb='. $_GET['linksperpage']);
|
||||||
$_SESSION['LINKS_PER_PAGE']=abs(intval($_GET['linksperpage']));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! empty($_SERVER['HTTP_REFERER'])) {
|
|
||||||
$location = generateLocation($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'], array('linksperpage'));
|
|
||||||
} else {
|
|
||||||
$location = '?';
|
|
||||||
}
|
|
||||||
header('Location: '. $location);
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------- User wants to see only private bookmarks (toggle)
|
// -------- User wants to see only private bookmarks (toggle)
|
||||||
if (isset($_GET['visibility'])) {
|
if (isset($_GET['visibility'])) {
|
||||||
if ($_GET['visibility'] === 'private') {
|
header('Location: ./visibility/'. $_GET['visibility']);
|
||||||
// Visibility not set or not already private, set private, otherwise reset it
|
|
||||||
if (empty($_SESSION['visibility']) || $_SESSION['visibility'] !== 'private') {
|
|
||||||
// See only private bookmarks
|
|
||||||
$_SESSION['visibility'] = 'private';
|
|
||||||
} else {
|
|
||||||
unset($_SESSION['visibility']);
|
|
||||||
}
|
|
||||||
} elseif ($_GET['visibility'] === 'public') {
|
|
||||||
if (empty($_SESSION['visibility']) || $_SESSION['visibility'] !== 'public') {
|
|
||||||
// See only public bookmarks
|
|
||||||
$_SESSION['visibility'] = 'public';
|
|
||||||
} else {
|
|
||||||
unset($_SESSION['visibility']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! empty($_SERVER['HTTP_REFERER'])) {
|
|
||||||
$location = generateLocation($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'], array('visibility'));
|
|
||||||
} else {
|
|
||||||
$location = '?';
|
|
||||||
}
|
|
||||||
header('Location: '. $location);
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------- User wants to see only untagged bookmarks (toggle)
|
// -------- User wants to see only untagged bookmarks (toggle)
|
||||||
if (isset($_GET['untaggedonly'])) {
|
if (isset($_GET['untaggedonly'])) {
|
||||||
$_SESSION['untaggedonly'] = empty($_SESSION['untaggedonly']);
|
header('Location: ./untagged-only');
|
||||||
|
|
||||||
if (! empty($_SERVER['HTTP_REFERER'])) {
|
|
||||||
$location = generateLocation($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'], array('untaggedonly'));
|
|
||||||
} else {
|
|
||||||
$location = '?';
|
|
||||||
}
|
|
||||||
header('Location: '. $location);
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1549,6 +1511,19 @@ function install($conf, $sessionManager, $loginManager)
|
||||||
|
|
||||||
$this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag');
|
$this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag');
|
||||||
$this->get('/remove-tag/{tag}', '\Shaarli\Front\Controller\TagController:removeTag')->setName('remove-tag');
|
$this->get('/remove-tag/{tag}', '\Shaarli\Front\Controller\TagController:removeTag')->setName('remove-tag');
|
||||||
|
|
||||||
|
$this
|
||||||
|
->get('/links-per-page', '\Shaarli\Front\Controller\SessionFilterController:linksPerPage')
|
||||||
|
->setName('filter-links-per-page')
|
||||||
|
;
|
||||||
|
$this
|
||||||
|
->get('/visibility/{visibility}', '\Shaarli\Front\Controller\SessionFilterController:visibility')
|
||||||
|
->setName('visibility')
|
||||||
|
;
|
||||||
|
$this
|
||||||
|
->get('/untagged-only', '\Shaarli\Front\Controller\SessionFilterController:untaggedOnly')
|
||||||
|
->setName('untagged-only')
|
||||||
|
;
|
||||||
})->add('\Shaarli\Front\ShaarliMiddleware');
|
})->add('\Shaarli\Front\ShaarliMiddleware');
|
||||||
|
|
||||||
$response = $app->run(true);
|
$response = $app->run(true);
|
||||||
|
|
290
tests/front/controller/SessionFilterControllerTest.php
Normal file
290
tests/front/controller/SessionFilterControllerTest.php
Normal file
|
@ -0,0 +1,290 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shaarli\Front\Controller;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Shaarli\Security\SessionManager;
|
||||||
|
use Slim\Http\Request;
|
||||||
|
use Slim\Http\Response;
|
||||||
|
|
||||||
|
class SessionFilterControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
use FrontControllerMockHelper;
|
||||||
|
|
||||||
|
/** @var SessionFilterController */
|
||||||
|
protected $controller;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->createContainer();
|
||||||
|
|
||||||
|
$this->controller = new SessionFilterController($this->container);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Link per page - Default call with valid parameter and a referer.
|
||||||
|
*/
|
||||||
|
public function testLinksPerPage(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$request->method('getParam')->with('nb')->willReturn('8');
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::once())
|
||||||
|
->method('setSessionParameter')
|
||||||
|
->with(SessionManager::KEY_LINKS_PER_PAGE, 8)
|
||||||
|
;
|
||||||
|
|
||||||
|
$result = $this->controller->linksPerPage($request, $response);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Link per page - Invalid value, should use default value (20)
|
||||||
|
*/
|
||||||
|
public function testLinksPerPageNotValid(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$request->method('getParam')->with('nb')->willReturn('test');
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::once())
|
||||||
|
->method('setSessionParameter')
|
||||||
|
->with(SessionManager::KEY_LINKS_PER_PAGE, 20)
|
||||||
|
;
|
||||||
|
|
||||||
|
$result = $this->controller->linksPerPage($request, $response);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['./'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visibility - Default call for private filter while logged in without current value
|
||||||
|
*/
|
||||||
|
public function testVisibility(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$arg = ['visibility' => 'private'];
|
||||||
|
|
||||||
|
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
|
||||||
|
|
||||||
|
$this->container->loginManager->method('isLoggedIn')->willReturn(true);
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::once())
|
||||||
|
->method('setSessionParameter')
|
||||||
|
->with(SessionManager::KEY_VISIBILITY, 'private')
|
||||||
|
;
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->visibility($request, $response, $arg);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visibility - Toggle off private visibility
|
||||||
|
*/
|
||||||
|
public function testVisibilityToggleOff(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$arg = ['visibility' => 'private'];
|
||||||
|
|
||||||
|
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
|
||||||
|
|
||||||
|
$this->container->loginManager->method('isLoggedIn')->willReturn(true);
|
||||||
|
$this->container->sessionManager
|
||||||
|
->method('getSessionParameter')
|
||||||
|
->with(SessionManager::KEY_VISIBILITY)
|
||||||
|
->willReturn('private')
|
||||||
|
;
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::never())
|
||||||
|
->method('setSessionParameter')
|
||||||
|
;
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::once())
|
||||||
|
->method('deleteSessionParameter')
|
||||||
|
->with(SessionManager::KEY_VISIBILITY)
|
||||||
|
;
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->visibility($request, $response, $arg);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visibility - Change private to public
|
||||||
|
*/
|
||||||
|
public function testVisibilitySwitch(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$arg = ['visibility' => 'private'];
|
||||||
|
|
||||||
|
$this->container->loginManager->method('isLoggedIn')->willReturn(true);
|
||||||
|
$this->container->sessionManager
|
||||||
|
->method('getSessionParameter')
|
||||||
|
->with(SessionManager::KEY_VISIBILITY)
|
||||||
|
->willReturn('public')
|
||||||
|
;
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::once())
|
||||||
|
->method('setSessionParameter')
|
||||||
|
->with(SessionManager::KEY_VISIBILITY, 'private')
|
||||||
|
;
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->visibility($request, $response, $arg);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['./'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visibility - With invalid value - should remove any visibility setting
|
||||||
|
*/
|
||||||
|
public function testVisibilityInvalidValue(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$arg = ['visibility' => 'test'];
|
||||||
|
|
||||||
|
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
|
||||||
|
|
||||||
|
$this->container->loginManager->method('isLoggedIn')->willReturn(true);
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::never())
|
||||||
|
->method('setSessionParameter')
|
||||||
|
;
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::once())
|
||||||
|
->method('deleteSessionParameter')
|
||||||
|
->with(SessionManager::KEY_VISIBILITY)
|
||||||
|
;
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->visibility($request, $response, $arg);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visibility - Try to change visibility while logged out
|
||||||
|
*/
|
||||||
|
public function testVisibilityLoggedOut(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$arg = ['visibility' => 'test'];
|
||||||
|
|
||||||
|
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
|
||||||
|
|
||||||
|
$this->container->loginManager->method('isLoggedIn')->willReturn(false);
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::never())
|
||||||
|
->method('setSessionParameter')
|
||||||
|
;
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::never())
|
||||||
|
->method('deleteSessionParameter')
|
||||||
|
->with(SessionManager::KEY_VISIBILITY)
|
||||||
|
;
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->visibility($request, $response, $arg);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Untagged only - valid call
|
||||||
|
*/
|
||||||
|
public function testUntaggedOnly(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::once())
|
||||||
|
->method('setSessionParameter')
|
||||||
|
->with(SessionManager::KEY_UNTAGGED_ONLY, true)
|
||||||
|
;
|
||||||
|
|
||||||
|
$result = $this->controller->untaggedOnly($request, $response);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Untagged only - toggle off
|
||||||
|
*/
|
||||||
|
public function testUntaggedOnlyToggleOff(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$this->container->sessionManager
|
||||||
|
->method('getSessionParameter')
|
||||||
|
->with(SessionManager::KEY_UNTAGGED_ONLY)
|
||||||
|
->willReturn(true)
|
||||||
|
;
|
||||||
|
$this->container->sessionManager
|
||||||
|
->expects(static::once())
|
||||||
|
->method('setSessionParameter')
|
||||||
|
->with(SessionManager::KEY_UNTAGGED_ONLY, false)
|
||||||
|
;
|
||||||
|
|
||||||
|
$result = $this->controller->untaggedOnly($request, $response);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Shaarli\Bookmark\BookmarkFilter;
|
use Shaarli\Bookmark\BookmarkFilter;
|
||||||
|
use Slim\Http\Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ShaarliControllerTest
|
* Class ShaarliControllerTest
|
||||||
|
@ -38,6 +39,14 @@ public function render(string $template): string
|
||||||
{
|
{
|
||||||
return parent::render($template);
|
return parent::render($template);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function redirectFromReferer(
|
||||||
|
Response $response,
|
||||||
|
array $loopTerms = [],
|
||||||
|
array $clearParams = []
|
||||||
|
): Response {
|
||||||
|
return parent::redirectFromReferer($response, $loopTerms, $clearParams);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
$this->assignedValues = [];
|
$this->assignedValues = [];
|
||||||
}
|
}
|
||||||
|
@ -91,4 +100,126 @@ public function testRender(): void
|
||||||
static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']);
|
static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']);
|
||||||
static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']);
|
static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test redirectFromReferer() - Default behaviour
|
||||||
|
*/
|
||||||
|
public function testRedirectFromRefererDefault(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
|
||||||
|
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->redirectFromReferer($response);
|
||||||
|
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test redirectFromReferer() - With a loop term not matched in the referer
|
||||||
|
*/
|
||||||
|
public function testRedirectFromRefererWithUnmatchedLoopTerm(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
|
||||||
|
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->redirectFromReferer($response, ['nope']);
|
||||||
|
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test redirectFromReferer() - With a loop term matching the referer in its path -> redirect to default
|
||||||
|
*/
|
||||||
|
public function testRedirectFromRefererWithMatchingLoopTermInPath(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
|
||||||
|
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->redirectFromReferer($response, ['nope', 'controller']);
|
||||||
|
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['./'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test redirectFromReferer() - With a loop term matching the referer in its query parameters -> redirect to default
|
||||||
|
*/
|
||||||
|
public function testRedirectFromRefererWithMatchingLoopTermInQueryParam(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
|
||||||
|
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->redirectFromReferer($response, ['nope', 'other']);
|
||||||
|
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['./'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test redirectFromReferer() - With a loop term matching the referer in its query value
|
||||||
|
* -> we do not block redirection for query parameter values.
|
||||||
|
*/
|
||||||
|
public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
|
||||||
|
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->redirectFromReferer($response, ['nope', 'param']);
|
||||||
|
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test redirectFromReferer() - With a loop term matching the referer in its domain name
|
||||||
|
* -> we do not block redirection for shaarli's hosts
|
||||||
|
*/
|
||||||
|
public function testRedirectFromRefererWithLoopTermInDomain(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
|
||||||
|
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->redirectFromReferer($response, ['shaarli']);
|
||||||
|
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test redirectFromReferer() - With a loop term matching a query parameter AND clear this query param
|
||||||
|
* -> the param should be cleared before checking if it matches the redir loop terms
|
||||||
|
*/
|
||||||
|
public function testRedirectFromRefererWithMatchingClearedParam(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
|
||||||
|
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$result = $this->controller->redirectFromReferer($response, ['query'], ['query']);
|
||||||
|
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -269,4 +269,61 @@ public function testHasClientIpChanged()
|
||||||
$this->session['ip'] = 'ip_id_one';
|
$this->session['ip'] = 'ip_id_one';
|
||||||
$this->assertTrue($this->sessionManager->hasClientIpChanged('ip_id_two'));
|
$this->assertTrue($this->sessionManager->hasClientIpChanged('ip_id_two'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test creating an entry in the session array
|
||||||
|
*/
|
||||||
|
public function testSetSessionParameterCreate(): void
|
||||||
|
{
|
||||||
|
$this->sessionManager->setSessionParameter('abc', 'def');
|
||||||
|
|
||||||
|
static::assertSame('def', $this->session['abc']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test updating an entry in the session array
|
||||||
|
*/
|
||||||
|
public function testSetSessionParameterUpdate(): void
|
||||||
|
{
|
||||||
|
$this->session['abc'] = 'ghi';
|
||||||
|
|
||||||
|
$this->sessionManager->setSessionParameter('abc', 'def');
|
||||||
|
|
||||||
|
static::assertSame('def', $this->session['abc']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test updating an entry in the session array with null value
|
||||||
|
*/
|
||||||
|
public function testSetSessionParameterUpdateNull(): void
|
||||||
|
{
|
||||||
|
$this->session['abc'] = 'ghi';
|
||||||
|
|
||||||
|
$this->sessionManager->setSessionParameter('abc', null);
|
||||||
|
|
||||||
|
static::assertArrayHasKey('abc', $this->session);
|
||||||
|
static::assertNull($this->session['abc']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test deleting an existing entry in the session array
|
||||||
|
*/
|
||||||
|
public function testDeleteSessionParameter(): void
|
||||||
|
{
|
||||||
|
$this->session['abc'] = 'def';
|
||||||
|
|
||||||
|
$this->sessionManager->deleteSessionParameter('abc');
|
||||||
|
|
||||||
|
static::assertArrayNotHasKey('abc', $this->session);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test deleting a non existent entry in the session array
|
||||||
|
*/
|
||||||
|
public function testDeleteSessionParameterNotExisting(): void
|
||||||
|
{
|
||||||
|
$this->sessionManager->deleteSessionParameter('abc');
|
||||||
|
|
||||||
|
static::assertArrayNotHasKey('abc', $this->session);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,14 @@
|
||||||
{'Filters'|t}
|
{'Filters'|t}
|
||||||
</span>
|
</span>
|
||||||
{if="$is_logged_in"}
|
{if="$is_logged_in"}
|
||||||
<a href="?visibility=private" aria-label="{'Only display private links'|t}" title="{'Only display private links'|t}"
|
<a href="./visibility/private" aria-label="{'Only display private links'|t}" title="{'Only display private links'|t}"
|
||||||
class="{if="$visibility==='private'"}filter-on{else}filter-off{/if}"
|
class="{if="$visibility==='private'"}filter-on{else}filter-off{/if}"
|
||||||
><i class="fa fa-user-secret" aria-hidden="true"></i></a>
|
><i class="fa fa-user-secret" aria-hidden="true"></i></a>
|
||||||
<a href="?visibility=public" aria-label="{'Only display public links'|t}" title="{'Only display public links'|t}"
|
<a href="./visibility/public" aria-label="{'Only display public links'|t}" title="{'Only display public links'|t}"
|
||||||
class="{if="$visibility==='public'"}filter-on{else}filter-off{/if}"
|
class="{if="$visibility==='public'"}filter-on{else}filter-off{/if}"
|
||||||
><i class="fa fa-globe" aria-hidden="true"></i></a>
|
><i class="fa fa-globe" aria-hidden="true"></i></a>
|
||||||
{/if}
|
{/if}
|
||||||
<a href="?untaggedonly" aria-label="{'Filter untagged links'|t}" title="{'Filter untagged links'|t}"
|
<a href="./untagged-only" aria-label="{'Filter untagged links'|t}" title="{'Filter untagged links'|t}"
|
||||||
class={if="$untaggedonly"}"filter-on"{else}"filter-off"{/if}
|
class={if="$untaggedonly"}"filter-on"{else}"filter-off"{/if}
|
||||||
><i class="fa fa-tag" aria-hidden="true"></i></a>
|
><i class="fa fa-tag" aria-hidden="true"></i></a>
|
||||||
<a href="#" aria-label="{'Select all'|t}" title="{'Select all'|t}"
|
<a href="#" aria-label="{'Select all'|t}" title="{'Select all'|t}"
|
||||||
|
@ -53,11 +53,11 @@
|
||||||
|
|
||||||
<div class="linksperpage pure-u-1-3">
|
<div class="linksperpage pure-u-1-3">
|
||||||
<div class="pure-u-0 pure-u-lg-visible">{'Links per page'|t}</div>
|
<div class="pure-u-0 pure-u-lg-visible">{'Links per page'|t}</div>
|
||||||
<a href="?linksperpage=20">20</a>
|
<a href="./links-per-page?nb=20">20</a>
|
||||||
<a href="?linksperpage=50">50</a>
|
<a href="./links-per-page?nb=50">50</a>
|
||||||
<a href="?linksperpage=100">100</a>
|
<a href="./links-per-page?nb=100">100</a>
|
||||||
<form method="GET" class="pure-u-0 pure-u-lg-visible">
|
<form method="GET" class="pure-u-0 pure-u-lg-visible" action="./links-per-page">
|
||||||
<input type="text" name="linksperpage" placeholder="133">
|
<input type="text" name="nb" placeholder="133">
|
||||||
</form>
|
</form>
|
||||||
<a href="#" class="filter-off fold-all pure-u-0 pure-u-lg-visible" aria-label="{'Fold all'|t}" title="{'Fold all'|t}">
|
<a href="#" class="filter-off fold-all pure-u-0 pure-u-lg-visible" aria-label="{'Fold all'|t}" title="{'Fold all'|t}">
|
||||||
<i class="fa fa-chevron-up" aria-hidden="true"></i>
|
<i class="fa fa-chevron-up" aria-hidden="true"></i>
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<div class="paging">
|
<div class="paging">
|
||||||
{if="$is_logged_in"}
|
{if="$is_logged_in"}
|
||||||
<div class="paging_privatelinks">
|
<div class="paging_privatelinks">
|
||||||
<a href="?visibility=private">
|
<a href="./visibility/private">
|
||||||
{if="$visibility=='private'"}
|
{if="$visibility=='private'"}
|
||||||
<img src="img/private_16x16_active.png" width="16" height="16" title="Click to see all links" alt="Click to see all links">
|
<img src="img/private_16x16_active.png" width="16" height="16" title="Click to see all links" alt="Click to see all links">
|
||||||
{else}
|
{else}
|
||||||
|
@ -23,8 +23,13 @@
|
||||||
</div>
|
</div>
|
||||||
{/loop}
|
{/loop}
|
||||||
<div class="paging_linksperpage">
|
<div class="paging_linksperpage">
|
||||||
Links per page: <a href="?linksperpage=20">20</a> <a href="?linksperpage=50">50</a> <a href="?linksperpage=100">100</a>
|
Links per page:
|
||||||
<form method="GET" class="linksperpage"><input type="text" name="linksperpage" size="2"></form>
|
<a href="./links-per-page?nb=20">20</a>
|
||||||
|
<a href="./links-per-page?nb=50">50</a>
|
||||||
|
<a href="./links-per-page?nb=100">100</a>
|
||||||
|
<form method="GET" class="linksperpage" action="./links-per-page">
|
||||||
|
<input type="text" name="nb" size="2">
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
{if="$previous_page_url"} <a href="{$previous_page_url}" class="paging_older">◄Older</a> {/if}
|
{if="$previous_page_url"} <a href="{$previous_page_url}" class="paging_older">◄Older</a> {/if}
|
||||||
<div class="paging_current">page {$page_current} / {$page_max} </div>
|
<div class="paging_current">page {$page_current} / {$page_max} </div>
|
||||||
|
|
Loading…
Reference in a new issue