3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Bookmark\Bookmark
;
9 use Shaarli\Bookmark\BookmarkFilter
;
10 use Shaarli\Front\Exception\WrongTokenException
;
11 use Shaarli\Security\SessionManager
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
15 class ManageTagControllerTest
extends TestCase
17 use FrontAdminControllerMockHelper
;
19 /** @var ManageTagController */
20 protected $controller;
22 public function setUp(): void
24 $this->createContainer();
26 $this->controller
= new ManageTagController($this->container
);
30 * Test displaying manage tag page
32 public function testIndex(): void
34 $assignedVariables = [];
35 $this->assignTemplateVars($assignedVariables);
37 $request = $this->createMock(Request
::class);
38 $request->method('getParam')->with('fromtag')->willReturn('fromtag');
39 $response = new Response();
41 $result = $this->controller
->index($request, $response);
43 static::assertSame(200, $result->getStatusCode());
44 static::assertSame('changetag', (string) $result->getBody());
46 static::assertSame('fromtag', $assignedVariables['fromtag']);
47 static::assertSame('Manage tags - Shaarli', $assignedVariables['pagetitle']);
51 * Test posting a tag update - rename tag - valid info provided.
53 public function testSaveRenameTagValid(): void
56 $this->assignSessionVars($session);
58 $requestParameters = [
59 'renametag' => 'rename',
60 'fromtag' => 'old-tag',
63 $request = $this->createMock(Request
::class);
65 ->expects(static::atLeastOnce())
67 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
68 return $requestParameters[$key] ?? null;
71 $response = new Response();
73 $bookmark1 = $this->createMock(Bookmark
::class);
74 $bookmark2 = $this->createMock(Bookmark
::class);
75 $this->container
->bookmarkService
76 ->expects(static::once())
78 ->with(['searchtags' => 'old-tag'], BookmarkFilter
::$ALL, true)
79 ->willReturnCallback(function () use ($bookmark1, $bookmark2): array {
80 $bookmark1->expects(static::once())->method('renameTag')->with('old-tag', 'new-tag');
81 $bookmark2->expects(static::once())->method('renameTag')->with('old-tag', 'new-tag');
83 return [$bookmark1, $bookmark2];
86 $this->container
->bookmarkService
87 ->expects(static::exactly(2))
89 ->withConsecutive([$bookmark1, false], [$bookmark2, false])
91 $this->container
->bookmarkService
->expects(static::once())->method('save');
93 $result = $this->controller
->save($request, $response);
95 static::assertSame(302, $result->getStatusCode());
96 static::assertSame(['/subfolder/?searchtags=new-tag'], $result->getHeader('location'));
98 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
99 static::assertArrayNotHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
100 static::assertArrayHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
101 static::assertSame(['The tag was renamed in 2 bookmarks.'], $session[SessionManager
::KEY_SUCCESS_MESSAGES
]);
105 * Test posting a tag update - delete tag - valid info provided.
107 public function testSaveDeleteTagValid(): void
110 $this->assignSessionVars($session);
112 $requestParameters = [
113 'deletetag' => 'delete',
114 'fromtag' => 'old-tag',
116 $request = $this->createMock(Request
::class);
118 ->expects(static::atLeastOnce())
120 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
121 return $requestParameters[$key] ?? null;
124 $response = new Response();
126 $bookmark1 = $this->createMock(Bookmark
::class);
127 $bookmark2 = $this->createMock(Bookmark
::class);
128 $this->container
->bookmarkService
129 ->expects(static::once())
131 ->with(['searchtags' => 'old-tag'], BookmarkFilter
::$ALL, true)
132 ->willReturnCallback(function () use ($bookmark1, $bookmark2): array {
133 $bookmark1->expects(static::once())->method('deleteTag')->with('old-tag');
134 $bookmark2->expects(static::once())->method('deleteTag')->with('old-tag');
136 return [$bookmark1, $bookmark2];
139 $this->container
->bookmarkService
140 ->expects(static::exactly(2))
142 ->withConsecutive([$bookmark1, false], [$bookmark2, false])
144 $this->container
->bookmarkService
->expects(static::once())->method('save');
146 $result = $this->controller
->save($request, $response);
148 static::assertSame(302, $result->getStatusCode());
149 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
151 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
152 static::assertArrayNotHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
153 static::assertArrayHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
154 static::assertSame(['The tag was removed from 2 bookmarks.'], $session[SessionManager
::KEY_SUCCESS_MESSAGES
]);
158 * Test posting a tag update - wrong token.
160 public function testSaveWrongToken(): void
162 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
163 $this->container
->sessionManager
->method('checkToken')->willReturn(false);
165 $this->container
->conf
->expects(static::never())->method('set');
166 $this->container
->conf
->expects(static::never())->method('write');
168 $request = $this->createMock(Request
::class);
169 $response = new Response();
171 $this->expectException(WrongTokenException
::class);
173 $this->controller
->save($request, $response);
177 * Test posting a tag update - rename tag - missing "FROM" tag.
179 public function testSaveRenameTagMissingFrom(): void
182 $this->assignSessionVars($session);
184 $requestParameters = [
185 'renametag' => 'rename',
187 $request = $this->createMock(Request
::class);
189 ->expects(static::atLeastOnce())
191 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
192 return $requestParameters[$key] ?? null;
195 $response = new Response();
197 $result = $this->controller
->save($request, $response);
199 static::assertSame(302, $result->getStatusCode());
200 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
202 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
203 static::assertArrayHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
204 static::assertArrayNotHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
205 static::assertSame(['Invalid tags provided.'], $session[SessionManager
::KEY_WARNING_MESSAGES
]);
209 * Test posting a tag update - delete tag - missing "FROM" tag.
211 public function testSaveDeleteTagMissingFrom(): void
214 $this->assignSessionVars($session);
216 $requestParameters = [
217 'deletetag' => 'delete',
219 $request = $this->createMock(Request
::class);
221 ->expects(static::atLeastOnce())
223 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
224 return $requestParameters[$key] ?? null;
227 $response = new Response();
229 $result = $this->controller
->save($request, $response);
231 static::assertSame(302, $result->getStatusCode());
232 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
234 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
235 static::assertArrayHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
236 static::assertArrayNotHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
237 static::assertSame(['Invalid tags provided.'], $session[SessionManager
::KEY_WARNING_MESSAGES
]);
241 * Test posting a tag update - rename tag - missing "TO" tag.
243 public function testSaveRenameTagMissingTo(): void
246 $this->assignSessionVars($session);
248 $requestParameters = [
249 'renametag' => 'rename',
250 'fromtag' => 'old-tag'
252 $request = $this->createMock(Request
::class);
254 ->expects(static::atLeastOnce())
256 ->willReturnCallback(function (string $key) use ($requestParameters): ?string {
257 return $requestParameters[$key] ?? null;
260 $response = new Response();
262 $result = $this->controller
->save($request, $response);
264 static::assertSame(302, $result->getStatusCode());
265 static::assertSame(['/subfolder/admin/tags'], $result->getHeader('location'));
267 static::assertArrayNotHasKey(SessionManager
::KEY_ERROR_MESSAGES
, $session);
268 static::assertArrayHasKey(SessionManager
::KEY_WARNING_MESSAGES
, $session);
269 static::assertArrayNotHasKey(SessionManager
::KEY_SUCCESS_MESSAGES
, $session);
270 static::assertSame(['Invalid tags provided.'], $session[SessionManager
::KEY_WARNING_MESSAGES
]);