3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 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::atLeastOnce())
57 ->method('executeHooks')
58 ->withConsecutive(['render_tagcloud'])
59 ->willReturnCallback(function (string $hook, array $data, array $param): array {
60 if ('render_tagcloud' === $hook) {
61 static::assertSame('', $data['search_tags']);
62 static::assertCount(3, $data['tags']);
64 static::assertArrayHasKey('loggedin', $param);
71 $result = $this->controller
->cloud($request, $response);
73 static::assertSame(200, $result->getStatusCode());
74 static::assertSame('tag.cloud', (string) $result->getBody());
75 static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']);
77 static::assertSame('', $assignedVariables['search_tags']);
78 static::assertCount(3, $assignedVariables['tags']);
79 static::assertSame($expectedOrder, array_keys($assignedVariables['tags']));
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']);
90 * Tag Cloud - Additional parameters:
92 * - visibility private
93 * - search tags: `ghi` and `def` (note that filtered tags are not displayed anymore)
95 public function testValidCloudControllerInvokeWithParameters(): void
97 $request = $this->createMock(Request
::class);
99 ->method('getQueryParam')
101 ->willReturnCallback(function (string $key): ?string {
102 if ('searchtags' === $key) {
109 $response = new Response();
111 // Save RainTPL assigned variables
112 $assignedVariables = [];
113 $this->assignTemplateVars($assignedVariables);
115 $this->container
->loginManager
->method('isLoggedin')->willReturn(true);
116 $this->container
->sessionManager
->expects(static::once())->method('getSessionParameter')->willReturn('private');
118 $this->container
->bookmarkService
119 ->expects(static::once())
120 ->method('bookmarksCountPerTag')
121 ->with(['ghi', 'def'], BookmarkFilter
::$PRIVATE)
122 ->willReturnCallback(function (): array {
127 // Make sure that PluginManager hook is triggered
128 $this->container
->pluginManager
129 ->expects(static::atLeastOnce())
130 ->method('executeHooks')
131 ->withConsecutive(['render_tagcloud'])
132 ->willReturnCallback(function (string $hook, array $data, array $param): array {
133 if ('render_tagcloud' === $hook) {
134 static::assertSame('ghi def', $data['search_tags']);
135 static::assertCount(1, $data['tags']);
137 static::assertArrayHasKey('loggedin', $param);
144 $result = $this->controller
->cloud($request, $response);
146 static::assertSame(200, $result->getStatusCode());
147 static::assertSame('tag.cloud', (string) $result->getBody());
148 static::assertSame('ghi def - Tag cloud - Shaarli', $assignedVariables['pagetitle']);
150 static::assertSame('ghi def', $assignedVariables['search_tags']);
151 static::assertCount(1, $assignedVariables['tags']);
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']);
162 public function testEmptyCloud(): void
164 $request = $this->createMock(Request
::class);
165 $response = new Response();
167 // Save RainTPL assigned variables
168 $assignedVariables = [];
169 $this->assignTemplateVars($assignedVariables);
171 $this->container
->bookmarkService
172 ->expects(static::once())
173 ->method('bookmarksCountPerTag')
175 ->willReturnCallback(function (array $parameters, ?string $visibility): array {
180 // Make sure that PluginManager hook is triggered
181 $this->container
->pluginManager
182 ->expects(static::atLeastOnce())
183 ->method('executeHooks')
184 ->withConsecutive(['render_tagcloud'])
185 ->willReturnCallback(function (string $hook, array $data, array $param): array {
186 if ('render_tagcloud' === $hook) {
187 static::assertSame('', $data['search_tags']);
188 static::assertCount(0, $data['tags']);
190 static::assertArrayHasKey('loggedin', $param);
197 $result = $this->controller
->cloud($request, $response);
199 static::assertSame(200, $result->getStatusCode());
200 static::assertSame('tag.cloud', (string) $result->getBody());
201 static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']);
203 static::assertSame('', $assignedVariables['search_tags']);
204 static::assertCount(0, $assignedVariables['tags']);
208 * Tag List - Default sort is by usage DESC
210 public function testValidListControllerInvokeDefault(): void
218 $request = $this->createMock(Request
::class);
219 $response = new Response();
221 // Save RainTPL assigned variables
222 $assignedVariables = [];
223 $this->assignTemplateVars($assignedVariables);
225 $this->container
->bookmarkService
226 ->expects(static::once())
227 ->method('bookmarksCountPerTag')
229 ->willReturnCallback(function () use ($allTags): array {
234 // Make sure that PluginManager hook is triggered
235 $this->container
->pluginManager
236 ->expects(static::atLeastOnce())
237 ->method('executeHooks')
238 ->withConsecutive(['render_taglist'])
239 ->willReturnCallback(function (string $hook, array $data, array $param): array {
240 if ('render_taglist' === $hook) {
241 static::assertSame('', $data['search_tags']);
242 static::assertCount(3, $data['tags']);
244 static::assertArrayHasKey('loggedin', $param);
251 $result = $this->controller
->list($request, $response);
253 static::assertSame(200, $result->getStatusCode());
254 static::assertSame('tag.list', (string) $result->getBody());
255 static::assertSame('Tag list - Shaarli', $assignedVariables['pagetitle']);
257 static::assertSame('', $assignedVariables['search_tags']);
258 static::assertCount(3, $assignedVariables['tags']);
260 foreach ($allTags as $tag => $count) {
261 static::assertSame($count, $assignedVariables['tags'][$tag]);
266 * Tag List - Additional parameters:
268 * - visibility private
269 * - search tags: `ghi` and `def` (note that filtered tags are not displayed anymore)
270 * - sort alphabetically
272 public function testValidListControllerInvokeWithParameters(): void
274 $request = $this->createMock(Request
::class);
276 ->method('getQueryParam')
278 ->willReturnCallback(function (string $key): ?string {
279 if ('searchtags' === $key) {
281 } elseif ('sort' === $key) {
288 $response = new Response();
290 // Save RainTPL assigned variables
291 $assignedVariables = [];
292 $this->assignTemplateVars($assignedVariables);
294 $this->container
->loginManager
->method('isLoggedin')->willReturn(true);
295 $this->container
->sessionManager
->expects(static::once())->method('getSessionParameter')->willReturn('private');
297 $this->container
->bookmarkService
298 ->expects(static::once())
299 ->method('bookmarksCountPerTag')
300 ->with(['ghi', 'def'], BookmarkFilter
::$PRIVATE)
301 ->willReturnCallback(function (): array {
306 // Make sure that PluginManager hook is triggered
307 $this->container
->pluginManager
308 ->expects(static::atLeastOnce())
309 ->method('executeHooks')
310 ->withConsecutive(['render_taglist'])
311 ->willReturnCallback(function (string $hook, array $data, array $param): array {
312 if ('render_taglist' === $hook) {
313 static::assertSame('ghi def', $data['search_tags']);
314 static::assertCount(1, $data['tags']);
316 static::assertArrayHasKey('loggedin', $param);
323 $result = $this->controller
->list($request, $response);
325 static::assertSame(200, $result->getStatusCode());
326 static::assertSame('tag.list', (string) $result->getBody());
327 static::assertSame('ghi def - Tag list - Shaarli', $assignedVariables['pagetitle']);
329 static::assertSame('ghi def', $assignedVariables['search_tags']);
330 static::assertCount(1, $assignedVariables['tags']);
331 static::assertSame(3, $assignedVariables['tags']['abc']);
337 public function testEmptyList(): void
339 $request = $this->createMock(Request
::class);
340 $response = new Response();
342 // Save RainTPL assigned variables
343 $assignedVariables = [];
344 $this->assignTemplateVars($assignedVariables);
346 $this->container
->bookmarkService
347 ->expects(static::once())
348 ->method('bookmarksCountPerTag')
350 ->willReturnCallback(function (array $parameters, ?string $visibility): array {
355 // Make sure that PluginManager hook is triggered
356 $this->container
->pluginManager
357 ->expects(static::atLeastOnce())
358 ->method('executeHooks')
359 ->withConsecutive(['render_taglist'])
360 ->willReturnCallback(function (string $hook, array $data, array $param): array {
361 if ('render_taglist' === $hook) {
362 static::assertSame('', $data['search_tags']);
363 static::assertCount(0, $data['tags']);
365 static::assertArrayHasKey('loggedin', $param);
372 $result = $this->controller
->list($request, $response);
374 static::assertSame(200, $result->getStatusCode());
375 static::assertSame('tag.list', (string) $result->getBody());
376 static::assertSame('Tag list - Shaarli', $assignedVariables['pagetitle']);
378 static::assertSame('', $assignedVariables['search_tags']);
379 static::assertCount(0, $assignedVariables['tags']);