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
31 $this->createValidContainerMockSet();
38 $expectedOrder = ['abc', 'def', 'ghi'];
40 $request = $this->createMock(Request
::class);
41 $response = new Response();
43 // Save RainTPL assigned variables
44 $assignedVariables = [];
45 $this->assignTemplateVars($assignedVariables);
47 $this->container
->bookmarkService
48 ->expects(static::once())
49 ->method('bookmarksCountPerTag')
51 ->willReturnCallback(function () use ($allTags): array {
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']);
65 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 $this->createValidContainerMockSet();
99 $request = $this->createMock(Request
::class);
101 ->method('getQueryParam')
103 ->willReturnCallback(function (string $key): ?string {
104 if ('searchtags' === $key) {
111 $response = new Response();
113 // Save RainTPL assigned variables
114 $assignedVariables = [];
115 $this->assignTemplateVars($assignedVariables);
117 $this->container
->loginManager
->method('isLoggedin')->willReturn(true);
118 $this->container
->sessionManager
->expects(static::once())->method('getSessionParameter')->willReturn('private');
120 $this->container
->bookmarkService
121 ->expects(static::once())
122 ->method('bookmarksCountPerTag')
123 ->with(['ghi', 'def'], BookmarkFilter
::$PRIVATE)
124 ->willReturnCallback(function (): array {
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']);
138 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 $this->createValidContainerMockSet();
166 $request = $this->createMock(Request
::class);
167 $response = new Response();
169 // Save RainTPL assigned variables
170 $assignedVariables = [];
171 $this->assignTemplateVars($assignedVariables);
173 $this->container
->bookmarkService
174 ->expects(static::once())
175 ->method('bookmarksCountPerTag')
177 ->willReturnCallback(function (array $parameters, ?string $visibility): array {
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']);
191 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
212 $this->createValidContainerMockSet();
220 $request = $this->createMock(Request
::class);
221 $response = new Response();
223 // Save RainTPL assigned variables
224 $assignedVariables = [];
225 $this->assignTemplateVars($assignedVariables);
227 $this->container
->bookmarkService
228 ->expects(static::once())
229 ->method('bookmarksCountPerTag')
231 ->willReturnCallback(function () use ($allTags): array {
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']);
245 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 $this->createValidContainerMockSet();
276 $request = $this->createMock(Request
::class);
278 ->method('getQueryParam')
280 ->willReturnCallback(function (string $key): ?string {
281 if ('searchtags' === $key) {
283 } elseif ('sort' === $key) {
290 $response = new Response();
292 // Save RainTPL assigned variables
293 $assignedVariables = [];
294 $this->assignTemplateVars($assignedVariables);
296 $this->container
->loginManager
->method('isLoggedin')->willReturn(true);
297 $this->container
->sessionManager
->expects(static::once())->method('getSessionParameter')->willReturn('private');
299 $this->container
->bookmarkService
300 ->expects(static::once())
301 ->method('bookmarksCountPerTag')
302 ->with(['ghi', 'def'], BookmarkFilter
::$PRIVATE)
303 ->willReturnCallback(function (): array {
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']);
317 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 $this->createValidContainerMockSet();
341 $request = $this->createMock(Request
::class);
342 $response = new Response();
344 // Save RainTPL assigned variables
345 $assignedVariables = [];
346 $this->assignTemplateVars($assignedVariables);
348 $this->container
->bookmarkService
349 ->expects(static::once())
350 ->method('bookmarksCountPerTag')
352 ->willReturnCallback(function (array $parameters, ?string $visibility): array {
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']);
366 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']);