diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-05-17 14:16:32 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | c4d5be53c2ae503c00da3cfe6b28d0ce9d2ca7f5 (patch) | |
tree | 2aa6b156d45da7a1bb3cfe0b6e8622030fddb990 /tests/front/controller/DailyControllerTest.php | |
parent | e3d28be9673a9f8404ff907b8191209729ad690c (diff) | |
download | Shaarli-c4d5be53c2ae503c00da3cfe6b28d0ce9d2ca7f5.tar.gz Shaarli-c4d5be53c2ae503c00da3cfe6b28d0ce9d2ca7f5.tar.zst Shaarli-c4d5be53c2ae503c00da3cfe6b28d0ce9d2ca7f5.zip |
Process Daily RSS feed through Slim controller
The daily RSS template has been entirely rewritten to handle the whole feed through the template engine.
Diffstat (limited to 'tests/front/controller/DailyControllerTest.php')
-rw-r--r-- | tests/front/controller/DailyControllerTest.php | 152 |
1 files changed, 148 insertions, 4 deletions
diff --git a/tests/front/controller/DailyControllerTest.php b/tests/front/controller/DailyControllerTest.php index 2714bfd9..72a0339f 100644 --- a/tests/front/controller/DailyControllerTest.php +++ b/tests/front/controller/DailyControllerTest.php | |||
@@ -9,11 +9,13 @@ use Shaarli\Bookmark\Bookmark; | |||
9 | use Shaarli\Bookmark\BookmarkServiceInterface; | 9 | use Shaarli\Bookmark\BookmarkServiceInterface; |
10 | use Shaarli\Config\ConfigManager; | 10 | use Shaarli\Config\ConfigManager; |
11 | use Shaarli\Container\ShaarliContainer; | 11 | use Shaarli\Container\ShaarliContainer; |
12 | use Shaarli\Feed\CachedPage; | ||
12 | use Shaarli\Formatter\BookmarkFormatter; | 13 | use Shaarli\Formatter\BookmarkFormatter; |
13 | use Shaarli\Formatter\BookmarkRawFormatter; | 14 | use Shaarli\Formatter\BookmarkRawFormatter; |
14 | use Shaarli\Formatter\FormatterFactory; | 15 | use Shaarli\Formatter\FormatterFactory; |
15 | use Shaarli\Plugin\PluginManager; | 16 | use Shaarli\Plugin\PluginManager; |
16 | use Shaarli\Render\PageBuilder; | 17 | use Shaarli\Render\PageBuilder; |
18 | use Shaarli\Render\PageCacheManager; | ||
17 | use Shaarli\Security\LoginManager; | 19 | use Shaarli\Security\LoginManager; |
18 | use Slim\Http\Request; | 20 | use Slim\Http\Request; |
19 | use Slim\Http\Response; | 21 | use Slim\Http\Response; |
@@ -30,9 +32,10 @@ class DailyControllerTest extends TestCase | |||
30 | { | 32 | { |
31 | $this->container = $this->createMock(ShaarliContainer::class); | 33 | $this->container = $this->createMock(ShaarliContainer::class); |
32 | $this->controller = new DailyController($this->container); | 34 | $this->controller = new DailyController($this->container); |
35 | DailyController::$DAILY_RSS_NB_DAYS = 2; | ||
33 | } | 36 | } |
34 | 37 | ||
35 | public function testValidControllerInvokeDefault(): void | 38 | public function testValidIndexControllerInvokeDefault(): void |
36 | { | 39 | { |
37 | $this->createValidContainerMockSet(); | 40 | $this->createValidContainerMockSet(); |
38 | 41 | ||
@@ -173,7 +176,7 @@ class DailyControllerTest extends TestCase | |||
173 | /** | 176 | /** |
174 | * Daily page - test that everything goes fine with no future or past bookmarks | 177 | * Daily page - test that everything goes fine with no future or past bookmarks |
175 | */ | 178 | */ |
176 | public function testValidControllerInvokeNoFutureOrPast(): void | 179 | public function testValidIndexControllerInvokeNoFutureOrPast(): void |
177 | { | 180 | { |
178 | $this->createValidContainerMockSet(); | 181 | $this->createValidContainerMockSet(); |
179 | 182 | ||
@@ -247,7 +250,7 @@ class DailyControllerTest extends TestCase | |||
247 | /** | 250 | /** |
248 | * Daily page - test that height adjustment in columns is working | 251 | * Daily page - test that height adjustment in columns is working |
249 | */ | 252 | */ |
250 | public function testValidControllerInvokeHeightAdjustment(): void | 253 | public function testValidIndexControllerInvokeHeightAdjustment(): void |
251 | { | 254 | { |
252 | $this->createValidContainerMockSet(); | 255 | $this->createValidContainerMockSet(); |
253 | 256 | ||
@@ -318,7 +321,7 @@ class DailyControllerTest extends TestCase | |||
318 | /** | 321 | /** |
319 | * Daily page - no bookmark | 322 | * Daily page - no bookmark |
320 | */ | 323 | */ |
321 | public function testValidControllerInvokeNoBookmark(): void | 324 | public function testValidIndexControllerInvokeNoBookmark(): void |
322 | { | 325 | { |
323 | $this->createValidContainerMockSet(); | 326 | $this->createValidContainerMockSet(); |
324 | 327 | ||
@@ -364,6 +367,136 @@ class DailyControllerTest extends TestCase | |||
364 | static::assertEquals((new \DateTime())->setTime(0, 0), $assignedVariables['dayDate']); | 367 | static::assertEquals((new \DateTime())->setTime(0, 0), $assignedVariables['dayDate']); |
365 | } | 368 | } |
366 | 369 | ||
370 | /** | ||
371 | * Daily RSS - default behaviour | ||
372 | */ | ||
373 | public function testValidRssControllerInvokeDefault(): void | ||
374 | { | ||
375 | $this->createValidContainerMockSet(); | ||
376 | |||
377 | $dates = [ | ||
378 | new \DateTimeImmutable('2020-05-17'), | ||
379 | new \DateTimeImmutable('2020-05-15'), | ||
380 | new \DateTimeImmutable('2020-05-13'), | ||
381 | ]; | ||
382 | |||
383 | $request = $this->createMock(Request::class); | ||
384 | $response = new Response(); | ||
385 | |||
386 | $this->container->bookmarkService->expects(static::once())->method('search')->willReturn([ | ||
387 | (new Bookmark())->setId(1)->setCreated($dates[0])->setUrl('http://domain.tld/1'), | ||
388 | (new Bookmark())->setId(2)->setCreated($dates[1])->setUrl('http://domain.tld/2'), | ||
389 | (new Bookmark())->setId(3)->setCreated($dates[1])->setUrl('http://domain.tld/3'), | ||
390 | (new Bookmark())->setId(4)->setCreated($dates[2])->setUrl('http://domain.tld/4'), | ||
391 | ]); | ||
392 | |||
393 | $this->container->pageCacheManager | ||
394 | ->expects(static::once()) | ||
395 | ->method('getCachePage') | ||
396 | ->willReturnCallback(function (): CachedPage { | ||
397 | $cachedPage = $this->createMock(CachedPage::class); | ||
398 | $cachedPage->expects(static::once())->method('cache')->with('dailyrss'); | ||
399 | |||
400 | return $cachedPage; | ||
401 | } | ||
402 | ); | ||
403 | |||
404 | // Save RainTPL assigned variables | ||
405 | $assignedVariables = []; | ||
406 | $this->assignTemplateVars($assignedVariables); | ||
407 | |||
408 | $result = $this->controller->rss($request, $response); | ||
409 | |||
410 | static::assertSame(200, $result->getStatusCode()); | ||
411 | static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); | ||
412 | static::assertSame('dailyrss', (string) $result->getBody()); | ||
413 | static::assertSame('Shaarli', $assignedVariables['title']); | ||
414 | static::assertSame('http://shaarli', $assignedVariables['index_url']); | ||
415 | static::assertSame('http://shaarli/daily-rss', $assignedVariables['page_url']); | ||
416 | static::assertFalse($assignedVariables['hide_timestamps']); | ||
417 | static::assertCount(2, $assignedVariables['days']); | ||
418 | |||
419 | $day = $assignedVariables['days'][$dates[0]->format('Ymd')]; | ||
420 | |||
421 | static::assertEquals($dates[0], $day['date']); | ||
422 | static::assertSame($dates[0]->format(\DateTimeInterface::RSS), $day['date_rss']); | ||
423 | static::assertSame(format_date($dates[0], false), $day['date_human']); | ||
424 | static::assertSame('http://shaarli/daily?day='. $dates[0]->format('Ymd'), $day['absolute_url']); | ||
425 | static::assertCount(1, $day['links']); | ||
426 | static::assertSame(1, $day['links'][0]['id']); | ||
427 | static::assertSame('http://domain.tld/1', $day['links'][0]['url']); | ||
428 | static::assertEquals($dates[0], $day['links'][0]['created']); | ||
429 | |||
430 | $day = $assignedVariables['days'][$dates[1]->format('Ymd')]; | ||
431 | |||
432 | static::assertEquals($dates[1], $day['date']); | ||
433 | static::assertSame($dates[1]->format(\DateTimeInterface::RSS), $day['date_rss']); | ||
434 | static::assertSame(format_date($dates[1], false), $day['date_human']); | ||
435 | static::assertSame('http://shaarli/daily?day='. $dates[1]->format('Ymd'), $day['absolute_url']); | ||
436 | static::assertCount(2, $day['links']); | ||
437 | |||
438 | static::assertSame(2, $day['links'][0]['id']); | ||
439 | static::assertSame('http://domain.tld/2', $day['links'][0]['url']); | ||
440 | static::assertEquals($dates[1], $day['links'][0]['created']); | ||
441 | static::assertSame(3, $day['links'][1]['id']); | ||
442 | static::assertSame('http://domain.tld/3', $day['links'][1]['url']); | ||
443 | static::assertEquals($dates[1], $day['links'][1]['created']); | ||
444 | } | ||
445 | |||
446 | /** | ||
447 | * Daily RSS - trigger cache rendering | ||
448 | */ | ||
449 | public function testValidRssControllerInvokeTriggerCache(): void | ||
450 | { | ||
451 | $this->createValidContainerMockSet(); | ||
452 | |||
453 | $request = $this->createMock(Request::class); | ||
454 | $response = new Response(); | ||
455 | |||
456 | $this->container->pageCacheManager->method('getCachePage')->willReturnCallback(function (): CachedPage { | ||
457 | $cachedPage = $this->createMock(CachedPage::class); | ||
458 | $cachedPage->method('cachedVersion')->willReturn('this is cache!'); | ||
459 | |||
460 | return $cachedPage; | ||
461 | }); | ||
462 | |||
463 | $this->container->bookmarkService->expects(static::never())->method('search'); | ||
464 | |||
465 | $result = $this->controller->rss($request, $response); | ||
466 | |||
467 | static::assertSame(200, $result->getStatusCode()); | ||
468 | static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); | ||
469 | static::assertSame('this is cache!', (string) $result->getBody()); | ||
470 | } | ||
471 | |||
472 | /** | ||
473 | * Daily RSS - No bookmark | ||
474 | */ | ||
475 | public function testValidRssControllerInvokeNoBookmark(): void | ||
476 | { | ||
477 | $this->createValidContainerMockSet(); | ||
478 | |||
479 | $request = $this->createMock(Request::class); | ||
480 | $response = new Response(); | ||
481 | |||
482 | $this->container->bookmarkService->expects(static::once())->method('search')->willReturn([]); | ||
483 | |||
484 | // Save RainTPL assigned variables | ||
485 | $assignedVariables = []; | ||
486 | $this->assignTemplateVars($assignedVariables); | ||
487 | |||
488 | $result = $this->controller->rss($request, $response); | ||
489 | |||
490 | static::assertSame(200, $result->getStatusCode()); | ||
491 | static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); | ||
492 | static::assertSame('dailyrss', (string) $result->getBody()); | ||
493 | static::assertSame('Shaarli', $assignedVariables['title']); | ||
494 | static::assertSame('http://shaarli', $assignedVariables['index_url']); | ||
495 | static::assertSame('http://shaarli/daily-rss', $assignedVariables['page_url']); | ||
496 | static::assertFalse($assignedVariables['hide_timestamps']); | ||
497 | static::assertCount(0, $assignedVariables['days']); | ||
498 | } | ||
499 | |||
367 | protected function createValidContainerMockSet(): void | 500 | protected function createValidContainerMockSet(): void |
368 | { | 501 | { |
369 | $loginManager = $this->createMock(LoginManager::class); | 502 | $loginManager = $this->createMock(LoginManager::class); |
@@ -403,6 +536,17 @@ class DailyControllerTest extends TestCase | |||
403 | }) | 536 | }) |
404 | ; | 537 | ; |
405 | $this->container->formatterFactory = $formatterFactory; | 538 | $this->container->formatterFactory = $formatterFactory; |
539 | |||
540 | // CacheManager | ||
541 | $pageCacheManager = $this->createMock(PageCacheManager::class); | ||
542 | $this->container->pageCacheManager = $pageCacheManager; | ||
543 | |||
544 | // $_SERVER | ||
545 | $this->container->environment = [ | ||
546 | 'SERVER_NAME' => 'shaarli', | ||
547 | 'SERVER_PORT' => '80', | ||
548 | 'REQUEST_URI' => '/daily-rss', | ||
549 | ]; | ||
406 | } | 550 | } |
407 | 551 | ||
408 | protected function assignTemplateVars(array &$variables): void | 552 | protected function assignTemplateVars(array &$variables): void |