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