]>
Commit | Line | Data |
---|---|---|
baa69791 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest; | |
6 | ||
7 | use PHPUnit\Framework\TestCase; | |
8 | use Shaarli\Bookmark\Bookmark; | |
9 | use Shaarli\Config\ConfigManager; | |
10 | use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; | |
11 | use Shaarli\Front\Controller\Admin\ManageShaareController; | |
12 | use Shaarli\Front\Exception\WrongTokenException; | |
13 | use Shaarli\Http\HttpAccess; | |
14 | use Shaarli\Security\SessionManager; | |
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.tld/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::at(0)) | |
92 | ->method('executeHooks') | |
93 | ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array { | |
94 | static::assertSame('save_link', $hook); | |
95 | ||
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 | return $data; | |
104 | }) | |
105 | ; | |
106 | ||
107 | $result = $this->controller->save($request, $response); | |
108 | ||
109 | static::assertSame(302, $result->getStatusCode()); | |
110 | static::assertRegExp('@/subfolder/#[\w\-]{6}@', $result->getHeader('location')[0]); | |
111 | } | |
112 | ||
113 | ||
114 | /** | |
115 | * Test save an existing bookmark | |
116 | */ | |
117 | public function testSaveExistingBookmark(): void | |
118 | { | |
119 | $id = 21; | |
120 | $parameters = [ | |
121 | 'lf_id' => (string) $id, | |
122 | 'lf_url' => 'http://url.tld/other?part=3#hash', | |
123 | 'lf_title' => 'Provided Title', | |
124 | 'lf_description' => 'Provided description.', | |
125 | 'lf_tags' => 'abc def', | |
126 | 'lf_private' => '1', | |
127 | 'returnurl' => 'http://shaarli.tld/subfolder/?page=2' | |
128 | ]; | |
129 | ||
130 | $request = $this->createMock(Request::class); | |
131 | $request | |
132 | ->method('getParam') | |
133 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | |
134 | return $parameters[$key] ?? null; | |
135 | }) | |
136 | ; | |
137 | $response = new Response(); | |
138 | ||
139 | $checkBookmark = function (Bookmark $bookmark) use ($parameters, $id) { | |
140 | static::assertSame($id, $bookmark->getId()); | |
141 | static::assertSame($parameters['lf_url'], $bookmark->getUrl()); | |
142 | static::assertSame($parameters['lf_title'], $bookmark->getTitle()); | |
143 | static::assertSame($parameters['lf_description'], $bookmark->getDescription()); | |
144 | static::assertSame($parameters['lf_tags'], $bookmark->getTagsString()); | |
145 | static::assertTrue($bookmark->isPrivate()); | |
146 | }; | |
147 | ||
148 | $this->container->bookmarkService->expects(static::atLeastOnce())->method('exists')->willReturn(true); | |
149 | $this->container->bookmarkService | |
150 | ->expects(static::once()) | |
151 | ->method('get') | |
152 | ->willReturn((new Bookmark())->setId($id)->setUrl('http://other.url')) | |
153 | ; | |
154 | $this->container->bookmarkService | |
155 | ->expects(static::once()) | |
156 | ->method('addOrSet') | |
157 | ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void { | |
158 | static::assertFalse($save); | |
159 | ||
160 | $checkBookmark($bookmark); | |
161 | }) | |
162 | ; | |
163 | $this->container->bookmarkService | |
164 | ->expects(static::once()) | |
165 | ->method('set') | |
166 | ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void { | |
167 | static::assertTrue($save); | |
168 | ||
169 | $checkBookmark($bookmark); | |
170 | ||
171 | static::assertSame($id, $bookmark->getId()); | |
172 | }) | |
173 | ; | |
174 | ||
175 | // Make sure that PluginManager hook is triggered | |
176 | $this->container->pluginManager | |
177 | ->expects(static::at(0)) | |
178 | ->method('executeHooks') | |
179 | ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array { | |
180 | static::assertSame('save_link', $hook); | |
181 | ||
182 | static::assertSame($id, $data['id']); | |
183 | static::assertSame($parameters['lf_url'], $data['url']); | |
184 | static::assertSame($parameters['lf_title'], $data['title']); | |
185 | static::assertSame($parameters['lf_description'], $data['description']); | |
186 | static::assertSame($parameters['lf_tags'], $data['tags']); | |
187 | static::assertTrue($data['private']); | |
188 | ||
189 | return $data; | |
190 | }) | |
191 | ; | |
192 | ||
193 | $result = $this->controller->save($request, $response); | |
194 | ||
195 | static::assertSame(302, $result->getStatusCode()); | |
196 | static::assertRegExp('@/subfolder/\?page=2#[\w\-]{6}@', $result->getHeader('location')[0]); | |
197 | } | |
198 | ||
199 | /** | |
200 | * Test save a bookmark - try to retrieve the thumbnail | |
201 | */ | |
202 | public function testSaveBookmarkWithThumbnail(): void | |
203 | { | |
204 | $parameters = ['lf_url' => 'http://url.tld/other?part=3#hash']; | |
205 | ||
206 | $request = $this->createMock(Request::class); | |
207 | $request | |
208 | ->method('getParam') | |
209 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | |
210 | return $parameters[$key] ?? null; | |
211 | }) | |
212 | ; | |
213 | $response = new Response(); | |
214 | ||
215 | $this->container->conf = $this->createMock(ConfigManager::class); | |
216 | $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) { | |
217 | return $key === 'thumbnails.mode' ? Thumbnailer::MODE_ALL : $default; | |
218 | }); | |
219 | ||
220 | $this->container->thumbnailer = $this->createMock(Thumbnailer::class); | |
221 | $this->container->thumbnailer | |
222 | ->expects(static::once()) | |
223 | ->method('get') | |
224 | ->with($parameters['lf_url']) | |
225 | ->willReturn($thumb = 'http://thumb.url') | |
226 | ; | |
227 | ||
228 | $this->container->bookmarkService | |
229 | ->expects(static::once()) | |
230 | ->method('addOrSet') | |
231 | ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($thumb): void { | |
232 | static::assertSame($thumb, $bookmark->getThumbnail()); | |
233 | }) | |
234 | ; | |
235 | ||
236 | $result = $this->controller->save($request, $response); | |
237 | ||
238 | static::assertSame(302, $result->getStatusCode()); | |
239 | } | |
240 | ||
241 | /** | |
242 | * Change the password with a wrong existing password | |
243 | */ | |
244 | public function testSaveBookmarkFromBookmarklet(): void | |
245 | { | |
246 | $parameters = ['source' => 'bookmarklet']; | |
247 | ||
248 | $request = $this->createMock(Request::class); | |
249 | $request | |
250 | ->method('getParam') | |
251 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | |
252 | return $parameters[$key] ?? null; | |
253 | }) | |
254 | ; | |
255 | $response = new Response(); | |
256 | ||
257 | $result = $this->controller->save($request, $response); | |
258 | ||
259 | static::assertSame(200, $result->getStatusCode()); | |
260 | static::assertSame('<script>self.close();</script>', (string) $result->getBody()); | |
261 | } | |
262 | ||
263 | /** | |
264 | * Change the password with a wrong existing password | |
265 | */ | |
266 | public function testSaveBookmarkWrongToken(): void | |
267 | { | |
268 | $this->container->sessionManager = $this->createMock(SessionManager::class); | |
269 | $this->container->sessionManager->method('checkToken')->willReturn(false); | |
270 | ||
271 | $this->container->bookmarkService->expects(static::never())->method('addOrSet'); | |
272 | $this->container->bookmarkService->expects(static::never())->method('set'); | |
273 | ||
274 | $request = $this->createMock(Request::class); | |
275 | $response = new Response(); | |
276 | ||
277 | $this->expectException(WrongTokenException::class); | |
278 | ||
279 | $this->controller->save($request, $response); | |
280 | } | |
281 | ||
282 | } |