]>
Commit | Line | Data |
---|---|---|
72caf4e8 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
2899ebb5 | 5 | namespace Shaarli\Front\Controller\Visitor; |
72caf4e8 A |
6 | |
7 | use PHPUnit\Framework\TestCase; | |
8 | use Shaarli\Bookmark\BookmarkFilter; | |
72caf4e8 A |
9 | use Slim\Http\Request; |
10 | use Slim\Http\Response; | |
11 | ||
12 | class 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 | { | |
72caf4e8 A |
31 | $allTags = [ |
32 | 'ghi' => 1, | |
33 | 'abc' => 3, | |
34 | 'def' => 12, | |
35 | ]; | |
36 | $expectedOrder = ['abc', 'def', 'ghi']; | |
37 | ||
38 | $request = $this->createMock(Request::class); | |
72caf4e8 A |
39 | $response = new Response(); |
40 | ||
41 | // Save RainTPL assigned variables | |
42 | $assignedVariables = []; | |
43 | $this->assignTemplateVars($assignedVariables); | |
44 | ||
45 | $this->container->bookmarkService | |
46 | ->expects(static::once()) | |
47 | ->method('bookmarksCountPerTag') | |
48 | ->with([], null) | |
49 | ->willReturnCallback(function () use ($allTags): array { | |
50 | return $allTags; | |
51 | }) | |
52 | ; | |
53 | ||
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']); | |
62 | ||
63 | static::assertArrayHasKey('loggedin', $param); | |
64 | ||
65 | return $data; | |
66 | }) | |
67 | ; | |
68 | ||
3772298e | 69 | $result = $this->controller->cloud($request, $response); |
72caf4e8 A |
70 | |
71 | static::assertSame(200, $result->getStatusCode()); | |
72 | static::assertSame('tag.cloud', (string) $result->getBody()); | |
73 | static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']); | |
74 | ||
75 | static::assertSame('', $assignedVariables['search_tags']); | |
76 | static::assertCount(3, $assignedVariables['tags']); | |
77 | static::assertSame($expectedOrder, array_keys($assignedVariables['tags'])); | |
78 | ||
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']); | |
84 | } | |
85 | } | |
86 | ||
87 | /** | |
60ae2412 | 88 | * Tag Cloud - Additional parameters: |
72caf4e8 A |
89 | * - logged in |
90 | * - visibility private | |
91 | * - search tags: `ghi` and `def` (note that filtered tags are not displayed anymore) | |
92 | */ | |
93 | public function testValidCloudControllerInvokeWithParameters(): void | |
94 | { | |
72caf4e8 A |
95 | $request = $this->createMock(Request::class); |
96 | $request | |
72caf4e8 | 97 | ->method('getQueryParam') |
60ae2412 A |
98 | ->with() |
99 | ->willReturnCallback(function (string $key): ?string { | |
100 | if ('searchtags' === $key) { | |
101 | return 'ghi def'; | |
102 | } | |
103 | ||
104 | return null; | |
105 | }) | |
72caf4e8 A |
106 | ; |
107 | $response = new Response(); | |
108 | ||
109 | // Save RainTPL assigned variables | |
110 | $assignedVariables = []; | |
111 | $this->assignTemplateVars($assignedVariables); | |
112 | ||
113 | $this->container->loginManager->method('isLoggedin')->willReturn(true); | |
114 | $this->container->sessionManager->expects(static::once())->method('getSessionParameter')->willReturn('private'); | |
115 | ||
116 | $this->container->bookmarkService | |
117 | ->expects(static::once()) | |
118 | ->method('bookmarksCountPerTag') | |
119 | ->with(['ghi', 'def'], BookmarkFilter::$PRIVATE) | |
c79473bd A |
120 | ->willReturnCallback(function (): array { |
121 | return ['abc' => 3]; | |
72caf4e8 A |
122 | }) |
123 | ; | |
124 | ||
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']); | |
133 | ||
134 | static::assertArrayHasKey('loggedin', $param); | |
135 | ||
136 | return $data; | |
137 | }) | |
138 | ; | |
139 | ||
3772298e | 140 | $result = $this->controller->cloud($request, $response); |
72caf4e8 A |
141 | |
142 | static::assertSame(200, $result->getStatusCode()); | |
143 | static::assertSame('tag.cloud', (string) $result->getBody()); | |
144 | static::assertSame('ghi def - Tag cloud - Shaarli', $assignedVariables['pagetitle']); | |
145 | ||
146 | static::assertSame('ghi def', $assignedVariables['search_tags']); | |
147 | static::assertCount(1, $assignedVariables['tags']); | |
148 | ||
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']); | |
153 | } | |
154 | ||
60ae2412 A |
155 | /** |
156 | * Tag Cloud - empty | |
157 | */ | |
72caf4e8 A |
158 | public function testEmptyCloud(): void |
159 | { | |
72caf4e8 | 160 | $request = $this->createMock(Request::class); |
72caf4e8 A |
161 | $response = new Response(); |
162 | ||
163 | // Save RainTPL assigned variables | |
164 | $assignedVariables = []; | |
165 | $this->assignTemplateVars($assignedVariables); | |
166 | ||
167 | $this->container->bookmarkService | |
168 | ->expects(static::once()) | |
169 | ->method('bookmarksCountPerTag') | |
170 | ->with([], null) | |
171 | ->willReturnCallback(function (array $parameters, ?string $visibility): array { | |
172 | return []; | |
173 | }) | |
174 | ; | |
175 | ||
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']); | |
184 | ||
185 | static::assertArrayHasKey('loggedin', $param); | |
186 | ||
187 | return $data; | |
188 | }) | |
189 | ; | |
190 | ||
3772298e | 191 | $result = $this->controller->cloud($request, $response); |
72caf4e8 A |
192 | |
193 | static::assertSame(200, $result->getStatusCode()); | |
194 | static::assertSame('tag.cloud', (string) $result->getBody()); | |
195 | static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']); | |
196 | ||
197 | static::assertSame('', $assignedVariables['search_tags']); | |
198 | static::assertCount(0, $assignedVariables['tags']); | |
199 | } | |
200 | ||
60ae2412 A |
201 | /** |
202 | * Tag List - Default sort is by usage DESC | |
203 | */ | |
204 | public function testValidListControllerInvokeDefault(): void | |
205 | { | |
60ae2412 A |
206 | $allTags = [ |
207 | 'def' => 12, | |
208 | 'abc' => 3, | |
209 | 'ghi' => 1, | |
210 | ]; | |
211 | ||
212 | $request = $this->createMock(Request::class); | |
213 | $response = new Response(); | |
214 | ||
215 | // Save RainTPL assigned variables | |
216 | $assignedVariables = []; | |
217 | $this->assignTemplateVars($assignedVariables); | |
218 | ||
219 | $this->container->bookmarkService | |
220 | ->expects(static::once()) | |
221 | ->method('bookmarksCountPerTag') | |
222 | ->with([], null) | |
223 | ->willReturnCallback(function () use ($allTags): array { | |
224 | return $allTags; | |
225 | }) | |
226 | ; | |
227 | ||
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']); | |
236 | ||
237 | static::assertArrayHasKey('loggedin', $param); | |
238 | ||
239 | return $data; | |
240 | }) | |
241 | ; | |
242 | ||
243 | $result = $this->controller->list($request, $response); | |
244 | ||
245 | static::assertSame(200, $result->getStatusCode()); | |
246 | static::assertSame('tag.list', (string) $result->getBody()); | |
247 | static::assertSame('Tag list - Shaarli', $assignedVariables['pagetitle']); | |
248 | ||
249 | static::assertSame('', $assignedVariables['search_tags']); | |
250 | static::assertCount(3, $assignedVariables['tags']); | |
251 | ||
252 | foreach ($allTags as $tag => $count) { | |
253 | static::assertSame($count, $assignedVariables['tags'][$tag]); | |
254 | } | |
255 | } | |
256 | ||
257 | /** | |
258 | * Tag List - Additional parameters: | |
259 | * - logged in | |
260 | * - visibility private | |
261 | * - search tags: `ghi` and `def` (note that filtered tags are not displayed anymore) | |
262 | * - sort alphabetically | |
263 | */ | |
264 | public function testValidListControllerInvokeWithParameters(): void | |
265 | { | |
60ae2412 A |
266 | $request = $this->createMock(Request::class); |
267 | $request | |
268 | ->method('getQueryParam') | |
269 | ->with() | |
270 | ->willReturnCallback(function (string $key): ?string { | |
271 | if ('searchtags' === $key) { | |
272 | return 'ghi def'; | |
273 | } elseif ('sort' === $key) { | |
274 | return 'alpha'; | |
275 | } | |
276 | ||
277 | return null; | |
278 | }) | |
279 | ; | |
280 | $response = new Response(); | |
281 | ||
282 | // Save RainTPL assigned variables | |
283 | $assignedVariables = []; | |
284 | $this->assignTemplateVars($assignedVariables); | |
285 | ||
286 | $this->container->loginManager->method('isLoggedin')->willReturn(true); | |
287 | $this->container->sessionManager->expects(static::once())->method('getSessionParameter')->willReturn('private'); | |
288 | ||
289 | $this->container->bookmarkService | |
290 | ->expects(static::once()) | |
291 | ->method('bookmarksCountPerTag') | |
292 | ->with(['ghi', 'def'], BookmarkFilter::$PRIVATE) | |
293 | ->willReturnCallback(function (): array { | |
294 | return ['abc' => 3]; | |
295 | }) | |
296 | ; | |
297 | ||
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']); | |
306 | ||
307 | static::assertArrayHasKey('loggedin', $param); | |
308 | ||
309 | return $data; | |
310 | }) | |
311 | ; | |
312 | ||
313 | $result = $this->controller->list($request, $response); | |
314 | ||
315 | static::assertSame(200, $result->getStatusCode()); | |
316 | static::assertSame('tag.list', (string) $result->getBody()); | |
317 | static::assertSame('ghi def - Tag list - Shaarli', $assignedVariables['pagetitle']); | |
318 | ||
319 | static::assertSame('ghi def', $assignedVariables['search_tags']); | |
320 | static::assertCount(1, $assignedVariables['tags']); | |
321 | static::assertSame(3, $assignedVariables['tags']['abc']); | |
322 | } | |
323 | ||
324 | /** | |
325 | * Tag List - empty | |
326 | */ | |
327 | public function testEmptyList(): void | |
328 | { | |
60ae2412 A |
329 | $request = $this->createMock(Request::class); |
330 | $response = new Response(); | |
331 | ||
332 | // Save RainTPL assigned variables | |
333 | $assignedVariables = []; | |
334 | $this->assignTemplateVars($assignedVariables); | |
335 | ||
336 | $this->container->bookmarkService | |
337 | ->expects(static::once()) | |
338 | ->method('bookmarksCountPerTag') | |
339 | ->with([], null) | |
340 | ->willReturnCallback(function (array $parameters, ?string $visibility): array { | |
341 | return []; | |
342 | }) | |
343 | ; | |
344 | ||
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']); | |
353 | ||
354 | static::assertArrayHasKey('loggedin', $param); | |
355 | ||
356 | return $data; | |
357 | }) | |
358 | ; | |
359 | ||
360 | $result = $this->controller->list($request, $response); | |
361 | ||
362 | static::assertSame(200, $result->getStatusCode()); | |
363 | static::assertSame('tag.list', (string) $result->getBody()); | |
364 | static::assertSame('Tag list - Shaarli', $assignedVariables['pagetitle']); | |
365 | ||
366 | static::assertSame('', $assignedVariables['search_tags']); | |
367 | static::assertCount(0, $assignedVariables['tags']); | |
368 | } | |
72caf4e8 | 369 | } |