]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
Update translations (Japanese)
[github/shaarli/Shaarli.git] / tests / front / controller / admin / ManageShaareControllerTest / SaveBookmarkTest.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest;
6
7 use Shaarli\Bookmark\Bookmark;
8 use Shaarli\Config\ConfigManager;
9 use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper;
10 use Shaarli\Front\Controller\Admin\ManageShaareController;
11 use Shaarli\Front\Exception\WrongTokenException;
12 use Shaarli\Http\HttpAccess;
13 use Shaarli\Security\SessionManager;
14 use Shaarli\TestCase;
15 use Shaarli\Thumbnailer;
16 use Slim\Http\Request;
17 use Slim\Http\Response;
18
19 class 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 testSaveBookmarkWithThumbnail(): 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 return $key === 'thumbnails.mode' ? Thumbnailer::MODE_ALL : $default;
228 });
229
230 $this->container->thumbnailer = $this->createMock(Thumbnailer::class);
231 $this->container->thumbnailer
232 ->expects(static::once())
233 ->method('get')
234 ->with($parameters['lf_url'])
235 ->willReturn($thumb = 'http://thumb.url')
236 ;
237
238 $this->container->bookmarkService
239 ->expects(static::once())
240 ->method('addOrSet')
241 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($thumb): Bookmark {
242 static::assertSame($thumb, $bookmark->getThumbnail());
243
244 return $bookmark;
245 })
246 ;
247
248 $result = $this->controller->save($request, $response);
249
250 static::assertSame(302, $result->getStatusCode());
251 }
252
253 /**
254 * Test save a bookmark - with ID #0
255 */
256 public function testSaveBookmarkWithIdZero(): void
257 {
258 $parameters = ['lf_id' => '0'];
259
260 $request = $this->createMock(Request::class);
261 $request
262 ->method('getParam')
263 ->willReturnCallback(function (string $key) use ($parameters): ?string {
264 return $parameters[$key] ?? null;
265 })
266 ;
267 $response = new Response();
268
269 $this->container->bookmarkService->expects(static::once())->method('exists')->with(0)->willReturn(true);
270 $this->container->bookmarkService->expects(static::once())->method('get')->with(0)->willReturn(new Bookmark());
271
272 $result = $this->controller->save($request, $response);
273
274 static::assertSame(302, $result->getStatusCode());
275 }
276
277 /**
278 * Change the password with a wrong existing password
279 */
280 public function testSaveBookmarkFromBookmarklet(): void
281 {
282 $parameters = ['source' => 'bookmarklet'];
283
284 $request = $this->createMock(Request::class);
285 $request
286 ->method('getParam')
287 ->willReturnCallback(function (string $key) use ($parameters): ?string {
288 return $parameters[$key] ?? null;
289 })
290 ;
291 $response = new Response();
292
293 $result = $this->controller->save($request, $response);
294
295 static::assertSame(200, $result->getStatusCode());
296 static::assertSame('<script>self.close();</script>', (string) $result->getBody());
297 }
298
299 /**
300 * Change the password with a wrong existing password
301 */
302 public function testSaveBookmarkWrongToken(): void
303 {
304 $this->container->sessionManager = $this->createMock(SessionManager::class);
305 $this->container->sessionManager->method('checkToken')->willReturn(false);
306
307 $this->container->bookmarkService->expects(static::never())->method('addOrSet');
308 $this->container->bookmarkService->expects(static::never())->method('set');
309
310 $request = $this->createMock(Request::class);
311 $response = new Response();
312
313 $this->expectException(WrongTokenException::class);
314
315 $this->controller->save($request, $response);
316 }
317
318 }