]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/controller/visitor/TagCloudControllerTest.php
Initialize admin Slim controllers
[github/shaarli/Shaarli.git] / tests / front / controller / visitor / TagCloudControllerTest.php
CommitLineData
72caf4e8
A
1<?php
2
3declare(strict_types=1);
4
2899ebb5 5namespace Shaarli\Front\Controller\Visitor;
72caf4e8
A
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkFilter;
72caf4e8
A
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12class 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 {
31 $this->createValidContainerMockSet();
32
33 $allTags = [
34 'ghi' => 1,
35 'abc' => 3,
36 'def' => 12,
37 ];
38 $expectedOrder = ['abc', 'def', 'ghi'];
39
40 $request = $this->createMock(Request::class);
72caf4e8
A
41 $response = new Response();
42
43 // Save RainTPL assigned variables
44 $assignedVariables = [];
45 $this->assignTemplateVars($assignedVariables);
46
47 $this->container->bookmarkService
48 ->expects(static::once())
49 ->method('bookmarksCountPerTag')
50 ->with([], null)
51 ->willReturnCallback(function () use ($allTags): array {
52 return $allTags;
53 })
54 ;
55
56 // Make sure that PluginManager hook is triggered
57 $this->container->pluginManager
58 ->expects(static::at(0))
59 ->method('executeHooks')
60 ->willReturnCallback(function (string $hook, array $data, array $param): array {
61 static::assertSame('render_tagcloud', $hook);
62 static::assertSame('', $data['search_tags']);
63 static::assertCount(3, $data['tags']);
64
65 static::assertArrayHasKey('loggedin', $param);
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 {
97 $this->createValidContainerMockSet();
98
72caf4e8
A
99 $request = $this->createMock(Request::class);
100 $request
72caf4e8 101 ->method('getQueryParam')
60ae2412
A
102 ->with()
103 ->willReturnCallback(function (string $key): ?string {
104 if ('searchtags' === $key) {
105 return 'ghi def';
106 }
107
108 return null;
109 })
72caf4e8
A
110 ;
111 $response = new Response();
112
113 // Save RainTPL assigned variables
114 $assignedVariables = [];
115 $this->assignTemplateVars($assignedVariables);
116
117 $this->container->loginManager->method('isLoggedin')->willReturn(true);
118 $this->container->sessionManager->expects(static::once())->method('getSessionParameter')->willReturn('private');
119
120 $this->container->bookmarkService
121 ->expects(static::once())
122 ->method('bookmarksCountPerTag')
123 ->with(['ghi', 'def'], BookmarkFilter::$PRIVATE)
c79473bd
A
124 ->willReturnCallback(function (): array {
125 return ['abc' => 3];
72caf4e8
A
126 })
127 ;
128
129 // Make sure that PluginManager hook is triggered
130 $this->container->pluginManager
131 ->expects(static::at(0))
132 ->method('executeHooks')
133 ->willReturnCallback(function (string $hook, array $data, array $param): array {
134 static::assertSame('render_tagcloud', $hook);
135 static::assertSame('ghi def', $data['search_tags']);
136 static::assertCount(1, $data['tags']);
137
138 static::assertArrayHasKey('loggedin', $param);
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 {
164 $this->createValidContainerMockSet();
165
166 $request = $this->createMock(Request::class);
72caf4e8
A
167 $response = new Response();
168
169 // Save RainTPL assigned variables
170 $assignedVariables = [];
171 $this->assignTemplateVars($assignedVariables);
172
173 $this->container->bookmarkService
174 ->expects(static::once())
175 ->method('bookmarksCountPerTag')
176 ->with([], null)
177 ->willReturnCallback(function (array $parameters, ?string $visibility): array {
178 return [];
179 })
180 ;
181
182 // Make sure that PluginManager hook is triggered
183 $this->container->pluginManager
184 ->expects(static::at(0))
185 ->method('executeHooks')
186 ->willReturnCallback(function (string $hook, array $data, array $param): array {
187 static::assertSame('render_tagcloud', $hook);
188 static::assertSame('', $data['search_tags']);
189 static::assertCount(0, $data['tags']);
190
191 static::assertArrayHasKey('loggedin', $param);
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 {
212 $this->createValidContainerMockSet();
213
214 $allTags = [
215 'def' => 12,
216 'abc' => 3,
217 'ghi' => 1,
218 ];
219
220 $request = $this->createMock(Request::class);
221 $response = new Response();
222
223 // Save RainTPL assigned variables
224 $assignedVariables = [];
225 $this->assignTemplateVars($assignedVariables);
226
227 $this->container->bookmarkService
228 ->expects(static::once())
229 ->method('bookmarksCountPerTag')
230 ->with([], null)
231 ->willReturnCallback(function () use ($allTags): array {
232 return $allTags;
233 })
234 ;
235
236 // Make sure that PluginManager hook is triggered
237 $this->container->pluginManager
238 ->expects(static::at(0))
239 ->method('executeHooks')
240 ->willReturnCallback(function (string $hook, array $data, array $param): array {
241 static::assertSame('render_taglist', $hook);
242 static::assertSame('', $data['search_tags']);
243 static::assertCount(3, $data['tags']);
244
245 static::assertArrayHasKey('loggedin', $param);
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 {
274 $this->createValidContainerMockSet();
275
276 $request = $this->createMock(Request::class);
277 $request
278 ->method('getQueryParam')
279 ->with()
280 ->willReturnCallback(function (string $key): ?string {
281 if ('searchtags' === $key) {
282 return 'ghi def';
283 } elseif ('sort' === $key) {
284 return 'alpha';
285 }
286
287 return null;
288 })
289 ;
290 $response = new Response();
291
292 // Save RainTPL assigned variables
293 $assignedVariables = [];
294 $this->assignTemplateVars($assignedVariables);
295
296 $this->container->loginManager->method('isLoggedin')->willReturn(true);
297 $this->container->sessionManager->expects(static::once())->method('getSessionParameter')->willReturn('private');
298
299 $this->container->bookmarkService
300 ->expects(static::once())
301 ->method('bookmarksCountPerTag')
302 ->with(['ghi', 'def'], BookmarkFilter::$PRIVATE)
303 ->willReturnCallback(function (): array {
304 return ['abc' => 3];
305 })
306 ;
307
308 // Make sure that PluginManager hook is triggered
309 $this->container->pluginManager
310 ->expects(static::at(0))
311 ->method('executeHooks')
312 ->willReturnCallback(function (string $hook, array $data, array $param): array {
313 static::assertSame('render_taglist', $hook);
314 static::assertSame('ghi def', $data['search_tags']);
315 static::assertCount(1, $data['tags']);
316
317 static::assertArrayHasKey('loggedin', $param);
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 {
339 $this->createValidContainerMockSet();
340
341 $request = $this->createMock(Request::class);
342 $response = new Response();
343
344 // Save RainTPL assigned variables
345 $assignedVariables = [];
346 $this->assignTemplateVars($assignedVariables);
347
348 $this->container->bookmarkService
349 ->expects(static::once())
350 ->method('bookmarksCountPerTag')
351 ->with([], null)
352 ->willReturnCallback(function (array $parameters, ?string $visibility): array {
353 return [];
354 })
355 ;
356
357 // Make sure that PluginManager hook is triggered
358 $this->container->pluginManager
359 ->expects(static::at(0))
360 ->method('executeHooks')
361 ->willReturnCallback(function (string $hook, array $data, array $param): array {
362 static::assertSame('render_taglist', $hook);
363 static::assertSame('', $data['search_tags']);
364 static::assertCount(0, $data['tags']);
365
366 static::assertArrayHasKey('loggedin', $param);
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}