aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-10-13 12:05:08 +0200
committerArthurHoaro <arthur@hoa.ro>2020-10-13 12:05:08 +0200
commitb6f678a5a1d15acf284ebcec16c905e976671ce1 (patch)
tree33c7da831482ed79c44896ef19c73c72ada84f2e /tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
parentb14687036b9b800681197f51fdc47e62f0c88e2e (diff)
parent1c1520b6b98ab20201bfe15577782a52320339df (diff)
downloadShaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz
Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst
Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip
Merge branch 'v0.12' into latest
Diffstat (limited to 'tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php')
-rw-r--r--tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php308
1 files changed, 308 insertions, 0 deletions
diff --git a/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php b/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
new file mode 100644
index 00000000..f7a68226
--- /dev/null
+++ b/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
@@ -0,0 +1,308 @@
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): void {
70 static::assertFalse($save);
71
72 $checkBookmark($bookmark);
73
74 $bookmark->setId($id);
75 })
76 ;
77 $this->container->bookmarkService
78 ->expects(static::once())
79 ->method('set')
80 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void {
81 static::assertTrue($save);
82
83 $checkBookmark($bookmark);
84
85 static::assertSame($id, $bookmark->getId());
86 })
87 ;
88
89 // Make sure that PluginManager hook is triggered
90 $this->container->pluginManager
91 ->expects(static::atLeastOnce())
92 ->method('executeHooks')
93 ->withConsecutive(['save_link'])
94 ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
95 if ('save_link' === $hook) {
96 static::assertSame($id, $data['id']);
97 static::assertSame($parameters['lf_url'], $data['url']);
98 static::assertSame($parameters['lf_title'], $data['title']);
99 static::assertSame($parameters['lf_description'], $data['description']);
100 static::assertSame($parameters['lf_tags'], $data['tags']);
101 static::assertTrue($data['private']);
102 }
103
104 return $data;
105 })
106 ;
107
108 $result = $this->controller->save($request, $response);
109
110 static::assertSame(302, $result->getStatusCode());
111 static::assertRegExp('@/subfolder/#[\w\-]{6}@', $result->getHeader('location')[0]);
112 }
113
114
115 /**
116 * Test save an existing bookmark
117 */
118 public function testSaveExistingBookmark(): void
119 {
120 $id = 21;
121 $parameters = [
122 'lf_id' => (string) $id,
123 'lf_url' => 'http://url.tld/other?part=3#hash',
124 'lf_title' => 'Provided Title',
125 'lf_description' => 'Provided description.',
126 'lf_tags' => 'abc def',
127 'lf_private' => '1',
128 'returnurl' => 'http://shaarli/subfolder/?page=2'
129 ];
130
131 $request = $this->createMock(Request::class);
132 $request
133 ->method('getParam')
134 ->willReturnCallback(function (string $key) use ($parameters): ?string {
135 return $parameters[$key] ?? null;
136 })
137 ;
138 $response = new Response();
139
140 $checkBookmark = function (Bookmark $bookmark) use ($parameters, $id) {
141 static::assertSame($id, $bookmark->getId());
142 static::assertSame($parameters['lf_url'], $bookmark->getUrl());
143 static::assertSame($parameters['lf_title'], $bookmark->getTitle());
144 static::assertSame($parameters['lf_description'], $bookmark->getDescription());
145 static::assertSame($parameters['lf_tags'], $bookmark->getTagsString());
146 static::assertTrue($bookmark->isPrivate());
147 };
148
149 $this->container->bookmarkService->expects(static::atLeastOnce())->method('exists')->willReturn(true);
150 $this->container->bookmarkService
151 ->expects(static::once())
152 ->method('get')
153 ->willReturn((new Bookmark())->setId($id)->setUrl('http://other.url'))
154 ;
155 $this->container->bookmarkService
156 ->expects(static::once())
157 ->method('addOrSet')
158 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void {
159 static::assertFalse($save);
160
161 $checkBookmark($bookmark);
162 })
163 ;
164 $this->container->bookmarkService
165 ->expects(static::once())
166 ->method('set')
167 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void {
168 static::assertTrue($save);
169
170 $checkBookmark($bookmark);
171
172 static::assertSame($id, $bookmark->getId());
173 })
174 ;
175
176 // Make sure that PluginManager hook is triggered
177 $this->container->pluginManager
178 ->expects(static::atLeastOnce())
179 ->method('executeHooks')
180 ->withConsecutive(['save_link'])
181 ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
182 if ('save_link' === $hook) {
183 static::assertSame($id, $data['id']);
184 static::assertSame($parameters['lf_url'], $data['url']);
185 static::assertSame($parameters['lf_title'], $data['title']);
186 static::assertSame($parameters['lf_description'], $data['description']);
187 static::assertSame($parameters['lf_tags'], $data['tags']);
188 static::assertTrue($data['private']);
189 }
190
191 return $data;
192 })
193 ;
194
195 $result = $this->controller->save($request, $response);
196
197 static::assertSame(302, $result->getStatusCode());
198 static::assertRegExp('@/subfolder/\?page=2#[\w\-]{6}@', $result->getHeader('location')[0]);
199 }
200
201 /**
202 * Test save a bookmark - try to retrieve the thumbnail
203 */
204 public function testSaveBookmarkWithThumbnail(): void
205 {
206 $parameters = ['lf_url' => 'http://url.tld/other?part=3#hash'];
207
208 $request = $this->createMock(Request::class);
209 $request
210 ->method('getParam')
211 ->willReturnCallback(function (string $key) use ($parameters): ?string {
212 return $parameters[$key] ?? null;
213 })
214 ;
215 $response = new Response();
216
217 $this->container->conf = $this->createMock(ConfigManager::class);
218 $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) {
219 return $key === 'thumbnails.mode' ? Thumbnailer::MODE_ALL : $default;
220 });
221
222 $this->container->thumbnailer = $this->createMock(Thumbnailer::class);
223 $this->container->thumbnailer
224 ->expects(static::once())
225 ->method('get')
226 ->with($parameters['lf_url'])
227 ->willReturn($thumb = 'http://thumb.url')
228 ;
229
230 $this->container->bookmarkService
231 ->expects(static::once())
232 ->method('addOrSet')
233 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($thumb): void {
234 static::assertSame($thumb, $bookmark->getThumbnail());
235 })
236 ;
237
238 $result = $this->controller->save($request, $response);
239
240 static::assertSame(302, $result->getStatusCode());
241 }
242
243 /**
244 * Test save a bookmark - with ID #0
245 */
246 public function testSaveBookmarkWithIdZero(): void
247 {
248 $parameters = ['lf_id' => '0'];
249
250 $request = $this->createMock(Request::class);
251 $request
252 ->method('getParam')
253 ->willReturnCallback(function (string $key) use ($parameters): ?string {
254 return $parameters[$key] ?? null;
255 })
256 ;
257 $response = new Response();
258
259 $this->container->bookmarkService->expects(static::once())->method('exists')->with(0)->willReturn(true);
260 $this->container->bookmarkService->expects(static::once())->method('get')->with(0)->willReturn(new Bookmark());
261
262 $result = $this->controller->save($request, $response);
263
264 static::assertSame(302, $result->getStatusCode());
265 }
266
267 /**
268 * Change the password with a wrong existing password
269 */
270 public function testSaveBookmarkFromBookmarklet(): void
271 {
272 $parameters = ['source' => 'bookmarklet'];
273
274 $request = $this->createMock(Request::class);
275 $request
276 ->method('getParam')
277 ->willReturnCallback(function (string $key) use ($parameters): ?string {
278 return $parameters[$key] ?? null;
279 })
280 ;
281 $response = new Response();
282
283 $result = $this->controller->save($request, $response);
284
285 static::assertSame(200, $result->getStatusCode());
286 static::assertSame('<script>self.close();</script>', (string) $result->getBody());
287 }
288
289 /**
290 * Change the password with a wrong existing password
291 */
292 public function testSaveBookmarkWrongToken(): void
293 {
294 $this->container->sessionManager = $this->createMock(SessionManager::class);
295 $this->container->sessionManager->method('checkToken')->willReturn(false);
296
297 $this->container->bookmarkService->expects(static::never())->method('addOrSet');
298 $this->container->bookmarkService->expects(static::never())->method('set');
299
300 $request = $this->createMock(Request::class);
301 $response = new Response();
302
303 $this->expectException(WrongTokenException::class);
304
305 $this->controller->save($request, $response);
306 }
307
308}