]>
Commit | Line | Data |
---|---|---|
72caf4e8 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
2899ebb5 | 5 | namespace Shaarli\Front\Controller\Visitor; |
72caf4e8 | 6 | |
72caf4e8 | 7 | use Shaarli\Bookmark\BookmarkFilter; |
a5a9cf23 | 8 | use Shaarli\TestCase; |
72caf4e8 A |
9 | use Slim\Http\Request; |
10 | use Slim\Http\Response; | |
11 | ||
12 | class TagCloudControllerTest extends TestCase | |
13 | { | |
dd09ec52 | 14 | use FrontControllerMockHelper; |
72caf4e8 A |
15 | |
16 | /** @var TagCloudController */ | |
17 | protected $controller; | |
18 | ||
19 | public function setUp(): void | |
20 | { | |
dd09ec52 A |
21 | $this->createContainer(); |
22 | ||
72caf4e8 A |
23 | $this->controller = new TagCloudController($this->container); |
24 | } | |
25 | ||
60ae2412 A |
26 | /** |
27 | * Tag Cloud - default parameters | |
28 | */ | |
72caf4e8 A |
29 | public function testValidCloudControllerInvokeDefault(): void |
30 | { | |
72caf4e8 A |
31 | $allTags = [ |
32 | 'ghi' => 1, | |
33 | 'abc' => 3, | |
34 | 'def' => 12, | |
35 | ]; | |
36 | $expectedOrder = ['abc', 'def', 'ghi']; | |
37 | ||
38 | $request = $this->createMock(Request::class); | |
72caf4e8 A |
39 | $response = new Response(); |
40 | ||
41 | // Save RainTPL assigned variables | |
42 | $assignedVariables = []; | |
43 | $this->assignTemplateVars($assignedVariables); | |
44 | ||
45 | $this->container->bookmarkService | |
46 | ->expects(static::once()) | |
47 | ->method('bookmarksCountPerTag') | |
48 | ->with([], null) | |
49 | ->willReturnCallback(function () use ($allTags): array { | |
50 | return $allTags; | |
51 | }) | |
52 | ; | |
53 | ||
54 | // Make sure that PluginManager hook is triggered | |
55 | $this->container->pluginManager | |
a5a9cf23 | 56 | ->expects(static::atLeastOnce()) |
72caf4e8 | 57 | ->method('executeHooks') |
a5a9cf23 | 58 | ->withConsecutive(['render_tagcloud']) |
72caf4e8 | 59 | ->willReturnCallback(function (string $hook, array $data, array $param): array { |
a5a9cf23 A |
60 | if ('render_tagcloud' === $hook) { |
61 | static::assertSame('', $data['search_tags']); | |
62 | static::assertCount(3, $data['tags']); | |
72caf4e8 | 63 | |
a5a9cf23 A |
64 | static::assertArrayHasKey('loggedin', $param); |
65 | } | |
72caf4e8 A |
66 | |
67 | return $data; | |
68 | }) | |
69 | ; | |
70 | ||
3772298e | 71 | $result = $this->controller->cloud($request, $response); |
72caf4e8 A |
72 | |
73 | static::assertSame(200, $result->getStatusCode()); | |
74 | static::assertSame('tag.cloud', (string) $result->getBody()); | |
75 | static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']); | |
76 | ||
77 | static::assertSame('', $assignedVariables['search_tags']); | |
78 | static::assertCount(3, $assignedVariables['tags']); | |
79 | static::assertSame($expectedOrder, array_keys($assignedVariables['tags'])); | |
80 | ||
81 | foreach ($allTags as $tag => $count) { | |
82 | static::assertArrayHasKey($tag, $assignedVariables['tags']); | |
83 | static::assertSame($count, $assignedVariables['tags'][$tag]['count']); | |
84 | static::assertGreaterThan(0, $assignedVariables['tags'][$tag]['size']); | |
85 | static::assertLessThan(5, $assignedVariables['tags'][$tag]['size']); | |
86 | } | |
87 | } | |
88 | ||
89 | /** | |
60ae2412 | 90 | * Tag Cloud - Additional parameters: |
72caf4e8 A |
91 | * - logged in |
92 | * - visibility private | |
93 | * - search tags: `ghi` and `def` (note that filtered tags are not displayed anymore) | |
94 | */ | |
95 | public function testValidCloudControllerInvokeWithParameters(): void | |
96 | { | |
72caf4e8 A |
97 | $request = $this->createMock(Request::class); |
98 | $request | |
72caf4e8 | 99 | ->method('getQueryParam') |
60ae2412 A |
100 | ->with() |
101 | ->willReturnCallback(function (string $key): ?string { | |
102 | if ('searchtags' === $key) { | |
103 | return 'ghi def'; | |
104 | } | |
105 | ||
106 | return null; | |
107 | }) | |
72caf4e8 A |
108 | ; |
109 | $response = new Response(); | |
110 | ||
111 | // Save RainTPL assigned variables | |
112 | $assignedVariables = []; | |
113 | $this->assignTemplateVars($assignedVariables); | |
114 | ||
115 | $this->container->loginManager->method('isLoggedin')->willReturn(true); | |
116 | $this->container->sessionManager->expects(static::once())->method('getSessionParameter')->willReturn('private'); | |
117 | ||
118 | $this->container->bookmarkService | |
119 | ->expects(static::once()) | |
120 | ->method('bookmarksCountPerTag') | |
121 | ->with(['ghi', 'def'], BookmarkFilter::$PRIVATE) | |
c79473bd A |
122 | ->willReturnCallback(function (): array { |
123 | return ['abc' => 3]; | |
72caf4e8 A |
124 | }) |
125 | ; | |
126 | ||
127 | // Make sure that PluginManager hook is triggered | |
128 | $this->container->pluginManager | |
a5a9cf23 | 129 | ->expects(static::atLeastOnce()) |
72caf4e8 | 130 | ->method('executeHooks') |
a5a9cf23 | 131 | ->withConsecutive(['render_tagcloud']) |
72caf4e8 | 132 | ->willReturnCallback(function (string $hook, array $data, array $param): array { |
a5a9cf23 A |
133 | if ('render_tagcloud' === $hook) { |
134 | static::assertSame('ghi def', $data['search_tags']); | |
135 | static::assertCount(1, $data['tags']); | |
72caf4e8 | 136 | |
a5a9cf23 A |
137 | static::assertArrayHasKey('loggedin', $param); |
138 | } | |
72caf4e8 A |
139 | |
140 | return $data; | |
141 | }) | |
142 | ; | |
143 | ||
3772298e | 144 | $result = $this->controller->cloud($request, $response); |
72caf4e8 A |
145 | |
146 | static::assertSame(200, $result->getStatusCode()); | |
147 | static::assertSame('tag.cloud', (string) $result->getBody()); | |
148 | static::assertSame('ghi def - Tag cloud - Shaarli', $assignedVariables['pagetitle']); | |
149 | ||
150 | static::assertSame('ghi def', $assignedVariables['search_tags']); | |
151 | static::assertCount(1, $assignedVariables['tags']); | |
152 | ||
153 | static::assertArrayHasKey('abc', $assignedVariables['tags']); | |
154 | static::assertSame(3, $assignedVariables['tags']['abc']['count']); | |
155 | static::assertGreaterThan(0, $assignedVariables['tags']['abc']['size']); | |
156 | static::assertLessThan(5, $assignedVariables['tags']['abc']['size']); | |
157 | } | |
158 | ||
60ae2412 A |
159 | /** |
160 | * Tag Cloud - empty | |
161 | */ | |
72caf4e8 A |
162 | public function testEmptyCloud(): void |
163 | { | |
72caf4e8 | 164 | $request = $this->createMock(Request::class); |
72caf4e8 A |
165 | $response = new Response(); |
166 | ||
167 | // Save RainTPL assigned variables | |
168 | $assignedVariables = []; | |
169 | $this->assignTemplateVars($assignedVariables); | |
170 | ||
171 | $this->container->bookmarkService | |
172 | ->expects(static::once()) | |
173 | ->method('bookmarksCountPerTag') | |
174 | ->with([], null) | |
175 | ->willReturnCallback(function (array $parameters, ?string $visibility): array { | |
176 | return []; | |
177 | }) | |
178 | ; | |
179 | ||
180 | // Make sure that PluginManager hook is triggered | |
181 | $this->container->pluginManager | |
a5a9cf23 | 182 | ->expects(static::atLeastOnce()) |
72caf4e8 | 183 | ->method('executeHooks') |
a5a9cf23 | 184 | ->withConsecutive(['render_tagcloud']) |
72caf4e8 | 185 | ->willReturnCallback(function (string $hook, array $data, array $param): array { |
a5a9cf23 A |
186 | if ('render_tagcloud' === $hook) { |
187 | static::assertSame('', $data['search_tags']); | |
188 | static::assertCount(0, $data['tags']); | |
72caf4e8 | 189 | |
a5a9cf23 A |
190 | static::assertArrayHasKey('loggedin', $param); |
191 | } | |
72caf4e8 A |
192 | |
193 | return $data; | |
194 | }) | |
195 | ; | |
196 | ||
3772298e | 197 | $result = $this->controller->cloud($request, $response); |
72caf4e8 A |
198 | |
199 | static::assertSame(200, $result->getStatusCode()); | |
200 | static::assertSame('tag.cloud', (string) $result->getBody()); | |
201 | static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']); | |
202 | ||
203 | static::assertSame('', $assignedVariables['search_tags']); | |
204 | static::assertCount(0, $assignedVariables['tags']); | |
205 | } | |
206 | ||
60ae2412 A |
207 | /** |
208 | * Tag List - Default sort is by usage DESC | |
209 | */ | |
210 | public function testValidListControllerInvokeDefault(): void | |
211 | { | |
60ae2412 A |
212 | $allTags = [ |
213 | 'def' => 12, | |
214 | 'abc' => 3, | |
215 | 'ghi' => 1, | |
216 | ]; | |
217 | ||
218 | $request = $this->createMock(Request::class); | |
219 | $response = new Response(); | |
220 | ||
221 | // Save RainTPL assigned variables | |
222 | $assignedVariables = []; | |
223 | $this->assignTemplateVars($assignedVariables); | |
224 | ||
225 | $this->container->bookmarkService | |
226 | ->expects(static::once()) | |
227 | ->method('bookmarksCountPerTag') | |
228 | ->with([], null) | |
229 | ->willReturnCallback(function () use ($allTags): array { | |
230 | return $allTags; | |
231 | }) | |
232 | ; | |
233 | ||
234 | // Make sure that PluginManager hook is triggered | |
235 | $this->container->pluginManager | |
a5a9cf23 | 236 | ->expects(static::atLeastOnce()) |
60ae2412 | 237 | ->method('executeHooks') |
a5a9cf23 | 238 | ->withConsecutive(['render_taglist']) |
60ae2412 | 239 | ->willReturnCallback(function (string $hook, array $data, array $param): array { |
a5a9cf23 A |
240 | if ('render_taglist' === $hook) { |
241 | static::assertSame('', $data['search_tags']); | |
242 | static::assertCount(3, $data['tags']); | |
60ae2412 | 243 | |
a5a9cf23 A |
244 | static::assertArrayHasKey('loggedin', $param); |
245 | } | |
60ae2412 A |
246 | |
247 | return $data; | |
248 | }) | |
249 | ; | |
250 | ||
251 | $result = $this->controller->list($request, $response); | |
252 | ||
253 | static::assertSame(200, $result->getStatusCode()); | |
254 | static::assertSame('tag.list', (string) $result->getBody()); | |
255 | static::assertSame('Tag list - Shaarli', $assignedVariables['pagetitle']); | |
256 | ||
257 | static::assertSame('', $assignedVariables['search_tags']); | |
258 | static::assertCount(3, $assignedVariables['tags']); | |
259 | ||
260 | foreach ($allTags as $tag => $count) { | |
261 | static::assertSame($count, $assignedVariables['tags'][$tag]); | |
262 | } | |
263 | } | |
264 | ||
265 | /** | |
266 | * Tag List - Additional parameters: | |
267 | * - logged in | |
268 | * - visibility private | |
269 | * - search tags: `ghi` and `def` (note that filtered tags are not displayed anymore) | |
270 | * - sort alphabetically | |
271 | */ | |
272 | public function testValidListControllerInvokeWithParameters(): void | |
273 | { | |
60ae2412 A |
274 | $request = $this->createMock(Request::class); |
275 | $request | |
276 | ->method('getQueryParam') | |
277 | ->with() | |
278 | ->willReturnCallback(function (string $key): ?string { | |
279 | if ('searchtags' === $key) { | |
280 | return 'ghi def'; | |
281 | } elseif ('sort' === $key) { | |
282 | return 'alpha'; | |
283 | } | |
284 | ||
285 | return null; | |
286 | }) | |
287 | ; | |
288 | $response = new Response(); | |
289 | ||
290 | // Save RainTPL assigned variables | |
291 | $assignedVariables = []; | |
292 | $this->assignTemplateVars($assignedVariables); | |
293 | ||
294 | $this->container->loginManager->method('isLoggedin')->willReturn(true); | |
295 | $this->container->sessionManager->expects(static::once())->method('getSessionParameter')->willReturn('private'); | |
296 | ||
297 | $this->container->bookmarkService | |
298 | ->expects(static::once()) | |
299 | ->method('bookmarksCountPerTag') | |
300 | ->with(['ghi', 'def'], BookmarkFilter::$PRIVATE) | |
301 | ->willReturnCallback(function (): array { | |
302 | return ['abc' => 3]; | |
303 | }) | |
304 | ; | |
305 | ||
306 | // Make sure that PluginManager hook is triggered | |
307 | $this->container->pluginManager | |
a5a9cf23 | 308 | ->expects(static::atLeastOnce()) |
60ae2412 | 309 | ->method('executeHooks') |
a5a9cf23 | 310 | ->withConsecutive(['render_taglist']) |
60ae2412 | 311 | ->willReturnCallback(function (string $hook, array $data, array $param): array { |
a5a9cf23 A |
312 | if ('render_taglist' === $hook) { |
313 | static::assertSame('ghi def', $data['search_tags']); | |
314 | static::assertCount(1, $data['tags']); | |
60ae2412 | 315 | |
a5a9cf23 A |
316 | static::assertArrayHasKey('loggedin', $param); |
317 | } | |
60ae2412 A |
318 | |
319 | return $data; | |
320 | }) | |
321 | ; | |
322 | ||
323 | $result = $this->controller->list($request, $response); | |
324 | ||
325 | static::assertSame(200, $result->getStatusCode()); | |
326 | static::assertSame('tag.list', (string) $result->getBody()); | |
327 | static::assertSame('ghi def - Tag list - Shaarli', $assignedVariables['pagetitle']); | |
328 | ||
329 | static::assertSame('ghi def', $assignedVariables['search_tags']); | |
330 | static::assertCount(1, $assignedVariables['tags']); | |
331 | static::assertSame(3, $assignedVariables['tags']['abc']); | |
332 | } | |
333 | ||
334 | /** | |
335 | * Tag List - empty | |
336 | */ | |
337 | public function testEmptyList(): void | |
338 | { | |
60ae2412 A |
339 | $request = $this->createMock(Request::class); |
340 | $response = new Response(); | |
341 | ||
342 | // Save RainTPL assigned variables | |
343 | $assignedVariables = []; | |
344 | $this->assignTemplateVars($assignedVariables); | |
345 | ||
346 | $this->container->bookmarkService | |
347 | ->expects(static::once()) | |
348 | ->method('bookmarksCountPerTag') | |
349 | ->with([], null) | |
350 | ->willReturnCallback(function (array $parameters, ?string $visibility): array { | |
351 | return []; | |
352 | }) | |
353 | ; | |
354 | ||
355 | // Make sure that PluginManager hook is triggered | |
356 | $this->container->pluginManager | |
a5a9cf23 | 357 | ->expects(static::atLeastOnce()) |
60ae2412 | 358 | ->method('executeHooks') |
a5a9cf23 | 359 | ->withConsecutive(['render_taglist']) |
60ae2412 | 360 | ->willReturnCallback(function (string $hook, array $data, array $param): array { |
a5a9cf23 A |
361 | if ('render_taglist' === $hook) { |
362 | static::assertSame('', $data['search_tags']); | |
363 | static::assertCount(0, $data['tags']); | |
60ae2412 | 364 | |
a5a9cf23 A |
365 | static::assertArrayHasKey('loggedin', $param); |
366 | } | |
60ae2412 A |
367 | |
368 | return $data; | |
369 | }) | |
370 | ; | |
371 | ||
372 | $result = $this->controller->list($request, $response); | |
373 | ||
374 | static::assertSame(200, $result->getStatusCode()); | |
375 | static::assertSame('tag.list', (string) $result->getBody()); | |
376 | static::assertSame('Tag list - Shaarli', $assignedVariables['pagetitle']); | |
377 | ||
378 | static::assertSame('', $assignedVariables['search_tags']); | |
379 | static::assertCount(0, $assignedVariables['tags']); | |
380 | } | |
72caf4e8 | 381 | } |