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