3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Bookmark\BookmarkFilter
;
10 use Slim\Http\Response
;
12 class TagCloudControllerTest
extends TestCase
14 use FrontControllerMockHelper
;
16 /** @var TagCloudController */
17 protected $controller;
19 public function setUp(): void
21 $this->createContainer();
23 $this->controller
= new TagCloudController($this->container
);
27 * Tag Cloud - default parameters
29 public function testValidCloudControllerInvokeDefault(): void
36 $expectedOrder = ['abc', 'def', 'ghi'];
38 $request = $this->createMock(Request
::class);
39 $response = new Response();
41 // Save RainTPL assigned variables
42 $assignedVariables = [];
43 $this->assignTemplateVars($assignedVariables);
45 $this->container
->bookmarkService
46 ->expects(static::once())
47 ->method('bookmarksCountPerTag')
49 ->willReturnCallback(function () use ($allTags): array {
54 // Make sure that PluginManager hook is triggered
55 $this->container
->pluginManager
56 ->expects(static::at(0))
57 ->method('executeHooks')
58 ->willReturnCallback(function (string $hook, array $data, array $param): array {
59 static::assertSame('render_tagcloud', $hook);
60 static::assertSame('', $data['search_tags']);
61 static::assertCount(3, $data['tags']);
63 static::assertArrayHasKey('loggedin', $param);
69 $result = $this->controller
->cloud($request, $response);
71 static::assertSame(200, $result->getStatusCode());
72 static::assertSame('tag.cloud', (string) $result->getBody());
73 static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']);
75 static::assertSame('', $assignedVariables['search_tags']);
76 static::assertCount(3, $assignedVariables['tags']);
77 static::assertSame($expectedOrder, array_keys($assignedVariables['tags']));
79 foreach ($allTags as $tag => $count) {
80 static::assertArrayHasKey($tag, $assignedVariables['tags']);
81 static::assertSame($count, $assignedVariables['tags'][$tag]['count']);
82 static::assertGreaterThan(0, $assignedVariables['tags'][$tag]['size']);
83 static::assertLessThan(5, $assignedVariables['tags'][$tag]['size']);
88 * Tag Cloud - Additional parameters:
90 * - visibility private
91 * - search tags: `ghi` and `def` (note that filtered tags are not displayed anymore)
93 public function testValidCloudControllerInvokeWithParameters(): void
95 $request = $this->createMock(Request
::class);
97 ->method('getQueryParam')
99 ->willReturnCallback(function (string $key): ?string {
100 if ('searchtags' === $key) {
107 $response = new Response();
109 // Save RainTPL assigned variables
110 $assignedVariables = [];
111 $this->assignTemplateVars($assignedVariables);
113 $this->container
->loginManager
->method('isLoggedin')->willReturn(true);
114 $this->container
->sessionManager
->expects(static::once())->method('getSessionParameter')->willReturn('private');
116 $this->container
->bookmarkService
117 ->expects(static::once())
118 ->method('bookmarksCountPerTag')
119 ->with(['ghi', 'def'], BookmarkFilter
::$PRIVATE)
120 ->willReturnCallback(function (): array {
125 // Make sure that PluginManager hook is triggered
126 $this->container
->pluginManager
127 ->expects(static::at(0))
128 ->method('executeHooks')
129 ->willReturnCallback(function (string $hook, array $data, array $param): array {
130 static::assertSame('render_tagcloud', $hook);
131 static::assertSame('ghi def', $data['search_tags']);
132 static::assertCount(1, $data['tags']);
134 static::assertArrayHasKey('loggedin', $param);
140 $result = $this->controller
->cloud($request, $response);
142 static::assertSame(200, $result->getStatusCode());
143 static::assertSame('tag.cloud', (string) $result->getBody());
144 static::assertSame('ghi def - Tag cloud - Shaarli', $assignedVariables['pagetitle']);
146 static::assertSame('ghi def', $assignedVariables['search_tags']);
147 static::assertCount(1, $assignedVariables['tags']);
149 static::assertArrayHasKey('abc', $assignedVariables['tags']);
150 static::assertSame(3, $assignedVariables['tags']['abc']['count']);
151 static::assertGreaterThan(0, $assignedVariables['tags']['abc']['size']);
152 static::assertLessThan(5, $assignedVariables['tags']['abc']['size']);
158 public function testEmptyCloud(): void
160 $request = $this->createMock(Request
::class);
161 $response = new Response();
163 // Save RainTPL assigned variables
164 $assignedVariables = [];
165 $this->assignTemplateVars($assignedVariables);
167 $this->container
->bookmarkService
168 ->expects(static::once())
169 ->method('bookmarksCountPerTag')
171 ->willReturnCallback(function (array $parameters, ?string $visibility): array {
176 // Make sure that PluginManager hook is triggered
177 $this->container
->pluginManager
178 ->expects(static::at(0))
179 ->method('executeHooks')
180 ->willReturnCallback(function (string $hook, array $data, array $param): array {
181 static::assertSame('render_tagcloud', $hook);
182 static::assertSame('', $data['search_tags']);
183 static::assertCount(0, $data['tags']);
185 static::assertArrayHasKey('loggedin', $param);
191 $result = $this->controller
->cloud($request, $response);
193 static::assertSame(200, $result->getStatusCode());
194 static::assertSame('tag.cloud', (string) $result->getBody());
195 static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']);
197 static::assertSame('', $assignedVariables['search_tags']);
198 static::assertCount(0, $assignedVariables['tags']);
202 * Tag List - Default sort is by usage DESC
204 public function testValidListControllerInvokeDefault(): void
212 $request = $this->createMock(Request
::class);
213 $response = new Response();
215 // Save RainTPL assigned variables
216 $assignedVariables = [];
217 $this->assignTemplateVars($assignedVariables);
219 $this->container
->bookmarkService
220 ->expects(static::once())
221 ->method('bookmarksCountPerTag')
223 ->willReturnCallback(function () use ($allTags): array {
228 // Make sure that PluginManager hook is triggered
229 $this->container
->pluginManager
230 ->expects(static::at(0))
231 ->method('executeHooks')
232 ->willReturnCallback(function (string $hook, array $data, array $param): array {
233 static::assertSame('render_taglist', $hook);
234 static::assertSame('', $data['search_tags']);
235 static::assertCount(3, $data['tags']);
237 static::assertArrayHasKey('loggedin', $param);
243 $result = $this->controller
->list($request, $response);
245 static::assertSame(200, $result->getStatusCode());
246 static::assertSame('tag.list', (string) $result->getBody());
247 static::assertSame('Tag list - Shaarli', $assignedVariables['pagetitle']);
249 static::assertSame('', $assignedVariables['search_tags']);
250 static::assertCount(3, $assignedVariables['tags']);
252 foreach ($allTags as $tag => $count) {
253 static::assertSame($count, $assignedVariables['tags'][$tag]);
258 * Tag List - Additional parameters:
260 * - visibility private
261 * - search tags: `ghi` and `def` (note that filtered tags are not displayed anymore)
262 * - sort alphabetically
264 public function testValidListControllerInvokeWithParameters(): void
266 $request = $this->createMock(Request
::class);
268 ->method('getQueryParam')
270 ->willReturnCallback(function (string $key): ?string {
271 if ('searchtags' === $key) {
273 } elseif ('sort' === $key) {
280 $response = new Response();
282 // Save RainTPL assigned variables
283 $assignedVariables = [];
284 $this->assignTemplateVars($assignedVariables);
286 $this->container
->loginManager
->method('isLoggedin')->willReturn(true);
287 $this->container
->sessionManager
->expects(static::once())->method('getSessionParameter')->willReturn('private');
289 $this->container
->bookmarkService
290 ->expects(static::once())
291 ->method('bookmarksCountPerTag')
292 ->with(['ghi', 'def'], BookmarkFilter
::$PRIVATE)
293 ->willReturnCallback(function (): array {
298 // Make sure that PluginManager hook is triggered
299 $this->container
->pluginManager
300 ->expects(static::at(0))
301 ->method('executeHooks')
302 ->willReturnCallback(function (string $hook, array $data, array $param): array {
303 static::assertSame('render_taglist', $hook);
304 static::assertSame('ghi def', $data['search_tags']);
305 static::assertCount(1, $data['tags']);
307 static::assertArrayHasKey('loggedin', $param);
313 $result = $this->controller
->list($request, $response);
315 static::assertSame(200, $result->getStatusCode());
316 static::assertSame('tag.list', (string) $result->getBody());
317 static::assertSame('ghi def - Tag list - Shaarli', $assignedVariables['pagetitle']);
319 static::assertSame('ghi def', $assignedVariables['search_tags']);
320 static::assertCount(1, $assignedVariables['tags']);
321 static::assertSame(3, $assignedVariables['tags']['abc']);
327 public function testEmptyList(): void
329 $request = $this->createMock(Request
::class);
330 $response = new Response();
332 // Save RainTPL assigned variables
333 $assignedVariables = [];
334 $this->assignTemplateVars($assignedVariables);
336 $this->container
->bookmarkService
337 ->expects(static::once())
338 ->method('bookmarksCountPerTag')
340 ->willReturnCallback(function (array $parameters, ?string $visibility): array {
345 // Make sure that PluginManager hook is triggered
346 $this->container
->pluginManager
347 ->expects(static::at(0))
348 ->method('executeHooks')
349 ->willReturnCallback(function (string $hook, array $data, array $param): array {
350 static::assertSame('render_taglist', $hook);
351 static::assertSame('', $data['search_tags']);
352 static::assertCount(0, $data['tags']);
354 static::assertArrayHasKey('loggedin', $param);
360 $result = $this->controller
->list($request, $response);
362 static::assertSame(200, $result->getStatusCode());
363 static::assertSame('tag.list', (string) $result->getBody());
364 static::assertSame('Tag list - Shaarli', $assignedVariables['pagetitle']);
366 static::assertSame('', $assignedVariables['search_tags']);
367 static::assertCount(0, $assignedVariables['tags']);