aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php')
-rw-r--r--tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php384
1 files changed, 384 insertions, 0 deletions
diff --git a/tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php b/tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php
new file mode 100644
index 00000000..a276d988
--- /dev/null
+++ b/tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php
@@ -0,0 +1,384 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin\ShaareManageControllerTest;
6
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
9use Shaarli\Formatter\BookmarkFormatter;
10use Shaarli\Formatter\FormatterFactory;
11use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper;
12use Shaarli\Front\Controller\Admin\ShaareManageController;
13use Shaarli\Http\HttpAccess;
14use Shaarli\Security\SessionManager;
15use Shaarli\TestCase;
16use Slim\Http\Request;
17use Slim\Http\Response;
18
19class DeleteBookmarkTest extends TestCase
20{
21 use FrontAdminControllerMockHelper;
22
23 /** @var ShaareManageController */
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 ShaareManageController($this->container);
32 }
33
34 /**
35 * Delete bookmark - Single bookmark with valid parameters
36 */
37 public function testDeleteSingleBookmark(): void
38 {
39 $parameters = ['id' => '123'];
40
41 $this->container->environment['HTTP_REFERER'] = 'http://shaarli/subfolder/shaare/abcdef';
42
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);
64 $formatter
65 ->expects(static::once())
66 ->method('format')
67 ->with($bookmark)
68 ->willReturn(['formatted' => $bookmark])
69 ;
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')
79 ->with('delete_link', ['formatted' => $bookmark])
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
95 $this->container->environment['HTTP_REFERER'] = 'http://shaarli/subfolder/?searchtags=abcdef';
96
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))
140 ->willReturnOnConsecutiveCalls(...array_map(function (Bookmark $bookmark): array {
141 return ['formatted' => $bookmark];
142 }, $bookmarks))
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());
159 static::assertSame(['/subfolder/?searchtags=abcdef'], $result->getHeader('location'));
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))
268 ->willReturnOnConsecutiveCalls(...array_map(function (Bookmark $bookmark): array {
269 return ['formatted' => $bookmark];
270 }, $bookmarks))
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
363 $this->container->bookmarkService->method('get')->with('123')->willReturn(
364 (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123')
365 );
366
367 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
368 $this->container->formatterFactory
369 ->expects(static::once())
370 ->method('getFormatter')
371 ->willReturnCallback(function (): BookmarkFormatter {
372 $formatter = $this->createMock(BookmarkFormatter::class);
373 $formatter->method('format')->willReturn(['formatted']);
374
375 return $formatter;
376 })
377 ;
378
379 $result = $this->controller->deleteBookmark($request, $response);
380
381 static::assertSame(200, $result->getStatusCode());
382 static::assertSame('<script>self.close();</script>', (string) $result->getBody('location'));
383 }
384}