3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Bookmark\Bookmark
;
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Front\Exception\WrongTokenException
;
11 use Shaarli\Http\HttpAccess
;
12 use Shaarli\Security\SessionManager
;
13 use Shaarli\Thumbnailer
;
14 use Slim\Http\Request
;
15 use Slim\Http\Response
;
17 class PostBookmarkControllerTest
extends TestCase
19 use FrontAdminControllerMockHelper
;
21 /** @var PostBookmarkController */
22 protected $controller;
24 public function setUp(): void
26 $this->createContainer();
28 $this->container
->httpAccess
= $this->createMock(HttpAccess
::class);
29 $this->controller
= new PostBookmarkController($this->container
);
33 * Test displaying add link page
35 public function testAddShaare(): void
37 $assignedVariables = [];
38 $this->assignTemplateVars($assignedVariables);
40 $request = $this->createMock(Request
::class);
41 $response = new Response();
43 $result = $this->controller
->addShaare($request, $response);
45 static::assertSame(200, $result->getStatusCode());
46 static::assertSame('addlink', (string) $result->getBody());
48 static::assertSame('Shaare a new link - Shaarli', $assignedVariables['pagetitle']);
52 * Test displaying bookmark create form
53 * Ensure that every step of the standard workflow works properly.
55 public function testDisplayCreateFormWithUrl(): void
57 $this->container
->environment
= [
58 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc'
61 $assignedVariables = [];
62 $this->assignTemplateVars($assignedVariables);
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';
70 $request = $this->createMock(Request
::class);
71 $request->method('getParam')->willReturnCallback(function (string $key) use ($url): ?string {
72 return $key === 'post' ? $url : null;
74 $response = new Response();
76 $this->container
->httpAccess
77 ->expects(static::once())
78 ->method('getCurlDownloadCallback')
80 function (&$charset, &$title, &$description, &$tags) use (
85 return function () use (
94 $charset = 'ISO-8859-1';
95 $title = $remoteTitle;
96 $description = $remoteDesc;
102 $this->container
->httpAccess
103 ->expects(static::once())
104 ->method('getHttpResponse')
105 ->with($expectedUrl, 30, 4194304)
106 ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void {
111 $this->container
->bookmarkService
112 ->expects(static::once())
113 ->method('bookmarksCountPerTag')
114 ->willReturn($tags = ['tag1' => 2, 'tag2' => 1])
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']);
130 $result = $this->controller
->displayCreateForm($request, $response);
132 static::assertSame(200, $result->getStatusCode());
133 static::assertSame('editlink', (string) $result->getBody());
135 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
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']);
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);
151 * Test displaying bookmark create form
152 * Ensure all available query parameters are handled properly.
154 public function testDisplayCreateFormWithFullParameters(): void
156 $assignedVariables = [];
157 $this->assignTemplateVars($assignedVariables);
160 'post' => 'http://url.tld/other?part=3&utm_ad=pay#hash',
161 'title' => 'Provided Title',
162 'description' => 'Provided description.',
167 $expectedUrl = str_replace('&utm_ad=pay', '', $parameters['post']);
169 $request = $this->createMock(Request
::class);
172 ->willReturnCallback(function (string $key) use ($parameters): ?string {
173 return $parameters[$key] ?? null;
175 $response = new Response();
177 $result = $this->controller
->displayCreateForm($request, $response);
179 static::assertSame(200, $result->getStatusCode());
180 static::assertSame('editlink', (string) $result->getBody());
182 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
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']);
194 * Test displaying bookmark create form
195 * Without any parameter.
197 public function testDisplayCreateFormEmpty(): void
199 $assignedVariables = [];
200 $this->assignTemplateVars($assignedVariables);
202 $request = $this->createMock(Request
::class);
203 $response = new Response();
205 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
206 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
208 $result = $this->controller
->displayCreateForm($request, $response);
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']);
221 * Test displaying bookmark create form
222 * URL not using HTTP protocol: do not try to retrieve the title
224 public function testDisplayCreateFormNotHttp(): void
226 $assignedVariables = [];
227 $this->assignTemplateVars($assignedVariables);
229 $url = 'magnet://kubuntu.torrent';
230 $request = $this->createMock(Request
::class);
233 ->willReturnCallback(function (string $key) use ($url): ?string {
234 return $key === 'post' ? $url : null;
236 $response = new Response();
238 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
239 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
241 $result = $this->controller
->displayCreateForm($request, $response);
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']);
250 * Test displaying bookmark create form
251 * When markdown formatter is enabled, the no markdown tag should be added to existing tags.
253 public function testDisplayCreateFormWithMarkdownEnabled(): void
255 $assignedVariables = [];
256 $this->assignTemplateVars($assignedVariables);
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') {
270 $request = $this->createMock(Request
::class);
271 $response = new Response();
273 $result = $this->controller
->displayCreateForm($request, $response);
275 static::assertSame(200, $result->getStatusCode());
276 static::assertSame('editlink', (string) $result->getBody());
277 static::assertSame(['nomarkdown' => 1], $assignedVariables['tags']);
281 * Test displaying bookmark create form
282 * When an existing URL is submitted, we want to edit the existing link.
284 public function testDisplayCreateFormWithExistingUrl(): void
286 $assignedVariables = [];
287 $this->assignTemplateVars($assignedVariables);
289 $url = 'http://url.tld/other?part=3&utm_ad=pay#hash';
290 $expectedUrl = str_replace('&utm_ad=pay', '', $url);
292 $request = $this->createMock(Request
::class);
295 ->willReturnCallback(function (string $key) use ($url): ?string {
296 return $key === 'post' ? $url : null;
298 $response = new Response();
300 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
301 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
303 $this->container
->bookmarkService
304 ->expects(static::once())
305 ->method('findByUrl')
310 ->setUrl($expectedUrl)
311 ->setTitle($title = 'Bookmark Title')
312 ->setDescription($description = 'Bookmark description.')
313 ->setTags($tags = ['abc', 'def'])
315 ->setCreated($createdAt = new \
DateTime('2020-06-10 18:45:44'))
319 $result = $this->controller
->displayCreateForm($request, $response);
321 static::assertSame(200, $result->getStatusCode());
322 static::assertSame('editlink', (string) $result->getBody());
324 static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']);
325 static::assertFalse($assignedVariables['link_is_new']);
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']);
337 * Test displaying bookmark edit form
338 * When an existing ID is provided, ensure that default workflow works properly.
340 public function testDisplayEditFormDefault(): void
342 $assignedVariables = [];
343 $this->assignTemplateVars($assignedVariables);
347 $request = $this->createMock(Request
::class);
348 $response = new Response();
350 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
351 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
353 $this->container
->bookmarkService
354 ->expects(static::once())
360 ->setUrl($url = 'http://domain.tld')
361 ->setTitle($title = 'Bookmark Title')
362 ->setDescription($description = 'Bookmark description.')
363 ->setTags($tags = ['abc', 'def'])
365 ->setCreated($createdAt = new \
DateTime('2020-06-10 18:45:44'))
369 $result = $this->controller
->displayEditForm($request, $response, ['id' => (string) $id]);
371 static::assertSame(200, $result->getStatusCode());
372 static::assertSame('editlink', (string) $result->getBody());
374 static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']);
375 static::assertFalse($assignedVariables['link_is_new']);
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']);
387 * Test save a new bookmark
389 public function testSaveBookmark(): void
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',
398 'returnurl' => 'http://shaarli.tld/subfolder/add-shaare'
401 $request = $this->createMock(Request
::class);
404 ->willReturnCallback(function (string $key) use ($parameters): ?string {
405 return $parameters[$key] ?? null;
408 $response = new Response();
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());
418 $this->container
->bookmarkService
419 ->expects(static::once())
421 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($checkBookmark, $id): void {
422 static::assertFalse($save);
424 $checkBookmark($bookmark);
426 $bookmark->setId($id);
429 $this->container
->bookmarkService
430 ->expects(static::once())
432 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($checkBookmark, $id): void {
433 static::assertTrue($save);
435 $checkBookmark($bookmark);
437 static::assertSame($id, $bookmark->getId());
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);
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']);
459 $result = $this->controller
->save($request, $response);
461 static::assertSame(302, $result->getStatusCode());
462 static::assertRegExp('@/subfolder/#\w{6}@', $result->getHeader('location')[0]);
467 * Test save an existing bookmark
469 public function testSaveExistingBookmark(): void
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',
479 'returnurl' => 'http://shaarli.tld/subfolder/?page=2'
482 $request = $this->createMock(Request
::class);
485 ->willReturnCallback(function (string $key) use ($parameters): ?string {
486 return $parameters[$key] ?? null;
489 $response = new Response();
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());
500 $this->container
->bookmarkService
->expects(static::atLeastOnce())->method('exists')->willReturn(true);
501 $this->container
->bookmarkService
502 ->expects(static::once())
504 ->willReturn((new Bookmark())->setId($id)->setUrl('http://other.url'))
506 $this->container
->bookmarkService
507 ->expects(static::once())
509 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($checkBookmark, $id): void {
510 static::assertFalse($save);
512 $checkBookmark($bookmark);
515 $this->container
->bookmarkService
516 ->expects(static::once())
518 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($checkBookmark, $id): void {
519 static::assertTrue($save);
521 $checkBookmark($bookmark);
523 static::assertSame($id, $bookmark->getId());
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);
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']);
545 $result = $this->controller
->save($request, $response);
547 static::assertSame(302, $result->getStatusCode());
548 static::assertRegExp('@/subfolder/\?page=2#\w{6}@', $result->getHeader('location')[0]);
552 * Test save a bookmark - try to retrieve the thumbnail
554 public function testSaveBookmarkWithThumbnail(): void
556 $parameters = ['lf_url' => 'http://url.tld/other?part=3#hash'];
558 $request = $this->createMock(Request
::class);
561 ->willReturnCallback(function (string $key) use ($parameters): ?string {
562 return $parameters[$key] ?? null;
565 $response = new Response();
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;
572 $this->container
->thumbnailer
= $this->createMock(Thumbnailer
::class);
573 $this->container
->thumbnailer
574 ->expects(static::once())
576 ->with($parameters['lf_url'])
577 ->willReturn($thumb = 'http://thumb.url')
580 $this->container
->bookmarkService
581 ->expects(static::once())
583 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($thumb): void {
584 static::assertSame($thumb, $bookmark->getThumbnail());
588 $result = $this->controller
->save($request, $response);
590 static::assertSame(302, $result->getStatusCode());
594 * Change the password with a wrong existing password
596 public function testSaveBookmarkFromBookmarklet(): void
598 $parameters = ['source' => 'bookmarklet'];
600 $request = $this->createMock(Request
::class);
603 ->willReturnCallback(function (string $key) use ($parameters): ?string {
604 return $parameters[$key] ?? null;
607 $response = new Response();
609 $result = $this->controller
->save($request, $response);
611 static::assertSame(200, $result->getStatusCode());
612 static::assertSame('<script>self.close();</script>', (string) $result->getBody());
616 * Change the password with a wrong existing password
618 public function testSaveBookmarkWrongToken(): void
620 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
621 $this->container
->sessionManager
->method('checkToken')->willReturn(false);
623 $this->container
->bookmarkService
->expects(static::never())->method('addOrSet');
624 $this->container
->bookmarkService
->expects(static::never())->method('set');
626 $request = $this->createMock(Request
::class);
627 $response = new Response();
629 $this->expectException(WrongTokenException
::class);
631 $this->controller
->save($request, $response);