diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-05-30 15:51:14 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 8eac2e54882d8adae8cbb45386dca1b465242632 (patch) | |
tree | e833e0ada87ac350ec2de8b7519a62e30e6731c6 /tests/front | |
parent | 66063ed1a18d739b1a60bfb163d8656417a4c529 (diff) | |
download | Shaarli-8eac2e54882d8adae8cbb45386dca1b465242632.tar.gz Shaarli-8eac2e54882d8adae8cbb45386dca1b465242632.tar.zst Shaarli-8eac2e54882d8adae8cbb45386dca1b465242632.zip |
Process manage tags page through Slim controller
Diffstat (limited to 'tests/front')
-rw-r--r-- | tests/front/controller/admin/ManageTagControllerTest.php | 272 |
1 files changed, 272 insertions, 0 deletions
diff --git a/tests/front/controller/admin/ManageTagControllerTest.php b/tests/front/controller/admin/ManageTagControllerTest.php new file mode 100644 index 00000000..eed99231 --- /dev/null +++ b/tests/front/controller/admin/ManageTagControllerTest.php | |||
@@ -0,0 +1,272 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin; | ||
6 | |||
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; | ||
14 | |||
15 | class ManageTagControllerTest extends TestCase | ||
16 | { | ||
17 | use FrontAdminControllerMockHelper; | ||
18 | |||
19 | /** @var ManageTagController */ | ||
20 | protected $controller; | ||
21 | |||
22 | public function setUp(): void | ||
23 | { | ||
24 | $this->createContainer(); | ||
25 | |||
26 | $this->controller = new ManageTagController($this->container); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Test displaying manage tag page | ||
31 | */ | ||
32 | public function testIndex(): void | ||
33 | { | ||
34 | $assignedVariables = []; | ||
35 | $this->assignTemplateVars($assignedVariables); | ||
36 | |||
37 | $request = $this->createMock(Request::class); | ||
38 | $request->method('getParam')->with('fromtag')->willReturn('fromtag'); | ||
39 | $response = new Response(); | ||
40 | |||
41 | $result = $this->controller->index($request, $response); | ||
42 | |||
43 | static::assertSame(200, $result->getStatusCode()); | ||
44 | static::assertSame('changetag', (string) $result->getBody()); | ||
45 | |||
46 | static::assertSame('fromtag', $assignedVariables['fromtag']); | ||
47 | static::assertSame('Manage tags - Shaarli', $assignedVariables['pagetitle']); | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * Test posting a tag update - rename tag - valid info provided. | ||
52 | */ | ||
53 | public function testSaveRenameTagValid(): void | ||
54 | { | ||
55 | $session = []; | ||
56 | $this->assignSessionVars($session); | ||
57 | |||
58 | $requestParameters = [ | ||
59 | 'renametag' => 'rename', | ||
60 | 'fromtag' => 'old-tag', | ||
61 | 'totag' => 'new-tag', | ||
62 | ]; | ||
63 | $request = $this->createMock(Request::class); | ||
64 | $request | ||
65 | ->expects(static::atLeastOnce()) | ||
66 | ->method('getParam') | ||
67 | ->willReturnCallback(function (string $key) use ($requestParameters): ?string { | ||
68 | return $requestParameters[$key] ?? null; | ||
69 | }) | ||
70 | ; | ||
71 | $response = new Response(); | ||
72 | |||
73 | $bookmark1 = $this->createMock(Bookmark::class); | ||
74 | $bookmark2 = $this->createMock(Bookmark::class); | ||
75 | $this->container->bookmarkService | ||
76 | ->expects(static::once()) | ||
77 | ->method('search') | ||
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'); | ||
82 | |||
83 | return [$bookmark1, $bookmark2]; | ||
84 | }) | ||
85 | ; | ||
86 | $this->container->bookmarkService | ||
87 | ->expects(static::exactly(2)) | ||
88 | ->method('set') | ||
89 | ->withConsecutive([$bookmark1, false], [$bookmark2, false]) | ||
90 | ; | ||
91 | $this->container->bookmarkService->expects(static::once())->method('save'); | ||
92 | |||
93 | $result = $this->controller->save($request, $response); | ||
94 | |||
95 | static::assertSame(302, $result->getStatusCode()); | ||
96 | static::assertSame(['./?searchtags=new-tag'], $result->getHeader('location')); | ||
97 | |||
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]); | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * Test posting a tag update - delete tag - valid info provided. | ||
106 | */ | ||
107 | public function testSaveDeleteTagValid(): void | ||
108 | { | ||
109 | $session = []; | ||
110 | $this->assignSessionVars($session); | ||
111 | |||
112 | $requestParameters = [ | ||
113 | 'deletetag' => 'delete', | ||
114 | 'fromtag' => 'old-tag', | ||
115 | ]; | ||
116 | $request = $this->createMock(Request::class); | ||
117 | $request | ||
118 | ->expects(static::atLeastOnce()) | ||
119 | ->method('getParam') | ||
120 | ->willReturnCallback(function (string $key) use ($requestParameters): ?string { | ||
121 | return $requestParameters[$key] ?? null; | ||
122 | }) | ||
123 | ; | ||
124 | $response = new Response(); | ||
125 | |||
126 | $bookmark1 = $this->createMock(Bookmark::class); | ||
127 | $bookmark2 = $this->createMock(Bookmark::class); | ||
128 | $this->container->bookmarkService | ||
129 | ->expects(static::once()) | ||
130 | ->method('search') | ||
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'); | ||
135 | |||
136 | return [$bookmark1, $bookmark2]; | ||
137 | }) | ||
138 | ; | ||
139 | $this->container->bookmarkService | ||
140 | ->expects(static::exactly(2)) | ||
141 | ->method('set') | ||
142 | ->withConsecutive([$bookmark1, false], [$bookmark2, false]) | ||
143 | ; | ||
144 | $this->container->bookmarkService->expects(static::once())->method('save'); | ||
145 | |||
146 | $result = $this->controller->save($request, $response); | ||
147 | |||
148 | static::assertSame(302, $result->getStatusCode()); | ||
149 | static::assertSame(['./manage-tags'], $result->getHeader('location')); | ||
150 | |||
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]); | ||
155 | } | ||
156 | |||
157 | /** | ||
158 | * Test posting a tag update - wrong token. | ||
159 | */ | ||
160 | public function testSaveWrongToken(): void | ||
161 | { | ||
162 | $this->container->sessionManager = $this->createMock(SessionManager::class); | ||
163 | $this->container->sessionManager->method('checkToken')->willReturn(false); | ||
164 | |||
165 | $this->container->conf->expects(static::never())->method('set'); | ||
166 | $this->container->conf->expects(static::never())->method('write'); | ||
167 | |||
168 | $request = $this->createMock(Request::class); | ||
169 | $response = new Response(); | ||
170 | |||
171 | $this->expectException(WrongTokenException::class); | ||
172 | |||
173 | $this->controller->save($request, $response); | ||
174 | } | ||
175 | |||
176 | /** | ||
177 | * Test posting a tag update - rename tag - missing "FROM" tag. | ||
178 | */ | ||
179 | public function testSaveRenameTagMissingFrom(): void | ||
180 | { | ||
181 | $session = []; | ||
182 | $this->assignSessionVars($session); | ||
183 | |||
184 | $requestParameters = [ | ||
185 | 'renametag' => 'rename', | ||
186 | ]; | ||
187 | $request = $this->createMock(Request::class); | ||
188 | $request | ||
189 | ->expects(static::atLeastOnce()) | ||
190 | ->method('getParam') | ||
191 | ->willReturnCallback(function (string $key) use ($requestParameters): ?string { | ||
192 | return $requestParameters[$key] ?? null; | ||
193 | }) | ||
194 | ; | ||
195 | $response = new Response(); | ||
196 | |||
197 | $result = $this->controller->save($request, $response); | ||
198 | |||
199 | static::assertSame(302, $result->getStatusCode()); | ||
200 | static::assertSame(['./manage-tags'], $result->getHeader('location')); | ||
201 | |||
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]); | ||
206 | } | ||
207 | |||
208 | /** | ||
209 | * Test posting a tag update - delete tag - missing "FROM" tag. | ||
210 | */ | ||
211 | public function testSaveDeleteTagMissingFrom(): void | ||
212 | { | ||
213 | $session = []; | ||
214 | $this->assignSessionVars($session); | ||
215 | |||
216 | $requestParameters = [ | ||
217 | 'deletetag' => 'delete', | ||
218 | ]; | ||
219 | $request = $this->createMock(Request::class); | ||
220 | $request | ||
221 | ->expects(static::atLeastOnce()) | ||
222 | ->method('getParam') | ||
223 | ->willReturnCallback(function (string $key) use ($requestParameters): ?string { | ||
224 | return $requestParameters[$key] ?? null; | ||
225 | }) | ||
226 | ; | ||
227 | $response = new Response(); | ||
228 | |||
229 | $result = $this->controller->save($request, $response); | ||
230 | |||
231 | static::assertSame(302, $result->getStatusCode()); | ||
232 | static::assertSame(['./manage-tags'], $result->getHeader('location')); | ||
233 | |||
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]); | ||
238 | } | ||
239 | |||
240 | /** | ||
241 | * Test posting a tag update - rename tag - missing "TO" tag. | ||
242 | */ | ||
243 | public function testSaveRenameTagMissingTo(): void | ||
244 | { | ||
245 | $session = []; | ||
246 | $this->assignSessionVars($session); | ||
247 | |||
248 | $requestParameters = [ | ||
249 | 'renametag' => 'rename', | ||
250 | 'fromtag' => 'old-tag' | ||
251 | ]; | ||
252 | $request = $this->createMock(Request::class); | ||
253 | $request | ||
254 | ->expects(static::atLeastOnce()) | ||
255 | ->method('getParam') | ||
256 | ->willReturnCallback(function (string $key) use ($requestParameters): ?string { | ||
257 | return $requestParameters[$key] ?? null; | ||
258 | }) | ||
259 | ; | ||
260 | $response = new Response(); | ||
261 | |||
262 | $result = $this->controller->save($request, $response); | ||
263 | |||
264 | static::assertSame(302, $result->getStatusCode()); | ||
265 | static::assertSame(['./manage-tags'], $result->getHeader('location')); | ||
266 | |||
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]); | ||
271 | } | ||
272 | } | ||