aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php')
-rw-r--r--tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php369
1 files changed, 0 insertions, 369 deletions
diff --git a/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php b/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
deleted file mode 100644
index 1adeef5a..00000000
--- a/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
+++ /dev/null
@@ -1,369 +0,0 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest;
6
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper;
10use Shaarli\Front\Controller\Admin\ManageShaareController;
11use Shaarli\Front\Exception\WrongTokenException;
12use Shaarli\Http\HttpAccess;
13use Shaarli\Security\SessionManager;
14use Shaarli\TestCase;
15use Shaarli\Thumbnailer;
16use Slim\Http\Request;
17use Slim\Http\Response;
18
19class SaveBookmarkTest 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 * 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}