3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Bookmark\BookmarkFilter
;
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Front\Exception\WrongTokenException
;
11 use Shaarli\Security\SessionManager
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
16 class ManageTagControllerTest
extends TestCase
18 use FrontAdminControllerMockHelper
;
20 /** @var ManageTagController */
21 protected $controller;
23 public function setUp(): void
25 $this->createContainer();
27 $this->controller
= new ManageTagController($this->container
);
31 * Test displaying manage tag page
33 public function testIndex(): void
35 $assignedVariables = [];
36 $this->assignTemplateVars($assignedVariables);
38 $request = $this->createMock(Request
::class);
39 $request->method('getParam')->with('fromtag')->willReturn('fromtag');
40 $response = new Response();
42 $result = $this->controller
->index($request, $response);
44 static::assertSame(200, $result->getStatusCode());
45 static::assertSame('changetag', (string) $result->getBody());
47 static::assertSame('fromtag', $assignedVariables['fromtag']);
48 static::assertSame('@', $assignedVariables['tags_separator']);
49 static::assertSame('Manage tags - Shaarli', $assignedVariables['pagetitle']);
53 * Test displaying manage tag page
55 public function testIndexWhitespaceSeparator(): void
57 $assignedVariables = [];
58 $this->assignTemplateVars($assignedVariables);
60 $this->container
->conf
= $this->createMock(ConfigManager
::class);
61 $this->container
->conf
->method('get')->willReturnCallback(function (string $key) {
62 return $key === 'general.tags_separator' ? ' ' : $key;
65 $request = $this->createMock(Request
::class);
66 $response = new Response();
68 $this->controller
->index($request, $response);
70 static::assertSame(' ', $assignedVariables['tags_separator']);
71 static::assertSame('whitespace', $assignedVariables['tags_separator_desc']);
75 * Test posting a tag update - rename tag - valid info provided.
77 public function testSaveRenameTagValid(): void
80 $this->assignSessionVars($session);
82 $requestParameters = [
83 'renametag' => 'rename',
84 'fromtag' => 'old-tag',
87 $request = $this->createMock(Request
::class);
89 ->expects(static::atLeastOnce())
91 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
92 return $requestParameters[$key] ?? null;
95 $response = new Response();
97 $bookmark1 = $this->createMock(Bookmark
::class);
98 $bookmark2 = $this->createMock(Bookmark
::class);
99 $this->container
->bookmarkService
100 ->expects(static::once())
102 ->with(['searchtags' => 'old-tag'], BookmarkFilter
::$ALL, true)
103 ->willReturnCallback(function () use ($bookmark1, $bookmark2): array {
104 $bookmark1->expects(static::once())->method('renameTag')->with('old-tag', 'new-tag');
105 $bookmark2->expects(static::once())->method('renameTag')->with('old-tag', 'new-tag');
107 return [$bookmark1, $bookmark2];
110 $this->container
->bookmarkService
111 ->expects(static::exactly(2))
113 ->withConsecutive([$bookmark1, false], [$bookmark2, false])
115 $this->container
->bookmarkService
->expects(static::once())->method('save');
117 $result = $this->controller
->save($request, $response);
119 static::assertSame(302, $result->getStatusCode());
120 static::assertSame(['/subfolder/?searchtags=new-tag'], $result->getHeader('location'));
122 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
123 static::assertArrayNotHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
124 static::assertArrayHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
125 static::assertSame(['The tag was renamed in 2 bookmarks.'], $session[SessionManager
::KEY_SUCCESS_MESSAGES
]);
129 * Test posting a tag update - delete tag - valid info provided.
131 public function testSaveDeleteTagValid(): void
134 $this->assignSessionVars($session);
136 $requestParameters = [
137 'deletetag' => 'delete',
138 'fromtag' => 'old-tag',
140 $request = $this->createMock(Request
::class);
142 ->expects(static::atLeastOnce())
144 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
145 return $requestParameters[$key] ?? null;
148 $response = new Response();
150 $bookmark1 = $this->createMock(Bookmark
::class);
151 $bookmark2 = $this->createMock(Bookmark
::class);
152 $this->container
->bookmarkService
153 ->expects(static::once())
155 ->with(['searchtags' => 'old-tag'], BookmarkFilter
::$ALL, true)
156 ->willReturnCallback(function () use ($bookmark1, $bookmark2): array {
157 $bookmark1->expects(static::once())->method('deleteTag')->with('old-tag');
158 $bookmark2->expects(static::once())->method('deleteTag')->with('old-tag');
160 return [$bookmark1, $bookmark2];
163 $this->container
->bookmarkService
164 ->expects(static::exactly(2))
166 ->withConsecutive([$bookmark1, false], [$bookmark2, false])
168 $this->container
->bookmarkService
->expects(static::once())->method('save');
170 $result = $this->controller
->save($request, $response);
172 static::assertSame(302, $result->getStatusCode());
173 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
175 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
176 static::assertArrayNotHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
177 static::assertArrayHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
178 static::assertSame(['The tag was removed from 2 bookmarks.'], $session[SessionManager
::KEY_SUCCESS_MESSAGES
]);
182 * Test posting a tag update - wrong token.
184 public function testSaveWrongToken(): void
186 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
187 $this->container
->sessionManager
->method('checkToken')->willReturn(false);
189 $this->container
->conf
->expects(static::never())->method('set');
190 $this->container
->conf
->expects(static::never())->method('write');
192 $request = $this->createMock(Request
::class);
193 $response = new Response();
195 $this->expectException(WrongTokenException
::class);
197 $this->controller
->save($request, $response);
201 * Test posting a tag update - rename tag - missing "FROM" tag.
203 public function testSaveRenameTagMissingFrom(): void
206 $this->assignSessionVars($session);
208 $requestParameters = [
209 'renametag' => 'rename',
211 $request = $this->createMock(Request
::class);
213 ->expects(static::atLeastOnce())
215 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
216 return $requestParameters[$key] ?? null;
219 $response = new Response();
221 $result = $this->controller
->save($request, $response);
223 static::assertSame(302, $result->getStatusCode());
224 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
226 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
227 static::assertArrayHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
228 static::assertArrayNotHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
229 static::assertSame(['Invalid tags provided.'], $session[SessionManager
::KEY_WARNING_MESSAGES
]);
233 * Test posting a tag update - delete tag - missing "FROM" tag.
235 public function testSaveDeleteTagMissingFrom(): void
238 $this->assignSessionVars($session);
240 $requestParameters = [
241 'deletetag' => 'delete',
243 $request = $this->createMock(Request
::class);
245 ->expects(static::atLeastOnce())
247 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
248 return $requestParameters[$key] ?? null;
251 $response = new Response();
253 $result = $this->controller
->save($request, $response);
255 static::assertSame(302, $result->getStatusCode());
256 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
258 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
259 static::assertArrayHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
260 static::assertArrayNotHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
261 static::assertSame(['Invalid tags provided.'], $session[SessionManager
::KEY_WARNING_MESSAGES
]);
265 * Test posting a tag update - rename tag - missing "TO" tag.
267 public function testSaveRenameTagMissingTo(): void
270 $this->assignSessionVars($session);
272 $requestParameters = [
273 'renametag' => 'rename',
274 'fromtag' => 'old-tag'
276 $request = $this->createMock(Request
::class);
278 ->expects(static::atLeastOnce())
280 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
281 return $requestParameters[$key] ?? null;
284 $response = new Response();
286 $result = $this->controller
->save($request, $response);
288 static::assertSame(302, $result->getStatusCode());
289 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
291 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
292 static::assertArrayHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
293 static::assertArrayNotHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
294 static::assertSame(['Invalid tags provided.'], $session[SessionManager
::KEY_WARNING_MESSAGES
]);
298 * Test changeSeparator to '#': redirection + success message.
300 public function testChangeSeparatorValid(): void
305 $this->assignSessionVars($session);
307 $request = $this->createMock(Request
::class);
309 ->expects(static::atLeastOnce())
311 ->willReturnCallback(function (string $key) use ($toSeparator): ?string {
312 return $key === 'separator' ? $toSeparator : $key;
315 $response = new Response();
317 $this->container
->conf
318 ->expects(static::once())
320 ->with('general.tags_separator', $toSeparator, true, true)
323 $result = $this->controller
->changeSeparator($request, $response);
325 static::assertSame(302, $result->getStatusCode());
326 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
328 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
329 static::assertArrayNotHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
330 static::assertArrayHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
332 ['Your tags separator setting has been updated!'],
333 $session[SessionManager
::KEY_SUCCESS_MESSAGES
]
338 * Test changeSeparator to '#@' (too long): redirection + error message.
340 public function testChangeSeparatorInvalidTooLong(): void
345 $this->assignSessionVars($session);
347 $request = $this->createMock(Request
::class);
349 ->expects(static::atLeastOnce())
351 ->willReturnCallback(function (string $key) use ($toSeparator): ?string {
352 return $key === 'separator' ? $toSeparator : $key;
355 $response = new Response();
357 $this->container
->conf
->expects(static::never())->method('set');
359 $result = $this->controller
->changeSeparator($request, $response);
361 static::assertSame(302, $result->getStatusCode());
362 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
364 static::assertArrayNotHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
365 static::assertArrayNotHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
366 static::assertArrayHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
368 ['Tags separator must be a single character.'],
369 $session[SessionManager
::KEY_ERROR_MESSAGES
]
374 * Test changeSeparator to '#@' (too long): redirection + error message.
376 public function testChangeSeparatorInvalidReservedCharacter(): void
381 $this->assignSessionVars($session);
383 $request = $this->createMock(Request
::class);
385 ->expects(static::atLeastOnce())
387 ->willReturnCallback(function (string $key) use ($toSeparator): ?string {
388 return $key === 'separator' ? $toSeparator : $key;
391 $response = new Response();
393 $this->container
->conf
->expects(static::never())->method('set');
395 $result = $this->controller
->changeSeparator($request, $response);
397 static::assertSame(302, $result->getStatusCode());
398 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
400 static::assertArrayNotHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
401 static::assertArrayNotHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
402 static::assertArrayHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
403 static::assertStringStartsWith(
404 'These characters are reserved and can\'t be used as tags separator',
405 $session[SessionManager
::KEY_ERROR_MESSAGES
][0]