]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php
Merge pull request #1693 from ArthurHoaro/fix/bulk-add-delete
[github/shaarli/Shaarli.git] / tests / front / controller / admin / ShaareManageControllerTest / DeleteBookmarkTest.php
CommitLineData
baa69791
A
1<?php
2
3declare(strict_types=1);
4
5d8de758 5namespace Shaarli\Front\Controller\Admin\ShaareManageControllerTest;
baa69791 6
baa69791
A
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
9use Shaarli\Formatter\BookmarkFormatter;
10use Shaarli\Formatter\FormatterFactory;
11use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper;
5d8de758 12use Shaarli\Front\Controller\Admin\ShaareManageController;
baa69791
A
13use Shaarli\Http\HttpAccess;
14use Shaarli\Security\SessionManager;
a5a9cf23 15use Shaarli\TestCase;
baa69791
A
16use Slim\Http\Request;
17use Slim\Http\Response;
18
19class DeleteBookmarkTest extends TestCase
20{
21 use FrontAdminControllerMockHelper;
22
5d8de758 23 /** @var ShaareManageController */
baa69791
A
24 protected $controller;
25
26 public function setUp(): void
27 {
28 $this->createContainer();
29
30 $this->container->httpAccess = $this->createMock(HttpAccess::class);
5d8de758 31 $this->controller = new ShaareManageController($this->container);
baa69791
A
32 }
33
34 /**
35 * Delete bookmark - Single bookmark with valid parameters
36 */
37 public function testDeleteSingleBookmark(): void
38 {
39 $parameters = ['id' => '123'];
40
330ac859
A
41 $this->container->environment['HTTP_REFERER'] = 'http://shaarli/subfolder/shaare/abcdef';
42
baa69791
A
43 $request = $this->createMock(Request::class);
44 $request
45 ->method('getParam')
46 ->willReturnCallback(function (string $key) use ($parameters): ?string {
47 return $parameters[$key] ?? null;
48 })
49 ;
50 $response = new Response();
51
52 $bookmark = (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123');
53
54 $this->container->bookmarkService->expects(static::once())->method('get')->with(123)->willReturn($bookmark);
55 $this->container->bookmarkService->expects(static::once())->method('remove')->with($bookmark, false);
56 $this->container->bookmarkService->expects(static::once())->method('save');
57 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
58 $this->container->formatterFactory
59 ->expects(static::once())
60 ->method('getFormatter')
61 ->with('raw')
62 ->willReturnCallback(function () use ($bookmark): BookmarkFormatter {
63 $formatter = $this->createMock(BookmarkFormatter::class);
9fbc4229
A
64 $formatter
65 ->expects(static::once())
66 ->method('format')
67 ->with($bookmark)
68 ->willReturn(['formatted' => $bookmark])
69 ;
baa69791
A
70
71 return $formatter;
72 })
73 ;
74
75 // Make sure that PluginManager hook is triggered
76 $this->container->pluginManager
77 ->expects(static::once())
78 ->method('executeHooks')
9fbc4229 79 ->with('delete_link', ['formatted' => $bookmark])
baa69791
A
80 ;
81
82 $result = $this->controller->deleteBookmark($request, $response);
83
84 static::assertSame(302, $result->getStatusCode());
85 static::assertSame(['/subfolder/'], $result->getHeader('location'));
86 }
87
88 /**
89 * Delete bookmark - Multiple bookmarks with valid parameters
90 */
91 public function testDeleteMultipleBookmarks(): void
92 {
93 $parameters = ['id' => '123 456 789'];
94
330ac859
A
95 $this->container->environment['HTTP_REFERER'] = 'http://shaarli/subfolder/?searchtags=abcdef';
96
baa69791
A
97 $request = $this->createMock(Request::class);
98 $request
99 ->method('getParam')
100 ->willReturnCallback(function (string $key) use ($parameters): ?string {
101 return $parameters[$key] ?? null;
102 })
103 ;
104 $response = new Response();
105
106 $bookmarks = [
107 (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123'),
108 (new Bookmark())->setId(456)->setUrl('http://domain.tld')->setTitle('Title 456'),
109 (new Bookmark())->setId(789)->setUrl('http://domain.tld')->setTitle('Title 789'),
110 ];
111
112 $this->container->bookmarkService
113 ->expects(static::exactly(3))
114 ->method('get')
115 ->withConsecutive([123], [456], [789])
116 ->willReturnOnConsecutiveCalls(...$bookmarks)
117 ;
118 $this->container->bookmarkService
119 ->expects(static::exactly(3))
120 ->method('remove')
121 ->withConsecutive(...array_map(function (Bookmark $bookmark): array {
122 return [$bookmark, false];
123 }, $bookmarks))
124 ;
125 $this->container->bookmarkService->expects(static::once())->method('save');
126 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
127 $this->container->formatterFactory
128 ->expects(static::once())
129 ->method('getFormatter')
130 ->with('raw')
131 ->willReturnCallback(function () use ($bookmarks): BookmarkFormatter {
132 $formatter = $this->createMock(BookmarkFormatter::class);
133
134 $formatter
135 ->expects(static::exactly(3))
136 ->method('format')
137 ->withConsecutive(...array_map(function (Bookmark $bookmark): array {
138 return [$bookmark];
139 }, $bookmarks))
9fbc4229
A
140 ->willReturnOnConsecutiveCalls(...array_map(function (Bookmark $bookmark): array {
141 return ['formatted' => $bookmark];
142 }, $bookmarks))
baa69791
A
143 ;
144
145 return $formatter;
146 })
147 ;
148
149 // Make sure that PluginManager hook is triggered
150 $this->container->pluginManager
151 ->expects(static::exactly(3))
152 ->method('executeHooks')
153 ->with('delete_link')
154 ;
155
156 $result = $this->controller->deleteBookmark($request, $response);
157
158 static::assertSame(302, $result->getStatusCode());
330ac859 159 static::assertSame(['/subfolder/?searchtags=abcdef'], $result->getHeader('location'));
baa69791
A
160 }
161
162 /**
163 * Delete bookmark - Single bookmark not found in the data store
164 */
165 public function testDeleteSingleBookmarkNotFound(): void
166 {
167 $parameters = ['id' => '123'];
168
169 $request = $this->createMock(Request::class);
170 $request
171 ->method('getParam')
172 ->willReturnCallback(function (string $key) use ($parameters): ?string {
173 return $parameters[$key] ?? null;
174 })
175 ;
176 $response = new Response();
177
178 $this->container->bookmarkService
179 ->expects(static::once())
180 ->method('get')
181 ->willThrowException(new BookmarkNotFoundException())
182 ;
183 $this->container->bookmarkService->expects(static::never())->method('remove');
184 $this->container->bookmarkService->expects(static::never())->method('save');
185 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
186 $this->container->formatterFactory
187 ->expects(static::once())
188 ->method('getFormatter')
189 ->with('raw')
190 ->willReturnCallback(function (): BookmarkFormatter {
191 $formatter = $this->createMock(BookmarkFormatter::class);
192
193 $formatter->expects(static::never())->method('format');
194
195 return $formatter;
196 })
197 ;
198 // Make sure that PluginManager hook is not triggered
199 $this->container->pluginManager
200 ->expects(static::never())
201 ->method('executeHooks')
202 ->with('delete_link')
203 ;
204
205 $result = $this->controller->deleteBookmark($request, $response);
206
207 static::assertSame(302, $result->getStatusCode());
208 static::assertSame(['/subfolder/'], $result->getHeader('location'));
209 }
210
211 /**
212 * Delete bookmark - Multiple bookmarks with one not found in the data store
213 */
214 public function testDeleteMultipleBookmarksOneNotFound(): void
215 {
216 $parameters = ['id' => '123 456 789'];
217
218 $request = $this->createMock(Request::class);
219 $request
220 ->method('getParam')
221 ->willReturnCallback(function (string $key) use ($parameters): ?string {
222 return $parameters[$key] ?? null;
223 })
224 ;
225 $response = new Response();
226
227 $bookmarks = [
228 (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123'),
229 (new Bookmark())->setId(789)->setUrl('http://domain.tld')->setTitle('Title 789'),
230 ];
231
232 $this->container->bookmarkService
233 ->expects(static::exactly(3))
234 ->method('get')
235 ->withConsecutive([123], [456], [789])
236 ->willReturnCallback(function (int $id) use ($bookmarks): Bookmark {
237 if ($id === 123) {
238 return $bookmarks[0];
239 }
240 if ($id === 789) {
241 return $bookmarks[1];
242 }
243 throw new BookmarkNotFoundException();
244 })
245 ;
246 $this->container->bookmarkService
247 ->expects(static::exactly(2))
248 ->method('remove')
249 ->withConsecutive(...array_map(function (Bookmark $bookmark): array {
250 return [$bookmark, false];
251 }, $bookmarks))
252 ;
253 $this->container->bookmarkService->expects(static::once())->method('save');
254 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
255 $this->container->formatterFactory
256 ->expects(static::once())
257 ->method('getFormatter')
258 ->with('raw')
259 ->willReturnCallback(function () use ($bookmarks): BookmarkFormatter {
260 $formatter = $this->createMock(BookmarkFormatter::class);
261
262 $formatter
263 ->expects(static::exactly(2))
264 ->method('format')
265 ->withConsecutive(...array_map(function (Bookmark $bookmark): array {
266 return [$bookmark];
267 }, $bookmarks))
9fbc4229
A
268 ->willReturnOnConsecutiveCalls(...array_map(function (Bookmark $bookmark): array {
269 return ['formatted' => $bookmark];
270 }, $bookmarks))
baa69791
A
271 ;
272
273 return $formatter;
274 })
275 ;
276
277 // Make sure that PluginManager hook is not triggered
278 $this->container->pluginManager
279 ->expects(static::exactly(2))
280 ->method('executeHooks')
281 ->with('delete_link')
282 ;
283
284 $this->container->sessionManager
285 ->expects(static::once())
286 ->method('setSessionParameter')
287 ->with(SessionManager::KEY_ERROR_MESSAGES, ['Bookmark with identifier 456 could not be found.'])
288 ;
289
290 $result = $this->controller->deleteBookmark($request, $response);
291
292 static::assertSame(302, $result->getStatusCode());
293 static::assertSame(['/subfolder/'], $result->getHeader('location'));
294 }
295
296 /**
297 * Delete bookmark - Invalid ID
298 */
299 public function testDeleteInvalidId(): void
300 {
301 $parameters = ['id' => 'nope not an ID'];
302
303 $request = $this->createMock(Request::class);
304 $request
305 ->method('getParam')
306 ->willReturnCallback(function (string $key) use ($parameters): ?string {
307 return $parameters[$key] ?? null;
308 })
309 ;
310 $response = new Response();
311
312 $this->container->sessionManager
313 ->expects(static::once())
314 ->method('setSessionParameter')
315 ->with(SessionManager::KEY_ERROR_MESSAGES, ['Invalid bookmark ID provided.'])
316 ;
317
318 $result = $this->controller->deleteBookmark($request, $response);
319
320 static::assertSame(302, $result->getStatusCode());
321 static::assertSame(['/subfolder/'], $result->getHeader('location'));
322 }
323
324 /**
325 * Delete bookmark - Empty ID
326 */
327 public function testDeleteEmptyId(): void
328 {
329 $request = $this->createMock(Request::class);
330 $response = new Response();
331
332 $this->container->sessionManager
333 ->expects(static::once())
334 ->method('setSessionParameter')
335 ->with(SessionManager::KEY_ERROR_MESSAGES, ['Invalid bookmark ID provided.'])
336 ;
337
338 $result = $this->controller->deleteBookmark($request, $response);
339
340 static::assertSame(302, $result->getStatusCode());
341 static::assertSame(['/subfolder/'], $result->getHeader('location'));
342 }
343
344 /**
345 * Delete bookmark - from bookmarklet
346 */
347 public function testDeleteBookmarkFromBookmarklet(): void
348 {
349 $parameters = [
350 'id' => '123',
351 'source' => 'bookmarklet',
352 ];
353
354 $request = $this->createMock(Request::class);
355 $request
356 ->method('getParam')
357 ->willReturnCallback(function (string $key) use ($parameters): ?string {
358 return $parameters[$key] ?? null;
359 })
360 ;
361 $response = new Response();
362
efb7d21b
A
363 $this->container->bookmarkService->method('get')->with('123')->willReturn(
364 (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123')
365 );
93175b6e 366 $this->container->bookmarkService->expects(static::once())->method('remove');
efb7d21b 367
baa69791
A
368 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
369 $this->container->formatterFactory
370 ->expects(static::once())
371 ->method('getFormatter')
9fbc4229
A
372 ->willReturnCallback(function (): BookmarkFormatter {
373 $formatter = $this->createMock(BookmarkFormatter::class);
374 $formatter->method('format')->willReturn(['formatted']);
375
376 return $formatter;
377 })
baa69791
A
378 ;
379
380 $result = $this->controller->deleteBookmark($request, $response);
381
382 static::assertSame(200, $result->getStatusCode());
93175b6e
A
383 static::assertSame('<script>self.close();</script>', (string) $result->getBody());
384 }
385
386 /**
387 * Delete bookmark - from batch view
388 */
389 public function testDeleteBookmarkFromBatch(): void
390 {
391 $parameters = [
392 'id' => '123',
393 'source' => 'batch',
394 ];
395
396 $request = $this->createMock(Request::class);
397 $request
398 ->method('getParam')
399 ->willReturnCallback(function (string $key) use ($parameters): ?string {
400 return $parameters[$key] ?? null;
401 })
402 ;
403 $response = new Response();
404
405 $this->container->bookmarkService->method('get')->with('123')->willReturn(
406 (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123')
407 );
408 $this->container->bookmarkService->expects(static::once())->method('remove');
409
410 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
411 $this->container->formatterFactory
412 ->expects(static::once())
413 ->method('getFormatter')
414 ->willReturnCallback(function (): BookmarkFormatter {
415 $formatter = $this->createMock(BookmarkFormatter::class);
416 $formatter->method('format')->willReturn(['formatted']);
417
418 return $formatter;
419 })
420 ;
421
422 $result = $this->controller->deleteBookmark($request, $response);
423
424 static::assertSame(204, $result->getStatusCode());
425 static::assertEmpty((string) $result->getBody());
baa69791
A
426 }
427}