3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Bookmark\BookmarkFilter
;
9 use Shaarli\Bookmark\SearchResult
;
10 use Shaarli\Config\ConfigManager
;
11 use Shaarli\Front\Exception\WrongTokenException
;
12 use Shaarli\Security\SessionManager
;
14 use Slim\Http\Request
;
15 use Slim\Http\Response
;
17 class ManageTagControllerTest
extends TestCase
19 use FrontAdminControllerMockHelper
;
21 /** @var ManageTagController */
22 protected $controller;
24 public function setUp(): void
26 $this->createContainer();
28 $this->controller
= new ManageTagController($this->container
);
32 * Test displaying manage tag page
34 public function testIndex(): void
36 $assignedVariables = [];
37 $this->assignTemplateVars($assignedVariables);
39 $request = $this->createMock(Request
::class);
40 $request->method('getParam')->with('fromtag')->willReturn('fromtag');
41 $response = new Response();
43 $result = $this->controller
->index($request, $response);
45 static::assertSame(200, $result->getStatusCode());
46 static::assertSame('changetag', (string) $result->getBody());
48 static::assertSame('fromtag', $assignedVariables['fromtag']);
49 static::assertSame('@', $assignedVariables['tags_separator']);
50 static::assertSame('Manage tags - Shaarli', $assignedVariables['pagetitle']);
54 * Test displaying manage tag page
56 public function testIndexWhitespaceSeparator(): void
58 $assignedVariables = [];
59 $this->assignTemplateVars($assignedVariables);
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;
66 $request = $this->createMock(Request
::class);
67 $response = new Response();
69 $this->controller
->index($request, $response);
71 static::assertSame(' ', $assignedVariables['tags_separator']);
72 static::assertSame('whitespace', $assignedVariables['tags_separator_desc']);
76 * Test posting a tag update - rename tag - valid info provided.
78 public function testSaveRenameTagValid(): void
81 $this->assignSessionVars($session);
83 $requestParameters = [
84 'renametag' => 'rename',
85 'fromtag' => 'old-tag',
88 $request = $this->createMock(Request
::class);
90 ->expects(static::atLeastOnce())
92 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
93 return $requestParameters[$key] ?? null;
96 $response = new Response();
98 $bookmark1 = $this->createMock(Bookmark
::class);
99 $bookmark2 = $this->createMock(Bookmark
::class);
100 $this->container
->bookmarkService
101 ->expects(static::once())
103 ->with(['searchtags' => 'old-tag'], BookmarkFilter
::$ALL, true)
104 ->willReturnCallback(function () use ($bookmark1, $bookmark2): SearchResult
{
105 $bookmark1->expects(static::once())->method('renameTag')->with('old-tag', 'new-tag');
106 $bookmark2->expects(static::once())->method('renameTag')->with('old-tag', 'new-tag');
108 return SearchResult
::getSearchResult([$bookmark1, $bookmark2]);
111 $this->container
->bookmarkService
112 ->expects(static::exactly(2))
114 ->withConsecutive([$bookmark1, false], [$bookmark2, false])
116 $this->container
->bookmarkService
->expects(static::once())->method('save');
118 $result = $this->controller
->save($request, $response);
120 static::assertSame(302, $result->getStatusCode());
121 static::assertSame(['/subfolder/?searchtags=new-tag'], $result->getHeader('location'));
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
]);
130 * Test posting a tag update - delete tag - valid info provided.
132 public function testSaveDeleteTagValid(): void
135 $this->assignSessionVars($session);
137 $requestParameters = [
138 'deletetag' => 'delete',
139 'fromtag' => 'old-tag',
141 $request = $this->createMock(Request
::class);
143 ->expects(static::atLeastOnce())
145 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
146 return $requestParameters[$key] ?? null;
149 $response = new Response();
151 $bookmark1 = $this->createMock(Bookmark
::class);
152 $bookmark2 = $this->createMock(Bookmark
::class);
153 $this->container
->bookmarkService
154 ->expects(static::once())
156 ->with(['searchtags' => 'old-tag'], BookmarkFilter
::$ALL, true)
157 ->willReturnCallback(function () use ($bookmark1, $bookmark2): SearchResult
{
158 $bookmark1->expects(static::once())->method('deleteTag')->with('old-tag');
159 $bookmark2->expects(static::once())->method('deleteTag')->with('old-tag');
161 return SearchResult
::getSearchResult([$bookmark1, $bookmark2]);
164 $this->container
->bookmarkService
165 ->expects(static::exactly(2))
167 ->withConsecutive([$bookmark1, false], [$bookmark2, false])
169 $this->container
->bookmarkService
->expects(static::once())->method('save');
171 $result = $this->controller
->save($request, $response);
173 static::assertSame(302, $result->getStatusCode());
174 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
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
]);
183 * Test posting a tag update - wrong token.
185 public function testSaveWrongToken(): void
187 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
188 $this->container
->sessionManager
->method('checkToken')->willReturn(false);
190 $this->container
->conf
->expects(static::never())->method('set');
191 $this->container
->conf
->expects(static::never())->method('write');
193 $request = $this->createMock(Request
::class);
194 $response = new Response();
196 $this->expectException(WrongTokenException
::class);
198 $this->controller
->save($request, $response);
202 * Test posting a tag update - rename tag - missing "FROM" tag.
204 public function testSaveRenameTagMissingFrom(): void
207 $this->assignSessionVars($session);
209 $requestParameters = [
210 'renametag' => 'rename',
212 $request = $this->createMock(Request
::class);
214 ->expects(static::atLeastOnce())
216 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
217 return $requestParameters[$key] ?? null;
220 $response = new Response();
222 $result = $this->controller
->save($request, $response);
224 static::assertSame(302, $result->getStatusCode());
225 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
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
]);
234 * Test posting a tag update - delete tag - missing "FROM" tag.
236 public function testSaveDeleteTagMissingFrom(): void
239 $this->assignSessionVars($session);
241 $requestParameters = [
242 'deletetag' => 'delete',
244 $request = $this->createMock(Request
::class);
246 ->expects(static::atLeastOnce())
248 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
249 return $requestParameters[$key] ?? null;
252 $response = new Response();
254 $result = $this->controller
->save($request, $response);
256 static::assertSame(302, $result->getStatusCode());
257 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
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
]);
266 * Test posting a tag update - rename tag - missing "TO" tag.
268 public function testSaveRenameTagMissingTo(): void
271 $this->assignSessionVars($session);
273 $requestParameters = [
274 'renametag' => 'rename',
275 'fromtag' => 'old-tag'
277 $request = $this->createMock(Request
::class);
279 ->expects(static::atLeastOnce())
281 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
282 return $requestParameters[$key] ?? null;
285 $response = new Response();
287 $result = $this->controller
->save($request, $response);
289 static::assertSame(302, $result->getStatusCode());
290 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
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
]);
299 * Test changeSeparator to '#': redirection + success message.
301 public function testChangeSeparatorValid(): void
306 $this->assignSessionVars($session);
308 $request = $this->createMock(Request
::class);
310 ->expects(static::atLeastOnce())
312 ->willReturnCallback(function (string $key) use ($toSeparator): ?string {
313 return $key === 'separator' ? $toSeparator : $key;
316 $response = new Response();
318 $this->container
->conf
319 ->expects(static::once())
321 ->with('general.tags_separator', $toSeparator, true, true)
324 $result = $this->controller
->changeSeparator($request, $response);
326 static::assertSame(302, $result->getStatusCode());
327 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
329 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
330 static::assertArrayNotHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
331 static::assertArrayHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
333 ['Your tags separator setting has been updated!'],
334 $session[SessionManager
::KEY_SUCCESS_MESSAGES
]
339 * Test changeSeparator to '#@' (too long): redirection + error message.
341 public function testChangeSeparatorInvalidTooLong(): void
346 $this->assignSessionVars($session);
348 $request = $this->createMock(Request
::class);
350 ->expects(static::atLeastOnce())
352 ->willReturnCallback(function (string $key) use ($toSeparator): ?string {
353 return $key === 'separator' ? $toSeparator : $key;
356 $response = new Response();
358 $this->container
->conf
->expects(static::never())->method('set');
360 $result = $this->controller
->changeSeparator($request, $response);
362 static::assertSame(302, $result->getStatusCode());
363 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
365 static::assertArrayNotHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
366 static::assertArrayNotHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
367 static::assertArrayHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
369 ['Tags separator must be a single character.'],
370 $session[SessionManager
::KEY_ERROR_MESSAGES
]
375 * Test changeSeparator to '#@' (too long): redirection + error message.
377 public function testChangeSeparatorInvalidReservedCharacter(): void
382 $this->assignSessionVars($session);
384 $request = $this->createMock(Request
::class);
386 ->expects(static::atLeastOnce())
388 ->willReturnCallback(function (string $key) use ($toSeparator): ?string {
389 return $key === 'separator' ? $toSeparator : $key;
392 $response = new Response();
394 $this->container
->conf
->expects(static::never())->method('set');
396 $result = $this->controller
->changeSeparator($request, $response);
398 static::assertSame(302, $result->getStatusCode());
399 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
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]