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/DailyControllerTest.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/DailyControllerTest.php')
-rw-r--r-- | tests/front/controller/visitor/DailyControllerTest.php | 478 |
1 files changed, 478 insertions, 0 deletions
diff --git a/tests/front/controller/visitor/DailyControllerTest.php b/tests/front/controller/visitor/DailyControllerTest.php new file mode 100644 index 00000000..fc78bc13 --- /dev/null +++ b/tests/front/controller/visitor/DailyControllerTest.php | |||
@@ -0,0 +1,478 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
7 | use Shaarli\Bookmark\Bookmark; | ||
8 | use Shaarli\Feed\CachedPage; | ||
9 | use Shaarli\TestCase; | ||
10 | use Slim\Http\Request; | ||
11 | use Slim\Http\Response; | ||
12 | |||
13 | class DailyControllerTest extends TestCase | ||
14 | { | ||
15 | use FrontControllerMockHelper; | ||
16 | |||
17 | /** @var DailyController */ | ||
18 | protected $controller; | ||
19 | |||
20 | public function setUp(): void | ||
21 | { | ||
22 | $this->createContainer(); | ||
23 | |||
24 | $this->controller = new DailyController($this->container); | ||
25 | DailyController::$DAILY_RSS_NB_DAYS = 2; | ||
26 | } | ||
27 | |||
28 | public function testValidIndexControllerInvokeDefault(): void | ||
29 | { | ||
30 | $currentDay = new \DateTimeImmutable('2020-05-13'); | ||
31 | |||
32 | $request = $this->createMock(Request::class); | ||
33 | $request->method('getQueryParam')->willReturn($currentDay->format('Ymd')); | ||
34 | $response = new Response(); | ||
35 | |||
36 | // Save RainTPL assigned variables | ||
37 | $assignedVariables = []; | ||
38 | $this->assignTemplateVars($assignedVariables); | ||
39 | |||
40 | // Links dataset: 2 links with thumbnails | ||
41 | $this->container->bookmarkService | ||
42 | ->expects(static::once()) | ||
43 | ->method('days') | ||
44 | ->willReturnCallback(function () use ($currentDay): array { | ||
45 | return [ | ||
46 | '20200510', | ||
47 | $currentDay->format('Ymd'), | ||
48 | '20200516', | ||
49 | ]; | ||
50 | }) | ||
51 | ; | ||
52 | $this->container->bookmarkService | ||
53 | ->expects(static::once()) | ||
54 | ->method('filterDay') | ||
55 | ->willReturnCallback(function (): array { | ||
56 | return [ | ||
57 | (new Bookmark()) | ||
58 | ->setId(1) | ||
59 | ->setUrl('http://url.tld') | ||
60 | ->setTitle(static::generateString(50)) | ||
61 | ->setDescription(static::generateString(500)) | ||
62 | , | ||
63 | (new Bookmark()) | ||
64 | ->setId(2) | ||
65 | ->setUrl('http://url2.tld') | ||
66 | ->setTitle(static::generateString(50)) | ||
67 | ->setDescription(static::generateString(500)) | ||
68 | , | ||
69 | (new Bookmark()) | ||
70 | ->setId(3) | ||
71 | ->setUrl('http://url3.tld') | ||
72 | ->setTitle(static::generateString(50)) | ||
73 | ->setDescription(static::generateString(500)) | ||
74 | , | ||
75 | ]; | ||
76 | }) | ||
77 | ; | ||
78 | |||
79 | // Make sure that PluginManager hook is triggered | ||
80 | $this->container->pluginManager | ||
81 | ->expects(static::atLeastOnce()) | ||
82 | ->method('executeHooks') | ||
83 | ->withConsecutive(['render_daily']) | ||
84 | ->willReturnCallback(function (string $hook, array $data, array $param) use ($currentDay): array { | ||
85 | if ('render_daily' === $hook) { | ||
86 | static::assertArrayHasKey('linksToDisplay', $data); | ||
87 | static::assertCount(3, $data['linksToDisplay']); | ||
88 | static::assertSame(1, $data['linksToDisplay'][0]['id']); | ||
89 | static::assertSame($currentDay->getTimestamp(), $data['day']); | ||
90 | static::assertSame('20200510', $data['previousday']); | ||
91 | static::assertSame('20200516', $data['nextday']); | ||
92 | |||
93 | static::assertArrayHasKey('loggedin', $param); | ||
94 | } | ||
95 | |||
96 | return $data; | ||
97 | }) | ||
98 | ; | ||
99 | |||
100 | $result = $this->controller->index($request, $response); | ||
101 | |||
102 | static::assertSame(200, $result->getStatusCode()); | ||
103 | static::assertSame('daily', (string) $result->getBody()); | ||
104 | static::assertSame( | ||
105 | 'Daily - '. format_date($currentDay, false, true) .' - Shaarli', | ||
106 | $assignedVariables['pagetitle'] | ||
107 | ); | ||
108 | static::assertEquals($currentDay, $assignedVariables['dayDate']); | ||
109 | static::assertEquals($currentDay->getTimestamp(), $assignedVariables['day']); | ||
110 | static::assertCount(3, $assignedVariables['linksToDisplay']); | ||
111 | |||
112 | $link = $assignedVariables['linksToDisplay'][0]; | ||
113 | |||
114 | static::assertSame(1, $link['id']); | ||
115 | static::assertSame('http://url.tld', $link['url']); | ||
116 | static::assertNotEmpty($link['title']); | ||
117 | static::assertNotEmpty($link['description']); | ||
118 | static::assertNotEmpty($link['formatedDescription']); | ||
119 | |||
120 | $link = $assignedVariables['linksToDisplay'][1]; | ||
121 | |||
122 | static::assertSame(2, $link['id']); | ||
123 | static::assertSame('http://url2.tld', $link['url']); | ||
124 | static::assertNotEmpty($link['title']); | ||
125 | static::assertNotEmpty($link['description']); | ||
126 | static::assertNotEmpty($link['formatedDescription']); | ||
127 | |||
128 | $link = $assignedVariables['linksToDisplay'][2]; | ||
129 | |||
130 | static::assertSame(3, $link['id']); | ||
131 | static::assertSame('http://url3.tld', $link['url']); | ||
132 | static::assertNotEmpty($link['title']); | ||
133 | static::assertNotEmpty($link['description']); | ||
134 | static::assertNotEmpty($link['formatedDescription']); | ||
135 | |||
136 | static::assertCount(3, $assignedVariables['cols']); | ||
137 | static::assertCount(1, $assignedVariables['cols'][0]); | ||
138 | static::assertCount(1, $assignedVariables['cols'][1]); | ||
139 | static::assertCount(1, $assignedVariables['cols'][2]); | ||
140 | |||
141 | $link = $assignedVariables['cols'][0][0]; | ||
142 | |||
143 | static::assertSame(1, $link['id']); | ||
144 | static::assertSame('http://url.tld', $link['url']); | ||
145 | static::assertNotEmpty($link['title']); | ||
146 | static::assertNotEmpty($link['description']); | ||
147 | static::assertNotEmpty($link['formatedDescription']); | ||
148 | |||
149 | $link = $assignedVariables['cols'][1][0]; | ||
150 | |||
151 | static::assertSame(2, $link['id']); | ||
152 | static::assertSame('http://url2.tld', $link['url']); | ||
153 | static::assertNotEmpty($link['title']); | ||
154 | static::assertNotEmpty($link['description']); | ||
155 | static::assertNotEmpty($link['formatedDescription']); | ||
156 | |||
157 | $link = $assignedVariables['cols'][2][0]; | ||
158 | |||
159 | static::assertSame(3, $link['id']); | ||
160 | static::assertSame('http://url3.tld', $link['url']); | ||
161 | static::assertNotEmpty($link['title']); | ||
162 | static::assertNotEmpty($link['description']); | ||
163 | static::assertNotEmpty($link['formatedDescription']); | ||
164 | } | ||
165 | |||
166 | /** | ||
167 | * Daily page - test that everything goes fine with no future or past bookmarks | ||
168 | */ | ||
169 | public function testValidIndexControllerInvokeNoFutureOrPast(): void | ||
170 | { | ||
171 | $currentDay = new \DateTimeImmutable('2020-05-13'); | ||
172 | |||
173 | $request = $this->createMock(Request::class); | ||
174 | $response = new Response(); | ||
175 | |||
176 | // Save RainTPL assigned variables | ||
177 | $assignedVariables = []; | ||
178 | $this->assignTemplateVars($assignedVariables); | ||
179 | |||
180 | // Links dataset: 2 links with thumbnails | ||
181 | $this->container->bookmarkService | ||
182 | ->expects(static::once()) | ||
183 | ->method('days') | ||
184 | ->willReturnCallback(function () use ($currentDay): array { | ||
185 | return [ | ||
186 | $currentDay->format($currentDay->format('Ymd')), | ||
187 | ]; | ||
188 | }) | ||
189 | ; | ||
190 | $this->container->bookmarkService | ||
191 | ->expects(static::once()) | ||
192 | ->method('filterDay') | ||
193 | ->willReturnCallback(function (): array { | ||
194 | return [ | ||
195 | (new Bookmark()) | ||
196 | ->setId(1) | ||
197 | ->setUrl('http://url.tld') | ||
198 | ->setTitle(static::generateString(50)) | ||
199 | ->setDescription(static::generateString(500)) | ||
200 | , | ||
201 | ]; | ||
202 | }) | ||
203 | ; | ||
204 | |||
205 | // Make sure that PluginManager hook is triggered | ||
206 | $this->container->pluginManager | ||
207 | ->expects(static::atLeastOnce()) | ||
208 | ->method('executeHooks') | ||
209 | ->withConsecutive(['render_daily']) | ||
210 | ->willReturnCallback(function (string $hook, array $data, array $param) use ($currentDay): array { | ||
211 | if ('render_daily' === $hook) { | ||
212 | static::assertArrayHasKey('linksToDisplay', $data); | ||
213 | static::assertCount(1, $data['linksToDisplay']); | ||
214 | static::assertSame(1, $data['linksToDisplay'][0]['id']); | ||
215 | static::assertSame($currentDay->getTimestamp(), $data['day']); | ||
216 | static::assertEmpty($data['previousday']); | ||
217 | static::assertEmpty($data['nextday']); | ||
218 | |||
219 | static::assertArrayHasKey('loggedin', $param); | ||
220 | } | ||
221 | |||
222 | return $data; | ||
223 | }); | ||
224 | |||
225 | $result = $this->controller->index($request, $response); | ||
226 | |||
227 | static::assertSame(200, $result->getStatusCode()); | ||
228 | static::assertSame('daily', (string) $result->getBody()); | ||
229 | static::assertSame( | ||
230 | 'Daily - '. format_date($currentDay, false, true) .' - Shaarli', | ||
231 | $assignedVariables['pagetitle'] | ||
232 | ); | ||
233 | static::assertCount(1, $assignedVariables['linksToDisplay']); | ||
234 | |||
235 | $link = $assignedVariables['linksToDisplay'][0]; | ||
236 | static::assertSame(1, $link['id']); | ||
237 | } | ||
238 | |||
239 | /** | ||
240 | * Daily page - test that height adjustment in columns is working | ||
241 | */ | ||
242 | public function testValidIndexControllerInvokeHeightAdjustment(): void | ||
243 | { | ||
244 | $currentDay = new \DateTimeImmutable('2020-05-13'); | ||
245 | |||
246 | $request = $this->createMock(Request::class); | ||
247 | $response = new Response(); | ||
248 | |||
249 | // Save RainTPL assigned variables | ||
250 | $assignedVariables = []; | ||
251 | $this->assignTemplateVars($assignedVariables); | ||
252 | |||
253 | // Links dataset: 2 links with thumbnails | ||
254 | $this->container->bookmarkService | ||
255 | ->expects(static::once()) | ||
256 | ->method('days') | ||
257 | ->willReturnCallback(function () use ($currentDay): array { | ||
258 | return [ | ||
259 | $currentDay->format($currentDay->format('Ymd')), | ||
260 | ]; | ||
261 | }) | ||
262 | ; | ||
263 | $this->container->bookmarkService | ||
264 | ->expects(static::once()) | ||
265 | ->method('filterDay') | ||
266 | ->willReturnCallback(function (): array { | ||
267 | return [ | ||
268 | (new Bookmark())->setId(1)->setUrl('http://url.tld')->setTitle('title'), | ||
269 | (new Bookmark()) | ||
270 | ->setId(2) | ||
271 | ->setUrl('http://url.tld') | ||
272 | ->setTitle(static::generateString(50)) | ||
273 | ->setDescription(static::generateString(5000)) | ||
274 | , | ||
275 | (new Bookmark())->setId(3)->setUrl('http://url.tld')->setTitle('title'), | ||
276 | (new Bookmark())->setId(4)->setUrl('http://url.tld')->setTitle('title'), | ||
277 | (new Bookmark())->setId(5)->setUrl('http://url.tld')->setTitle('title'), | ||
278 | (new Bookmark())->setId(6)->setUrl('http://url.tld')->setTitle('title'), | ||
279 | (new Bookmark())->setId(7)->setUrl('http://url.tld')->setTitle('title'), | ||
280 | ]; | ||
281 | }) | ||
282 | ; | ||
283 | |||
284 | // Make sure that PluginManager hook is triggered | ||
285 | $this->container->pluginManager | ||
286 | ->expects(static::atLeastOnce()) | ||
287 | ->method('executeHooks') | ||
288 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | ||
289 | return $data; | ||
290 | }) | ||
291 | ; | ||
292 | |||
293 | $result = $this->controller->index($request, $response); | ||
294 | |||
295 | static::assertSame(200, $result->getStatusCode()); | ||
296 | static::assertSame('daily', (string) $result->getBody()); | ||
297 | static::assertCount(7, $assignedVariables['linksToDisplay']); | ||
298 | |||
299 | $columnIds = function (array $column): array { | ||
300 | return array_map(function (array $item): int { return $item['id']; }, $column); | ||
301 | }; | ||
302 | |||
303 | static::assertSame([1, 4, 6], $columnIds($assignedVariables['cols'][0])); | ||
304 | static::assertSame([2], $columnIds($assignedVariables['cols'][1])); | ||
305 | static::assertSame([3, 5, 7], $columnIds($assignedVariables['cols'][2])); | ||
306 | } | ||
307 | |||
308 | /** | ||
309 | * Daily page - no bookmark | ||
310 | */ | ||
311 | public function testValidIndexControllerInvokeNoBookmark(): void | ||
312 | { | ||
313 | $request = $this->createMock(Request::class); | ||
314 | $response = new Response(); | ||
315 | |||
316 | // Save RainTPL assigned variables | ||
317 | $assignedVariables = []; | ||
318 | $this->assignTemplateVars($assignedVariables); | ||
319 | |||
320 | // Links dataset: 2 links with thumbnails | ||
321 | $this->container->bookmarkService | ||
322 | ->expects(static::once()) | ||
323 | ->method('days') | ||
324 | ->willReturnCallback(function (): array { | ||
325 | return []; | ||
326 | }) | ||
327 | ; | ||
328 | $this->container->bookmarkService | ||
329 | ->expects(static::once()) | ||
330 | ->method('filterDay') | ||
331 | ->willReturnCallback(function (): array { | ||
332 | return []; | ||
333 | }) | ||
334 | ; | ||
335 | |||
336 | // Make sure that PluginManager hook is triggered | ||
337 | $this->container->pluginManager | ||
338 | ->expects(static::atLeastOnce()) | ||
339 | ->method('executeHooks') | ||
340 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | ||
341 | return $data; | ||
342 | }) | ||
343 | ; | ||
344 | |||
345 | $result = $this->controller->index($request, $response); | ||
346 | |||
347 | static::assertSame(200, $result->getStatusCode()); | ||
348 | static::assertSame('daily', (string) $result->getBody()); | ||
349 | static::assertCount(0, $assignedVariables['linksToDisplay']); | ||
350 | static::assertSame('Today', $assignedVariables['dayDesc']); | ||
351 | static::assertEquals((new \DateTime())->setTime(0, 0)->getTimestamp(), $assignedVariables['day']); | ||
352 | static::assertEquals((new \DateTime())->setTime(0, 0), $assignedVariables['dayDate']); | ||
353 | } | ||
354 | |||
355 | /** | ||
356 | * Daily RSS - default behaviour | ||
357 | */ | ||
358 | public function testValidRssControllerInvokeDefault(): void | ||
359 | { | ||
360 | $dates = [ | ||
361 | new \DateTimeImmutable('2020-05-17'), | ||
362 | new \DateTimeImmutable('2020-05-15'), | ||
363 | new \DateTimeImmutable('2020-05-13'), | ||
364 | ]; | ||
365 | |||
366 | $request = $this->createMock(Request::class); | ||
367 | $response = new Response(); | ||
368 | |||
369 | $this->container->bookmarkService->expects(static::once())->method('search')->willReturn([ | ||
370 | (new Bookmark())->setId(1)->setCreated($dates[0])->setUrl('http://domain.tld/1'), | ||
371 | (new Bookmark())->setId(2)->setCreated($dates[1])->setUrl('http://domain.tld/2'), | ||
372 | (new Bookmark())->setId(3)->setCreated($dates[1])->setUrl('http://domain.tld/3'), | ||
373 | (new Bookmark())->setId(4)->setCreated($dates[2])->setUrl('http://domain.tld/4'), | ||
374 | ]); | ||
375 | |||
376 | $this->container->pageCacheManager | ||
377 | ->expects(static::once()) | ||
378 | ->method('getCachePage') | ||
379 | ->willReturnCallback(function (): CachedPage { | ||
380 | $cachedPage = $this->createMock(CachedPage::class); | ||
381 | $cachedPage->expects(static::once())->method('cache')->with('dailyrss'); | ||
382 | |||
383 | return $cachedPage; | ||
384 | } | ||
385 | ); | ||
386 | |||
387 | // Save RainTPL assigned variables | ||
388 | $assignedVariables = []; | ||
389 | $this->assignTemplateVars($assignedVariables); | ||
390 | |||
391 | $result = $this->controller->rss($request, $response); | ||
392 | |||
393 | static::assertSame(200, $result->getStatusCode()); | ||
394 | static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); | ||
395 | static::assertSame('dailyrss', (string) $result->getBody()); | ||
396 | static::assertSame('Shaarli', $assignedVariables['title']); | ||
397 | static::assertSame('http://shaarli/subfolder/', $assignedVariables['index_url']); | ||
398 | static::assertSame('http://shaarli/subfolder/daily-rss', $assignedVariables['page_url']); | ||
399 | static::assertFalse($assignedVariables['hide_timestamps']); | ||
400 | static::assertCount(2, $assignedVariables['days']); | ||
401 | |||
402 | $day = $assignedVariables['days'][$dates[0]->format('Ymd')]; | ||
403 | |||
404 | static::assertEquals($dates[0], $day['date']); | ||
405 | static::assertSame($dates[0]->format(\DateTime::RSS), $day['date_rss']); | ||
406 | static::assertSame(format_date($dates[0], false), $day['date_human']); | ||
407 | static::assertSame('http://shaarli/subfolder/daily?day='. $dates[0]->format('Ymd'), $day['absolute_url']); | ||
408 | static::assertCount(1, $day['links']); | ||
409 | static::assertSame(1, $day['links'][0]['id']); | ||
410 | static::assertSame('http://domain.tld/1', $day['links'][0]['url']); | ||
411 | static::assertEquals($dates[0], $day['links'][0]['created']); | ||
412 | |||
413 | $day = $assignedVariables['days'][$dates[1]->format('Ymd')]; | ||
414 | |||
415 | static::assertEquals($dates[1], $day['date']); | ||
416 | static::assertSame($dates[1]->format(\DateTime::RSS), $day['date_rss']); | ||
417 | static::assertSame(format_date($dates[1], false), $day['date_human']); | ||
418 | static::assertSame('http://shaarli/subfolder/daily?day='. $dates[1]->format('Ymd'), $day['absolute_url']); | ||
419 | static::assertCount(2, $day['links']); | ||
420 | |||
421 | static::assertSame(2, $day['links'][0]['id']); | ||
422 | static::assertSame('http://domain.tld/2', $day['links'][0]['url']); | ||
423 | static::assertEquals($dates[1], $day['links'][0]['created']); | ||
424 | static::assertSame(3, $day['links'][1]['id']); | ||
425 | static::assertSame('http://domain.tld/3', $day['links'][1]['url']); | ||
426 | static::assertEquals($dates[1], $day['links'][1]['created']); | ||
427 | } | ||
428 | |||
429 | /** | ||
430 | * Daily RSS - trigger cache rendering | ||
431 | */ | ||
432 | public function testValidRssControllerInvokeTriggerCache(): void | ||
433 | { | ||
434 | $request = $this->createMock(Request::class); | ||
435 | $response = new Response(); | ||
436 | |||
437 | $this->container->pageCacheManager->method('getCachePage')->willReturnCallback(function (): CachedPage { | ||
438 | $cachedPage = $this->createMock(CachedPage::class); | ||
439 | $cachedPage->method('cachedVersion')->willReturn('this is cache!'); | ||
440 | |||
441 | return $cachedPage; | ||
442 | }); | ||
443 | |||
444 | $this->container->bookmarkService->expects(static::never())->method('search'); | ||
445 | |||
446 | $result = $this->controller->rss($request, $response); | ||
447 | |||
448 | static::assertSame(200, $result->getStatusCode()); | ||
449 | static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); | ||
450 | static::assertSame('this is cache!', (string) $result->getBody()); | ||
451 | } | ||
452 | |||
453 | /** | ||
454 | * Daily RSS - No bookmark | ||
455 | */ | ||
456 | public function testValidRssControllerInvokeNoBookmark(): void | ||
457 | { | ||
458 | $request = $this->createMock(Request::class); | ||
459 | $response = new Response(); | ||
460 | |||
461 | $this->container->bookmarkService->expects(static::once())->method('search')->willReturn([]); | ||
462 | |||
463 | // Save RainTPL assigned variables | ||
464 | $assignedVariables = []; | ||
465 | $this->assignTemplateVars($assignedVariables); | ||
466 | |||
467 | $result = $this->controller->rss($request, $response); | ||
468 | |||
469 | static::assertSame(200, $result->getStatusCode()); | ||
470 | static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); | ||
471 | static::assertSame('dailyrss', (string) $result->getBody()); | ||
472 | static::assertSame('Shaarli', $assignedVariables['title']); | ||
473 | static::assertSame('http://shaarli/subfolder/', $assignedVariables['index_url']); | ||
474 | static::assertSame('http://shaarli/subfolder/daily-rss', $assignedVariables['page_url']); | ||
475 | static::assertFalse($assignedVariables['hide_timestamps']); | ||
476 | static::assertCount(0, $assignedVariables['days']); | ||
477 | } | ||
478 | } | ||