]>
Commit | Line | Data |
---|---|---|
6132d647 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin; | |
6 | ||
6132d647 A |
7 | use Shaarli\Bookmark\Bookmark; |
8 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | |
9b8c0a45 | 9 | use Shaarli\Bookmark\SearchResult; |
a5a9cf23 | 10 | use Shaarli\TestCase; |
6132d647 A |
11 | use Shaarli\Thumbnailer; |
12 | use Slim\Http\Request; | |
13 | use Slim\Http\Response; | |
14 | ||
15 | class ThumbnailsControllerTest extends TestCase | |
16 | { | |
17 | use FrontAdminControllerMockHelper; | |
18 | ||
19 | /** @var ThumbnailsController */ | |
20 | protected $controller; | |
21 | ||
22 | public function setUp(): void | |
23 | { | |
24 | $this->createContainer(); | |
25 | ||
26 | $this->controller = new ThumbnailsController($this->container); | |
27 | } | |
28 | ||
29 | /** | |
30 | * Test displaying the thumbnails update page | |
31 | * Note that only non-note and HTTP bookmarks should be returned. | |
32 | */ | |
33 | public function testIndex(): 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') | |
9b8c0a45 | 44 | ->willReturn(SearchResult::getSearchResult([ |
6132d647 A |
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'), | |
9b8c0a45 | 49 | ])) |
6132d647 A |
50 | ; |
51 | ||
52 | $result = $this->controller->index($request, $response); | |
53 | ||
54 | static::assertSame(200, $result->getStatusCode()); | |
55 | static::assertSame('thumbnails', (string) $result->getBody()); | |
56 | ||
57 | static::assertSame('Thumbnails update - Shaarli', $assignedVariables['pagetitle']); | |
58 | static::assertSame([1, 3], $assignedVariables['ids']); | |
59 | } | |
60 | ||
61 | /** | |
62 | * Test updating a bookmark thumbnail with valid parameters | |
63 | */ | |
64 | public function testAjaxUpdateValid(): void | |
65 | { | |
66 | $request = $this->createMock(Request::class); | |
67 | $response = new Response(); | |
68 | ||
69 | $bookmark = (new Bookmark()) | |
70 | ->setId($id = 123) | |
71 | ->setUrl($url = 'http://url1.tld') | |
72 | ->setTitle('Title 1') | |
73 | ->setThumbnail(false) | |
74 | ; | |
75 | ||
76 | $this->container->thumbnailer = $this->createMock(Thumbnailer::class); | |
77 | $this->container->thumbnailer | |
78 | ->expects(static::once()) | |
79 | ->method('get') | |
80 | ->with($url) | |
81 | ->willReturn($thumb = 'http://img.tld/pic.png') | |
82 | ; | |
83 | ||
84 | $this->container->bookmarkService | |
85 | ->expects(static::once()) | |
86 | ->method('get') | |
87 | ->with($id) | |
88 | ->willReturn($bookmark) | |
89 | ; | |
90 | $this->container->bookmarkService | |
91 | ->expects(static::once()) | |
92 | ->method('set') | |
efb7d21b | 93 | ->willReturnCallback(function (Bookmark $bookmark) use ($thumb): Bookmark { |
6132d647 | 94 | static::assertSame($thumb, $bookmark->getThumbnail()); |
efb7d21b A |
95 | |
96 | return $bookmark; | |
6132d647 A |
97 | }) |
98 | ; | |
99 | ||
100 | $result = $this->controller->ajaxUpdate($request, $response, ['id' => (string) $id]); | |
101 | ||
102 | static::assertSame(200, $result->getStatusCode()); | |
103 | ||
104 | $payload = json_decode((string) $result->getBody(), true); | |
105 | ||
106 | static::assertSame($id, $payload['id']); | |
107 | static::assertSame($url, $payload['url']); | |
108 | static::assertSame($thumb, $payload['thumbnail']); | |
109 | } | |
110 | ||
111 | /** | |
112 | * Test updating a bookmark thumbnail - Invalid ID | |
113 | */ | |
114 | public function testAjaxUpdateInvalidId(): void | |
115 | { | |
116 | $request = $this->createMock(Request::class); | |
117 | $response = new Response(); | |
118 | ||
119 | $result = $this->controller->ajaxUpdate($request, $response, ['id' => 'nope']); | |
120 | ||
121 | static::assertSame(400, $result->getStatusCode()); | |
122 | } | |
123 | ||
124 | /** | |
125 | * Test updating a bookmark thumbnail - No ID | |
126 | */ | |
127 | public function testAjaxUpdateNoId(): void | |
128 | { | |
129 | $request = $this->createMock(Request::class); | |
130 | $response = new Response(); | |
131 | ||
132 | $result = $this->controller->ajaxUpdate($request, $response, []); | |
133 | ||
134 | static::assertSame(400, $result->getStatusCode()); | |
135 | } | |
136 | ||
137 | /** | |
138 | * Test updating a bookmark thumbnail with valid parameters | |
139 | */ | |
140 | public function testAjaxUpdateBookmarkNotFound(): void | |
141 | { | |
142 | $id = 123; | |
143 | $request = $this->createMock(Request::class); | |
144 | $response = new Response(); | |
145 | ||
146 | $this->container->bookmarkService | |
147 | ->expects(static::once()) | |
148 | ->method('get') | |
149 | ->with($id) | |
150 | ->willThrowException(new BookmarkNotFoundException()) | |
151 | ; | |
152 | ||
153 | $result = $this->controller->ajaxUpdate($request, $response, ['id' => (string) $id]); | |
154 | ||
155 | static::assertSame(404, $result->getStatusCode()); | |
156 | } | |
157 | } |