diff options
Diffstat (limited to 'tests/front/controller/visitor/DailyControllerTest.php')
-rw-r--r-- | tests/front/controller/visitor/DailyControllerTest.php | 716 |
1 files changed, 716 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..758e7219 --- /dev/null +++ b/tests/front/controller/visitor/DailyControllerTest.php | |||
@@ -0,0 +1,716 @@ | |||
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 | $previousDate = new \DateTime('2 days ago 00:00:00'); | ||
32 | $nextDate = new \DateTime('today 00:00:00'); | ||
33 | |||
34 | $request = $this->createMock(Request::class); | ||
35 | $request->method('getQueryParam')->willReturnCallback(function (string $key) use ($currentDay): ?string { | ||
36 | return $key === 'day' ? $currentDay->format('Ymd') : null; | ||
37 | }); | ||
38 | $response = new Response(); | ||
39 | |||
40 | // Save RainTPL assigned variables | ||
41 | $assignedVariables = []; | ||
42 | $this->assignTemplateVars($assignedVariables); | ||
43 | |||
44 | $this->container->bookmarkService | ||
45 | ->expects(static::once()) | ||
46 | ->method('findByDate') | ||
47 | ->willReturnCallback( | ||
48 | function ($from, $to, &$previous, &$next) use ($currentDay, $previousDate, $nextDate): array { | ||
49 | $previous = $previousDate; | ||
50 | $next = $nextDate; | ||
51 | |||
52 | return [ | ||
53 | (new Bookmark()) | ||
54 | ->setId(1) | ||
55 | ->setUrl('http://url.tld') | ||
56 | ->setTitle(static::generateString(50)) | ||
57 | ->setDescription(static::generateString(500)) | ||
58 | , | ||
59 | (new Bookmark()) | ||
60 | ->setId(2) | ||
61 | ->setUrl('http://url2.tld') | ||
62 | ->setTitle(static::generateString(50)) | ||
63 | ->setDescription(static::generateString(500)) | ||
64 | , | ||
65 | (new Bookmark()) | ||
66 | ->setId(3) | ||
67 | ->setUrl('http://url3.tld') | ||
68 | ->setTitle(static::generateString(50)) | ||
69 | ->setDescription(static::generateString(500)) | ||
70 | , | ||
71 | ]; | ||
72 | } | ||
73 | ) | ||
74 | ; | ||
75 | |||
76 | // Make sure that PluginManager hook is triggered | ||
77 | $this->container->pluginManager | ||
78 | ->expects(static::atLeastOnce()) | ||
79 | ->method('executeHooks') | ||
80 | ->withConsecutive(['render_daily']) | ||
81 | ->willReturnCallback( | ||
82 | function (string $hook, array $data, array $param) use ($currentDay, $previousDate, $nextDate): array { | ||
83 | if ('render_daily' === $hook) { | ||
84 | static::assertArrayHasKey('linksToDisplay', $data); | ||
85 | static::assertCount(3, $data['linksToDisplay']); | ||
86 | static::assertSame(1, $data['linksToDisplay'][0]['id']); | ||
87 | static::assertSame($currentDay->getTimestamp(), $data['day']); | ||
88 | static::assertSame($previousDate->format('Ymd'), $data['previousday']); | ||
89 | static::assertSame($nextDate->format('Ymd'), $data['nextday']); | ||
90 | |||
91 | static::assertArrayHasKey('loggedin', $param); | ||
92 | } | ||
93 | |||
94 | return $data; | ||
95 | } | ||
96 | ) | ||
97 | ; | ||
98 | |||
99 | $result = $this->controller->index($request, $response); | ||
100 | |||
101 | static::assertSame(200, $result->getStatusCode()); | ||
102 | static::assertSame('daily', (string) $result->getBody()); | ||
103 | static::assertSame( | ||
104 | 'Daily - '. format_date($currentDay, false, true) .' - Shaarli', | ||
105 | $assignedVariables['pagetitle'] | ||
106 | ); | ||
107 | static::assertEquals($currentDay, $assignedVariables['dayDate']); | ||
108 | static::assertEquals($currentDay->getTimestamp(), $assignedVariables['day']); | ||
109 | static::assertSame($previousDate->format('Ymd'), $assignedVariables['previousday']); | ||
110 | static::assertSame($nextDate->format('Ymd'), $assignedVariables['nextday']); | ||
111 | static::assertSame('day', $assignedVariables['type']); | ||
112 | static::assertSame('May 13, 2020', $assignedVariables['dayDesc']); | ||
113 | static::assertSame('Daily', $assignedVariables['localizedType']); | ||
114 | static::assertCount(3, $assignedVariables['linksToDisplay']); | ||
115 | |||
116 | $link = $assignedVariables['linksToDisplay'][0]; | ||
117 | |||
118 | static::assertSame(1, $link['id']); | ||
119 | static::assertSame('http://url.tld', $link['url']); | ||
120 | static::assertNotEmpty($link['title']); | ||
121 | static::assertNotEmpty($link['description']); | ||
122 | static::assertNotEmpty($link['formatedDescription']); | ||
123 | |||
124 | $link = $assignedVariables['linksToDisplay'][1]; | ||
125 | |||
126 | static::assertSame(2, $link['id']); | ||
127 | static::assertSame('http://url2.tld', $link['url']); | ||
128 | static::assertNotEmpty($link['title']); | ||
129 | static::assertNotEmpty($link['description']); | ||
130 | static::assertNotEmpty($link['formatedDescription']); | ||
131 | |||
132 | $link = $assignedVariables['linksToDisplay'][2]; | ||
133 | |||
134 | static::assertSame(3, $link['id']); | ||
135 | static::assertSame('http://url3.tld', $link['url']); | ||
136 | static::assertNotEmpty($link['title']); | ||
137 | static::assertNotEmpty($link['description']); | ||
138 | static::assertNotEmpty($link['formatedDescription']); | ||
139 | |||
140 | static::assertCount(3, $assignedVariables['cols']); | ||
141 | static::assertCount(1, $assignedVariables['cols'][0]); | ||
142 | static::assertCount(1, $assignedVariables['cols'][1]); | ||
143 | static::assertCount(1, $assignedVariables['cols'][2]); | ||
144 | |||
145 | $link = $assignedVariables['cols'][0][0]; | ||
146 | |||
147 | static::assertSame(1, $link['id']); | ||
148 | static::assertSame('http://url.tld', $link['url']); | ||
149 | static::assertNotEmpty($link['title']); | ||
150 | static::assertNotEmpty($link['description']); | ||
151 | static::assertNotEmpty($link['formatedDescription']); | ||
152 | |||
153 | $link = $assignedVariables['cols'][1][0]; | ||
154 | |||
155 | static::assertSame(2, $link['id']); | ||
156 | static::assertSame('http://url2.tld', $link['url']); | ||
157 | static::assertNotEmpty($link['title']); | ||
158 | static::assertNotEmpty($link['description']); | ||
159 | static::assertNotEmpty($link['formatedDescription']); | ||
160 | |||
161 | $link = $assignedVariables['cols'][2][0]; | ||
162 | |||
163 | static::assertSame(3, $link['id']); | ||
164 | static::assertSame('http://url3.tld', $link['url']); | ||
165 | static::assertNotEmpty($link['title']); | ||
166 | static::assertNotEmpty($link['description']); | ||
167 | static::assertNotEmpty($link['formatedDescription']); | ||
168 | } | ||
169 | |||
170 | /** | ||
171 | * Daily page - test that everything goes fine with no future or past bookmarks | ||
172 | */ | ||
173 | public function testValidIndexControllerInvokeNoFutureOrPast(): void | ||
174 | { | ||
175 | $currentDay = new \DateTimeImmutable('2020-05-13'); | ||
176 | |||
177 | $request = $this->createMock(Request::class); | ||
178 | $request->method('getQueryParam')->willReturnCallback(function (string $key) use ($currentDay): ?string { | ||
179 | return $key === 'day' ? $currentDay->format('Ymd') : null; | ||
180 | }); | ||
181 | $response = new Response(); | ||
182 | |||
183 | // Save RainTPL assigned variables | ||
184 | $assignedVariables = []; | ||
185 | $this->assignTemplateVars($assignedVariables); | ||
186 | |||
187 | $this->container->bookmarkService | ||
188 | ->expects(static::once()) | ||
189 | ->method('findByDate') | ||
190 | ->willReturnCallback(function () use ($currentDay): array { | ||
191 | return [ | ||
192 | (new Bookmark()) | ||
193 | ->setId(1) | ||
194 | ->setUrl('http://url.tld') | ||
195 | ->setTitle(static::generateString(50)) | ||
196 | ->setDescription(static::generateString(500)) | ||
197 | , | ||
198 | ]; | ||
199 | }) | ||
200 | ; | ||
201 | |||
202 | // Make sure that PluginManager hook is triggered | ||
203 | $this->container->pluginManager | ||
204 | ->expects(static::atLeastOnce()) | ||
205 | ->method('executeHooks') | ||
206 | ->withConsecutive(['render_daily']) | ||
207 | ->willReturnCallback(function (string $hook, array $data, array $param) use ($currentDay): array { | ||
208 | if ('render_daily' === $hook) { | ||
209 | static::assertArrayHasKey('linksToDisplay', $data); | ||
210 | static::assertCount(1, $data['linksToDisplay']); | ||
211 | static::assertSame(1, $data['linksToDisplay'][0]['id']); | ||
212 | static::assertSame($currentDay->getTimestamp(), $data['day']); | ||
213 | static::assertEmpty($data['previousday']); | ||
214 | static::assertEmpty($data['nextday']); | ||
215 | |||
216 | static::assertArrayHasKey('loggedin', $param); | ||
217 | } | ||
218 | |||
219 | return $data; | ||
220 | }); | ||
221 | |||
222 | $result = $this->controller->index($request, $response); | ||
223 | |||
224 | static::assertSame(200, $result->getStatusCode()); | ||
225 | static::assertSame('daily', (string) $result->getBody()); | ||
226 | static::assertSame( | ||
227 | 'Daily - '. format_date($currentDay, false, true) .' - Shaarli', | ||
228 | $assignedVariables['pagetitle'] | ||
229 | ); | ||
230 | static::assertCount(1, $assignedVariables['linksToDisplay']); | ||
231 | |||
232 | $link = $assignedVariables['linksToDisplay'][0]; | ||
233 | static::assertSame(1, $link['id']); | ||
234 | } | ||
235 | |||
236 | /** | ||
237 | * Daily page - test that height adjustment in columns is working | ||
238 | */ | ||
239 | public function testValidIndexControllerInvokeHeightAdjustment(): void | ||
240 | { | ||
241 | $currentDay = new \DateTimeImmutable('2020-05-13'); | ||
242 | |||
243 | $request = $this->createMock(Request::class); | ||
244 | $response = new Response(); | ||
245 | |||
246 | // Save RainTPL assigned variables | ||
247 | $assignedVariables = []; | ||
248 | $this->assignTemplateVars($assignedVariables); | ||
249 | |||
250 | $this->container->bookmarkService | ||
251 | ->expects(static::once()) | ||
252 | ->method('findByDate') | ||
253 | ->willReturnCallback(function () use ($currentDay): array { | ||
254 | return [ | ||
255 | (new Bookmark())->setId(1)->setUrl('http://url.tld')->setTitle('title'), | ||
256 | (new Bookmark()) | ||
257 | ->setId(2) | ||
258 | ->setUrl('http://url.tld') | ||
259 | ->setTitle(static::generateString(50)) | ||
260 | ->setDescription(static::generateString(5000)) | ||
261 | , | ||
262 | (new Bookmark())->setId(3)->setUrl('http://url.tld')->setTitle('title'), | ||
263 | (new Bookmark())->setId(4)->setUrl('http://url.tld')->setTitle('title'), | ||
264 | (new Bookmark())->setId(5)->setUrl('http://url.tld')->setTitle('title'), | ||
265 | (new Bookmark())->setId(6)->setUrl('http://url.tld')->setTitle('title'), | ||
266 | (new Bookmark())->setId(7)->setUrl('http://url.tld')->setTitle('title'), | ||
267 | ]; | ||
268 | }) | ||
269 | ; | ||
270 | |||
271 | // Make sure that PluginManager hook is triggered | ||
272 | $this->container->pluginManager | ||
273 | ->expects(static::atLeastOnce()) | ||
274 | ->method('executeHooks') | ||
275 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | ||
276 | return $data; | ||
277 | }) | ||
278 | ; | ||
279 | |||
280 | $result = $this->controller->index($request, $response); | ||
281 | |||
282 | static::assertSame(200, $result->getStatusCode()); | ||
283 | static::assertSame('daily', (string) $result->getBody()); | ||
284 | static::assertCount(7, $assignedVariables['linksToDisplay']); | ||
285 | |||
286 | $columnIds = function (array $column): array { | ||
287 | return array_map(function (array $item): int { return $item['id']; }, $column); | ||
288 | }; | ||
289 | |||
290 | static::assertSame([1, 4, 6], $columnIds($assignedVariables['cols'][0])); | ||
291 | static::assertSame([2], $columnIds($assignedVariables['cols'][1])); | ||
292 | static::assertSame([3, 5, 7], $columnIds($assignedVariables['cols'][2])); | ||
293 | } | ||
294 | |||
295 | /** | ||
296 | * Daily page - no bookmark | ||
297 | */ | ||
298 | public function testValidIndexControllerInvokeNoBookmark(): void | ||
299 | { | ||
300 | $request = $this->createMock(Request::class); | ||
301 | $response = new Response(); | ||
302 | |||
303 | // Save RainTPL assigned variables | ||
304 | $assignedVariables = []; | ||
305 | $this->assignTemplateVars($assignedVariables); | ||
306 | |||
307 | // Links dataset: 2 links with thumbnails | ||
308 | $this->container->bookmarkService | ||
309 | ->expects(static::once()) | ||
310 | ->method('findByDate') | ||
311 | ->willReturnCallback(function (): array { | ||
312 | return []; | ||
313 | }) | ||
314 | ; | ||
315 | |||
316 | // Make sure that PluginManager hook is triggered | ||
317 | $this->container->pluginManager | ||
318 | ->expects(static::atLeastOnce()) | ||
319 | ->method('executeHooks') | ||
320 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | ||
321 | return $data; | ||
322 | }) | ||
323 | ; | ||
324 | |||
325 | $result = $this->controller->index($request, $response); | ||
326 | |||
327 | static::assertSame(200, $result->getStatusCode()); | ||
328 | static::assertSame('daily', (string) $result->getBody()); | ||
329 | static::assertCount(0, $assignedVariables['linksToDisplay']); | ||
330 | static::assertSame('Today - ' . (new \DateTime())->format('F d, Y'), $assignedVariables['dayDesc']); | ||
331 | static::assertEquals((new \DateTime())->setTime(0, 0)->getTimestamp(), $assignedVariables['day']); | ||
332 | static::assertEquals((new \DateTime())->setTime(0, 0), $assignedVariables['dayDate']); | ||
333 | } | ||
334 | |||
335 | /** | ||
336 | * Daily RSS - default behaviour | ||
337 | */ | ||
338 | public function testValidRssControllerInvokeDefault(): void | ||
339 | { | ||
340 | $dates = [ | ||
341 | new \DateTimeImmutable('2020-05-17'), | ||
342 | new \DateTimeImmutable('2020-05-15'), | ||
343 | new \DateTimeImmutable('2020-05-13'), | ||
344 | new \DateTimeImmutable('+1 month'), | ||
345 | ]; | ||
346 | |||
347 | $request = $this->createMock(Request::class); | ||
348 | $response = new Response(); | ||
349 | |||
350 | $this->container->bookmarkService->expects(static::once())->method('search')->willReturn([ | ||
351 | (new Bookmark())->setId(1)->setCreated($dates[0])->setUrl('http://domain.tld/1'), | ||
352 | (new Bookmark())->setId(2)->setCreated($dates[1])->setUrl('http://domain.tld/2'), | ||
353 | (new Bookmark())->setId(3)->setCreated($dates[1])->setUrl('http://domain.tld/3'), | ||
354 | (new Bookmark())->setId(4)->setCreated($dates[2])->setUrl('http://domain.tld/4'), | ||
355 | (new Bookmark())->setId(5)->setCreated($dates[3])->setUrl('http://domain.tld/5'), | ||
356 | ]); | ||
357 | |||
358 | $this->container->pageCacheManager | ||
359 | ->expects(static::once()) | ||
360 | ->method('getCachePage') | ||
361 | ->willReturnCallback(function (): CachedPage { | ||
362 | $cachedPage = $this->createMock(CachedPage::class); | ||
363 | $cachedPage->expects(static::once())->method('cache')->with('dailyrss'); | ||
364 | |||
365 | return $cachedPage; | ||
366 | } | ||
367 | ); | ||
368 | |||
369 | // Save RainTPL assigned variables | ||
370 | $assignedVariables = []; | ||
371 | $this->assignTemplateVars($assignedVariables); | ||
372 | |||
373 | $result = $this->controller->rss($request, $response); | ||
374 | |||
375 | static::assertSame(200, $result->getStatusCode()); | ||
376 | static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); | ||
377 | static::assertSame('dailyrss', (string) $result->getBody()); | ||
378 | static::assertSame('Shaarli', $assignedVariables['title']); | ||
379 | static::assertSame('http://shaarli/subfolder/', $assignedVariables['index_url']); | ||
380 | static::assertSame('http://shaarli/subfolder/daily-rss', $assignedVariables['page_url']); | ||
381 | static::assertFalse($assignedVariables['hide_timestamps']); | ||
382 | static::assertCount(3, $assignedVariables['days']); | ||
383 | |||
384 | $day = $assignedVariables['days'][$dates[0]->format('Ymd')]; | ||
385 | $date = $dates[0]->setTime(23, 59, 59); | ||
386 | |||
387 | static::assertEquals($date, $day['date']); | ||
388 | static::assertSame($date->format(\DateTime::RSS), $day['date_rss']); | ||
389 | static::assertSame(format_date($date, false), $day['date_human']); | ||
390 | static::assertSame('http://shaarli/subfolder/daily?day='. $dates[0]->format('Ymd'), $day['absolute_url']); | ||
391 | static::assertCount(1, $day['links']); | ||
392 | static::assertSame(1, $day['links'][0]['id']); | ||
393 | static::assertSame('http://domain.tld/1', $day['links'][0]['url']); | ||
394 | static::assertEquals($dates[0], $day['links'][0]['created']); | ||
395 | |||
396 | $day = $assignedVariables['days'][$dates[1]->format('Ymd')]; | ||
397 | $date = $dates[1]->setTime(23, 59, 59); | ||
398 | |||
399 | static::assertEquals($date, $day['date']); | ||
400 | static::assertSame($date->format(\DateTime::RSS), $day['date_rss']); | ||
401 | static::assertSame(format_date($date, false), $day['date_human']); | ||
402 | static::assertSame('http://shaarli/subfolder/daily?day='. $dates[1]->format('Ymd'), $day['absolute_url']); | ||
403 | static::assertCount(2, $day['links']); | ||
404 | |||
405 | static::assertSame(2, $day['links'][0]['id']); | ||
406 | static::assertSame('http://domain.tld/2', $day['links'][0]['url']); | ||
407 | static::assertEquals($dates[1], $day['links'][0]['created']); | ||
408 | static::assertSame(3, $day['links'][1]['id']); | ||
409 | static::assertSame('http://domain.tld/3', $day['links'][1]['url']); | ||
410 | static::assertEquals($dates[1], $day['links'][1]['created']); | ||
411 | |||
412 | $day = $assignedVariables['days'][$dates[2]->format('Ymd')]; | ||
413 | $date = $dates[2]->setTime(23, 59, 59); | ||
414 | |||
415 | static::assertEquals($date, $day['date']); | ||
416 | static::assertSame($date->format(\DateTime::RSS), $day['date_rss']); | ||
417 | static::assertSame(format_date($date, false), $day['date_human']); | ||
418 | static::assertSame('http://shaarli/subfolder/daily?day='. $dates[2]->format('Ymd'), $day['absolute_url']); | ||
419 | static::assertCount(1, $day['links']); | ||
420 | static::assertSame(4, $day['links'][0]['id']); | ||
421 | static::assertSame('http://domain.tld/4', $day['links'][0]['url']); | ||
422 | static::assertEquals($dates[2], $day['links'][0]['created']); | ||
423 | } | ||
424 | |||
425 | /** | ||
426 | * Daily RSS - trigger cache rendering | ||
427 | */ | ||
428 | public function testValidRssControllerInvokeTriggerCache(): void | ||
429 | { | ||
430 | $request = $this->createMock(Request::class); | ||
431 | $response = new Response(); | ||
432 | |||
433 | $this->container->pageCacheManager->method('getCachePage')->willReturnCallback(function (): CachedPage { | ||
434 | $cachedPage = $this->createMock(CachedPage::class); | ||
435 | $cachedPage->method('cachedVersion')->willReturn('this is cache!'); | ||
436 | |||
437 | return $cachedPage; | ||
438 | }); | ||
439 | |||
440 | $this->container->bookmarkService->expects(static::never())->method('search'); | ||
441 | |||
442 | $result = $this->controller->rss($request, $response); | ||
443 | |||
444 | static::assertSame(200, $result->getStatusCode()); | ||
445 | static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); | ||
446 | static::assertSame('this is cache!', (string) $result->getBody()); | ||
447 | } | ||
448 | |||
449 | /** | ||
450 | * Daily RSS - No bookmark | ||
451 | */ | ||
452 | public function testValidRssControllerInvokeNoBookmark(): void | ||
453 | { | ||
454 | $request = $this->createMock(Request::class); | ||
455 | $response = new Response(); | ||
456 | |||
457 | $this->container->bookmarkService->expects(static::once())->method('search')->willReturn([]); | ||
458 | |||
459 | // Save RainTPL assigned variables | ||
460 | $assignedVariables = []; | ||
461 | $this->assignTemplateVars($assignedVariables); | ||
462 | |||
463 | $result = $this->controller->rss($request, $response); | ||
464 | |||
465 | static::assertSame(200, $result->getStatusCode()); | ||
466 | static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); | ||
467 | static::assertSame('dailyrss', (string) $result->getBody()); | ||
468 | static::assertSame('Shaarli', $assignedVariables['title']); | ||
469 | static::assertSame('http://shaarli/subfolder/', $assignedVariables['index_url']); | ||
470 | static::assertSame('http://shaarli/subfolder/daily-rss', $assignedVariables['page_url']); | ||
471 | static::assertFalse($assignedVariables['hide_timestamps']); | ||
472 | static::assertCount(0, $assignedVariables['days']); | ||
473 | } | ||
474 | |||
475 | /** | ||
476 | * Test simple display index with week parameter | ||
477 | */ | ||
478 | public function testSimpleIndexWeekly(): void | ||
479 | { | ||
480 | $currentDay = new \DateTimeImmutable('2020-05-13'); | ||
481 | $expectedDay = new \DateTimeImmutable('2020-05-11'); | ||
482 | |||
483 | $request = $this->createMock(Request::class); | ||
484 | $request->method('getQueryParam')->willReturnCallback(function (string $key) use ($currentDay): ?string { | ||
485 | return $key === 'week' ? $currentDay->format('YW') : null; | ||
486 | }); | ||
487 | $response = new Response(); | ||
488 | |||
489 | // Save RainTPL assigned variables | ||
490 | $assignedVariables = []; | ||
491 | $this->assignTemplateVars($assignedVariables); | ||
492 | |||
493 | $this->container->bookmarkService | ||
494 | ->expects(static::once()) | ||
495 | ->method('findByDate') | ||
496 | ->willReturnCallback( | ||
497 | function (): array { | ||
498 | return [ | ||
499 | (new Bookmark()) | ||
500 | ->setId(1) | ||
501 | ->setUrl('http://url.tld') | ||
502 | ->setTitle(static::generateString(50)) | ||
503 | ->setDescription(static::generateString(500)) | ||
504 | , | ||
505 | (new Bookmark()) | ||
506 | ->setId(2) | ||
507 | ->setUrl('http://url2.tld') | ||
508 | ->setTitle(static::generateString(50)) | ||
509 | ->setDescription(static::generateString(500)) | ||
510 | , | ||
511 | ]; | ||
512 | } | ||
513 | ) | ||
514 | ; | ||
515 | |||
516 | $result = $this->controller->index($request, $response); | ||
517 | |||
518 | static::assertSame(200, $result->getStatusCode()); | ||
519 | static::assertSame('daily', (string) $result->getBody()); | ||
520 | static::assertSame( | ||
521 | 'Weekly - Week 20 (May 11, 2020) - Shaarli', | ||
522 | $assignedVariables['pagetitle'] | ||
523 | ); | ||
524 | |||
525 | static::assertCount(2, $assignedVariables['linksToDisplay']); | ||
526 | static::assertEquals($expectedDay->setTime(0, 0), $assignedVariables['dayDate']); | ||
527 | static::assertSame($expectedDay->setTime(0, 0)->getTimestamp(), $assignedVariables['day']); | ||
528 | static::assertSame('', $assignedVariables['previousday']); | ||
529 | static::assertSame('', $assignedVariables['nextday']); | ||
530 | static::assertSame('Week 20 (May 11, 2020)', $assignedVariables['dayDesc']); | ||
531 | static::assertSame('week', $assignedVariables['type']); | ||
532 | static::assertSame('Weekly', $assignedVariables['localizedType']); | ||
533 | } | ||
534 | |||
535 | /** | ||
536 | * Test simple display index with month parameter | ||
537 | */ | ||
538 | public function testSimpleIndexMonthly(): void | ||
539 | { | ||
540 | $currentDay = new \DateTimeImmutable('2020-05-13'); | ||
541 | $expectedDay = new \DateTimeImmutable('2020-05-01'); | ||
542 | |||
543 | $request = $this->createMock(Request::class); | ||
544 | $request->method('getQueryParam')->willReturnCallback(function (string $key) use ($currentDay): ?string { | ||
545 | return $key === 'month' ? $currentDay->format('Ym') : null; | ||
546 | }); | ||
547 | $response = new Response(); | ||
548 | |||
549 | // Save RainTPL assigned variables | ||
550 | $assignedVariables = []; | ||
551 | $this->assignTemplateVars($assignedVariables); | ||
552 | |||
553 | $this->container->bookmarkService | ||
554 | ->expects(static::once()) | ||
555 | ->method('findByDate') | ||
556 | ->willReturnCallback( | ||
557 | function (): array { | ||
558 | return [ | ||
559 | (new Bookmark()) | ||
560 | ->setId(1) | ||
561 | ->setUrl('http://url.tld') | ||
562 | ->setTitle(static::generateString(50)) | ||
563 | ->setDescription(static::generateString(500)) | ||
564 | , | ||
565 | (new Bookmark()) | ||
566 | ->setId(2) | ||
567 | ->setUrl('http://url2.tld') | ||
568 | ->setTitle(static::generateString(50)) | ||
569 | ->setDescription(static::generateString(500)) | ||
570 | , | ||
571 | ]; | ||
572 | } | ||
573 | ) | ||
574 | ; | ||
575 | |||
576 | $result = $this->controller->index($request, $response); | ||
577 | |||
578 | static::assertSame(200, $result->getStatusCode()); | ||
579 | static::assertSame('daily', (string) $result->getBody()); | ||
580 | static::assertSame( | ||
581 | 'Monthly - May, 2020 - Shaarli', | ||
582 | $assignedVariables['pagetitle'] | ||
583 | ); | ||
584 | |||
585 | static::assertCount(2, $assignedVariables['linksToDisplay']); | ||
586 | static::assertEquals($expectedDay->setTime(0, 0), $assignedVariables['dayDate']); | ||
587 | static::assertSame($expectedDay->setTime(0, 0)->getTimestamp(), $assignedVariables['day']); | ||
588 | static::assertSame('', $assignedVariables['previousday']); | ||
589 | static::assertSame('', $assignedVariables['nextday']); | ||
590 | static::assertSame('May, 2020', $assignedVariables['dayDesc']); | ||
591 | static::assertSame('month', $assignedVariables['type']); | ||
592 | static::assertSame('Monthly', $assignedVariables['localizedType']); | ||
593 | } | ||
594 | |||
595 | /** | ||
596 | * Test simple display RSS with week parameter | ||
597 | */ | ||
598 | public function testSimpleRssWeekly(): void | ||
599 | { | ||
600 | $dates = [ | ||
601 | new \DateTimeImmutable('2020-05-19'), | ||
602 | new \DateTimeImmutable('2020-05-13'), | ||
603 | ]; | ||
604 | $expectedDates = [ | ||
605 | new \DateTimeImmutable('2020-05-24 23:59:59'), | ||
606 | new \DateTimeImmutable('2020-05-17 23:59:59'), | ||
607 | ]; | ||
608 | |||
609 | $this->container->environment['QUERY_STRING'] = 'week'; | ||
610 | $request = $this->createMock(Request::class); | ||
611 | $request->method('getQueryParam')->willReturnCallback(function (string $key): ?string { | ||
612 | return $key === 'week' ? '' : null; | ||
613 | }); | ||
614 | $response = new Response(); | ||
615 | |||
616 | $this->container->bookmarkService->expects(static::once())->method('search')->willReturn([ | ||
617 | (new Bookmark())->setId(1)->setCreated($dates[0])->setUrl('http://domain.tld/1'), | ||
618 | (new Bookmark())->setId(2)->setCreated($dates[1])->setUrl('http://domain.tld/2'), | ||
619 | (new Bookmark())->setId(3)->setCreated($dates[1])->setUrl('http://domain.tld/3'), | ||
620 | ]); | ||
621 | |||
622 | // Save RainTPL assigned variables | ||
623 | $assignedVariables = []; | ||
624 | $this->assignTemplateVars($assignedVariables); | ||
625 | |||
626 | $result = $this->controller->rss($request, $response); | ||
627 | |||
628 | static::assertSame(200, $result->getStatusCode()); | ||
629 | static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); | ||
630 | static::assertSame('dailyrss', (string) $result->getBody()); | ||
631 | static::assertSame('Shaarli', $assignedVariables['title']); | ||
632 | static::assertSame('http://shaarli/subfolder/', $assignedVariables['index_url']); | ||
633 | static::assertSame('http://shaarli/subfolder/daily-rss?week', $assignedVariables['page_url']); | ||
634 | static::assertFalse($assignedVariables['hide_timestamps']); | ||
635 | static::assertCount(2, $assignedVariables['days']); | ||
636 | |||
637 | $day = $assignedVariables['days'][$dates[0]->format('YW')]; | ||
638 | $date = $expectedDates[0]; | ||
639 | |||
640 | static::assertEquals($date, $day['date']); | ||
641 | static::assertSame($date->format(\DateTime::RSS), $day['date_rss']); | ||
642 | static::assertSame('Week 21 (May 18, 2020)', $day['date_human']); | ||
643 | static::assertSame('http://shaarli/subfolder/daily?week='. $dates[0]->format('YW'), $day['absolute_url']); | ||
644 | static::assertCount(1, $day['links']); | ||
645 | |||
646 | $day = $assignedVariables['days'][$dates[1]->format('YW')]; | ||
647 | $date = $expectedDates[1]; | ||
648 | |||
649 | static::assertEquals($date, $day['date']); | ||
650 | static::assertSame($date->format(\DateTime::RSS), $day['date_rss']); | ||
651 | static::assertSame('Week 20 (May 11, 2020)', $day['date_human']); | ||
652 | static::assertSame('http://shaarli/subfolder/daily?week='. $dates[1]->format('YW'), $day['absolute_url']); | ||
653 | static::assertCount(2, $day['links']); | ||
654 | } | ||
655 | |||
656 | /** | ||
657 | * Test simple display RSS with month parameter | ||
658 | */ | ||
659 | public function testSimpleRssMonthly(): void | ||
660 | { | ||
661 | $dates = [ | ||
662 | new \DateTimeImmutable('2020-05-19'), | ||
663 | new \DateTimeImmutable('2020-04-13'), | ||
664 | ]; | ||
665 | $expectedDates = [ | ||
666 | new \DateTimeImmutable('2020-05-31 23:59:59'), | ||
667 | new \DateTimeImmutable('2020-04-30 23:59:59'), | ||
668 | ]; | ||
669 | |||
670 | $this->container->environment['QUERY_STRING'] = 'month'; | ||
671 | $request = $this->createMock(Request::class); | ||
672 | $request->method('getQueryParam')->willReturnCallback(function (string $key): ?string { | ||
673 | return $key === 'month' ? '' : null; | ||
674 | }); | ||
675 | $response = new Response(); | ||
676 | |||
677 | $this->container->bookmarkService->expects(static::once())->method('search')->willReturn([ | ||
678 | (new Bookmark())->setId(1)->setCreated($dates[0])->setUrl('http://domain.tld/1'), | ||
679 | (new Bookmark())->setId(2)->setCreated($dates[1])->setUrl('http://domain.tld/2'), | ||
680 | (new Bookmark())->setId(3)->setCreated($dates[1])->setUrl('http://domain.tld/3'), | ||
681 | ]); | ||
682 | |||
683 | // Save RainTPL assigned variables | ||
684 | $assignedVariables = []; | ||
685 | $this->assignTemplateVars($assignedVariables); | ||
686 | |||
687 | $result = $this->controller->rss($request, $response); | ||
688 | |||
689 | static::assertSame(200, $result->getStatusCode()); | ||
690 | static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); | ||
691 | static::assertSame('dailyrss', (string) $result->getBody()); | ||
692 | static::assertSame('Shaarli', $assignedVariables['title']); | ||
693 | static::assertSame('http://shaarli/subfolder/', $assignedVariables['index_url']); | ||
694 | static::assertSame('http://shaarli/subfolder/daily-rss?month', $assignedVariables['page_url']); | ||
695 | static::assertFalse($assignedVariables['hide_timestamps']); | ||
696 | static::assertCount(2, $assignedVariables['days']); | ||
697 | |||
698 | $day = $assignedVariables['days'][$dates[0]->format('Ym')]; | ||
699 | $date = $expectedDates[0]; | ||
700 | |||
701 | static::assertEquals($date, $day['date']); | ||
702 | static::assertSame($date->format(\DateTime::RSS), $day['date_rss']); | ||
703 | static::assertSame('May, 2020', $day['date_human']); | ||
704 | static::assertSame('http://shaarli/subfolder/daily?month='. $dates[0]->format('Ym'), $day['absolute_url']); | ||
705 | static::assertCount(1, $day['links']); | ||
706 | |||
707 | $day = $assignedVariables['days'][$dates[1]->format('Ym')]; | ||
708 | $date = $expectedDates[1]; | ||
709 | |||
710 | static::assertEquals($date, $day['date']); | ||
711 | static::assertSame($date->format(\DateTime::RSS), $day['date_rss']); | ||
712 | static::assertSame('April, 2020', $day['date_human']); | ||
713 | static::assertSame('http://shaarli/subfolder/daily?month='. $dates[1]->format('Ym'), $day['absolute_url']); | ||
714 | static::assertCount(2, $day['links']); | ||
715 | } | ||
716 | } | ||