3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
9 use Shaarli\Bookmark\SearchResult
;
11 use Shaarli\Thumbnailer
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
15 class ThumbnailsControllerTest
extends TestCase
17 use FrontAdminControllerMockHelper
;
19 /** @var ThumbnailsController */
20 protected $controller;
22 public function setUp(): void
24 $this->createContainer();
26 $this->controller
= new ThumbnailsController($this->container
);
30 * Test displaying the thumbnails update page
31 * Note that only non-note and HTTP bookmarks should be returned.
33 public function testIndex(): 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())
44 ->willReturn(SearchResult
::getSearchResult([
45 (new Bookmark())->setId(1)->setUrl('http://url1.tld')->setTitle('Title 1'),
46 (new Bookmark())->setId(2)->setUrl('?abcdef')->setTitle('Note 1'),
47 (new Bookmark())->setId(3)->setUrl('http://url2.tld')->setTitle('Title 2'),
48 (new Bookmark())->setId(4)->setUrl('ftp://domain.tld', ['ftp'])->setTitle('FTP'),
52 $result = $this->controller
->index($request, $response);
54 static::assertSame(200, $result->getStatusCode());
55 static::assertSame('thumbnails', (string) $result->getBody());
57 static::assertSame('Thumbnails update - Shaarli', $assignedVariables['pagetitle']);
58 static::assertSame([1, 3], $assignedVariables['ids']);
62 * Test updating a bookmark thumbnail with valid parameters
64 public function testAjaxUpdateValid(): void
66 $request = $this->createMock(Request
::class);
67 $response = new Response();
69 $bookmark = (new Bookmark())
71 ->setUrl($url = 'http://url1.tld')
76 $this->container
->thumbnailer
= $this->createMock(Thumbnailer
::class);
77 $this->container
->thumbnailer
78 ->expects(static::once())
81 ->willReturn($thumb = 'http://img.tld/pic.png')
84 $this->container
->bookmarkService
85 ->expects(static::once())
88 ->willReturn($bookmark)
90 $this->container
->bookmarkService
91 ->expects(static::once())
93 ->willReturnCallback(function (Bookmark
$bookmark) use ($thumb): Bookmark
{
94 static::assertSame($thumb, $bookmark->getThumbnail());
100 $result = $this->controller
->ajaxUpdate($request, $response, ['id' => (string) $id]);
102 static::assertSame(200, $result->getStatusCode());
104 $payload = json_decode((string) $result->getBody(), true);
106 static::assertSame($id, $payload['id']);
107 static::assertSame($url, $payload['url']);
108 static::assertSame($thumb, $payload['thumbnail']);
112 * Test updating a bookmark thumbnail - Invalid ID
114 public function testAjaxUpdateInvalidId(): void
116 $request = $this->createMock(Request
::class);
117 $response = new Response();
119 $result = $this->controller
->ajaxUpdate($request, $response, ['id' => 'nope']);
121 static::assertSame(400, $result->getStatusCode());
125 * Test updating a bookmark thumbnail - No ID
127 public function testAjaxUpdateNoId(): void
129 $request = $this->createMock(Request
::class);
130 $response = new Response();
132 $result = $this->controller
->ajaxUpdate($request, $response, []);
134 static::assertSame(400, $result->getStatusCode());
138 * Test updating a bookmark thumbnail with valid parameters
140 public function testAjaxUpdateBookmarkNotFound(): void
143 $request = $this->createMock(Request
::class);
144 $response = new Response();
146 $this->container
->bookmarkService
147 ->expects(static::once())
150 ->willThrowException(new BookmarkNotFoundException())
153 $result = $this->controller
->ajaxUpdate($request, $response, ['id' => (string) $id]);
155 static::assertSame(404, $result->getStatusCode());