aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-06-13 15:37:02 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commitbaa6979194573855b260593094983c33ec338dc7 (patch)
tree9e67e798ac6ad402e77ad8d7ee6c6621184a0255 /tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php
parent9c75f877935fa6adec951a4d8d32b328aaab314f (diff)
downloadShaarli-baa6979194573855b260593094983c33ec338dc7.tar.gz
Shaarli-baa6979194573855b260593094983c33ec338dc7.tar.zst
Shaarli-baa6979194573855b260593094983c33ec338dc7.zip
Improve ManageTagController coverage and error handling
Diffstat (limited to 'tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php')
-rw-r--r--tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php361
1 files changed, 361 insertions, 0 deletions
diff --git a/tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php b/tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php
new file mode 100644
index 00000000..caaf549d
--- /dev/null
+++ b/tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php
@@ -0,0 +1,361 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\Bookmark;
9use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
10use Shaarli\Formatter\BookmarkFormatter;
11use Shaarli\Formatter\FormatterFactory;
12use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper;
13use Shaarli\Front\Controller\Admin\ManageShaareController;
14use Shaarli\Http\HttpAccess;
15use Shaarli\Security\SessionManager;
16use Slim\Http\Request;
17use Slim\Http\Response;
18
19class DeleteBookmarkTest extends TestCase
20{
21 use FrontAdminControllerMockHelper;
22
23 /** @var ManageShaareController */
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 ManageShaareController($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 $request = $this->createMock(Request::class);
42 $request
43 ->method('getParam')
44 ->willReturnCallback(function (string $key) use ($parameters): ?string {
45 return $parameters[$key] ?? null;
46 })
47 ;
48 $response = new Response();
49
50 $bookmark = (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123');
51
52 $this->container->bookmarkService->expects(static::once())->method('get')->with(123)->willReturn($bookmark);
53 $this->container->bookmarkService->expects(static::once())->method('remove')->with($bookmark, false);
54 $this->container->bookmarkService->expects(static::once())->method('save');
55 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
56 $this->container->formatterFactory
57 ->expects(static::once())
58 ->method('getFormatter')
59 ->with('raw')
60 ->willReturnCallback(function () use ($bookmark): BookmarkFormatter {
61 $formatter = $this->createMock(BookmarkFormatter::class);
62
63 $formatter->expects(static::once())->method('format')->with($bookmark);
64
65 return $formatter;
66 })
67 ;
68
69 // Make sure that PluginManager hook is triggered
70 $this->container->pluginManager
71 ->expects(static::once())
72 ->method('executeHooks')
73 ->with('delete_link')
74 ;
75
76 $result = $this->controller->deleteBookmark($request, $response);
77
78 static::assertSame(302, $result->getStatusCode());
79 static::assertSame(['/subfolder/'], $result->getHeader('location'));
80 }
81
82 /**
83 * Delete bookmark - Multiple bookmarks with valid parameters
84 */
85 public function testDeleteMultipleBookmarks(): void
86 {
87 $parameters = ['id' => '123 456 789'];
88
89 $request = $this->createMock(Request::class);
90 $request
91 ->method('getParam')
92 ->willReturnCallback(function (string $key) use ($parameters): ?string {
93 return $parameters[$key] ?? null;
94 })
95 ;
96 $response = new Response();
97
98 $bookmarks = [
99 (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123'),
100 (new Bookmark())->setId(456)->setUrl('http://domain.tld')->setTitle('Title 456'),
101 (new Bookmark())->setId(789)->setUrl('http://domain.tld')->setTitle('Title 789'),
102 ];
103
104 $this->container->bookmarkService
105 ->expects(static::exactly(3))
106 ->method('get')
107 ->withConsecutive([123], [456], [789])
108 ->willReturnOnConsecutiveCalls(...$bookmarks)
109 ;
110 $this->container->bookmarkService
111 ->expects(static::exactly(3))
112 ->method('remove')
113 ->withConsecutive(...array_map(function (Bookmark $bookmark): array {
114 return [$bookmark, false];
115 }, $bookmarks))
116 ;
117 $this->container->bookmarkService->expects(static::once())->method('save');
118 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
119 $this->container->formatterFactory
120 ->expects(static::once())
121 ->method('getFormatter')
122 ->with('raw')
123 ->willReturnCallback(function () use ($bookmarks): BookmarkFormatter {
124 $formatter = $this->createMock(BookmarkFormatter::class);
125
126 $formatter
127 ->expects(static::exactly(3))
128 ->method('format')
129 ->withConsecutive(...array_map(function (Bookmark $bookmark): array {
130 return [$bookmark];
131 }, $bookmarks))
132 ;
133
134 return $formatter;
135 })
136 ;
137
138 // Make sure that PluginManager hook is triggered
139 $this->container->pluginManager
140 ->expects(static::exactly(3))
141 ->method('executeHooks')
142 ->with('delete_link')
143 ;
144
145 $result = $this->controller->deleteBookmark($request, $response);
146
147 static::assertSame(302, $result->getStatusCode());
148 static::assertSame(['/subfolder/'], $result->getHeader('location'));
149 }
150
151 /**
152 * Delete bookmark - Single bookmark not found in the data store
153 */
154 public function testDeleteSingleBookmarkNotFound(): void
155 {
156 $parameters = ['id' => '123'];
157
158 $request = $this->createMock(Request::class);
159 $request
160 ->method('getParam')
161 ->willReturnCallback(function (string $key) use ($parameters): ?string {
162 return $parameters[$key] ?? null;
163 })
164 ;
165 $response = new Response();
166
167 $this->container->bookmarkService
168 ->expects(static::once())
169 ->method('get')
170 ->willThrowException(new BookmarkNotFoundException())
171 ;
172 $this->container->bookmarkService->expects(static::never())->method('remove');
173 $this->container->bookmarkService->expects(static::never())->method('save');
174 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
175 $this->container->formatterFactory
176 ->expects(static::once())
177 ->method('getFormatter')
178 ->with('raw')
179 ->willReturnCallback(function (): BookmarkFormatter {
180 $formatter = $this->createMock(BookmarkFormatter::class);
181
182 $formatter->expects(static::never())->method('format');
183
184 return $formatter;
185 })
186 ;
187 // Make sure that PluginManager hook is not triggered
188 $this->container->pluginManager
189 ->expects(static::never())
190 ->method('executeHooks')
191 ->with('delete_link')
192 ;
193
194 $result = $this->controller->deleteBookmark($request, $response);
195
196 static::assertSame(302, $result->getStatusCode());
197 static::assertSame(['/subfolder/'], $result->getHeader('location'));
198 }
199
200 /**
201 * Delete bookmark - Multiple bookmarks with one not found in the data store
202 */
203 public function testDeleteMultipleBookmarksOneNotFound(): void
204 {
205 $parameters = ['id' => '123 456 789'];
206
207 $request = $this->createMock(Request::class);
208 $request
209 ->method('getParam')
210 ->willReturnCallback(function (string $key) use ($parameters): ?string {
211 return $parameters[$key] ?? null;
212 })
213 ;
214 $response = new Response();
215
216 $bookmarks = [
217 (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123'),
218 (new Bookmark())->setId(789)->setUrl('http://domain.tld')->setTitle('Title 789'),
219 ];
220
221 $this->container->bookmarkService
222 ->expects(static::exactly(3))
223 ->method('get')
224 ->withConsecutive([123], [456], [789])
225 ->willReturnCallback(function (int $id) use ($bookmarks): Bookmark {
226 if ($id === 123) {
227 return $bookmarks[0];
228 }
229 if ($id === 789) {
230 return $bookmarks[1];
231 }
232 throw new BookmarkNotFoundException();
233 })
234 ;
235 $this->container->bookmarkService
236 ->expects(static::exactly(2))
237 ->method('remove')
238 ->withConsecutive(...array_map(function (Bookmark $bookmark): array {
239 return [$bookmark, false];
240 }, $bookmarks))
241 ;
242 $this->container->bookmarkService->expects(static::once())->method('save');
243 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
244 $this->container->formatterFactory
245 ->expects(static::once())
246 ->method('getFormatter')
247 ->with('raw')
248 ->willReturnCallback(function () use ($bookmarks): BookmarkFormatter {
249 $formatter = $this->createMock(BookmarkFormatter::class);
250
251 $formatter
252 ->expects(static::exactly(2))
253 ->method('format')
254 ->withConsecutive(...array_map(function (Bookmark $bookmark): array {
255 return [$bookmark];
256 }, $bookmarks))
257 ;
258
259 return $formatter;
260 })
261 ;
262
263 // Make sure that PluginManager hook is not triggered
264 $this->container->pluginManager
265 ->expects(static::exactly(2))
266 ->method('executeHooks')
267 ->with('delete_link')
268 ;
269
270 $this->container->sessionManager
271 ->expects(static::once())
272 ->method('setSessionParameter')
273 ->with(SessionManager::KEY_ERROR_MESSAGES, ['Bookmark with identifier 456 could not be found.'])
274 ;
275
276 $result = $this->controller->deleteBookmark($request, $response);
277
278 static::assertSame(302, $result->getStatusCode());
279 static::assertSame(['/subfolder/'], $result->getHeader('location'));
280 }
281
282 /**
283 * Delete bookmark - Invalid ID
284 */
285 public function testDeleteInvalidId(): void
286 {
287 $parameters = ['id' => 'nope not an ID'];
288
289 $request = $this->createMock(Request::class);
290 $request
291 ->method('getParam')
292 ->willReturnCallback(function (string $key) use ($parameters): ?string {
293 return $parameters[$key] ?? null;
294 })
295 ;
296 $response = new Response();
297
298 $this->container->sessionManager
299 ->expects(static::once())
300 ->method('setSessionParameter')
301 ->with(SessionManager::KEY_ERROR_MESSAGES, ['Invalid bookmark ID provided.'])
302 ;
303
304 $result = $this->controller->deleteBookmark($request, $response);
305
306 static::assertSame(302, $result->getStatusCode());
307 static::assertSame(['/subfolder/'], $result->getHeader('location'));
308 }
309
310 /**
311 * Delete bookmark - Empty ID
312 */
313 public function testDeleteEmptyId(): void
314 {
315 $request = $this->createMock(Request::class);
316 $response = new Response();
317
318 $this->container->sessionManager
319 ->expects(static::once())
320 ->method('setSessionParameter')
321 ->with(SessionManager::KEY_ERROR_MESSAGES, ['Invalid bookmark ID provided.'])
322 ;
323
324 $result = $this->controller->deleteBookmark($request, $response);
325
326 static::assertSame(302, $result->getStatusCode());
327 static::assertSame(['/subfolder/'], $result->getHeader('location'));
328 }
329
330 /**
331 * Delete bookmark - from bookmarklet
332 */
333 public function testDeleteBookmarkFromBookmarklet(): void
334 {
335 $parameters = [
336 'id' => '123',
337 'source' => 'bookmarklet',
338 ];
339
340 $request = $this->createMock(Request::class);
341 $request
342 ->method('getParam')
343 ->willReturnCallback(function (string $key) use ($parameters): ?string {
344 return $parameters[$key] ?? null;
345 })
346 ;
347 $response = new Response();
348
349 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
350 $this->container->formatterFactory
351 ->expects(static::once())
352 ->method('getFormatter')
353 ->willReturn($this->createMock(BookmarkFormatter::class))
354 ;
355
356 $result = $this->controller->deleteBookmark($request, $response);
357
358 static::assertSame(200, $result->getStatusCode());
359 static::assertSame('<script>self.close();</script>', (string) $result->getBody('location'));
360 }
361}