]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
Compatibility with PHPUnit 9
[github/shaarli/Shaarli.git] / tests / front / controller / admin / ManageShaareControllerTest / SaveBookmarkTest.php
CommitLineData
baa69791
A
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest;
6
baa69791
A
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;
a5a9cf23 14use Shaarli\TestCase;
baa69791
A
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',
abe033be 46 'returnurl' => 'http://shaarli/subfolder/admin/add-shaare'
baa69791
A
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
a5a9cf23 91 ->expects(static::atLeastOnce())
baa69791 92 ->method('executeHooks')
a5a9cf23 93 ->withConsecutive(['save_link'])
baa69791 94 ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
a5a9cf23
A
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 }
baa69791
A
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',
abe033be 128 'returnurl' => 'http://shaarli/subfolder/?page=2'
baa69791
A
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
a5a9cf23 178 ->expects(static::atLeastOnce())
baa69791 179 ->method('executeHooks')
a5a9cf23 180 ->withConsecutive(['save_link'])
baa69791 181 ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
a5a9cf23
A
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 }
baa69791
A
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 * Change the password with a wrong existing password
245 */
246 public function testSaveBookmarkFromBookmarklet(): void
247 {
248 $parameters = ['source' => 'bookmarklet'];
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 $result = $this->controller->save($request, $response);
260
261 static::assertSame(200, $result->getStatusCode());
262 static::assertSame('<script>self.close();</script>', (string) $result->getBody());
263 }
264
265 /**
266 * Change the password with a wrong existing password
267 */
268 public function testSaveBookmarkWrongToken(): void
269 {
270 $this->container->sessionManager = $this->createMock(SessionManager::class);
271 $this->container->sessionManager->method('checkToken')->willReturn(false);
272
273 $this->container->bookmarkService->expects(static::never())->method('addOrSet');
274 $this->container->bookmarkService->expects(static::never())->method('set');
275
276 $request = $this->createMock(Request::class);
277 $response = new Response();
278
279 $this->expectException(WrongTokenException::class);
280
281 $this->controller->save($request, $response);
282 }
283
284}