3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Security\LoginManager
;
12 use Shaarli\Thumbnailer
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
16 class BookmarkListControllerTest
extends TestCase
18 use FrontControllerMockHelper
;
20 /** @var BookmarkListController */
21 protected $controller;
23 public function setUp(): void
25 $this->createContainer();
27 $this->controller
= new BookmarkListController($this->container
);
31 * Test rendering list of bookmarks with default parameters (first page).
33 public function testIndexDefaultFirstPage(): void
35 $assignedVariables = [];
36 $this->assignTemplateVars($assignedVariables);
38 $request = $this->createMock(Request
::class);
39 $response = new Response();
41 $this->container
->bookmarkService
42 ->expects(static::once())
45 ['searchtags' => '', 'searchterm' => ''],
51 (new Bookmark())->setId(1)->setUrl('http://url1.tld')->setTitle('Title 1'),
52 (new Bookmark())->setId(2)->setUrl('http://url2.tld')->setTitle('Title 2'),
53 (new Bookmark())->setId(3)->setUrl('http://url3.tld')->setTitle('Title 3'),
57 $this->container
->sessionManager
58 ->method('getSessionParameter')
59 ->willReturnCallback(function (string $parameter, $default = null) {
60 if ('LINKS_PER_PAGE' === $parameter) {
68 $result = $this->controller
->index($request, $response);
70 static::assertSame(200, $result->getStatusCode());
71 static::assertSame('linklist', (string) $result->getBody());
73 static::assertSame('Shaarli', $assignedVariables['pagetitle']);
74 static::assertSame('?page=2', $assignedVariables['previous_page_url']);
75 static::assertSame('', $assignedVariables['next_page_url']);
76 static::assertSame(2, $assignedVariables['page_max']);
77 static::assertSame('', $assignedVariables['search_tags']);
78 static::assertSame(3, $assignedVariables['result_count']);
79 static::assertSame(1, $assignedVariables['page_current']);
80 static::assertSame('', $assignedVariables['search_term']);
81 static::assertNull($assignedVariables['visibility']);
82 static::assertCount(2, $assignedVariables['links']);
84 $link = $assignedVariables['links'][0];
86 static::assertSame(1, $link['id']);
87 static::assertSame('http://url1.tld', $link['url']);
88 static::assertSame('Title 1', $link['title']);
90 $link = $assignedVariables['links'][1];
92 static::assertSame(2, $link['id']);
93 static::assertSame('http://url2.tld', $link['url']);
94 static::assertSame('Title 2', $link['title']);
98 * Test rendering list of bookmarks with default parameters (second page).
100 public function testIndexDefaultSecondPage(): void
102 $assignedVariables = [];
103 $this->assignTemplateVars($assignedVariables);
105 $request = $this->createMock(Request
::class);
106 $request->method('getParam')->willReturnCallback(function (string $key) {
107 if ('page' === $key) {
113 $response = new Response();
115 $this->container
->bookmarkService
116 ->expects(static::once())
119 ['searchtags' => '', 'searchterm' => ''],
125 (new Bookmark())->setId(1)->setUrl('http://url1.tld')->setTitle('Title 1'),
126 (new Bookmark())->setId(2)->setUrl('http://url2.tld')->setTitle('Title 2'),
127 (new Bookmark())->setId(3)->setUrl('http://url3.tld')->setTitle('Title 3'),
131 $this->container
->sessionManager
132 ->method('getSessionParameter')
133 ->willReturnCallback(function (string $parameter, $default = null) {
134 if ('LINKS_PER_PAGE' === $parameter) {
142 $result = $this->controller
->index($request, $response);
144 static::assertSame(200, $result->getStatusCode());
145 static::assertSame('linklist', (string) $result->getBody());
147 static::assertSame('Shaarli', $assignedVariables['pagetitle']);
148 static::assertSame('', $assignedVariables['previous_page_url']);
149 static::assertSame('?page=1', $assignedVariables['next_page_url']);
150 static::assertSame(2, $assignedVariables['page_max']);
151 static::assertSame('', $assignedVariables['search_tags']);
152 static::assertSame(3, $assignedVariables['result_count']);
153 static::assertSame(2, $assignedVariables['page_current']);
154 static::assertSame('', $assignedVariables['search_term']);
155 static::assertNull($assignedVariables['visibility']);
156 static::assertCount(1, $assignedVariables['links']);
158 $link = $assignedVariables['links'][2];
160 static::assertSame(3, $link['id']);
161 static::assertSame('http://url3.tld', $link['url']);
162 static::assertSame('Title 3', $link['title']);
166 * Test rendering list of bookmarks with filters.
168 public function testIndexDefaultWithFilters(): void
170 $assignedVariables = [];
171 $this->assignTemplateVars($assignedVariables);
173 $request = $this->createMock(Request
::class);
174 $request->method('getParam')->willReturnCallback(function (string $key) {
175 if ('searchtags' === $key) {
178 if ('searchterm' === $key) {
184 $response = new Response();
186 $this->container
->sessionManager
187 ->method('getSessionParameter')
188 ->willReturnCallback(function (string $key, $default) {
189 if ('LINKS_PER_PAGE' === $key) {
192 if ('visibility' === $key) {
195 if ('untaggedonly' === $key) {
203 $this->container
->bookmarkService
204 ->expects(static::once())
207 ['searchtags' => 'abc@def', 'searchterm' => 'ghi jkl'],
213 (new Bookmark())->setId(1)->setUrl('http://url1.tld')->setTitle('Title 1'),
214 (new Bookmark())->setId(2)->setUrl('http://url2.tld')->setTitle('Title 2'),
215 (new Bookmark())->setId(3)->setUrl('http://url3.tld')->setTitle('Title 3'),
219 $result = $this->controller
->index($request, $response);
221 static::assertSame(200, $result->getStatusCode());
222 static::assertSame('linklist', (string) $result->getBody());
224 static::assertSame('Search: ghi jkl [abc] [def] - Shaarli', $assignedVariables['pagetitle']);
225 static::assertSame('?page=2&searchterm=ghi+jkl&searchtags=abc%40def', $assignedVariables['previous_page_url']);
229 * Test displaying a permalink with valid parameters
231 public function testPermalinkValid(): void
235 $assignedVariables = [];
236 $this->assignTemplateVars($assignedVariables);
238 $request = $this->createMock(Request
::class);
239 $response = new Response();
241 $this->container
->bookmarkService
242 ->expects(static::once())
243 ->method('findByHash')
245 ->willReturn((new Bookmark())->setId(123)->setTitle('Title 1')->setUrl('http://url1.tld'))
248 $result = $this->controller
->permalink($request, $response, ['hash' => $hash]);
250 static::assertSame(200, $result->getStatusCode());
251 static::assertSame('linklist', (string) $result->getBody());
253 static::assertSame('Title 1 - Shaarli', $assignedVariables['pagetitle']);
254 static::assertCount(1, $assignedVariables['links']);
256 $link = $assignedVariables['links'][0];
258 static::assertSame(123, $link['id']);
259 static::assertSame('http://url1.tld', $link['url']);
260 static::assertSame('Title 1', $link['title']);
264 * Test displaying a permalink with an unknown small hash : renders a 404 template error
266 public function testPermalinkNotFound(): void
270 $assignedVariables = [];
271 $this->assignTemplateVars($assignedVariables);
273 $request = $this->createMock(Request
::class);
274 $response = new Response();
276 $this->container
->bookmarkService
277 ->expects(static::once())
278 ->method('findByHash')
280 ->willThrowException(new BookmarkNotFoundException())
283 $result = $this->controller
->permalink($request, $response, ['hash' => $hash]);
285 static::assertSame(200, $result->getStatusCode());
286 static::assertSame('404', (string) $result->getBody());
289 'The link you are trying to reach does not exist or has been deleted.',
290 $assignedVariables['error_message']
295 * Test GET /shaare/{hash}?key={key} - Find a link by hash using a private link.
297 public function testPermalinkWithPrivateKey(): void
300 $privateKey = 'this is a private key';
302 $assignedVariables = [];
303 $this->assignTemplateVars($assignedVariables);
305 $request = $this->createMock(Request
::class);
306 $request->method('getParam')->willReturnCallback(function (string $key, $default = null) use ($privateKey) {
307 return $key === 'key' ? $privateKey : $default;
309 $response = new Response();
311 $this->container
->bookmarkService
312 ->expects(static::once())
313 ->method('findByHash')
314 ->with($hash, $privateKey)
315 ->willReturn((new Bookmark())->setId(123)->setTitle('Title 1')->setUrl('http://url1.tld'))
318 $result = $this->controller
->permalink($request, $response, ['hash' => $hash]);
320 static::assertSame(200, $result->getStatusCode());
321 static::assertSame('linklist', (string) $result->getBody());
322 static::assertCount(1, $assignedVariables['links']);
326 * Test getting link list with thumbnail updates.
327 * -> 2 thumbnails update, only 1 datastore write
329 public function testThumbnailUpdateFromLinkList(): void
331 $request = $this->createMock(Request
::class);
332 $response = new Response();
334 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
335 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
337 $this->container
->conf
= $this->createMock(ConfigManager
::class);
338 $this->container
->conf
340 ->willReturnCallback(function (string $key, $default) {
341 if ($key === 'thumbnails.mode') {
342 return Thumbnailer
::MODE_ALL
;
343 } elseif ($key === 'general.enable_async_metadata') {
351 $this->container
->thumbnailer
= $this->createMock(Thumbnailer
::class);
352 $this->container
->thumbnailer
353 ->expects(static::exactly(2))
355 ->withConsecutive(['https://url2.tld'], ['https://url4.tld'])
358 $this->container
->bookmarkService
359 ->expects(static::once())
362 (new Bookmark())->setId(1)->setUrl('https://url1.tld')->setTitle('Title 1')->setThumbnail(false),
363 $b1 = (new Bookmark())->setId(2)->setUrl('https://url2.tld')->setTitle('Title 2'),
364 (new Bookmark())->setId(3)->setUrl('https://url3.tld')->setTitle('Title 3')->setThumbnail(false),
365 $b2 = (new Bookmark())->setId(2)->setUrl('https://url4.tld')->setTitle('Title 4'),
366 (new Bookmark())->setId(2)->setUrl('ftp://url5.tld', ['ftp'])->setTitle('Title 5'),
369 $this->container
->bookmarkService
370 ->expects(static::exactly(2))
372 ->withConsecutive([$b1, false], [$b2, false])
374 $this->container
->bookmarkService
->expects(static::once())->method('save');
376 $result = $this->controller
->index($request, $response);
378 static::assertSame(200, $result->getStatusCode());
379 static::assertSame('linklist', (string) $result->getBody());
383 * Test getting a permalink with thumbnail update.
385 public function testThumbnailUpdateFromPermalink(): void
387 $request = $this->createMock(Request
::class);
388 $response = new Response();
390 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
391 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
393 $this->container
->conf
= $this->createMock(ConfigManager
::class);
394 $this->container
->conf
396 ->willReturnCallback(function (string $key, $default) {
397 if ($key === 'thumbnails.mode') {
398 return Thumbnailer
::MODE_ALL
;
399 } elseif ($key === 'general.enable_async_metadata') {
407 $this->container
->thumbnailer
= $this->createMock(Thumbnailer
::class);
408 $this->container
->thumbnailer
->expects(static::once())->method('get')->withConsecutive(['https://url.tld']);
410 $this->container
->bookmarkService
411 ->expects(static::once())
412 ->method('findByHash')
413 ->willReturn($bookmark = (new Bookmark())->setId(2)->setUrl('https://url.tld')->setTitle('Title 1'))
415 $this->container
->bookmarkService
->expects(static::once())->method('set')->with($bookmark, true);
416 $this->container
->bookmarkService
->expects(static::never())->method('save');
418 $result = $this->controller
->permalink($request, $response, ['hash' => 'abc']);
420 static::assertSame(200, $result->getStatusCode());
421 static::assertSame('linklist', (string) $result->getBody());
425 * Test getting a permalink with thumbnail update with async setting: no update should run.
427 public function testThumbnailUpdateFromPermalinkAsync(): void
429 $request = $this->createMock(Request
::class);
430 $response = new Response();
432 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
433 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
435 $this->container
->conf
= $this->createMock(ConfigManager
::class);
436 $this->container
->conf
438 ->willReturnCallback(function (string $key, $default) {
439 if ($key === 'thumbnails.mode') {
440 return Thumbnailer
::MODE_ALL
;
441 } elseif ($key === 'general.enable_async_metadata') {
449 $this->container
->thumbnailer
= $this->createMock(Thumbnailer
::class);
450 $this->container
->thumbnailer
->expects(static::never())->method('get');
452 $this->container
->bookmarkService
453 ->expects(static::once())
454 ->method('findByHash')
455 ->willReturn((new Bookmark())->setId(2)->setUrl('https://url.tld')->setTitle('Title 1'))
457 $this->container
->bookmarkService
->expects(static::never())->method('set');
458 $this->container
->bookmarkService
->expects(static::never())->method('save');
460 $result = $this->controller
->permalink($request, $response, ['hash' => 'abc']);
462 static::assertSame(200, $result->getStatusCode());
466 * Trigger legacy controller in link list controller: permalink
468 public function testLegacyControllerPermalink(): void
471 $this->container
->environment
['QUERY_STRING'] = $hash;
473 $request = $this->createMock(Request
::class);
474 $response = new Response();
476 $result = $this->controller
->index($request, $response);
478 static::assertSame(302, $result->getStatusCode());
479 static::assertSame('/subfolder/shaare/' . $hash, $result->getHeader('location')[0]);
483 * Trigger legacy controller in link list controller: ?do= query parameter
485 public function testLegacyControllerDoPage(): void
487 $request = $this->createMock(Request
::class);
488 $request->method('getQueryParam')->with('do')->willReturn('picwall');
489 $response = new Response();
491 $result = $this->controller
->index($request, $response);
493 static::assertSame(302, $result->getStatusCode());
494 static::assertSame('/subfolder/picture-wall', $result->getHeader('location')[0]);
498 * Trigger legacy controller in link list controller: ?do= query parameter with unknown legacy route
500 public function testLegacyControllerUnknownDoPage(): void
502 $request = $this->createMock(Request
::class);
503 $request->method('getQueryParam')->with('do')->willReturn('nope');
504 $response = new Response();
506 $result = $this->controller
->index($request, $response);
508 static::assertSame(200, $result->getStatusCode());
509 static::assertSame('linklist', (string) $result->getBody());
513 * Trigger legacy controller in link list controller: other GET route (e.g. ?post)
515 public function testLegacyControllerGetParameter(): void
517 $request = $this->createMock(Request
::class);
518 $request->method('getQueryParams')->willReturn(['post' => $url = 'http://url.tld']);
519 $response = new Response();
521 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
522 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
524 $result = $this->controller
->index($request, $response);
526 static::assertSame(302, $result->getStatusCode());
528 '/subfolder/admin/shaare?post=' . urlencode($url),
529 $result->getHeader('location')[0]