3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
10 use Shaarli\Thumbnailer
;
11 use Slim\Http\Request
;
12 use Slim\Http\Response
;
14 class ThumbnailsControllerTest
extends TestCase
16 use FrontAdminControllerMockHelper
;
18 /** @var ThumbnailsController */
19 protected $controller;
21 public function setUp(): void
23 $this->createContainer();
25 $this->controller
= new ThumbnailsController($this->container
);
29 * Test displaying the thumbnails update page
30 * Note that only non-note and HTTP bookmarks should be returned.
32 public function testIndex(): void
34 $assignedVariables = [];
35 $this->assignTemplateVars($assignedVariables);
37 $request = $this->createMock(Request
::class);
38 $response = new Response();
40 $this->container
->bookmarkService
41 ->expects(static::once())
44 (new Bookmark())->setId(1)->setUrl('http://url1.tld')->setTitle('Title 1'),
45 (new Bookmark())->setId(2)->setUrl('?abcdef')->setTitle('Note 1'),
46 (new Bookmark())->setId(3)->setUrl('http://url2.tld')->setTitle('Title 2'),
47 (new Bookmark())->setId(4)->setUrl('ftp://domain.tld', ['ftp'])->setTitle('FTP'),
51 $result = $this->controller
->index($request, $response);
53 static::assertSame(200, $result->getStatusCode());
54 static::assertSame('thumbnails', (string) $result->getBody());
56 static::assertSame('Thumbnails update - Shaarli', $assignedVariables['pagetitle']);
57 static::assertSame([1, 3], $assignedVariables['ids']);
61 * Test updating a bookmark thumbnail with valid parameters
63 public function testAjaxUpdateValid(): void
65 $request = $this->createMock(Request
::class);
66 $response = new Response();
68 $bookmark = (new Bookmark())
70 ->setUrl($url = 'http://url1.tld')
75 $this->container
->thumbnailer
= $this->createMock(Thumbnailer
::class);
76 $this->container
->thumbnailer
77 ->expects(static::once())
80 ->willReturn($thumb = 'http://img.tld/pic.png')
83 $this->container
->bookmarkService
84 ->expects(static::once())
87 ->willReturn($bookmark)
89 $this->container
->bookmarkService
90 ->expects(static::once())
92 ->willReturnCallback(function (Bookmark
$bookmark) use ($thumb): Bookmark
{
93 static::assertSame($thumb, $bookmark->getThumbnail());
99 $result = $this->controller
->ajaxUpdate($request, $response, ['id' => (string) $id]);
101 static::assertSame(200, $result->getStatusCode());
103 $payload = json_decode((string) $result->getBody(), true);
105 static::assertSame($id, $payload['id']);
106 static::assertSame($url, $payload['url']);
107 static::assertSame($thumb, $payload['thumbnail']);
111 * Test updating a bookmark thumbnail - Invalid ID
113 public function testAjaxUpdateInvalidId(): void
115 $request = $this->createMock(Request
::class);
116 $response = new Response();
118 $result = $this->controller
->ajaxUpdate($request, $response, ['id' => 'nope']);
120 static::assertSame(400, $result->getStatusCode());
124 * Test updating a bookmark thumbnail - No ID
126 public function testAjaxUpdateNoId(): void
128 $request = $this->createMock(Request
::class);
129 $response = new Response();
131 $result = $this->controller
->ajaxUpdate($request, $response, []);
133 static::assertSame(400, $result->getStatusCode());
137 * Test updating a bookmark thumbnail with valid parameters
139 public function testAjaxUpdateBookmarkNotFound(): void
142 $request = $this->createMock(Request
::class);
143 $response = new Response();
145 $this->container
->bookmarkService
146 ->expects(static::once())
149 ->willThrowException(new BookmarkNotFoundException())
152 $result = $this->controller
->ajaxUpdate($request, $response, ['id' => (string) $id]);
154 static::assertSame(404, $result->getStatusCode());