]>
Commit | Line | Data |
---|---|---|
1a8ac737 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Visitor; | |
6 | ||
1a8ac737 A |
7 | use Shaarli\Bookmark\Bookmark; |
8 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | |
9 | use Shaarli\Config\ConfigManager; | |
10 | use Shaarli\Security\LoginManager; | |
a5a9cf23 | 11 | use Shaarli\TestCase; |
1a8ac737 A |
12 | use Shaarli\Thumbnailer; |
13 | use Slim\Http\Request; | |
14 | use Slim\Http\Response; | |
15 | ||
16 | class BookmarkListControllerTest extends TestCase | |
17 | { | |
18 | use FrontControllerMockHelper; | |
19 | ||
20 | /** @var BookmarkListController */ | |
21 | protected $controller; | |
22 | ||
23 | public function setUp(): void | |
24 | { | |
25 | $this->createContainer(); | |
26 | ||
27 | $this->controller = new BookmarkListController($this->container); | |
28 | } | |
29 | ||
30 | /** | |
31 | * Test rendering list of bookmarks with default parameters (first page). | |
32 | */ | |
33 | public function testIndexDefaultFirstPage(): void | |
34 | { | |
35 | $assignedVariables = []; | |
36 | $this->assignTemplateVars($assignedVariables); | |
37 | ||
38 | $request = $this->createMock(Request::class); | |
39 | $response = new Response(); | |
40 | ||
41 | $this->container->bookmarkService | |
42 | ->expects(static::once()) | |
43 | ->method('search') | |
44 | ->with( | |
45 | ['searchtags' => '', 'searchterm' => ''], | |
46 | null, | |
47 | false, | |
48 | false | |
49 | ) | |
50 | ->willReturn([ | |
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'), | |
54 | ] | |
55 | ); | |
56 | ||
57 | $this->container->sessionManager | |
58 | ->method('getSessionParameter') | |
59 | ->willReturnCallback(function (string $parameter, $default = null) { | |
60 | if ('LINKS_PER_PAGE' === $parameter) { | |
61 | return 2; | |
62 | } | |
63 | ||
64 | return $default; | |
65 | }) | |
66 | ; | |
67 | ||
68 | $result = $this->controller->index($request, $response); | |
69 | ||
70 | static::assertSame(200, $result->getStatusCode()); | |
71 | static::assertSame('linklist', (string) $result->getBody()); | |
72 | ||
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']); | |
83 | ||
84 | $link = $assignedVariables['links'][0]; | |
85 | ||
86 | static::assertSame(1, $link['id']); | |
87 | static::assertSame('http://url1.tld', $link['url']); | |
88 | static::assertSame('Title 1', $link['title']); | |
89 | ||
90 | $link = $assignedVariables['links'][1]; | |
91 | ||
92 | static::assertSame(2, $link['id']); | |
93 | static::assertSame('http://url2.tld', $link['url']); | |
94 | static::assertSame('Title 2', $link['title']); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Test rendering list of bookmarks with default parameters (second page). | |
99 | */ | |
100 | public function testIndexDefaultSecondPage(): void | |
101 | { | |
102 | $assignedVariables = []; | |
103 | $this->assignTemplateVars($assignedVariables); | |
104 | ||
105 | $request = $this->createMock(Request::class); | |
106 | $request->method('getParam')->willReturnCallback(function (string $key) { | |
107 | if ('page' === $key) { | |
108 | return '2'; | |
109 | } | |
110 | ||
111 | return null; | |
112 | }); | |
113 | $response = new Response(); | |
114 | ||
115 | $this->container->bookmarkService | |
116 | ->expects(static::once()) | |
117 | ->method('search') | |
118 | ->with( | |
119 | ['searchtags' => '', 'searchterm' => ''], | |
120 | null, | |
121 | false, | |
122 | false | |
123 | ) | |
124 | ->willReturn([ | |
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'), | |
128 | ]) | |
129 | ; | |
130 | ||
131 | $this->container->sessionManager | |
132 | ->method('getSessionParameter') | |
133 | ->willReturnCallback(function (string $parameter, $default = null) { | |
134 | if ('LINKS_PER_PAGE' === $parameter) { | |
135 | return 2; | |
136 | } | |
137 | ||
138 | return $default; | |
139 | }) | |
140 | ; | |
141 | ||
142 | $result = $this->controller->index($request, $response); | |
143 | ||
144 | static::assertSame(200, $result->getStatusCode()); | |
145 | static::assertSame('linklist', (string) $result->getBody()); | |
146 | ||
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']); | |
157 | ||
158 | $link = $assignedVariables['links'][2]; | |
159 | ||
160 | static::assertSame(3, $link['id']); | |
161 | static::assertSame('http://url3.tld', $link['url']); | |
162 | static::assertSame('Title 3', $link['title']); | |
163 | } | |
164 | ||
165 | /** | |
166 | * Test rendering list of bookmarks with filters. | |
167 | */ | |
168 | public function testIndexDefaultWithFilters(): void | |
169 | { | |
170 | $assignedVariables = []; | |
171 | $this->assignTemplateVars($assignedVariables); | |
172 | ||
173 | $request = $this->createMock(Request::class); | |
174 | $request->method('getParam')->willReturnCallback(function (string $key) { | |
175 | if ('searchtags' === $key) { | |
176 | return 'abc def'; | |
177 | } | |
178 | if ('searchterm' === $key) { | |
179 | return 'ghi jkl'; | |
180 | } | |
181 | ||
182 | return null; | |
183 | }); | |
184 | $response = new Response(); | |
185 | ||
186 | $this->container->sessionManager | |
187 | ->method('getSessionParameter') | |
188 | ->willReturnCallback(function (string $key, $default) { | |
189 | if ('LINKS_PER_PAGE' === $key) { | |
190 | return 2; | |
191 | } | |
192 | if ('visibility' === $key) { | |
193 | return 'private'; | |
194 | } | |
195 | if ('untaggedonly' === $key) { | |
196 | return true; | |
197 | } | |
198 | ||
199 | return $default; | |
200 | }) | |
201 | ; | |
202 | ||
203 | $this->container->bookmarkService | |
204 | ->expects(static::once()) | |
205 | ->method('search') | |
206 | ->with( | |
207 | ['searchtags' => 'abc def', 'searchterm' => 'ghi jkl'], | |
208 | 'private', | |
209 | false, | |
210 | true | |
211 | ) | |
212 | ->willReturn([ | |
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'), | |
216 | ]) | |
217 | ; | |
218 | ||
219 | $result = $this->controller->index($request, $response); | |
220 | ||
221 | static::assertSame(200, $result->getStatusCode()); | |
222 | static::assertSame('linklist', (string) $result->getBody()); | |
223 | ||
224 | static::assertSame('Search: ghi jkl [abc] [def] - Shaarli', $assignedVariables['pagetitle']); | |
225 | static::assertSame('?page=2&searchterm=ghi+jkl&searchtags=abc+def', $assignedVariables['previous_page_url']); | |
226 | } | |
227 | ||
228 | /** | |
229 | * Test displaying a permalink with valid parameters | |
230 | */ | |
231 | public function testPermalinkValid(): void | |
232 | { | |
233 | $hash = 'abcdef'; | |
234 | ||
235 | $assignedVariables = []; | |
236 | $this->assignTemplateVars($assignedVariables); | |
237 | ||
238 | $request = $this->createMock(Request::class); | |
239 | $response = new Response(); | |
240 | ||
241 | $this->container->bookmarkService | |
242 | ->expects(static::once()) | |
243 | ->method('findByHash') | |
244 | ->with($hash) | |
245 | ->willReturn((new Bookmark())->setId(123)->setTitle('Title 1')->setUrl('http://url1.tld')) | |
246 | ; | |
247 | ||
248 | $result = $this->controller->permalink($request, $response, ['hash' => $hash]); | |
249 | ||
250 | static::assertSame(200, $result->getStatusCode()); | |
251 | static::assertSame('linklist', (string) $result->getBody()); | |
252 | ||
253 | static::assertSame('Title 1 - Shaarli', $assignedVariables['pagetitle']); | |
254 | static::assertCount(1, $assignedVariables['links']); | |
255 | ||
256 | $link = $assignedVariables['links'][0]; | |
257 | ||
258 | static::assertSame(123, $link['id']); | |
259 | static::assertSame('http://url1.tld', $link['url']); | |
260 | static::assertSame('Title 1', $link['title']); | |
261 | } | |
262 | ||
263 | /** | |
264 | * Test displaying a permalink with an unknown small hash : renders a 404 template error | |
265 | */ | |
266 | public function testPermalinkNotFound(): void | |
267 | { | |
268 | $hash = 'abcdef'; | |
269 | ||
270 | $assignedVariables = []; | |
271 | $this->assignTemplateVars($assignedVariables); | |
272 | ||
273 | $request = $this->createMock(Request::class); | |
274 | $response = new Response(); | |
275 | ||
276 | $this->container->bookmarkService | |
277 | ->expects(static::once()) | |
278 | ->method('findByHash') | |
279 | ->with($hash) | |
280 | ->willThrowException(new BookmarkNotFoundException()) | |
281 | ; | |
282 | ||
283 | $result = $this->controller->permalink($request, $response, ['hash' => $hash]); | |
284 | ||
285 | static::assertSame(200, $result->getStatusCode()); | |
286 | static::assertSame('404', (string) $result->getBody()); | |
287 | ||
288 | static::assertSame( | |
289 | 'The link you are trying to reach does not exist or has been deleted.', | |
290 | $assignedVariables['error_message'] | |
291 | ); | |
292 | } | |
293 | ||
294 | /** | |
295 | * Test getting link list with thumbnail updates. | |
296 | * -> 2 thumbnails update, only 1 datastore write | |
297 | */ | |
298 | public function testThumbnailUpdateFromLinkList(): void | |
299 | { | |
300 | $request = $this->createMock(Request::class); | |
301 | $response = new Response(); | |
302 | ||
303 | $this->container->loginManager = $this->createMock(LoginManager::class); | |
304 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | |
305 | ||
306 | $this->container->conf = $this->createMock(ConfigManager::class); | |
307 | $this->container->conf | |
308 | ->method('get') | |
309 | ->willReturnCallback(function (string $key, $default) { | |
21e72da9 A |
310 | if ($key === 'thumbnails.mode') { |
311 | return Thumbnailer::MODE_ALL; | |
312 | } elseif ($key === 'general.enable_async_metadata') { | |
313 | return false; | |
314 | } | |
315 | ||
316 | return $default; | |
1a8ac737 A |
317 | }) |
318 | ; | |
319 | ||
320 | $this->container->thumbnailer = $this->createMock(Thumbnailer::class); | |
321 | $this->container->thumbnailer | |
322 | ->expects(static::exactly(2)) | |
323 | ->method('get') | |
324 | ->withConsecutive(['https://url2.tld'], ['https://url4.tld']) | |
325 | ; | |
326 | ||
327 | $this->container->bookmarkService | |
328 | ->expects(static::once()) | |
329 | ->method('search') | |
330 | ->willReturn([ | |
331 | (new Bookmark())->setId(1)->setUrl('https://url1.tld')->setTitle('Title 1')->setThumbnail(false), | |
332 | $b1 = (new Bookmark())->setId(2)->setUrl('https://url2.tld')->setTitle('Title 2'), | |
333 | (new Bookmark())->setId(3)->setUrl('https://url3.tld')->setTitle('Title 3')->setThumbnail(false), | |
334 | $b2 = (new Bookmark())->setId(2)->setUrl('https://url4.tld')->setTitle('Title 4'), | |
335 | (new Bookmark())->setId(2)->setUrl('ftp://url5.tld', ['ftp'])->setTitle('Title 5'), | |
336 | ]) | |
337 | ; | |
338 | $this->container->bookmarkService | |
339 | ->expects(static::exactly(2)) | |
340 | ->method('set') | |
341 | ->withConsecutive([$b1, false], [$b2, false]) | |
342 | ; | |
343 | $this->container->bookmarkService->expects(static::once())->method('save'); | |
344 | ||
345 | $result = $this->controller->index($request, $response); | |
346 | ||
347 | static::assertSame(200, $result->getStatusCode()); | |
348 | static::assertSame('linklist', (string) $result->getBody()); | |
349 | } | |
350 | ||
351 | /** | |
352 | * Test getting a permalink with thumbnail update. | |
353 | */ | |
354 | public function testThumbnailUpdateFromPermalink(): void | |
355 | { | |
356 | $request = $this->createMock(Request::class); | |
357 | $response = new Response(); | |
358 | ||
359 | $this->container->loginManager = $this->createMock(LoginManager::class); | |
360 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | |
361 | ||
362 | $this->container->conf = $this->createMock(ConfigManager::class); | |
363 | $this->container->conf | |
364 | ->method('get') | |
365 | ->willReturnCallback(function (string $key, $default) { | |
21e72da9 A |
366 | if ($key === 'thumbnails.mode') { |
367 | return Thumbnailer::MODE_ALL; | |
368 | } elseif ($key === 'general.enable_async_metadata') { | |
369 | return false; | |
370 | } | |
371 | ||
372 | return $default; | |
1a8ac737 A |
373 | }) |
374 | ; | |
375 | ||
376 | $this->container->thumbnailer = $this->createMock(Thumbnailer::class); | |
377 | $this->container->thumbnailer->expects(static::once())->method('get')->withConsecutive(['https://url.tld']); | |
378 | ||
379 | $this->container->bookmarkService | |
380 | ->expects(static::once()) | |
381 | ->method('findByHash') | |
382 | ->willReturn($bookmark = (new Bookmark())->setId(2)->setUrl('https://url.tld')->setTitle('Title 1')) | |
383 | ; | |
384 | $this->container->bookmarkService->expects(static::once())->method('set')->with($bookmark, true); | |
385 | $this->container->bookmarkService->expects(static::never())->method('save'); | |
386 | ||
387 | $result = $this->controller->permalink($request, $response, ['hash' => 'abc']); | |
388 | ||
389 | static::assertSame(200, $result->getStatusCode()); | |
390 | static::assertSame('linklist', (string) $result->getBody()); | |
391 | } | |
392 | ||
21e72da9 A |
393 | /** |
394 | * Test getting a permalink with thumbnail update with async setting: no update should run. | |
395 | */ | |
396 | public function testThumbnailUpdateFromPermalinkAsync(): void | |
397 | { | |
398 | $request = $this->createMock(Request::class); | |
399 | $response = new Response(); | |
400 | ||
401 | $this->container->loginManager = $this->createMock(LoginManager::class); | |
402 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | |
403 | ||
404 | $this->container->conf = $this->createMock(ConfigManager::class); | |
405 | $this->container->conf | |
406 | ->method('get') | |
407 | ->willReturnCallback(function (string $key, $default) { | |
408 | if ($key === 'thumbnails.mode') { | |
409 | return Thumbnailer::MODE_ALL; | |
410 | } elseif ($key === 'general.enable_async_metadata') { | |
411 | return true; | |
412 | } | |
413 | ||
414 | return $default; | |
415 | }) | |
416 | ; | |
417 | ||
418 | $this->container->thumbnailer = $this->createMock(Thumbnailer::class); | |
419 | $this->container->thumbnailer->expects(static::never())->method('get'); | |
420 | ||
421 | $this->container->bookmarkService | |
422 | ->expects(static::once()) | |
423 | ->method('findByHash') | |
424 | ->willReturn((new Bookmark())->setId(2)->setUrl('https://url.tld')->setTitle('Title 1')) | |
425 | ; | |
426 | $this->container->bookmarkService->expects(static::never())->method('set'); | |
427 | $this->container->bookmarkService->expects(static::never())->method('save'); | |
428 | ||
429 | $result = $this->controller->permalink($request, $response, ['hash' => 'abc']); | |
430 | ||
431 | static::assertSame(200, $result->getStatusCode()); | |
432 | } | |
433 | ||
1a8ac737 A |
434 | /** |
435 | * Trigger legacy controller in link list controller: permalink | |
436 | */ | |
437 | public function testLegacyControllerPermalink(): void | |
438 | { | |
439 | $hash = 'abcdef'; | |
440 | $this->container->environment['QUERY_STRING'] = $hash; | |
441 | ||
442 | $request = $this->createMock(Request::class); | |
443 | $response = new Response(); | |
444 | ||
445 | $result = $this->controller->index($request, $response); | |
446 | ||
447 | static::assertSame(302, $result->getStatusCode()); | |
448 | static::assertSame('/subfolder/shaare/' . $hash, $result->getHeader('location')[0]); | |
449 | } | |
450 | ||
451 | /** | |
452 | * Trigger legacy controller in link list controller: ?do= query parameter | |
453 | */ | |
454 | public function testLegacyControllerDoPage(): void | |
455 | { | |
456 | $request = $this->createMock(Request::class); | |
457 | $request->method('getQueryParam')->with('do')->willReturn('picwall'); | |
458 | $response = new Response(); | |
459 | ||
460 | $result = $this->controller->index($request, $response); | |
461 | ||
462 | static::assertSame(302, $result->getStatusCode()); | |
463 | static::assertSame('/subfolder/picture-wall', $result->getHeader('location')[0]); | |
464 | } | |
465 | ||
466 | /** | |
467 | * Trigger legacy controller in link list controller: ?do= query parameter with unknown legacy route | |
468 | */ | |
469 | public function testLegacyControllerUnknownDoPage(): void | |
470 | { | |
471 | $request = $this->createMock(Request::class); | |
472 | $request->method('getQueryParam')->with('do')->willReturn('nope'); | |
473 | $response = new Response(); | |
474 | ||
475 | $result = $this->controller->index($request, $response); | |
476 | ||
477 | static::assertSame(200, $result->getStatusCode()); | |
478 | static::assertSame('linklist', (string) $result->getBody()); | |
479 | } | |
480 | ||
481 | /** | |
482 | * Trigger legacy controller in link list controller: other GET route (e.g. ?post) | |
483 | */ | |
484 | public function testLegacyControllerGetParameter(): void | |
485 | { | |
486 | $request = $this->createMock(Request::class); | |
487 | $request->method('getQueryParams')->willReturn(['post' => $url = 'http://url.tld']); | |
488 | $response = new Response(); | |
489 | ||
490 | $this->container->loginManager = $this->createMock(LoginManager::class); | |
491 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | |
492 | ||
493 | $result = $this->controller->index($request, $response); | |
494 | ||
495 | static::assertSame(302, $result->getStatusCode()); | |
496 | static::assertSame( | |
497 | '/subfolder/admin/shaare?post=' . urlencode($url), | |
498 | $result->getHeader('location')[0] | |
499 | ); | |
500 | } | |
501 | } |