]>
Commit | Line | Data |
---|---|---|
8eac2e54 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin; | |
6 | ||
8eac2e54 A |
7 | use Shaarli\Bookmark\Bookmark; |
8 | use Shaarli\Bookmark\BookmarkFilter; | |
9b8c0a45 | 9 | use Shaarli\Bookmark\SearchResult; |
b3bd8c3e | 10 | use Shaarli\Config\ConfigManager; |
8eac2e54 A |
11 | use Shaarli\Front\Exception\WrongTokenException; |
12 | use Shaarli\Security\SessionManager; | |
a5a9cf23 | 13 | use Shaarli\TestCase; |
8eac2e54 A |
14 | use Slim\Http\Request; |
15 | use Slim\Http\Response; | |
16 | ||
17 | class ManageTagControllerTest extends TestCase | |
18 | { | |
19 | use FrontAdminControllerMockHelper; | |
20 | ||
21 | /** @var ManageTagController */ | |
22 | protected $controller; | |
23 | ||
24 | public function setUp(): void | |
25 | { | |
26 | $this->createContainer(); | |
27 | ||
28 | $this->controller = new ManageTagController($this->container); | |
29 | } | |
30 | ||
31 | /** | |
32 | * Test displaying manage tag page | |
33 | */ | |
34 | public function testIndex(): void | |
35 | { | |
36 | $assignedVariables = []; | |
37 | $this->assignTemplateVars($assignedVariables); | |
38 | ||
39 | $request = $this->createMock(Request::class); | |
40 | $request->method('getParam')->with('fromtag')->willReturn('fromtag'); | |
41 | $response = new Response(); | |
42 | ||
43 | $result = $this->controller->index($request, $response); | |
44 | ||
45 | static::assertSame(200, $result->getStatusCode()); | |
46 | static::assertSame('changetag', (string) $result->getBody()); | |
47 | ||
48 | static::assertSame('fromtag', $assignedVariables['fromtag']); | |
b3bd8c3e | 49 | static::assertSame('@', $assignedVariables['tags_separator']); |
8eac2e54 A |
50 | static::assertSame('Manage tags - Shaarli', $assignedVariables['pagetitle']); |
51 | } | |
52 | ||
b3bd8c3e A |
53 | /** |
54 | * Test displaying manage tag page | |
55 | */ | |
56 | public function testIndexWhitespaceSeparator(): void | |
57 | { | |
58 | $assignedVariables = []; | |
59 | $this->assignTemplateVars($assignedVariables); | |
60 | ||
61 | $this->container->conf = $this->createMock(ConfigManager::class); | |
62 | $this->container->conf->method('get')->willReturnCallback(function (string $key) { | |
63 | return $key === 'general.tags_separator' ? ' ' : $key; | |
64 | }); | |
65 | ||
66 | $request = $this->createMock(Request::class); | |
67 | $response = new Response(); | |
68 | ||
69 | $this->controller->index($request, $response); | |
70 | ||
71 | static::assertSame(' ', $assignedVariables['tags_separator']); | |
72 | static::assertSame('whitespace', $assignedVariables['tags_separator_desc']); | |
73 | } | |
74 | ||
8eac2e54 A |
75 | /** |
76 | * Test posting a tag update - rename tag - valid info provided. | |
77 | */ | |
78 | public function testSaveRenameTagValid(): void | |
79 | { | |
80 | $session = []; | |
81 | $this->assignSessionVars($session); | |
82 | ||
83 | $requestParameters = [ | |
84 | 'renametag' => 'rename', | |
85 | 'fromtag' => 'old-tag', | |
86 | 'totag' => 'new-tag', | |
87 | ]; | |
88 | $request = $this->createMock(Request::class); | |
89 | $request | |
90 | ->expects(static::atLeastOnce()) | |
91 | ->method('getParam') | |
92 | ->willReturnCallback(function (string $key) use ($requestParameters): ?string { | |
93 | return $requestParameters[$key] ?? null; | |
94 | }) | |
95 | ; | |
96 | $response = new Response(); | |
97 | ||
98 | $bookmark1 = $this->createMock(Bookmark::class); | |
99 | $bookmark2 = $this->createMock(Bookmark::class); | |
100 | $this->container->bookmarkService | |
101 | ->expects(static::once()) | |
102 | ->method('search') | |
103 | ->with(['searchtags' => 'old-tag'], BookmarkFilter::$ALL, true) | |
9b8c0a45 | 104 | ->willReturnCallback(function () use ($bookmark1, $bookmark2): SearchResult { |
8eac2e54 A |
105 | $bookmark1->expects(static::once())->method('renameTag')->with('old-tag', 'new-tag'); |
106 | $bookmark2->expects(static::once())->method('renameTag')->with('old-tag', 'new-tag'); | |
107 | ||
9b8c0a45 | 108 | return SearchResult::getSearchResult([$bookmark1, $bookmark2]); |
8eac2e54 A |
109 | }) |
110 | ; | |
111 | $this->container->bookmarkService | |
112 | ->expects(static::exactly(2)) | |
113 | ->method('set') | |
114 | ->withConsecutive([$bookmark1, false], [$bookmark2, false]) | |
115 | ; | |
116 | $this->container->bookmarkService->expects(static::once())->method('save'); | |
117 | ||
118 | $result = $this->controller->save($request, $response); | |
119 | ||
120 | static::assertSame(302, $result->getStatusCode()); | |
9c75f877 | 121 | static::assertSame(['/subfolder/?searchtags=new-tag'], $result->getHeader('location')); |
8eac2e54 A |
122 | |
123 | static::assertArrayNotHasKey(SessionManager::KEY_ERROR_MESSAGES, $session); | |
124 | static::assertArrayNotHasKey(SessionManager::KEY_WARNING_MESSAGES, $session); | |
125 | static::assertArrayHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session); | |
126 | static::assertSame(['The tag was renamed in 2 bookmarks.'], $session[SessionManager::KEY_SUCCESS_MESSAGES]); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Test posting a tag update - delete tag - valid info provided. | |
131 | */ | |
132 | public function testSaveDeleteTagValid(): void | |
133 | { | |
134 | $session = []; | |
135 | $this->assignSessionVars($session); | |
136 | ||
137 | $requestParameters = [ | |
138 | 'deletetag' => 'delete', | |
139 | 'fromtag' => 'old-tag', | |
140 | ]; | |
141 | $request = $this->createMock(Request::class); | |
142 | $request | |
143 | ->expects(static::atLeastOnce()) | |
144 | ->method('getParam') | |
145 | ->willReturnCallback(function (string $key) use ($requestParameters): ?string { | |
146 | return $requestParameters[$key] ?? null; | |
147 | }) | |
148 | ; | |
149 | $response = new Response(); | |
150 | ||
151 | $bookmark1 = $this->createMock(Bookmark::class); | |
152 | $bookmark2 = $this->createMock(Bookmark::class); | |
153 | $this->container->bookmarkService | |
154 | ->expects(static::once()) | |
155 | ->method('search') | |
156 | ->with(['searchtags' => 'old-tag'], BookmarkFilter::$ALL, true) | |
9b8c0a45 | 157 | ->willReturnCallback(function () use ($bookmark1, $bookmark2): SearchResult { |
8eac2e54 A |
158 | $bookmark1->expects(static::once())->method('deleteTag')->with('old-tag'); |
159 | $bookmark2->expects(static::once())->method('deleteTag')->with('old-tag'); | |
160 | ||
9b8c0a45 | 161 | return SearchResult::getSearchResult([$bookmark1, $bookmark2]); |
8eac2e54 A |
162 | }) |
163 | ; | |
164 | $this->container->bookmarkService | |
165 | ->expects(static::exactly(2)) | |
166 | ->method('set') | |
167 | ->withConsecutive([$bookmark1, false], [$bookmark2, false]) | |
168 | ; | |
169 | $this->container->bookmarkService->expects(static::once())->method('save'); | |
170 | ||
171 | $result = $this->controller->save($request, $response); | |
172 | ||
173 | static::assertSame(302, $result->getStatusCode()); | |
9c75f877 | 174 | static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location')); |
8eac2e54 A |
175 | |
176 | static::assertArrayNotHasKey(SessionManager::KEY_ERROR_MESSAGES, $session); | |
177 | static::assertArrayNotHasKey(SessionManager::KEY_WARNING_MESSAGES, $session); | |
178 | static::assertArrayHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session); | |
179 | static::assertSame(['The tag was removed from 2 bookmarks.'], $session[SessionManager::KEY_SUCCESS_MESSAGES]); | |
180 | } | |
181 | ||
182 | /** | |
183 | * Test posting a tag update - wrong token. | |
184 | */ | |
185 | public function testSaveWrongToken(): void | |
186 | { | |
187 | $this->container->sessionManager = $this->createMock(SessionManager::class); | |
188 | $this->container->sessionManager->method('checkToken')->willReturn(false); | |
189 | ||
190 | $this->container->conf->expects(static::never())->method('set'); | |
191 | $this->container->conf->expects(static::never())->method('write'); | |
192 | ||
193 | $request = $this->createMock(Request::class); | |
194 | $response = new Response(); | |
195 | ||
196 | $this->expectException(WrongTokenException::class); | |
197 | ||
198 | $this->controller->save($request, $response); | |
199 | } | |
200 | ||
201 | /** | |
202 | * Test posting a tag update - rename tag - missing "FROM" tag. | |
203 | */ | |
204 | public function testSaveRenameTagMissingFrom(): void | |
205 | { | |
206 | $session = []; | |
207 | $this->assignSessionVars($session); | |
208 | ||
209 | $requestParameters = [ | |
210 | 'renametag' => 'rename', | |
211 | ]; | |
212 | $request = $this->createMock(Request::class); | |
213 | $request | |
214 | ->expects(static::atLeastOnce()) | |
215 | ->method('getParam') | |
216 | ->willReturnCallback(function (string $key) use ($requestParameters): ?string { | |
217 | return $requestParameters[$key] ?? null; | |
218 | }) | |
219 | ; | |
220 | $response = new Response(); | |
221 | ||
222 | $result = $this->controller->save($request, $response); | |
223 | ||
224 | static::assertSame(302, $result->getStatusCode()); | |
9c75f877 | 225 | static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location')); |
8eac2e54 A |
226 | |
227 | static::assertArrayNotHasKey(SessionManager::KEY_ERROR_MESSAGES, $session); | |
228 | static::assertArrayHasKey(SessionManager::KEY_WARNING_MESSAGES, $session); | |
229 | static::assertArrayNotHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session); | |
230 | static::assertSame(['Invalid tags provided.'], $session[SessionManager::KEY_WARNING_MESSAGES]); | |
231 | } | |
232 | ||
233 | /** | |
234 | * Test posting a tag update - delete tag - missing "FROM" tag. | |
235 | */ | |
236 | public function testSaveDeleteTagMissingFrom(): void | |
237 | { | |
238 | $session = []; | |
239 | $this->assignSessionVars($session); | |
240 | ||
241 | $requestParameters = [ | |
242 | 'deletetag' => 'delete', | |
243 | ]; | |
244 | $request = $this->createMock(Request::class); | |
245 | $request | |
246 | ->expects(static::atLeastOnce()) | |
247 | ->method('getParam') | |
248 | ->willReturnCallback(function (string $key) use ($requestParameters): ?string { | |
249 | return $requestParameters[$key] ?? null; | |
250 | }) | |
251 | ; | |
252 | $response = new Response(); | |
253 | ||
254 | $result = $this->controller->save($request, $response); | |
255 | ||
256 | static::assertSame(302, $result->getStatusCode()); | |
9c75f877 | 257 | static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location')); |
8eac2e54 A |
258 | |
259 | static::assertArrayNotHasKey(SessionManager::KEY_ERROR_MESSAGES, $session); | |
260 | static::assertArrayHasKey(SessionManager::KEY_WARNING_MESSAGES, $session); | |
261 | static::assertArrayNotHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session); | |
262 | static::assertSame(['Invalid tags provided.'], $session[SessionManager::KEY_WARNING_MESSAGES]); | |
263 | } | |
264 | ||
265 | /** | |
266 | * Test posting a tag update - rename tag - missing "TO" tag. | |
267 | */ | |
268 | public function testSaveRenameTagMissingTo(): void | |
269 | { | |
270 | $session = []; | |
271 | $this->assignSessionVars($session); | |
272 | ||
273 | $requestParameters = [ | |
274 | 'renametag' => 'rename', | |
275 | 'fromtag' => 'old-tag' | |
276 | ]; | |
277 | $request = $this->createMock(Request::class); | |
278 | $request | |
279 | ->expects(static::atLeastOnce()) | |
280 | ->method('getParam') | |
281 | ->willReturnCallback(function (string $key) use ($requestParameters): ?string { | |
282 | return $requestParameters[$key] ?? null; | |
283 | }) | |
284 | ; | |
285 | $response = new Response(); | |
286 | ||
287 | $result = $this->controller->save($request, $response); | |
288 | ||
289 | static::assertSame(302, $result->getStatusCode()); | |
9c75f877 | 290 | static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location')); |
8eac2e54 A |
291 | |
292 | static::assertArrayNotHasKey(SessionManager::KEY_ERROR_MESSAGES, $session); | |
293 | static::assertArrayHasKey(SessionManager::KEY_WARNING_MESSAGES, $session); | |
294 | static::assertArrayNotHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session); | |
295 | static::assertSame(['Invalid tags provided.'], $session[SessionManager::KEY_WARNING_MESSAGES]); | |
296 | } | |
b3bd8c3e A |
297 | |
298 | /** | |
299 | * Test changeSeparator to '#': redirection + success message. | |
300 | */ | |
301 | public function testChangeSeparatorValid(): void | |
302 | { | |
303 | $toSeparator = '#'; | |
304 | ||
305 | $session = []; | |
306 | $this->assignSessionVars($session); | |
307 | ||
308 | $request = $this->createMock(Request::class); | |
309 | $request | |
310 | ->expects(static::atLeastOnce()) | |
311 | ->method('getParam') | |
312 | ->willReturnCallback(function (string $key) use ($toSeparator): ?string { | |
313 | return $key === 'separator' ? $toSeparator : $key; | |
314 | }) | |
315 | ; | |
316 | $response = new Response(); | |
317 | ||
318 | $this->container->conf | |
319 | ->expects(static::once()) | |
320 | ->method('set') | |
321 | ->with('general.tags_separator', $toSeparator, true, true) | |
322 | ; | |
323 | ||
324 | $result = $this->controller->changeSeparator($request, $response); | |
325 | ||
326 | static::assertSame(302, $result->getStatusCode()); | |
327 | static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location')); | |
328 | ||
329 | static::assertArrayNotHasKey(SessionManager::KEY_ERROR_MESSAGES, $session); | |
330 | static::assertArrayNotHasKey(SessionManager::KEY_WARNING_MESSAGES, $session); | |
331 | static::assertArrayHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session); | |
332 | static::assertSame( | |
333 | ['Your tags separator setting has been updated!'], | |
334 | $session[SessionManager::KEY_SUCCESS_MESSAGES] | |
335 | ); | |
336 | } | |
337 | ||
338 | /** | |
339 | * Test changeSeparator to '#@' (too long): redirection + error message. | |
340 | */ | |
341 | public function testChangeSeparatorInvalidTooLong(): void | |
342 | { | |
343 | $toSeparator = '#@'; | |
344 | ||
345 | $session = []; | |
346 | $this->assignSessionVars($session); | |
347 | ||
348 | $request = $this->createMock(Request::class); | |
349 | $request | |
350 | ->expects(static::atLeastOnce()) | |
351 | ->method('getParam') | |
352 | ->willReturnCallback(function (string $key) use ($toSeparator): ?string { | |
353 | return $key === 'separator' ? $toSeparator : $key; | |
354 | }) | |
355 | ; | |
356 | $response = new Response(); | |
357 | ||
358 | $this->container->conf->expects(static::never())->method('set'); | |
359 | ||
360 | $result = $this->controller->changeSeparator($request, $response); | |
361 | ||
362 | static::assertSame(302, $result->getStatusCode()); | |
363 | static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location')); | |
364 | ||
365 | static::assertArrayNotHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session); | |
366 | static::assertArrayNotHasKey(SessionManager::KEY_WARNING_MESSAGES, $session); | |
367 | static::assertArrayHasKey(SessionManager::KEY_ERROR_MESSAGES, $session); | |
368 | static::assertSame( | |
369 | ['Tags separator must be a single character.'], | |
370 | $session[SessionManager::KEY_ERROR_MESSAGES] | |
371 | ); | |
372 | } | |
373 | ||
374 | /** | |
375 | * Test changeSeparator to '#@' (too long): redirection + error message. | |
376 | */ | |
377 | public function testChangeSeparatorInvalidReservedCharacter(): void | |
378 | { | |
379 | $toSeparator = '*'; | |
380 | ||
381 | $session = []; | |
382 | $this->assignSessionVars($session); | |
383 | ||
384 | $request = $this->createMock(Request::class); | |
385 | $request | |
386 | ->expects(static::atLeastOnce()) | |
387 | ->method('getParam') | |
388 | ->willReturnCallback(function (string $key) use ($toSeparator): ?string { | |
389 | return $key === 'separator' ? $toSeparator : $key; | |
390 | }) | |
391 | ; | |
392 | $response = new Response(); | |
393 | ||
394 | $this->container->conf->expects(static::never())->method('set'); | |
395 | ||
396 | $result = $this->controller->changeSeparator($request, $response); | |
397 | ||
398 | static::assertSame(302, $result->getStatusCode()); | |
399 | static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location')); | |
400 | ||
401 | static::assertArrayNotHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session); | |
402 | static::assertArrayNotHasKey(SessionManager::KEY_WARNING_MESSAGES, $session); | |
403 | static::assertArrayHasKey(SessionManager::KEY_ERROR_MESSAGES, $session); | |
404 | static::assertStringStartsWith( | |
405 | 'These characters are reserved and can\'t be used as tags separator', | |
406 | $session[SessionManager::KEY_ERROR_MESSAGES][0] | |
407 | ); | |
408 | } | |
8eac2e54 | 409 | } |