]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller; | |
6 | ||
7 | use PHPUnit\Framework\TestCase; | |
8 | use Shaarli\Bookmark\Bookmark; | |
9 | use Shaarli\Bookmark\BookmarkServiceInterface; | |
10 | use Shaarli\Config\ConfigManager; | |
11 | use Shaarli\Container\ShaarliContainer; | |
12 | use Shaarli\Formatter\BookmarkFormatter; | |
13 | use Shaarli\Formatter\BookmarkRawFormatter; | |
14 | use Shaarli\Formatter\FormatterFactory; | |
15 | use Shaarli\Plugin\PluginManager; | |
16 | use Shaarli\Render\PageBuilder; | |
17 | use Shaarli\Security\LoginManager; | |
18 | use Slim\Http\Request; | |
19 | use Slim\Http\Response; | |
20 | ||
21 | class DailyControllerTest extends TestCase | |
22 | { | |
23 | /** @var ShaarliContainer */ | |
24 | protected $container; | |
25 | ||
26 | /** @var DailyController */ | |
27 | protected $controller; | |
28 | ||
29 | public function setUp(): void | |
30 | { | |
31 | $this->container = $this->createMock(ShaarliContainer::class); | |
32 | $this->controller = new DailyController($this->container); | |
33 | } | |
34 | ||
35 | public function testValidControllerInvokeDefault(): void | |
36 | { | |
37 | $this->createValidContainerMockSet(); | |
38 | ||
39 | $currentDay = new \DateTimeImmutable('2020-05-13'); | |
40 | ||
41 | $request = $this->createMock(Request::class); | |
42 | $request->method('getQueryParam')->willReturn($currentDay->format('Ymd')); | |
43 | $response = new Response(); | |
44 | ||
45 | // Save RainTPL assigned variables | |
46 | $assignedVariables = []; | |
47 | $this->assignTemplateVars($assignedVariables); | |
48 | ||
49 | // Links dataset: 2 links with thumbnails | |
50 | $this->container->bookmarkService | |
51 | ->expects(static::once()) | |
52 | ->method('days') | |
53 | ->willReturnCallback(function () use ($currentDay): array { | |
54 | return [ | |
55 | '20200510', | |
56 | $currentDay->format('Ymd'), | |
57 | '20200516', | |
58 | ]; | |
59 | }) | |
60 | ; | |
61 | $this->container->bookmarkService | |
62 | ->expects(static::once()) | |
63 | ->method('filterDay') | |
64 | ->willReturnCallback(function (): array { | |
65 | return [ | |
66 | (new Bookmark()) | |
67 | ->setId(1) | |
68 | ->setUrl('http://url.tld') | |
69 | ->setTitle(static::generateContent(50)) | |
70 | ->setDescription(static::generateContent(500)) | |
71 | , | |
72 | (new Bookmark()) | |
73 | ->setId(2) | |
74 | ->setUrl('http://url2.tld') | |
75 | ->setTitle(static::generateContent(50)) | |
76 | ->setDescription(static::generateContent(500)) | |
77 | , | |
78 | (new Bookmark()) | |
79 | ->setId(3) | |
80 | ->setUrl('http://url3.tld') | |
81 | ->setTitle(static::generateContent(50)) | |
82 | ->setDescription(static::generateContent(500)) | |
83 | , | |
84 | ]; | |
85 | }) | |
86 | ; | |
87 | ||
88 | // Make sure that PluginManager hook is triggered | |
89 | $this->container->pluginManager | |
90 | ->expects(static::at(0)) | |
91 | ->method('executeHooks') | |
92 | ->willReturnCallback(function (string $hook, array $data, array $param) use ($currentDay): array { | |
93 | static::assertSame('render_daily', $hook); | |
94 | ||
95 | static::assertArrayHasKey('linksToDisplay', $data); | |
96 | static::assertCount(3, $data['linksToDisplay']); | |
97 | static::assertSame(1, $data['linksToDisplay'][0]['id']); | |
98 | static::assertSame($currentDay->getTimestamp(), $data['day']); | |
99 | static::assertSame('20200510', $data['previousday']); | |
100 | static::assertSame('20200516', $data['nextday']); | |
101 | ||
102 | static::assertArrayHasKey('loggedin', $param); | |
103 | ||
104 | return $data; | |
105 | }); | |
106 | ||
107 | $result = $this->controller->index($request, $response); | |
108 | ||
109 | static::assertSame(200, $result->getStatusCode()); | |
110 | static::assertSame('daily', (string) $result->getBody()); | |
111 | static::assertSame( | |
112 | 'Daily - '. format_date($currentDay, false, true) .' - Shaarli', | |
113 | $assignedVariables['pagetitle'] | |
114 | ); | |
115 | static::assertEquals($currentDay, $assignedVariables['dayDate']); | |
116 | static::assertEquals($currentDay->getTimestamp(), $assignedVariables['day']); | |
117 | static::assertCount(3, $assignedVariables['linksToDisplay']); | |
118 | ||
119 | $link = $assignedVariables['linksToDisplay'][0]; | |
120 | ||
121 | static::assertSame(1, $link['id']); | |
122 | static::assertSame('http://url.tld', $link['url']); | |
123 | static::assertNotEmpty($link['title']); | |
124 | static::assertNotEmpty($link['description']); | |
125 | static::assertNotEmpty($link['formatedDescription']); | |
126 | ||
127 | $link = $assignedVariables['linksToDisplay'][1]; | |
128 | ||
129 | static::assertSame(2, $link['id']); | |
130 | static::assertSame('http://url2.tld', $link['url']); | |
131 | static::assertNotEmpty($link['title']); | |
132 | static::assertNotEmpty($link['description']); | |
133 | static::assertNotEmpty($link['formatedDescription']); | |
134 | ||
135 | $link = $assignedVariables['linksToDisplay'][2]; | |
136 | ||
137 | static::assertSame(3, $link['id']); | |
138 | static::assertSame('http://url3.tld', $link['url']); | |
139 | static::assertNotEmpty($link['title']); | |
140 | static::assertNotEmpty($link['description']); | |
141 | static::assertNotEmpty($link['formatedDescription']); | |
142 | ||
143 | static::assertCount(3, $assignedVariables['cols']); | |
144 | static::assertCount(1, $assignedVariables['cols'][0]); | |
145 | static::assertCount(1, $assignedVariables['cols'][1]); | |
146 | static::assertCount(1, $assignedVariables['cols'][2]); | |
147 | ||
148 | $link = $assignedVariables['cols'][0][0]; | |
149 | ||
150 | static::assertSame(1, $link['id']); | |
151 | static::assertSame('http://url.tld', $link['url']); | |
152 | static::assertNotEmpty($link['title']); | |
153 | static::assertNotEmpty($link['description']); | |
154 | static::assertNotEmpty($link['formatedDescription']); | |
155 | ||
156 | $link = $assignedVariables['cols'][1][0]; | |
157 | ||
158 | static::assertSame(2, $link['id']); | |
159 | static::assertSame('http://url2.tld', $link['url']); | |
160 | static::assertNotEmpty($link['title']); | |
161 | static::assertNotEmpty($link['description']); | |
162 | static::assertNotEmpty($link['formatedDescription']); | |
163 | ||
164 | $link = $assignedVariables['cols'][2][0]; | |
165 | ||
166 | static::assertSame(3, $link['id']); | |
167 | static::assertSame('http://url3.tld', $link['url']); | |
168 | static::assertNotEmpty($link['title']); | |
169 | static::assertNotEmpty($link['description']); | |
170 | static::assertNotEmpty($link['formatedDescription']); | |
171 | } | |
172 | ||
173 | /** | |
174 | * Daily page - test that everything goes fine with no future or past bookmarks | |
175 | */ | |
176 | public function testValidControllerInvokeNoFutureOrPast(): void | |
177 | { | |
178 | $this->createValidContainerMockSet(); | |
179 | ||
180 | $currentDay = new \DateTimeImmutable('2020-05-13'); | |
181 | ||
182 | $request = $this->createMock(Request::class); | |
183 | $response = new Response(); | |
184 | ||
185 | // Save RainTPL assigned variables | |
186 | $assignedVariables = []; | |
187 | $this->assignTemplateVars($assignedVariables); | |
188 | ||
189 | // Links dataset: 2 links with thumbnails | |
190 | $this->container->bookmarkService | |
191 | ->expects(static::once()) | |
192 | ->method('days') | |
193 | ->willReturnCallback(function () use ($currentDay): array { | |
194 | return [ | |
195 | $currentDay->format($currentDay->format('Ymd')), | |
196 | ]; | |
197 | }) | |
198 | ; | |
199 | $this->container->bookmarkService | |
200 | ->expects(static::once()) | |
201 | ->method('filterDay') | |
202 | ->willReturnCallback(function (): array { | |
203 | return [ | |
204 | (new Bookmark()) | |
205 | ->setId(1) | |
206 | ->setUrl('http://url.tld') | |
207 | ->setTitle(static::generateContent(50)) | |
208 | ->setDescription(static::generateContent(500)) | |
209 | , | |
210 | ]; | |
211 | }) | |
212 | ; | |
213 | ||
214 | // Make sure that PluginManager hook is triggered | |
215 | $this->container->pluginManager | |
216 | ->expects(static::at(0)) | |
217 | ->method('executeHooks') | |
218 | ->willReturnCallback(function (string $hook, array $data, array $param) use ($currentDay): array { | |
219 | static::assertSame('render_daily', $hook); | |
220 | ||
221 | static::assertArrayHasKey('linksToDisplay', $data); | |
222 | static::assertCount(1, $data['linksToDisplay']); | |
223 | static::assertSame(1, $data['linksToDisplay'][0]['id']); | |
224 | static::assertSame($currentDay->getTimestamp(), $data['day']); | |
225 | static::assertEmpty($data['previousday']); | |
226 | static::assertEmpty($data['nextday']); | |
227 | ||
228 | static::assertArrayHasKey('loggedin', $param); | |
229 | ||
230 | return $data; | |
231 | }); | |
232 | ||
233 | $result = $this->controller->index($request, $response); | |
234 | ||
235 | static::assertSame(200, $result->getStatusCode()); | |
236 | static::assertSame('daily', (string) $result->getBody()); | |
237 | static::assertSame( | |
238 | 'Daily - '. format_date($currentDay, false, true) .' - Shaarli', | |
239 | $assignedVariables['pagetitle'] | |
240 | ); | |
241 | static::assertCount(1, $assignedVariables['linksToDisplay']); | |
242 | ||
243 | $link = $assignedVariables['linksToDisplay'][0]; | |
244 | static::assertSame(1, $link['id']); | |
245 | } | |
246 | ||
247 | /** | |
248 | * Daily page - test that height adjustment in columns is working | |
249 | */ | |
250 | public function testValidControllerInvokeHeightAdjustment(): void | |
251 | { | |
252 | $this->createValidContainerMockSet(); | |
253 | ||
254 | $currentDay = new \DateTimeImmutable('2020-05-13'); | |
255 | ||
256 | $request = $this->createMock(Request::class); | |
257 | $response = new Response(); | |
258 | ||
259 | // Save RainTPL assigned variables | |
260 | $assignedVariables = []; | |
261 | $this->assignTemplateVars($assignedVariables); | |
262 | ||
263 | // Links dataset: 2 links with thumbnails | |
264 | $this->container->bookmarkService | |
265 | ->expects(static::once()) | |
266 | ->method('days') | |
267 | ->willReturnCallback(function () use ($currentDay): array { | |
268 | return [ | |
269 | $currentDay->format($currentDay->format('Ymd')), | |
270 | ]; | |
271 | }) | |
272 | ; | |
273 | $this->container->bookmarkService | |
274 | ->expects(static::once()) | |
275 | ->method('filterDay') | |
276 | ->willReturnCallback(function (): array { | |
277 | return [ | |
278 | (new Bookmark())->setId(1)->setUrl('http://url.tld')->setTitle('title'), | |
279 | (new Bookmark()) | |
280 | ->setId(2) | |
281 | ->setUrl('http://url.tld') | |
282 | ->setTitle(static::generateContent(50)) | |
283 | ->setDescription(static::generateContent(5000)) | |
284 | , | |
285 | (new Bookmark())->setId(3)->setUrl('http://url.tld')->setTitle('title'), | |
286 | (new Bookmark())->setId(4)->setUrl('http://url.tld')->setTitle('title'), | |
287 | (new Bookmark())->setId(5)->setUrl('http://url.tld')->setTitle('title'), | |
288 | (new Bookmark())->setId(6)->setUrl('http://url.tld')->setTitle('title'), | |
289 | (new Bookmark())->setId(7)->setUrl('http://url.tld')->setTitle('title'), | |
290 | ]; | |
291 | }) | |
292 | ; | |
293 | ||
294 | // Make sure that PluginManager hook is triggered | |
295 | $this->container->pluginManager | |
296 | ->expects(static::at(0)) | |
297 | ->method('executeHooks') | |
298 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | |
299 | return $data; | |
300 | }) | |
301 | ; | |
302 | ||
303 | $result = $this->controller->index($request, $response); | |
304 | ||
305 | static::assertSame(200, $result->getStatusCode()); | |
306 | static::assertSame('daily', (string) $result->getBody()); | |
307 | static::assertCount(7, $assignedVariables['linksToDisplay']); | |
308 | ||
309 | $columnIds = function (array $column): array { | |
310 | return array_map(function (array $item): int { return $item['id']; }, $column); | |
311 | }; | |
312 | ||
313 | static::assertSame([1, 4, 6], $columnIds($assignedVariables['cols'][0])); | |
314 | static::assertSame([2], $columnIds($assignedVariables['cols'][1])); | |
315 | static::assertSame([3, 5, 7], $columnIds($assignedVariables['cols'][2])); | |
316 | } | |
317 | ||
318 | /** | |
319 | * Daily page - no bookmark | |
320 | */ | |
321 | public function testValidControllerInvokeNoBookmark(): void | |
322 | { | |
323 | $this->createValidContainerMockSet(); | |
324 | ||
325 | $request = $this->createMock(Request::class); | |
326 | $response = new Response(); | |
327 | ||
328 | // Save RainTPL assigned variables | |
329 | $assignedVariables = []; | |
330 | $this->assignTemplateVars($assignedVariables); | |
331 | ||
332 | // Links dataset: 2 links with thumbnails | |
333 | $this->container->bookmarkService | |
334 | ->expects(static::once()) | |
335 | ->method('days') | |
336 | ->willReturnCallback(function (): array { | |
337 | return []; | |
338 | }) | |
339 | ; | |
340 | $this->container->bookmarkService | |
341 | ->expects(static::once()) | |
342 | ->method('filterDay') | |
343 | ->willReturnCallback(function (): array { | |
344 | return []; | |
345 | }) | |
346 | ; | |
347 | ||
348 | // Make sure that PluginManager hook is triggered | |
349 | $this->container->pluginManager | |
350 | ->expects(static::at(0)) | |
351 | ->method('executeHooks') | |
352 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | |
353 | return $data; | |
354 | }) | |
355 | ; | |
356 | ||
357 | $result = $this->controller->index($request, $response); | |
358 | ||
359 | static::assertSame(200, $result->getStatusCode()); | |
360 | static::assertSame('daily', (string) $result->getBody()); | |
361 | static::assertCount(0, $assignedVariables['linksToDisplay']); | |
362 | static::assertSame('Today', $assignedVariables['dayDesc']); | |
363 | static::assertEquals((new \DateTime())->setTime(0, 0)->getTimestamp(), $assignedVariables['day']); | |
364 | static::assertEquals((new \DateTime())->setTime(0, 0), $assignedVariables['dayDate']); | |
365 | } | |
366 | ||
367 | protected function createValidContainerMockSet(): void | |
368 | { | |
369 | $loginManager = $this->createMock(LoginManager::class); | |
370 | $this->container->loginManager = $loginManager; | |
371 | ||
372 | // Config | |
373 | $conf = $this->createMock(ConfigManager::class); | |
374 | $this->container->conf = $conf; | |
375 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { | |
376 | return $default; | |
377 | }); | |
378 | ||
379 | // PageBuilder | |
380 | $pageBuilder = $this->createMock(PageBuilder::class); | |
381 | $pageBuilder | |
382 | ->method('render') | |
383 | ->willReturnCallback(function (string $template): string { | |
384 | return $template; | |
385 | }) | |
386 | ; | |
387 | $this->container->pageBuilder = $pageBuilder; | |
388 | ||
389 | // Plugin Manager | |
390 | $pluginManager = $this->createMock(PluginManager::class); | |
391 | $this->container->pluginManager = $pluginManager; | |
392 | ||
393 | // BookmarkService | |
394 | $bookmarkService = $this->createMock(BookmarkServiceInterface::class); | |
395 | $this->container->bookmarkService = $bookmarkService; | |
396 | ||
397 | // Formatter | |
398 | $formatterFactory = $this->createMock(FormatterFactory::class); | |
399 | $formatterFactory | |
400 | ->method('getFormatter') | |
401 | ->willReturnCallback(function (): BookmarkFormatter { | |
402 | return new BookmarkRawFormatter($this->container->conf, true); | |
403 | }) | |
404 | ; | |
405 | $this->container->formatterFactory = $formatterFactory; | |
406 | } | |
407 | ||
408 | protected function assignTemplateVars(array &$variables): void | |
409 | { | |
410 | $this->container->pageBuilder | |
411 | ->expects(static::atLeastOnce()) | |
412 | ->method('assign') | |
413 | ->willReturnCallback(function ($key, $value) use (&$variables) { | |
414 | $variables[$key] = $value; | |
415 | ||
416 | return $this; | |
417 | }) | |
418 | ; | |
419 | } | |
420 | ||
421 | protected static function generateContent(int $length): string | |
422 | { | |
423 | // bin2hex(random_bytes) generates string twice as long as given parameter | |
424 | $length = (int) ceil($length / 2); | |
425 | return bin2hex(random_bytes($length)); | |
426 | } | |
427 | } |