aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-07-24 12:48:53 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-24 12:48:53 +0200
commit204035bd3c91b9a5c39fcb6fc470e108b032dbd9 (patch)
treeb3b3c01bd162da4d0672a7fd3b598d8c4580c980
parent87ae3c4f08431e02869376cb57add257747910d1 (diff)
downloadShaarli-204035bd3c91b9a5c39fcb6fc470e108b032dbd9.tar.gz
Shaarli-204035bd3c91b9a5c39fcb6fc470e108b032dbd9.tar.zst
Shaarli-204035bd3c91b9a5c39fcb6fc470e108b032dbd9.zip
Fix: visitor are allowed to chose nb of links per page
-rw-r--r--application/front/controller/admin/SessionFilterController.php20
-rw-r--r--application/front/controller/visitor/PublicSessionFilterController.php33
-rw-r--r--tests/front/controller/admin/SessionFilterControllerTest.php48
-rw-r--r--tests/front/controller/visitor/PublicSessionFilterControllerTest.php71
4 files changed, 106 insertions, 66 deletions
diff --git a/application/front/controller/admin/SessionFilterController.php b/application/front/controller/admin/SessionFilterController.php
index 69a16ec3..081c0ba0 100644
--- a/application/front/controller/admin/SessionFilterController.php
+++ b/application/front/controller/admin/SessionFilterController.php
@@ -12,29 +12,11 @@ use Slim\Http\Response;
12/** 12/**
13 * Class SessionFilterController 13 * Class SessionFilterController
14 * 14 *
15 * Slim controller used to handle filters stored in the user session, such as visibility, links per page, etc. 15 * Slim controller used to handle filters stored in the user session, such as visibility, etc.
16 */ 16 */
17class SessionFilterController extends ShaarliAdminController 17class SessionFilterController extends ShaarliAdminController
18{ 18{
19 /** 19 /**
20 * GET /links-per-page: set the number of bookmarks to display per page in homepage
21 */
22 public function linksPerPage(Request $request, Response $response): Response
23 {
24 $linksPerPage = $request->getParam('nb') ?? null;
25 if (null === $linksPerPage || false === is_numeric($linksPerPage)) {
26 $linksPerPage = $this->container->conf->get('general.links_per_page', 20);
27 }
28
29 $this->container->sessionManager->setSessionParameter(
30 SessionManager::KEY_LINKS_PER_PAGE,
31 abs(intval($linksPerPage))
32 );
33
34 return $this->redirectFromReferer($request, $response, ['linksperpage'], ['nb']);
35 }
36
37 /**
38 * GET /visibility: allows to display only public or only private bookmarks in linklist 20 * GET /visibility: allows to display only public or only private bookmarks in linklist
39 */ 21 */
40 public function visibility(Request $request, Response $response, array $args): Response 22 public function visibility(Request $request, Response $response, array $args): Response
diff --git a/application/front/controller/visitor/PublicSessionFilterController.php b/application/front/controller/visitor/PublicSessionFilterController.php
new file mode 100644
index 00000000..35da0c5f
--- /dev/null
+++ b/application/front/controller/visitor/PublicSessionFilterController.php
@@ -0,0 +1,33 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Visitor;
6
7use Shaarli\Security\SessionManager;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Slim controller used to handle filters stored in the visitor session, links per page, etc.
13 */
14class PublicSessionFilterController extends ShaarliVisitorController
15{
16 /**
17 * GET /links-per-page: set the number of bookmarks to display per page in homepage
18 */
19 public function linksPerPage(Request $request, Response $response): Response
20 {
21 $linksPerPage = $request->getParam('nb') ?? null;
22 if (null === $linksPerPage || false === is_numeric($linksPerPage)) {
23 $linksPerPage = $this->container->conf->get('general.links_per_page', 20);
24 }
25
26 $this->container->sessionManager->setSessionParameter(
27 SessionManager::KEY_LINKS_PER_PAGE,
28 abs(intval($linksPerPage))
29 );
30
31 return $this->redirectFromReferer($request, $response, ['linksperpage'], ['nb']);
32 }
33}
diff --git a/tests/front/controller/admin/SessionFilterControllerTest.php b/tests/front/controller/admin/SessionFilterControllerTest.php
index ea07edee..124b0bf2 100644
--- a/tests/front/controller/admin/SessionFilterControllerTest.php
+++ b/tests/front/controller/admin/SessionFilterControllerTest.php
@@ -23,53 +23,7 @@ class SessionFilterControllerTest extends TestCase
23 23
24 $this->controller = new SessionFilterController($this->container); 24 $this->controller = new SessionFilterController($this->container);
25 } 25 }
26 26
27 /**
28 * Link per page - Default call with valid parameter and a referer.
29 */
30 public function testLinksPerPage(): void
31 {
32 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
33
34 $request = $this->createMock(Request::class);
35 $request->method('getParam')->with('nb')->willReturn('8');
36 $response = new Response();
37
38 $this->container->sessionManager
39 ->expects(static::once())
40 ->method('setSessionParameter')
41 ->with(SessionManager::KEY_LINKS_PER_PAGE, 8)
42 ;
43
44 $result = $this->controller->linksPerPage($request, $response);
45
46 static::assertInstanceOf(Response::class, $result);
47 static::assertSame(302, $result->getStatusCode());
48 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
49 }
50
51 /**
52 * Link per page - Invalid value, should use default value (20)
53 */
54 public function testLinksPerPageNotValid(): void
55 {
56 $request = $this->createMock(Request::class);
57 $request->method('getParam')->with('nb')->willReturn('test');
58 $response = new Response();
59
60 $this->container->sessionManager
61 ->expects(static::once())
62 ->method('setSessionParameter')
63 ->with(SessionManager::KEY_LINKS_PER_PAGE, 20)
64 ;
65
66 $result = $this->controller->linksPerPage($request, $response);
67
68 static::assertInstanceOf(Response::class, $result);
69 static::assertSame(302, $result->getStatusCode());
70 static::assertSame(['/subfolder/'], $result->getHeader('location'));
71 }
72
73 /** 27 /**
74 * Visibility - Default call for private filter while logged in without current value 28 * Visibility - Default call for private filter while logged in without current value
75 */ 29 */
diff --git a/tests/front/controller/visitor/PublicSessionFilterControllerTest.php b/tests/front/controller/visitor/PublicSessionFilterControllerTest.php
new file mode 100644
index 00000000..3aa1cb99
--- /dev/null
+++ b/tests/front/controller/visitor/PublicSessionFilterControllerTest.php
@@ -0,0 +1,71 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Visitor;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Security\SessionManager;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12class PublicSessionFilterControllerTest extends TestCase
13{
14 use FrontControllerMockHelper;
15
16 /** @var PublicSessionFilterController */
17 protected $controller;
18
19 public function setUp(): void
20 {
21 $this->createContainer();
22
23 $this->controller = new PublicSessionFilterController($this->container);
24 }
25
26 /**
27 * Link per page - Default call with valid parameter and a referer.
28 */
29 public function testLinksPerPage(): void
30 {
31 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
32
33 $request = $this->createMock(Request::class);
34 $request->method('getParam')->with('nb')->willReturn('8');
35 $response = new Response();
36
37 $this->container->sessionManager
38 ->expects(static::once())
39 ->method('setSessionParameter')
40 ->with(SessionManager::KEY_LINKS_PER_PAGE, 8)
41 ;
42
43 $result = $this->controller->linksPerPage($request, $response);
44
45 static::assertInstanceOf(Response::class, $result);
46 static::assertSame(302, $result->getStatusCode());
47 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
48 }
49
50 /**
51 * Link per page - Invalid value, should use default value (20)
52 */
53 public function testLinksPerPageNotValid(): void
54 {
55 $request = $this->createMock(Request::class);
56 $request->method('getParam')->with('nb')->willReturn('test');
57 $response = new Response();
58
59 $this->container->sessionManager
60 ->expects(static::once())
61 ->method('setSessionParameter')
62 ->with(SessionManager::KEY_LINKS_PER_PAGE, 20)
63 ;
64
65 $result = $this->controller->linksPerPage($request, $response);
66
67 static::assertInstanceOf(Response::class, $result);
68 static::assertSame(302, $result->getStatusCode());
69 static::assertSame(['/subfolder/'], $result->getHeader('location'));
70 }
71}