]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/controller/admin/PostBookmarkControllerTest.php
Use multi-level routes for existing controllers instead of 1 level everywhere
[github/shaarli/Shaarli.git] / tests / front / controller / admin / PostBookmarkControllerTest.php
CommitLineData
c22fa57a
A
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\Bookmark;
9use Shaarli\Config\ConfigManager;
10use Shaarli\Front\Exception\WrongTokenException;
11use Shaarli\Http\HttpAccess;
12use Shaarli\Security\SessionManager;
13use Shaarli\Thumbnailer;
14use Slim\Http\Request;
15use Slim\Http\Response;
c22fa57a
A
16
17class PostBookmarkControllerTest extends TestCase
18{
19 use FrontAdminControllerMockHelper;
20
21 /** @var PostBookmarkController */
22 protected $controller;
23
24 public function setUp(): void
25 {
26 $this->createContainer();
27
28 $this->container->httpAccess = $this->createMock(HttpAccess::class);
29 $this->controller = new PostBookmarkController($this->container);
30 }
31
32 /**
33 * Test displaying add link page
34 */
35 public function testAddShaare(): void
36 {
37 $assignedVariables = [];
38 $this->assignTemplateVars($assignedVariables);
39
40 $request = $this->createMock(Request::class);
41 $response = new Response();
42
43 $result = $this->controller->addShaare($request, $response);
44
45 static::assertSame(200, $result->getStatusCode());
46 static::assertSame('addlink', (string) $result->getBody());
47
48 static::assertSame('Shaare a new link - Shaarli', $assignedVariables['pagetitle']);
49 }
50
51 /**
52 * Test displaying bookmark create form
53 * Ensure that every step of the standard workflow works properly.
54 */
55 public function testDisplayCreateFormWithUrl(): void
56 {
57 $this->container->environment = [
58 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc'
59 ];
60
61 $assignedVariables = [];
62 $this->assignTemplateVars($assignedVariables);
63
64 $url = 'http://url.tld/other?part=3&utm_ad=pay#hash';
65 $expectedUrl = str_replace('&utm_ad=pay', '', $url);
66 $remoteTitle = 'Remote Title';
67 $remoteDesc = 'Sometimes the meta description is relevant.';
68 $remoteTags = 'abc def';
69
70 $request = $this->createMock(Request::class);
71 $request->method('getParam')->willReturnCallback(function (string $key) use ($url): ?string {
72 return $key === 'post' ? $url : null;
73 });
74 $response = new Response();
75
76 $this->container->httpAccess
77 ->expects(static::once())
78 ->method('getCurlDownloadCallback')
79 ->willReturnCallback(
80 function (&$charset, &$title, &$description, &$tags) use (
81 $remoteTitle,
82 $remoteDesc,
83 $remoteTags
84 ): callable {
85 return function () use (
86 &$charset,
87 &$title,
88 &$description,
89 &$tags,
90 $remoteTitle,
91 $remoteDesc,
92 $remoteTags
93 ): void {
94 $charset = 'ISO-8859-1';
95 $title = $remoteTitle;
96 $description = $remoteDesc;
97 $tags = $remoteTags;
98 };
99 }
100 )
101 ;
102 $this->container->httpAccess
103 ->expects(static::once())
104 ->method('getHttpResponse')
105 ->with($expectedUrl, 30, 4194304)
106 ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void {
107 $callback();
108 })
109 ;
110
111 $this->container->bookmarkService
112 ->expects(static::once())
113 ->method('bookmarksCountPerTag')
114 ->willReturn($tags = ['tag1' => 2, 'tag2' => 1])
115 ;
116
117 // Make sure that PluginManager hook is triggered
118 $this->container->pluginManager
119 ->expects(static::at(0))
120 ->method('executeHooks')
121 ->willReturnCallback(function (string $hook, array $data) use ($remoteTitle, $remoteDesc): array {
122 static::assertSame('render_editlink', $hook);
123 static::assertSame($remoteTitle, $data['link']['title']);
124 static::assertSame($remoteDesc, $data['link']['description']);
125
126 return $data;
127 })
128 ;
129
130 $result = $this->controller->displayCreateForm($request, $response);
131
132 static::assertSame(200, $result->getStatusCode());
133 static::assertSame('editlink', (string) $result->getBody());
134
135 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
136
137 static::assertSame($expectedUrl, $assignedVariables['link']['url']);
138 static::assertSame($remoteTitle, $assignedVariables['link']['title']);
139 static::assertSame($remoteDesc, $assignedVariables['link']['description']);
140 static::assertSame($remoteTags, $assignedVariables['link']['tags']);
141 static::assertFalse($assignedVariables['link']['private']);
142
143 static::assertTrue($assignedVariables['link_is_new']);
144 static::assertSame($referer, $assignedVariables['http_referer']);
145 static::assertSame($tags, $assignedVariables['tags']);
146 static::assertArrayHasKey('source', $assignedVariables);
147 static::assertArrayHasKey('default_private_links', $assignedVariables);
148 }
149
150 /**
151 * Test displaying bookmark create form
152 * Ensure all available query parameters are handled properly.
153 */
154 public function testDisplayCreateFormWithFullParameters(): void
155 {
156 $assignedVariables = [];
157 $this->assignTemplateVars($assignedVariables);
158
159 $parameters = [
160 'post' => 'http://url.tld/other?part=3&utm_ad=pay#hash',
161 'title' => 'Provided Title',
162 'description' => 'Provided description.',
163 'tags' => 'abc def',
164 'private' => '1',
165 'source' => 'apps',
166 ];
167 $expectedUrl = str_replace('&utm_ad=pay', '', $parameters['post']);
168
169 $request = $this->createMock(Request::class);
170 $request
171 ->method('getParam')
172 ->willReturnCallback(function (string $key) use ($parameters): ?string {
173 return $parameters[$key] ?? null;
174 });
175 $response = new Response();
176
177 $result = $this->controller->displayCreateForm($request, $response);
178
179 static::assertSame(200, $result->getStatusCode());
180 static::assertSame('editlink', (string) $result->getBody());
181
182 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
183
184 static::assertSame($expectedUrl, $assignedVariables['link']['url']);
185 static::assertSame($parameters['title'], $assignedVariables['link']['title']);
186 static::assertSame($parameters['description'], $assignedVariables['link']['description']);
187 static::assertSame($parameters['tags'], $assignedVariables['link']['tags']);
188 static::assertTrue($assignedVariables['link']['private']);
189 static::assertTrue($assignedVariables['link_is_new']);
190 static::assertSame($parameters['source'], $assignedVariables['source']);
191 }
192
193 /**
194 * Test displaying bookmark create form
195 * Without any parameter.
196 */
197 public function testDisplayCreateFormEmpty(): void
198 {
199 $assignedVariables = [];
200 $this->assignTemplateVars($assignedVariables);
201
202 $request = $this->createMock(Request::class);
203 $response = new Response();
204
205 $this->container->httpAccess->expects(static::never())->method('getHttpResponse');
206 $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback');
207
208 $result = $this->controller->displayCreateForm($request, $response);
209
210 static::assertSame(200, $result->getStatusCode());
211 static::assertSame('editlink', (string) $result->getBody());
212 static::assertSame('', $assignedVariables['link']['url']);
213 static::assertSame('Note: ', $assignedVariables['link']['title']);
214 static::assertSame('', $assignedVariables['link']['description']);
215 static::assertSame('', $assignedVariables['link']['tags']);
216 static::assertFalse($assignedVariables['link']['private']);
217 static::assertTrue($assignedVariables['link_is_new']);
218 }
219
220 /**
221 * Test displaying bookmark create form
222 * URL not using HTTP protocol: do not try to retrieve the title
223 */
224 public function testDisplayCreateFormNotHttp(): void
225 {
226 $assignedVariables = [];
227 $this->assignTemplateVars($assignedVariables);
228
229 $url = 'magnet://kubuntu.torrent';
230 $request = $this->createMock(Request::class);
231 $request
232 ->method('getParam')
233 ->willReturnCallback(function (string $key) use ($url): ?string {
234 return $key === 'post' ? $url : null;
235 });
236 $response = new Response();
237
238 $this->container->httpAccess->expects(static::never())->method('getHttpResponse');
239 $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback');
240
241 $result = $this->controller->displayCreateForm($request, $response);
242
243 static::assertSame(200, $result->getStatusCode());
244 static::assertSame('editlink', (string) $result->getBody());
245 static::assertSame($url, $assignedVariables['link']['url']);
246 static::assertTrue($assignedVariables['link_is_new']);
247 }
248
249 /**
250 * Test displaying bookmark create form
251 * When markdown formatter is enabled, the no markdown tag should be added to existing tags.
252 */
253 public function testDisplayCreateFormWithMarkdownEnabled(): void
254 {
255 $assignedVariables = [];
256 $this->assignTemplateVars($assignedVariables);
257
258 $this->container->conf = $this->createMock(ConfigManager::class);
259 $this->container->conf
260 ->expects(static::atLeastOnce())
261 ->method('get')->willReturnCallback(function (string $key): ?string {
262 if ($key === 'formatter') {
263 return 'markdown';
264 }
265
266 return $key;
267 })
268 ;
269
270 $request = $this->createMock(Request::class);
271 $response = new Response();
272
273 $result = $this->controller->displayCreateForm($request, $response);
274
275 static::assertSame(200, $result->getStatusCode());
276 static::assertSame('editlink', (string) $result->getBody());
277 static::assertSame(['nomarkdown' => 1], $assignedVariables['tags']);
278 }
279
280 /**
281 * Test displaying bookmark create form
282 * When an existing URL is submitted, we want to edit the existing link.
283 */
284 public function testDisplayCreateFormWithExistingUrl(): void
285 {
286 $assignedVariables = [];
287 $this->assignTemplateVars($assignedVariables);
288
289 $url = 'http://url.tld/other?part=3&utm_ad=pay#hash';
290 $expectedUrl = str_replace('&utm_ad=pay', '', $url);
291
292 $request = $this->createMock(Request::class);
293 $request
294 ->method('getParam')
295 ->willReturnCallback(function (string $key) use ($url): ?string {
296 return $key === 'post' ? $url : null;
297 });
298 $response = new Response();
299
300 $this->container->httpAccess->expects(static::never())->method('getHttpResponse');
301 $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback');
302
303 $this->container->bookmarkService
304 ->expects(static::once())
305 ->method('findByUrl')
306 ->with($expectedUrl)
307 ->willReturn(
308 (new Bookmark())
309 ->setId($id = 23)
310 ->setUrl($expectedUrl)
311 ->setTitle($title = 'Bookmark Title')
312 ->setDescription($description = 'Bookmark description.')
313 ->setTags($tags = ['abc', 'def'])
314 ->setPrivate(true)
315 ->setCreated($createdAt = new \DateTime('2020-06-10 18:45:44'))
316 )
317 ;
318
319 $result = $this->controller->displayCreateForm($request, $response);
320
321 static::assertSame(200, $result->getStatusCode());
322 static::assertSame('editlink', (string) $result->getBody());
323
324 static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']);
325 static::assertFalse($assignedVariables['link_is_new']);
326
327 static::assertSame($id, $assignedVariables['link']['id']);
328 static::assertSame($expectedUrl, $assignedVariables['link']['url']);
329 static::assertSame($title, $assignedVariables['link']['title']);
330 static::assertSame($description, $assignedVariables['link']['description']);
331 static::assertSame(implode(' ', $tags), $assignedVariables['link']['tags']);
332 static::assertTrue($assignedVariables['link']['private']);
333 static::assertSame($createdAt, $assignedVariables['link']['created']);
334 }
335
336 /**
337 * Test displaying bookmark edit form
338 * When an existing ID is provided, ensure that default workflow works properly.
339 */
340 public function testDisplayEditFormDefault(): void
341 {
342 $assignedVariables = [];
343 $this->assignTemplateVars($assignedVariables);
344
345 $id = 11;
346
347 $request = $this->createMock(Request::class);
348 $response = new Response();
349
350 $this->container->httpAccess->expects(static::never())->method('getHttpResponse');
351 $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback');
352
353 $this->container->bookmarkService
354 ->expects(static::once())
355 ->method('get')
356 ->with($id)
357 ->willReturn(
358 (new Bookmark())
359 ->setId($id)
360 ->setUrl($url = 'http://domain.tld')
361 ->setTitle($title = 'Bookmark Title')
362 ->setDescription($description = 'Bookmark description.')
363 ->setTags($tags = ['abc', 'def'])
364 ->setPrivate(true)
365 ->setCreated($createdAt = new \DateTime('2020-06-10 18:45:44'))
366 )
367 ;
368
369 $result = $this->controller->displayEditForm($request, $response, ['id' => (string) $id]);
370
371 static::assertSame(200, $result->getStatusCode());
372 static::assertSame('editlink', (string) $result->getBody());
373
374 static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']);
375 static::assertFalse($assignedVariables['link_is_new']);
376
377 static::assertSame($id, $assignedVariables['link']['id']);
378 static::assertSame($url, $assignedVariables['link']['url']);
379 static::assertSame($title, $assignedVariables['link']['title']);
380 static::assertSame($description, $assignedVariables['link']['description']);
381 static::assertSame(implode(' ', $tags), $assignedVariables['link']['tags']);
382 static::assertTrue($assignedVariables['link']['private']);
383 static::assertSame($createdAt, $assignedVariables['link']['created']);
384 }
385
386 /**
387 * Test save a new bookmark
388 */
389 public function testSaveBookmark(): void
390 {
391 $id = 21;
392 $parameters = [
393 'lf_url' => 'http://url.tld/other?part=3#hash',
394 'lf_title' => 'Provided Title',
395 'lf_description' => 'Provided description.',
396 'lf_tags' => 'abc def',
397 'lf_private' => '1',
9c75f877 398 'returnurl' => 'http://shaarli.tld/subfolder/admin/add-shaare'
c22fa57a
A
399 ];
400
401 $request = $this->createMock(Request::class);
402 $request
403 ->method('getParam')
404 ->willReturnCallback(function (string $key) use ($parameters): ?string {
405 return $parameters[$key] ?? null;
406 })
407 ;
c22fa57a
A
408 $response = new Response();
409
410 $checkBookmark = function (Bookmark $bookmark) use ($parameters) {
411 static::assertSame($parameters['lf_url'], $bookmark->getUrl());
412 static::assertSame($parameters['lf_title'], $bookmark->getTitle());
413 static::assertSame($parameters['lf_description'], $bookmark->getDescription());
414 static::assertSame($parameters['lf_tags'], $bookmark->getTagsString());
415 static::assertTrue($bookmark->isPrivate());
416 };
417
418 $this->container->bookmarkService
419 ->expects(static::once())
420 ->method('addOrSet')
421 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void {
422 static::assertFalse($save);
423
424 $checkBookmark($bookmark);
425
426 $bookmark->setId($id);
427 })
428 ;
429 $this->container->bookmarkService
430 ->expects(static::once())
431 ->method('set')
432 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void {
433 static::assertTrue($save);
434
435 $checkBookmark($bookmark);
436
437 static::assertSame($id, $bookmark->getId());
438 })
439 ;
440
441 // Make sure that PluginManager hook is triggered
442 $this->container->pluginManager
443 ->expects(static::at(0))
444 ->method('executeHooks')
445 ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
446 static::assertSame('save_link', $hook);
447
448 static::assertSame($id, $data['id']);
449 static::assertSame($parameters['lf_url'], $data['url']);
450 static::assertSame($parameters['lf_title'], $data['title']);
451 static::assertSame($parameters['lf_description'], $data['description']);
452 static::assertSame($parameters['lf_tags'], $data['tags']);
453 static::assertTrue($data['private']);
454
455 return $data;
456 })
457 ;
458
459 $result = $this->controller->save($request, $response);
460
461 static::assertSame(302, $result->getStatusCode());
9c75f877 462 static::assertRegExp('@/subfolder/#[\w\-]{6}@', $result->getHeader('location')[0]);
c22fa57a
A
463 }
464
465
466 /**
467 * Test save an existing bookmark
468 */
469 public function testSaveExistingBookmark(): void
470 {
471 $id = 21;
472 $parameters = [
473 'lf_id' => (string) $id,
474 'lf_url' => 'http://url.tld/other?part=3#hash',
475 'lf_title' => 'Provided Title',
476 'lf_description' => 'Provided description.',
477 'lf_tags' => 'abc def',
478 'lf_private' => '1',
479 'returnurl' => 'http://shaarli.tld/subfolder/?page=2'
480 ];
481
482 $request = $this->createMock(Request::class);
483 $request
484 ->method('getParam')
485 ->willReturnCallback(function (string $key) use ($parameters): ?string {
486 return $parameters[$key] ?? null;
487 })
488 ;
c22fa57a
A
489 $response = new Response();
490
491 $checkBookmark = function (Bookmark $bookmark) use ($parameters, $id) {
492 static::assertSame($id, $bookmark->getId());
493 static::assertSame($parameters['lf_url'], $bookmark->getUrl());
494 static::assertSame($parameters['lf_title'], $bookmark->getTitle());
495 static::assertSame($parameters['lf_description'], $bookmark->getDescription());
496 static::assertSame($parameters['lf_tags'], $bookmark->getTagsString());
497 static::assertTrue($bookmark->isPrivate());
498 };
499
500 $this->container->bookmarkService->expects(static::atLeastOnce())->method('exists')->willReturn(true);
501 $this->container->bookmarkService
502 ->expects(static::once())
503 ->method('get')
504 ->willReturn((new Bookmark())->setId($id)->setUrl('http://other.url'))
505 ;
506 $this->container->bookmarkService
507 ->expects(static::once())
508 ->method('addOrSet')
509 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void {
510 static::assertFalse($save);
511
512 $checkBookmark($bookmark);
513 })
514 ;
515 $this->container->bookmarkService
516 ->expects(static::once())
517 ->method('set')
518 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void {
519 static::assertTrue($save);
520
521 $checkBookmark($bookmark);
522
523 static::assertSame($id, $bookmark->getId());
524 })
525 ;
526
527 // Make sure that PluginManager hook is triggered
528 $this->container->pluginManager
529 ->expects(static::at(0))
530 ->method('executeHooks')
531 ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
532 static::assertSame('save_link', $hook);
533
534 static::assertSame($id, $data['id']);
535 static::assertSame($parameters['lf_url'], $data['url']);
536 static::assertSame($parameters['lf_title'], $data['title']);
537 static::assertSame($parameters['lf_description'], $data['description']);
538 static::assertSame($parameters['lf_tags'], $data['tags']);
539 static::assertTrue($data['private']);
540
541 return $data;
542 })
543 ;
544
545 $result = $this->controller->save($request, $response);
546
547 static::assertSame(302, $result->getStatusCode());
9c75f877 548 static::assertRegExp('@/subfolder/\?page=2#[\w\-]{6}@', $result->getHeader('location')[0]);
c22fa57a
A
549 }
550
551 /**
552 * Test save a bookmark - try to retrieve the thumbnail
553 */
554 public function testSaveBookmarkWithThumbnail(): void
555 {
556 $parameters = ['lf_url' => 'http://url.tld/other?part=3#hash'];
557
558 $request = $this->createMock(Request::class);
559 $request
560 ->method('getParam')
561 ->willReturnCallback(function (string $key) use ($parameters): ?string {
562 return $parameters[$key] ?? null;
563 })
564 ;
c22fa57a
A
565 $response = new Response();
566
567 $this->container->conf = $this->createMock(ConfigManager::class);
568 $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) {
569 return $key === 'thumbnails.mode' ? Thumbnailer::MODE_ALL : $default;
570 });
571
572 $this->container->thumbnailer = $this->createMock(Thumbnailer::class);
573 $this->container->thumbnailer
574 ->expects(static::once())
575 ->method('get')
576 ->with($parameters['lf_url'])
577 ->willReturn($thumb = 'http://thumb.url')
578 ;
579
580 $this->container->bookmarkService
581 ->expects(static::once())
582 ->method('addOrSet')
583 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($thumb): void {
584 static::assertSame($thumb, $bookmark->getThumbnail());
585 })
586 ;
587
588 $result = $this->controller->save($request, $response);
589
590 static::assertSame(302, $result->getStatusCode());
591 }
592
593 /**
594 * Change the password with a wrong existing password
595 */
596 public function testSaveBookmarkFromBookmarklet(): void
597 {
598 $parameters = ['source' => 'bookmarklet'];
599
600 $request = $this->createMock(Request::class);
601 $request
602 ->method('getParam')
603 ->willReturnCallback(function (string $key) use ($parameters): ?string {
604 return $parameters[$key] ?? null;
605 })
606 ;
607 $response = new Response();
608
609 $result = $this->controller->save($request, $response);
610
611 static::assertSame(200, $result->getStatusCode());
612 static::assertSame('<script>self.close();</script>', (string) $result->getBody());
613 }
614
615 /**
616 * Change the password with a wrong existing password
617 */
618 public function testSaveBookmarkWrongToken(): void
619 {
620 $this->container->sessionManager = $this->createMock(SessionManager::class);
621 $this->container->sessionManager->method('checkToken')->willReturn(false);
622
623 $this->container->bookmarkService->expects(static::never())->method('addOrSet');
624 $this->container->bookmarkService->expects(static::never())->method('set');
625
626 $request = $this->createMock(Request::class);
627 $response = new Response();
628
629 $this->expectException(WrongTokenException::class);
630
631 $this->controller->save($request, $response);
632 }
633}