diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
commit | b6f678a5a1d15acf284ebcec16c905e976671ce1 (patch) | |
tree | 33c7da831482ed79c44896ef19c73c72ada84f2e /tests/front/controller/visitor/TagCloudControllerTest.php | |
parent | b14687036b9b800681197f51fdc47e62f0c88e2e (diff) | |
parent | 1c1520b6b98ab20201bfe15577782a52320339df (diff) | |
download | Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip |
Merge branch 'v0.12' into latest
Diffstat (limited to 'tests/front/controller/visitor/TagCloudControllerTest.php')
-rw-r--r-- | tests/front/controller/visitor/TagCloudControllerTest.php | 381 |
1 files changed, 381 insertions, 0 deletions
diff --git a/tests/front/controller/visitor/TagCloudControllerTest.php b/tests/front/controller/visitor/TagCloudControllerTest.php new file mode 100644 index 00000000..9305612e --- /dev/null +++ b/tests/front/controller/visitor/TagCloudControllerTest.php | |||
@@ -0,0 +1,381 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
7 | use Shaarli\Bookmark\BookmarkFilter; | ||
8 | use Shaarli\TestCase; | ||
9 | use Slim\Http\Request; | ||
10 | use Slim\Http\Response; | ||
11 | |||
12 | class TagCloudControllerTest extends TestCase | ||
13 | { | ||
14 | use FrontControllerMockHelper; | ||
15 | |||
16 | /** @var TagCloudController */ | ||
17 | protected $controller; | ||
18 | |||
19 | public function setUp(): void | ||
20 | { | ||
21 | $this->createContainer(); | ||
22 | |||
23 | $this->controller = new TagCloudController($this->container); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Tag Cloud - default parameters | ||
28 | */ | ||
29 | public function testValidCloudControllerInvokeDefault(): void | ||
30 | { | ||
31 | $allTags = [ | ||
32 | 'ghi' => 1, | ||
33 | 'abc' => 3, | ||
34 | 'def' => 12, | ||
35 | ]; | ||
36 | $expectedOrder = ['abc', 'def', 'ghi']; | ||
37 | |||
38 | $request = $this->createMock(Request::class); | ||
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::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']); | ||
63 | |||
64 | static::assertArrayHasKey('loggedin', $param); | ||
65 | } | ||
66 | |||
67 | return $data; | ||
68 | }) | ||
69 | ; | ||
70 | |||
71 | $result = $this->controller->cloud($request, $response); | ||
72 | |||
73 | static::assertSame(200, $result->getStatusCode()); | ||
74 | static::assertSame('tag.cloud', (string) $result->getBody()); | ||
75 | static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']); | ||
76 | |||
77 | static::assertSame('', $assignedVariables['search_tags']); | ||
78 | static::assertCount(3, $assignedVariables['tags']); | ||
79 | static::assertSame($expectedOrder, array_keys($assignedVariables['tags'])); | ||
80 | |||
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']); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Tag Cloud - Additional parameters: | ||
91 | * - logged in | ||
92 | * - visibility private | ||
93 | * - search tags: `ghi` and `def` (note that filtered tags are not displayed anymore) | ||
94 | */ | ||
95 | public function testValidCloudControllerInvokeWithParameters(): void | ||
96 | { | ||
97 | $request = $this->createMock(Request::class); | ||
98 | $request | ||
99 | ->method('getQueryParam') | ||
100 | ->with() | ||
101 | ->willReturnCallback(function (string $key): ?string { | ||
102 | if ('searchtags' === $key) { | ||
103 | return 'ghi def'; | ||
104 | } | ||
105 | |||
106 | return null; | ||
107 | }) | ||
108 | ; | ||
109 | $response = new Response(); | ||
110 | |||
111 | // Save RainTPL assigned variables | ||
112 | $assignedVariables = []; | ||
113 | $this->assignTemplateVars($assignedVariables); | ||
114 | |||
115 | $this->container->loginManager->method('isLoggedin')->willReturn(true); | ||
116 | $this->container->sessionManager->expects(static::once())->method('getSessionParameter')->willReturn('private'); | ||
117 | |||
118 | $this->container->bookmarkService | ||
119 | ->expects(static::once()) | ||
120 | ->method('bookmarksCountPerTag') | ||
121 | ->with(['ghi', 'def'], BookmarkFilter::$PRIVATE) | ||
122 | ->willReturnCallback(function (): array { | ||
123 | return ['abc' => 3]; | ||
124 | }) | ||
125 | ; | ||
126 | |||
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']); | ||
136 | |||
137 | static::assertArrayHasKey('loggedin', $param); | ||
138 | } | ||
139 | |||
140 | return $data; | ||
141 | }) | ||
142 | ; | ||
143 | |||
144 | $result = $this->controller->cloud($request, $response); | ||
145 | |||
146 | static::assertSame(200, $result->getStatusCode()); | ||
147 | static::assertSame('tag.cloud', (string) $result->getBody()); | ||
148 | static::assertSame('ghi def - Tag cloud - Shaarli', $assignedVariables['pagetitle']); | ||
149 | |||
150 | static::assertSame('ghi def', $assignedVariables['search_tags']); | ||
151 | static::assertCount(1, $assignedVariables['tags']); | ||
152 | |||
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']); | ||
157 | } | ||
158 | |||
159 | /** | ||
160 | * Tag Cloud - empty | ||
161 | */ | ||
162 | public function testEmptyCloud(): void | ||
163 | { | ||
164 | $request = $this->createMock(Request::class); | ||
165 | $response = new Response(); | ||
166 | |||
167 | // Save RainTPL assigned variables | ||
168 | $assignedVariables = []; | ||
169 | $this->assignTemplateVars($assignedVariables); | ||
170 | |||
171 | $this->container->bookmarkService | ||
172 | ->expects(static::once()) | ||
173 | ->method('bookmarksCountPerTag') | ||
174 | ->with([], null) | ||
175 | ->willReturnCallback(function (array $parameters, ?string $visibility): array { | ||
176 | return []; | ||
177 | }) | ||
178 | ; | ||
179 | |||
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']); | ||
189 | |||
190 | static::assertArrayHasKey('loggedin', $param); | ||
191 | } | ||
192 | |||
193 | return $data; | ||
194 | }) | ||
195 | ; | ||
196 | |||
197 | $result = $this->controller->cloud($request, $response); | ||
198 | |||
199 | static::assertSame(200, $result->getStatusCode()); | ||
200 | static::assertSame('tag.cloud', (string) $result->getBody()); | ||
201 | static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']); | ||
202 | |||
203 | static::assertSame('', $assignedVariables['search_tags']); | ||
204 | static::assertCount(0, $assignedVariables['tags']); | ||
205 | } | ||
206 | |||
207 | /** | ||
208 | * Tag List - Default sort is by usage DESC | ||
209 | */ | ||
210 | public function testValidListControllerInvokeDefault(): void | ||
211 | { | ||
212 | $allTags = [ | ||
213 | 'def' => 12, | ||
214 | 'abc' => 3, | ||
215 | 'ghi' => 1, | ||
216 | ]; | ||
217 | |||
218 | $request = $this->createMock(Request::class); | ||
219 | $response = new Response(); | ||
220 | |||
221 | // Save RainTPL assigned variables | ||
222 | $assignedVariables = []; | ||
223 | $this->assignTemplateVars($assignedVariables); | ||
224 | |||
225 | $this->container->bookmarkService | ||
226 | ->expects(static::once()) | ||
227 | ->method('bookmarksCountPerTag') | ||
228 | ->with([], null) | ||
229 | ->willReturnCallback(function () use ($allTags): array { | ||
230 | return $allTags; | ||
231 | }) | ||
232 | ; | ||
233 | |||
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']); | ||
243 | |||
244 | static::assertArrayHasKey('loggedin', $param); | ||
245 | } | ||
246 | |||
247 | return $data; | ||
248 | }) | ||
249 | ; | ||
250 | |||
251 | $result = $this->controller->list($request, $response); | ||
252 | |||
253 | static::assertSame(200, $result->getStatusCode()); | ||
254 | static::assertSame('tag.list', (string) $result->getBody()); | ||
255 | static::assertSame('Tag list - Shaarli', $assignedVariables['pagetitle']); | ||
256 | |||
257 | static::assertSame('', $assignedVariables['search_tags']); | ||
258 | static::assertCount(3, $assignedVariables['tags']); | ||
259 | |||
260 | foreach ($allTags as $tag => $count) { | ||
261 | static::assertSame($count, $assignedVariables['tags'][$tag]); | ||
262 | } | ||
263 | } | ||
264 | |||
265 | /** | ||
266 | * Tag List - Additional parameters: | ||
267 | * - logged in | ||
268 | * - visibility private | ||
269 | * - search tags: `ghi` and `def` (note that filtered tags are not displayed anymore) | ||
270 | * - sort alphabetically | ||
271 | */ | ||
272 | public function testValidListControllerInvokeWithParameters(): void | ||
273 | { | ||
274 | $request = $this->createMock(Request::class); | ||
275 | $request | ||
276 | ->method('getQueryParam') | ||
277 | ->with() | ||
278 | ->willReturnCallback(function (string $key): ?string { | ||
279 | if ('searchtags' === $key) { | ||
280 | return 'ghi def'; | ||
281 | } elseif ('sort' === $key) { | ||
282 | return 'alpha'; | ||
283 | } | ||
284 | |||
285 | return null; | ||
286 | }) | ||
287 | ; | ||
288 | $response = new Response(); | ||
289 | |||
290 | // Save RainTPL assigned variables | ||
291 | $assignedVariables = []; | ||
292 | $this->assignTemplateVars($assignedVariables); | ||
293 | |||
294 | $this->container->loginManager->method('isLoggedin')->willReturn(true); | ||
295 | $this->container->sessionManager->expects(static::once())->method('getSessionParameter')->willReturn('private'); | ||
296 | |||
297 | $this->container->bookmarkService | ||
298 | ->expects(static::once()) | ||
299 | ->method('bookmarksCountPerTag') | ||
300 | ->with(['ghi', 'def'], BookmarkFilter::$PRIVATE) | ||
301 | ->willReturnCallback(function (): array { | ||
302 | return ['abc' => 3]; | ||
303 | }) | ||
304 | ; | ||
305 | |||
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']); | ||
315 | |||
316 | static::assertArrayHasKey('loggedin', $param); | ||
317 | } | ||
318 | |||
319 | return $data; | ||
320 | }) | ||
321 | ; | ||
322 | |||
323 | $result = $this->controller->list($request, $response); | ||
324 | |||
325 | static::assertSame(200, $result->getStatusCode()); | ||
326 | static::assertSame('tag.list', (string) $result->getBody()); | ||
327 | static::assertSame('ghi def - Tag list - Shaarli', $assignedVariables['pagetitle']); | ||
328 | |||
329 | static::assertSame('ghi def', $assignedVariables['search_tags']); | ||
330 | static::assertCount(1, $assignedVariables['tags']); | ||
331 | static::assertSame(3, $assignedVariables['tags']['abc']); | ||
332 | } | ||
333 | |||
334 | /** | ||
335 | * Tag List - empty | ||
336 | */ | ||
337 | public function testEmptyList(): void | ||
338 | { | ||
339 | $request = $this->createMock(Request::class); | ||
340 | $response = new Response(); | ||
341 | |||
342 | // Save RainTPL assigned variables | ||
343 | $assignedVariables = []; | ||
344 | $this->assignTemplateVars($assignedVariables); | ||
345 | |||
346 | $this->container->bookmarkService | ||
347 | ->expects(static::once()) | ||
348 | ->method('bookmarksCountPerTag') | ||
349 | ->with([], null) | ||
350 | ->willReturnCallback(function (array $parameters, ?string $visibility): array { | ||
351 | return []; | ||
352 | }) | ||
353 | ; | ||
354 | |||
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']); | ||
364 | |||
365 | static::assertArrayHasKey('loggedin', $param); | ||
366 | } | ||
367 | |||
368 | return $data; | ||
369 | }) | ||
370 | ; | ||
371 | |||
372 | $result = $this->controller->list($request, $response); | ||
373 | |||
374 | static::assertSame(200, $result->getStatusCode()); | ||
375 | static::assertSame('tag.list', (string) $result->getBody()); | ||
376 | static::assertSame('Tag list - Shaarli', $assignedVariables['pagetitle']); | ||
377 | |||
378 | static::assertSame('', $assignedVariables['search_tags']); | ||
379 | static::assertCount(0, $assignedVariables['tags']); | ||
380 | } | ||
381 | } | ||