diff options
Diffstat (limited to 'tests/front/controller/admin/ShaarePublishControllerTest')
4 files changed, 954 insertions, 0 deletions
diff --git a/tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateBatchFormTest.php b/tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateBatchFormTest.php new file mode 100644 index 00000000..ce8e112b --- /dev/null +++ b/tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateBatchFormTest.php | |||
@@ -0,0 +1,63 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin\ShaarePublishControllerTest; | ||
6 | |||
7 | use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; | ||
8 | use Shaarli\Front\Controller\Admin\ShaarePublishController; | ||
9 | use Shaarli\Http\HttpAccess; | ||
10 | use Shaarli\Http\MetadataRetriever; | ||
11 | use Shaarli\TestCase; | ||
12 | use Slim\Http\Request; | ||
13 | use Slim\Http\Response; | ||
14 | |||
15 | class DisplayCreateBatchFormTest extends TestCase | ||
16 | { | ||
17 | use FrontAdminControllerMockHelper; | ||
18 | |||
19 | /** @var ShaarePublishController */ | ||
20 | protected $controller; | ||
21 | |||
22 | public function setUp(): void | ||
23 | { | ||
24 | $this->createContainer(); | ||
25 | |||
26 | $this->container->httpAccess = $this->createMock(HttpAccess::class); | ||
27 | $this->container->metadataRetriever = $this->createMock(MetadataRetriever::class); | ||
28 | $this->controller = new ShaarePublishController($this->container); | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * TODO | ||
33 | */ | ||
34 | public function testDisplayCreateFormBatch(): void | ||
35 | { | ||
36 | $urls = [ | ||
37 | 'https://domain1.tld/url1', | ||
38 | 'https://domain2.tld/url2', | ||
39 | ' ', | ||
40 | 'https://domain3.tld/url3', | ||
41 | ]; | ||
42 | |||
43 | $request = $this->createMock(Request::class); | ||
44 | $request->method('getParam')->willReturnCallback(function (string $key) use ($urls): ?string { | ||
45 | return $key === 'urls' ? implode(PHP_EOL, $urls) : null; | ||
46 | }); | ||
47 | $response = new Response(); | ||
48 | |||
49 | $assignedVariables = []; | ||
50 | $this->assignTemplateVars($assignedVariables); | ||
51 | |||
52 | $result = $this->controller->displayCreateBatchForms($request, $response); | ||
53 | |||
54 | static::assertSame(200, $result->getStatusCode()); | ||
55 | static::assertSame('editlink.batch', (string) $result->getBody()); | ||
56 | |||
57 | static::assertTrue($assignedVariables['batch_mode']); | ||
58 | static::assertCount(3, $assignedVariables['links']); | ||
59 | static::assertSame($urls[0], $assignedVariables['links'][0]['link']['url']); | ||
60 | static::assertSame($urls[1], $assignedVariables['links'][1]['link']['url']); | ||
61 | static::assertSame($urls[3], $assignedVariables['links'][2]['link']['url']); | ||
62 | } | ||
63 | } | ||
diff --git a/tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateFormTest.php b/tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateFormTest.php new file mode 100644 index 00000000..964773da --- /dev/null +++ b/tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateFormTest.php | |||
@@ -0,0 +1,367 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin\ShaarePublishControllerTest; | ||
6 | |||
7 | use Shaarli\Bookmark\Bookmark; | ||
8 | use Shaarli\Config\ConfigManager; | ||
9 | use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; | ||
10 | use Shaarli\Front\Controller\Admin\ShaarePublishController; | ||
11 | use Shaarli\Http\HttpAccess; | ||
12 | use Shaarli\Http\MetadataRetriever; | ||
13 | use Shaarli\TestCase; | ||
14 | use Slim\Http\Request; | ||
15 | use Slim\Http\Response; | ||
16 | |||
17 | class DisplayCreateFormTest extends TestCase | ||
18 | { | ||
19 | use FrontAdminControllerMockHelper; | ||
20 | |||
21 | /** @var ShaarePublishController */ | ||
22 | protected $controller; | ||
23 | |||
24 | public function setUp(): void | ||
25 | { | ||
26 | $this->createContainer(); | ||
27 | |||
28 | $this->container->httpAccess = $this->createMock(HttpAccess::class); | ||
29 | $this->container->metadataRetriever = $this->createMock(MetadataRetriever::class); | ||
30 | $this->controller = new ShaarePublishController($this->container); | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * Test displaying bookmark create form | ||
35 | * Ensure that every step of the standard workflow works properly. | ||
36 | */ | ||
37 | public function testDisplayCreateFormWithUrlAndWithMetadataRetrieval(): void | ||
38 | { | ||
39 | $this->container->environment = [ | ||
40 | 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc' | ||
41 | ]; | ||
42 | |||
43 | $assignedVariables = []; | ||
44 | $this->assignTemplateVars($assignedVariables); | ||
45 | |||
46 | $url = 'http://url.tld/other?part=3&utm_ad=pay#hash'; | ||
47 | $expectedUrl = str_replace('&utm_ad=pay', '', $url); | ||
48 | $remoteTitle = 'Remote Title'; | ||
49 | $remoteDesc = 'Sometimes the meta description is relevant.'; | ||
50 | $remoteTags = 'abc def'; | ||
51 | |||
52 | $request = $this->createMock(Request::class); | ||
53 | $request->method('getParam')->willReturnCallback(function (string $key) use ($url): ?string { | ||
54 | return $key === 'post' ? $url : null; | ||
55 | }); | ||
56 | $response = new Response(); | ||
57 | |||
58 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
59 | $this->container->conf->method('get')->willReturnCallback(function (string $param, $default) { | ||
60 | if ($param === 'general.enable_async_metadata') { | ||
61 | return false; | ||
62 | } | ||
63 | |||
64 | return $default; | ||
65 | }); | ||
66 | |||
67 | $this->container->metadataRetriever->expects(static::once())->method('retrieve')->willReturn([ | ||
68 | 'title' => $remoteTitle, | ||
69 | 'description' => $remoteDesc, | ||
70 | 'tags' => $remoteTags, | ||
71 | ]); | ||
72 | |||
73 | $this->container->bookmarkService | ||
74 | ->expects(static::once()) | ||
75 | ->method('bookmarksCountPerTag') | ||
76 | ->willReturn($tags = ['tag1' => 2, 'tag2' => 1]) | ||
77 | ; | ||
78 | |||
79 | // Make sure that PluginManager hook is triggered | ||
80 | $this->container->pluginManager | ||
81 | ->expects(static::atLeastOnce()) | ||
82 | ->method('executeHooks') | ||
83 | ->withConsecutive(['render_editlink'], ['render_includes']) | ||
84 | ->willReturnCallback(function (string $hook, array $data) use ($remoteTitle, $remoteDesc): array { | ||
85 | if ('render_editlink' === $hook) { | ||
86 | static::assertSame($remoteTitle, $data['link']['title']); | ||
87 | static::assertSame($remoteDesc, $data['link']['description']); | ||
88 | } | ||
89 | |||
90 | return $data; | ||
91 | }) | ||
92 | ; | ||
93 | |||
94 | $result = $this->controller->displayCreateForm($request, $response); | ||
95 | |||
96 | static::assertSame(200, $result->getStatusCode()); | ||
97 | static::assertSame('editlink', (string) $result->getBody()); | ||
98 | |||
99 | static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']); | ||
100 | |||
101 | static::assertSame($expectedUrl, $assignedVariables['link']['url']); | ||
102 | static::assertSame($remoteTitle, $assignedVariables['link']['title']); | ||
103 | static::assertSame($remoteDesc, $assignedVariables['link']['description']); | ||
104 | static::assertSame($remoteTags . ' ', $assignedVariables['link']['tags']); | ||
105 | static::assertFalse($assignedVariables['link']['private']); | ||
106 | |||
107 | static::assertTrue($assignedVariables['link_is_new']); | ||
108 | static::assertSame($referer, $assignedVariables['http_referer']); | ||
109 | static::assertSame($tags, $assignedVariables['tags']); | ||
110 | static::assertArrayHasKey('source', $assignedVariables); | ||
111 | static::assertArrayHasKey('default_private_links', $assignedVariables); | ||
112 | static::assertArrayHasKey('async_metadata', $assignedVariables); | ||
113 | static::assertArrayHasKey('retrieve_description', $assignedVariables); | ||
114 | } | ||
115 | |||
116 | /** | ||
117 | * Test displaying bookmark create form without any external metadata retrieval attempt | ||
118 | */ | ||
119 | public function testDisplayCreateFormWithUrlAndWithoutMetadata(): void | ||
120 | { | ||
121 | $this->container->environment = [ | ||
122 | 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc' | ||
123 | ]; | ||
124 | |||
125 | $assignedVariables = []; | ||
126 | $this->assignTemplateVars($assignedVariables); | ||
127 | |||
128 | $url = 'http://url.tld/other?part=3&utm_ad=pay#hash'; | ||
129 | $expectedUrl = str_replace('&utm_ad=pay', '', $url); | ||
130 | |||
131 | $request = $this->createMock(Request::class); | ||
132 | $request->method('getParam')->willReturnCallback(function (string $key) use ($url): ?string { | ||
133 | return $key === 'post' ? $url : null; | ||
134 | }); | ||
135 | $response = new Response(); | ||
136 | |||
137 | $this->container->metadataRetriever->expects(static::never())->method('retrieve'); | ||
138 | |||
139 | $this->container->bookmarkService | ||
140 | ->expects(static::once()) | ||
141 | ->method('bookmarksCountPerTag') | ||
142 | ->willReturn($tags = ['tag1' => 2, 'tag2' => 1]) | ||
143 | ; | ||
144 | |||
145 | // Make sure that PluginManager hook is triggered | ||
146 | $this->container->pluginManager | ||
147 | ->expects(static::atLeastOnce()) | ||
148 | ->method('executeHooks') | ||
149 | ->withConsecutive(['render_editlink'], ['render_includes']) | ||
150 | ->willReturnCallback(function (string $hook, array $data): array { | ||
151 | if ('render_editlink' === $hook) { | ||
152 | static::assertSame('', $data['link']['title']); | ||
153 | static::assertSame('', $data['link']['description']); | ||
154 | } | ||
155 | |||
156 | return $data; | ||
157 | }) | ||
158 | ; | ||
159 | |||
160 | $result = $this->controller->displayCreateForm($request, $response); | ||
161 | |||
162 | static::assertSame(200, $result->getStatusCode()); | ||
163 | static::assertSame('editlink', (string) $result->getBody()); | ||
164 | |||
165 | static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']); | ||
166 | |||
167 | static::assertSame($expectedUrl, $assignedVariables['link']['url']); | ||
168 | static::assertSame('', $assignedVariables['link']['title']); | ||
169 | static::assertSame('', $assignedVariables['link']['description']); | ||
170 | static::assertSame('', $assignedVariables['link']['tags']); | ||
171 | static::assertFalse($assignedVariables['link']['private']); | ||
172 | |||
173 | static::assertTrue($assignedVariables['link_is_new']); | ||
174 | static::assertSame($referer, $assignedVariables['http_referer']); | ||
175 | static::assertSame($tags, $assignedVariables['tags']); | ||
176 | static::assertArrayHasKey('source', $assignedVariables); | ||
177 | static::assertArrayHasKey('default_private_links', $assignedVariables); | ||
178 | static::assertArrayHasKey('async_metadata', $assignedVariables); | ||
179 | static::assertArrayHasKey('retrieve_description', $assignedVariables); | ||
180 | } | ||
181 | |||
182 | /** | ||
183 | * Test displaying bookmark create form | ||
184 | * Ensure all available query parameters are handled properly. | ||
185 | */ | ||
186 | public function testDisplayCreateFormWithFullParameters(): void | ||
187 | { | ||
188 | $assignedVariables = []; | ||
189 | $this->assignTemplateVars($assignedVariables); | ||
190 | |||
191 | $parameters = [ | ||
192 | 'post' => 'http://url.tld/other?part=3&utm_ad=pay#hash', | ||
193 | 'title' => 'Provided Title', | ||
194 | 'description' => 'Provided description.', | ||
195 | 'tags' => 'abc@def', | ||
196 | 'private' => '1', | ||
197 | 'source' => 'apps', | ||
198 | ]; | ||
199 | $expectedUrl = str_replace('&utm_ad=pay', '', $parameters['post']); | ||
200 | |||
201 | $request = $this->createMock(Request::class); | ||
202 | $request | ||
203 | ->method('getParam') | ||
204 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | ||
205 | return $parameters[$key] ?? null; | ||
206 | }); | ||
207 | $response = new Response(); | ||
208 | |||
209 | $result = $this->controller->displayCreateForm($request, $response); | ||
210 | |||
211 | static::assertSame(200, $result->getStatusCode()); | ||
212 | static::assertSame('editlink', (string) $result->getBody()); | ||
213 | |||
214 | static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']); | ||
215 | |||
216 | static::assertSame($expectedUrl, $assignedVariables['link']['url']); | ||
217 | static::assertSame($parameters['title'], $assignedVariables['link']['title']); | ||
218 | static::assertSame($parameters['description'], $assignedVariables['link']['description']); | ||
219 | static::assertSame($parameters['tags'] . '@', $assignedVariables['link']['tags']); | ||
220 | static::assertTrue($assignedVariables['link']['private']); | ||
221 | static::assertTrue($assignedVariables['link_is_new']); | ||
222 | static::assertSame($parameters['source'], $assignedVariables['source']); | ||
223 | } | ||
224 | |||
225 | /** | ||
226 | * Test displaying bookmark create form | ||
227 | * Without any parameter. | ||
228 | */ | ||
229 | public function testDisplayCreateFormEmpty(): void | ||
230 | { | ||
231 | $assignedVariables = []; | ||
232 | $this->assignTemplateVars($assignedVariables); | ||
233 | |||
234 | $request = $this->createMock(Request::class); | ||
235 | $response = new Response(); | ||
236 | |||
237 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | ||
238 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | ||
239 | |||
240 | $result = $this->controller->displayCreateForm($request, $response); | ||
241 | |||
242 | static::assertSame(200, $result->getStatusCode()); | ||
243 | static::assertSame('editlink', (string) $result->getBody()); | ||
244 | static::assertSame('', $assignedVariables['link']['url']); | ||
245 | static::assertSame('Note: ', $assignedVariables['link']['title']); | ||
246 | static::assertSame('', $assignedVariables['link']['description']); | ||
247 | static::assertSame('', $assignedVariables['link']['tags']); | ||
248 | static::assertFalse($assignedVariables['link']['private']); | ||
249 | static::assertTrue($assignedVariables['link_is_new']); | ||
250 | } | ||
251 | |||
252 | /** | ||
253 | * Test displaying bookmark create form | ||
254 | * URL not using HTTP protocol: do not try to retrieve the title | ||
255 | */ | ||
256 | public function testDisplayCreateFormNotHttp(): void | ||
257 | { | ||
258 | $assignedVariables = []; | ||
259 | $this->assignTemplateVars($assignedVariables); | ||
260 | |||
261 | $url = 'magnet://kubuntu.torrent'; | ||
262 | $request = $this->createMock(Request::class); | ||
263 | $request | ||
264 | ->method('getParam') | ||
265 | ->willReturnCallback(function (string $key) use ($url): ?string { | ||
266 | return $key === 'post' ? $url : null; | ||
267 | }); | ||
268 | $response = new Response(); | ||
269 | |||
270 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | ||
271 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | ||
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($url, $assignedVariables['link']['url']); | ||
278 | static::assertTrue($assignedVariables['link_is_new']); | ||
279 | } | ||
280 | |||
281 | /** | ||
282 | * Test displaying bookmark create form | ||
283 | * When markdown formatter is enabled, the no markdown tag should be added to existing tags. | ||
284 | */ | ||
285 | public function testDisplayCreateFormWithMarkdownEnabled(): void | ||
286 | { | ||
287 | $assignedVariables = []; | ||
288 | $this->assignTemplateVars($assignedVariables); | ||
289 | |||
290 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
291 | $this->container->conf | ||
292 | ->expects(static::atLeastOnce()) | ||
293 | ->method('get')->willReturnCallback(function (string $key): ?string { | ||
294 | if ($key === 'formatter') { | ||
295 | return 'markdown'; | ||
296 | } | ||
297 | |||
298 | return $key; | ||
299 | }) | ||
300 | ; | ||
301 | |||
302 | $request = $this->createMock(Request::class); | ||
303 | $response = new Response(); | ||
304 | |||
305 | $result = $this->controller->displayCreateForm($request, $response); | ||
306 | |||
307 | static::assertSame(200, $result->getStatusCode()); | ||
308 | static::assertSame('editlink', (string) $result->getBody()); | ||
309 | static::assertSame(['nomarkdown' => 1], $assignedVariables['tags']); | ||
310 | } | ||
311 | |||
312 | /** | ||
313 | * Test displaying bookmark create form | ||
314 | * When an existing URL is submitted, we want to edit the existing link. | ||
315 | */ | ||
316 | public function testDisplayCreateFormWithExistingUrl(): void | ||
317 | { | ||
318 | $assignedVariables = []; | ||
319 | $this->assignTemplateVars($assignedVariables); | ||
320 | |||
321 | $url = 'http://url.tld/other?part=3&utm_ad=pay#hash'; | ||
322 | $expectedUrl = str_replace('&utm_ad=pay', '', $url); | ||
323 | |||
324 | $request = $this->createMock(Request::class); | ||
325 | $request | ||
326 | ->method('getParam') | ||
327 | ->willReturnCallback(function (string $key) use ($url): ?string { | ||
328 | return $key === 'post' ? $url : null; | ||
329 | }); | ||
330 | $response = new Response(); | ||
331 | |||
332 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | ||
333 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | ||
334 | |||
335 | $this->container->bookmarkService | ||
336 | ->expects(static::once()) | ||
337 | ->method('findByUrl') | ||
338 | ->with($expectedUrl) | ||
339 | ->willReturn( | ||
340 | (new Bookmark()) | ||
341 | ->setId($id = 23) | ||
342 | ->setUrl($expectedUrl) | ||
343 | ->setTitle($title = 'Bookmark Title') | ||
344 | ->setDescription($description = 'Bookmark description.') | ||
345 | ->setTags($tags = ['abc', 'def']) | ||
346 | ->setPrivate(true) | ||
347 | ->setCreated($createdAt = new \DateTime('2020-06-10 18:45:44')) | ||
348 | ) | ||
349 | ; | ||
350 | |||
351 | $result = $this->controller->displayCreateForm($request, $response); | ||
352 | |||
353 | static::assertSame(200, $result->getStatusCode()); | ||
354 | static::assertSame('editlink', (string) $result->getBody()); | ||
355 | |||
356 | static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']); | ||
357 | static::assertFalse($assignedVariables['link_is_new']); | ||
358 | |||
359 | static::assertSame($id, $assignedVariables['link']['id']); | ||
360 | static::assertSame($expectedUrl, $assignedVariables['link']['url']); | ||
361 | static::assertSame($title, $assignedVariables['link']['title']); | ||
362 | static::assertSame($description, $assignedVariables['link']['description']); | ||
363 | static::assertSame(implode('@', $tags) . '@', $assignedVariables['link']['tags']); | ||
364 | static::assertTrue($assignedVariables['link']['private']); | ||
365 | static::assertSame($createdAt, $assignedVariables['link']['created']); | ||
366 | } | ||
367 | } | ||
diff --git a/tests/front/controller/admin/ShaarePublishControllerTest/DisplayEditFormTest.php b/tests/front/controller/admin/ShaarePublishControllerTest/DisplayEditFormTest.php new file mode 100644 index 00000000..738cea12 --- /dev/null +++ b/tests/front/controller/admin/ShaarePublishControllerTest/DisplayEditFormTest.php | |||
@@ -0,0 +1,155 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin\ShaarePublishControllerTest; | ||
6 | |||
7 | use Shaarli\Bookmark\Bookmark; | ||
8 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | ||
9 | use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; | ||
10 | use Shaarli\Front\Controller\Admin\ShaarePublishController; | ||
11 | use Shaarli\Http\HttpAccess; | ||
12 | use Shaarli\Security\SessionManager; | ||
13 | use Shaarli\TestCase; | ||
14 | use Slim\Http\Request; | ||
15 | use Slim\Http\Response; | ||
16 | |||
17 | class DisplayEditFormTest extends TestCase | ||
18 | { | ||
19 | use FrontAdminControllerMockHelper; | ||
20 | |||
21 | /** @var ShaarePublishController */ | ||
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 ShaarePublishController($this->container); | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * Test displaying bookmark edit form | ||
34 | * When an existing ID is provided, ensure that default workflow works properly. | ||
35 | */ | ||
36 | public function testDisplayEditFormDefault(): void | ||
37 | { | ||
38 | $assignedVariables = []; | ||
39 | $this->assignTemplateVars($assignedVariables); | ||
40 | |||
41 | $id = 11; | ||
42 | |||
43 | $request = $this->createMock(Request::class); | ||
44 | $response = new Response(); | ||
45 | |||
46 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | ||
47 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | ||
48 | |||
49 | $this->container->bookmarkService | ||
50 | ->expects(static::once()) | ||
51 | ->method('get') | ||
52 | ->with($id) | ||
53 | ->willReturn( | ||
54 | (new Bookmark()) | ||
55 | ->setId($id) | ||
56 | ->setUrl($url = 'http://domain.tld') | ||
57 | ->setTitle($title = 'Bookmark Title') | ||
58 | ->setDescription($description = 'Bookmark description.') | ||
59 | ->setTags($tags = ['abc', 'def']) | ||
60 | ->setPrivate(true) | ||
61 | ->setCreated($createdAt = new \DateTime('2020-06-10 18:45:44')) | ||
62 | ) | ||
63 | ; | ||
64 | |||
65 | $result = $this->controller->displayEditForm($request, $response, ['id' => (string) $id]); | ||
66 | |||
67 | static::assertSame(200, $result->getStatusCode()); | ||
68 | static::assertSame('editlink', (string) $result->getBody()); | ||
69 | |||
70 | static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']); | ||
71 | static::assertFalse($assignedVariables['link_is_new']); | ||
72 | |||
73 | static::assertSame($id, $assignedVariables['link']['id']); | ||
74 | static::assertSame($url, $assignedVariables['link']['url']); | ||
75 | static::assertSame($title, $assignedVariables['link']['title']); | ||
76 | static::assertSame($description, $assignedVariables['link']['description']); | ||
77 | static::assertSame(implode('@', $tags) . '@', $assignedVariables['link']['tags']); | ||
78 | static::assertTrue($assignedVariables['link']['private']); | ||
79 | static::assertSame($createdAt, $assignedVariables['link']['created']); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * Test displaying bookmark edit form | ||
84 | * Invalid ID provided. | ||
85 | */ | ||
86 | public function testDisplayEditFormInvalidId(): void | ||
87 | { | ||
88 | $id = 'invalid'; | ||
89 | |||
90 | $request = $this->createMock(Request::class); | ||
91 | $response = new Response(); | ||
92 | |||
93 | $this->container->sessionManager | ||
94 | ->expects(static::once()) | ||
95 | ->method('setSessionParameter') | ||
96 | ->with(SessionManager::KEY_ERROR_MESSAGES, ['Bookmark with identifier invalid could not be found.']) | ||
97 | ; | ||
98 | |||
99 | $result = $this->controller->displayEditForm($request, $response, ['id' => $id]); | ||
100 | |||
101 | static::assertSame(302, $result->getStatusCode()); | ||
102 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * Test displaying bookmark edit form | ||
107 | * ID not provided. | ||
108 | */ | ||
109 | public function testDisplayEditFormIdNotProvided(): void | ||
110 | { | ||
111 | $request = $this->createMock(Request::class); | ||
112 | $response = new Response(); | ||
113 | |||
114 | $this->container->sessionManager | ||
115 | ->expects(static::once()) | ||
116 | ->method('setSessionParameter') | ||
117 | ->with(SessionManager::KEY_ERROR_MESSAGES, ['Bookmark with identifier could not be found.']) | ||
118 | ; | ||
119 | |||
120 | $result = $this->controller->displayEditForm($request, $response, []); | ||
121 | |||
122 | static::assertSame(302, $result->getStatusCode()); | ||
123 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | ||
124 | } | ||
125 | |||
126 | /** | ||
127 | * Test displaying bookmark edit form | ||
128 | * Bookmark not found. | ||
129 | */ | ||
130 | public function testDisplayEditFormBookmarkNotFound(): void | ||
131 | { | ||
132 | $id = 123; | ||
133 | |||
134 | $request = $this->createMock(Request::class); | ||
135 | $response = new Response(); | ||
136 | |||
137 | $this->container->bookmarkService | ||
138 | ->expects(static::once()) | ||
139 | ->method('get') | ||
140 | ->with($id) | ||
141 | ->willThrowException(new BookmarkNotFoundException()) | ||
142 | ; | ||
143 | |||
144 | $this->container->sessionManager | ||
145 | ->expects(static::once()) | ||
146 | ->method('setSessionParameter') | ||
147 | ->with(SessionManager::KEY_ERROR_MESSAGES, ['Bookmark with identifier 123 could not be found.']) | ||
148 | ; | ||
149 | |||
150 | $result = $this->controller->displayEditForm($request, $response, ['id' => (string) $id]); | ||
151 | |||
152 | static::assertSame(302, $result->getStatusCode()); | ||
153 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | ||
154 | } | ||
155 | } | ||
diff --git a/tests/front/controller/admin/ShaarePublishControllerTest/SaveBookmarkTest.php b/tests/front/controller/admin/ShaarePublishControllerTest/SaveBookmarkTest.php new file mode 100644 index 00000000..b6a861bc --- /dev/null +++ b/tests/front/controller/admin/ShaarePublishControllerTest/SaveBookmarkTest.php | |||
@@ -0,0 +1,369 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin\ShaarePublishControllerTest; | ||
6 | |||
7 | use Shaarli\Bookmark\Bookmark; | ||
8 | use Shaarli\Config\ConfigManager; | ||
9 | use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; | ||
10 | use Shaarli\Front\Controller\Admin\ShaarePublishController; | ||
11 | use Shaarli\Front\Exception\WrongTokenException; | ||
12 | use Shaarli\Http\HttpAccess; | ||
13 | use Shaarli\Security\SessionManager; | ||
14 | use Shaarli\TestCase; | ||
15 | use Shaarli\Thumbnailer; | ||
16 | use Slim\Http\Request; | ||
17 | use Slim\Http\Response; | ||
18 | |||
19 | class SaveBookmarkTest extends TestCase | ||
20 | { | ||
21 | use FrontAdminControllerMockHelper; | ||
22 | |||
23 | /** @var ShaarePublishController */ | ||
24 | protected $controller; | ||
25 | |||
26 | public function setUp(): void | ||
27 | { | ||
28 | $this->createContainer(); | ||
29 | |||
30 | $this->container->httpAccess = $this->createMock(HttpAccess::class); | ||
31 | $this->controller = new ShaarePublishController($this->container); | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * Test save a new bookmark | ||
36 | */ | ||
37 | public function testSaveBookmark(): void | ||
38 | { | ||
39 | $id = 21; | ||
40 | $parameters = [ | ||
41 | 'lf_url' => 'http://url.tld/other?part=3#hash', | ||
42 | 'lf_title' => 'Provided Title', | ||
43 | 'lf_description' => 'Provided description.', | ||
44 | 'lf_tags' => 'abc def', | ||
45 | 'lf_private' => '1', | ||
46 | 'returnurl' => 'http://shaarli/subfolder/admin/add-shaare' | ||
47 | ]; | ||
48 | |||
49 | $request = $this->createMock(Request::class); | ||
50 | $request | ||
51 | ->method('getParam') | ||
52 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | ||
53 | return $parameters[$key] ?? null; | ||
54 | }) | ||
55 | ; | ||
56 | $response = new Response(); | ||
57 | |||
58 | $checkBookmark = function (Bookmark $bookmark) use ($parameters) { | ||
59 | static::assertSame($parameters['lf_url'], $bookmark->getUrl()); | ||
60 | static::assertSame($parameters['lf_title'], $bookmark->getTitle()); | ||
61 | static::assertSame($parameters['lf_description'], $bookmark->getDescription()); | ||
62 | static::assertSame($parameters['lf_tags'], $bookmark->getTagsString()); | ||
63 | static::assertTrue($bookmark->isPrivate()); | ||
64 | }; | ||
65 | |||
66 | $this->container->bookmarkService | ||
67 | ->expects(static::once()) | ||
68 | ->method('addOrSet') | ||
69 | ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): Bookmark { | ||
70 | static::assertFalse($save); | ||
71 | |||
72 | $checkBookmark($bookmark); | ||
73 | |||
74 | $bookmark->setId($id); | ||
75 | |||
76 | return $bookmark; | ||
77 | }) | ||
78 | ; | ||
79 | $this->container->bookmarkService | ||
80 | ->expects(static::once()) | ||
81 | ->method('set') | ||
82 | ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): Bookmark { | ||
83 | static::assertTrue($save); | ||
84 | |||
85 | $checkBookmark($bookmark); | ||
86 | |||
87 | static::assertSame($id, $bookmark->getId()); | ||
88 | |||
89 | return $bookmark; | ||
90 | }) | ||
91 | ; | ||
92 | |||
93 | // Make sure that PluginManager hook is triggered | ||
94 | $this->container->pluginManager | ||
95 | ->expects(static::atLeastOnce()) | ||
96 | ->method('executeHooks') | ||
97 | ->withConsecutive(['save_link']) | ||
98 | ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array { | ||
99 | if ('save_link' === $hook) { | ||
100 | static::assertSame($id, $data['id']); | ||
101 | static::assertSame($parameters['lf_url'], $data['url']); | ||
102 | static::assertSame($parameters['lf_title'], $data['title']); | ||
103 | static::assertSame($parameters['lf_description'], $data['description']); | ||
104 | static::assertSame($parameters['lf_tags'], $data['tags']); | ||
105 | static::assertTrue($data['private']); | ||
106 | } | ||
107 | |||
108 | return $data; | ||
109 | }) | ||
110 | ; | ||
111 | |||
112 | $result = $this->controller->save($request, $response); | ||
113 | |||
114 | static::assertSame(302, $result->getStatusCode()); | ||
115 | static::assertRegExp('@/subfolder/#[\w\-]{6}@', $result->getHeader('location')[0]); | ||
116 | } | ||
117 | |||
118 | |||
119 | /** | ||
120 | * Test save an existing bookmark | ||
121 | */ | ||
122 | public function testSaveExistingBookmark(): void | ||
123 | { | ||
124 | $id = 21; | ||
125 | $parameters = [ | ||
126 | 'lf_id' => (string) $id, | ||
127 | 'lf_url' => 'http://url.tld/other?part=3#hash', | ||
128 | 'lf_title' => 'Provided Title', | ||
129 | 'lf_description' => 'Provided description.', | ||
130 | 'lf_tags' => 'abc def', | ||
131 | 'lf_private' => '1', | ||
132 | 'returnurl' => 'http://shaarli/subfolder/?page=2' | ||
133 | ]; | ||
134 | |||
135 | $request = $this->createMock(Request::class); | ||
136 | $request | ||
137 | ->method('getParam') | ||
138 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | ||
139 | return $parameters[$key] ?? null; | ||
140 | }) | ||
141 | ; | ||
142 | $response = new Response(); | ||
143 | |||
144 | $checkBookmark = function (Bookmark $bookmark) use ($parameters, $id) { | ||
145 | static::assertSame($id, $bookmark->getId()); | ||
146 | static::assertSame($parameters['lf_url'], $bookmark->getUrl()); | ||
147 | static::assertSame($parameters['lf_title'], $bookmark->getTitle()); | ||
148 | static::assertSame($parameters['lf_description'], $bookmark->getDescription()); | ||
149 | static::assertSame($parameters['lf_tags'], $bookmark->getTagsString()); | ||
150 | static::assertTrue($bookmark->isPrivate()); | ||
151 | }; | ||
152 | |||
153 | $this->container->bookmarkService->expects(static::atLeastOnce())->method('exists')->willReturn(true); | ||
154 | $this->container->bookmarkService | ||
155 | ->expects(static::once()) | ||
156 | ->method('get') | ||
157 | ->willReturn((new Bookmark())->setId($id)->setUrl('http://other.url')) | ||
158 | ; | ||
159 | $this->container->bookmarkService | ||
160 | ->expects(static::once()) | ||
161 | ->method('addOrSet') | ||
162 | ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): Bookmark { | ||
163 | static::assertFalse($save); | ||
164 | |||
165 | $checkBookmark($bookmark); | ||
166 | |||
167 | return $bookmark; | ||
168 | }) | ||
169 | ; | ||
170 | $this->container->bookmarkService | ||
171 | ->expects(static::once()) | ||
172 | ->method('set') | ||
173 | ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): Bookmark { | ||
174 | static::assertTrue($save); | ||
175 | |||
176 | $checkBookmark($bookmark); | ||
177 | |||
178 | static::assertSame($id, $bookmark->getId()); | ||
179 | |||
180 | return $bookmark; | ||
181 | }) | ||
182 | ; | ||
183 | |||
184 | // Make sure that PluginManager hook is triggered | ||
185 | $this->container->pluginManager | ||
186 | ->expects(static::atLeastOnce()) | ||
187 | ->method('executeHooks') | ||
188 | ->withConsecutive(['save_link']) | ||
189 | ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array { | ||
190 | if ('save_link' === $hook) { | ||
191 | static::assertSame($id, $data['id']); | ||
192 | static::assertSame($parameters['lf_url'], $data['url']); | ||
193 | static::assertSame($parameters['lf_title'], $data['title']); | ||
194 | static::assertSame($parameters['lf_description'], $data['description']); | ||
195 | static::assertSame($parameters['lf_tags'], $data['tags']); | ||
196 | static::assertTrue($data['private']); | ||
197 | } | ||
198 | |||
199 | return $data; | ||
200 | }) | ||
201 | ; | ||
202 | |||
203 | $result = $this->controller->save($request, $response); | ||
204 | |||
205 | static::assertSame(302, $result->getStatusCode()); | ||
206 | static::assertRegExp('@/subfolder/\?page=2#[\w\-]{6}@', $result->getHeader('location')[0]); | ||
207 | } | ||
208 | |||
209 | /** | ||
210 | * Test save a bookmark - try to retrieve the thumbnail | ||
211 | */ | ||
212 | public function testSaveBookmarkWithThumbnailSync(): void | ||
213 | { | ||
214 | $parameters = ['lf_url' => 'http://url.tld/other?part=3#hash']; | ||
215 | |||
216 | $request = $this->createMock(Request::class); | ||
217 | $request | ||
218 | ->method('getParam') | ||
219 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | ||
220 | return $parameters[$key] ?? null; | ||
221 | }) | ||
222 | ; | ||
223 | $response = new Response(); | ||
224 | |||
225 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
226 | $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) { | ||
227 | if ($key === 'thumbnails.mode') { | ||
228 | return Thumbnailer::MODE_ALL; | ||
229 | } elseif ($key === 'general.enable_async_metadata') { | ||
230 | return false; | ||
231 | } | ||
232 | |||
233 | return $default; | ||
234 | }); | ||
235 | |||
236 | $this->container->thumbnailer = $this->createMock(Thumbnailer::class); | ||
237 | $this->container->thumbnailer | ||
238 | ->expects(static::once()) | ||
239 | ->method('get') | ||
240 | ->with($parameters['lf_url']) | ||
241 | ->willReturn($thumb = 'http://thumb.url') | ||
242 | ; | ||
243 | |||
244 | $this->container->bookmarkService | ||
245 | ->expects(static::once()) | ||
246 | ->method('addOrSet') | ||
247 | ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($thumb): Bookmark { | ||
248 | static::assertSame($thumb, $bookmark->getThumbnail()); | ||
249 | |||
250 | return $bookmark; | ||
251 | }) | ||
252 | ; | ||
253 | |||
254 | $result = $this->controller->save($request, $response); | ||
255 | |||
256 | static::assertSame(302, $result->getStatusCode()); | ||
257 | } | ||
258 | |||
259 | /** | ||
260 | * Test save a bookmark - with ID #0 | ||
261 | */ | ||
262 | public function testSaveBookmarkWithIdZero(): void | ||
263 | { | ||
264 | $parameters = ['lf_id' => '0']; | ||
265 | |||
266 | $request = $this->createMock(Request::class); | ||
267 | $request | ||
268 | ->method('getParam') | ||
269 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | ||
270 | return $parameters[$key] ?? null; | ||
271 | }) | ||
272 | ; | ||
273 | $response = new Response(); | ||
274 | |||
275 | $this->container->bookmarkService->expects(static::once())->method('exists')->with(0)->willReturn(true); | ||
276 | $this->container->bookmarkService->expects(static::once())->method('get')->with(0)->willReturn(new Bookmark()); | ||
277 | |||
278 | $result = $this->controller->save($request, $response); | ||
279 | |||
280 | static::assertSame(302, $result->getStatusCode()); | ||
281 | } | ||
282 | |||
283 | /** | ||
284 | * Test save a bookmark - do not attempt to retrieve thumbnails if async mode is enabled. | ||
285 | */ | ||
286 | public function testSaveBookmarkWithThumbnailAsync(): void | ||
287 | { | ||
288 | $parameters = ['lf_url' => 'http://url.tld/other?part=3#hash']; | ||
289 | |||
290 | $request = $this->createMock(Request::class); | ||
291 | $request | ||
292 | ->method('getParam') | ||
293 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | ||
294 | return $parameters[$key] ?? null; | ||
295 | }) | ||
296 | ; | ||
297 | $response = new Response(); | ||
298 | |||
299 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
300 | $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) { | ||
301 | if ($key === 'thumbnails.mode') { | ||
302 | return Thumbnailer::MODE_ALL; | ||
303 | } elseif ($key === 'general.enable_async_metadata') { | ||
304 | return true; | ||
305 | } | ||
306 | |||
307 | return $default; | ||
308 | }); | ||
309 | |||
310 | $this->container->thumbnailer = $this->createMock(Thumbnailer::class); | ||
311 | $this->container->thumbnailer->expects(static::never())->method('get'); | ||
312 | |||
313 | $this->container->bookmarkService | ||
314 | ->expects(static::once()) | ||
315 | ->method('addOrSet') | ||
316 | ->willReturnCallback(function (Bookmark $bookmark): Bookmark { | ||
317 | static::assertNull($bookmark->getThumbnail()); | ||
318 | |||
319 | return $bookmark; | ||
320 | }) | ||
321 | ; | ||
322 | |||
323 | $result = $this->controller->save($request, $response); | ||
324 | |||
325 | static::assertSame(302, $result->getStatusCode()); | ||
326 | } | ||
327 | |||
328 | /** | ||
329 | * Change the password with a wrong existing password | ||
330 | */ | ||
331 | public function testSaveBookmarkFromBookmarklet(): void | ||
332 | { | ||
333 | $parameters = ['source' => 'bookmarklet']; | ||
334 | |||
335 | $request = $this->createMock(Request::class); | ||
336 | $request | ||
337 | ->method('getParam') | ||
338 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | ||
339 | return $parameters[$key] ?? null; | ||
340 | }) | ||
341 | ; | ||
342 | $response = new Response(); | ||
343 | |||
344 | $result = $this->controller->save($request, $response); | ||
345 | |||
346 | static::assertSame(200, $result->getStatusCode()); | ||
347 | static::assertSame('<script>self.close();</script>', (string) $result->getBody()); | ||
348 | } | ||
349 | |||
350 | /** | ||
351 | * Change the password with a wrong existing password | ||
352 | */ | ||
353 | public function testSaveBookmarkWrongToken(): void | ||
354 | { | ||
355 | $this->container->sessionManager = $this->createMock(SessionManager::class); | ||
356 | $this->container->sessionManager->method('checkToken')->willReturn(false); | ||
357 | |||
358 | $this->container->bookmarkService->expects(static::never())->method('addOrSet'); | ||
359 | $this->container->bookmarkService->expects(static::never())->method('set'); | ||
360 | |||
361 | $request = $this->createMock(Request::class); | ||
362 | $response = new Response(); | ||
363 | |||
364 | $this->expectException(WrongTokenException::class); | ||
365 | |||
366 | $this->controller->save($request, $response); | ||
367 | } | ||
368 | |||
369 | } | ||