3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Bookmark\SearchResult
;
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Front\Exception\ThumbnailsDisabledException
;
12 use Shaarli\Thumbnailer
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
16 class PictureWallControllerTest
extends TestCase
18 use FrontControllerMockHelper
;
20 /** @var PictureWallController */
21 protected $controller;
23 public function setUp(): void
25 $this->createContainer();
27 $this->controller
= new PictureWallController($this->container
);
30 public function testValidControllerInvokeDefault(): void
32 $request = $this->createMock(Request
::class);
33 $request->expects(static::once())->method('getQueryParams')->willReturn([]);
34 $response = new Response();
36 // ConfigManager: thumbnails are enabled
37 $this->container
->conf
= $this->createMock(ConfigManager
::class);
38 $this->container
->conf
->method('get')->willReturnCallback(function (string $parameter, $default) {
39 if ($parameter === 'thumbnails.mode') {
40 return Thumbnailer
::MODE_COMMON
;
46 // Save RainTPL assigned variables
47 $assignedVariables = [];
48 $this->assignTemplateVars($assignedVariables);
50 // Links dataset: 2 links with thumbnails
51 $this->container
->bookmarkService
52 ->expects(static::once())
54 ->willReturnCallback(function (array $parameters, ?string $visibility): SearchResult
{
55 // Visibility is set through the container, not the call
56 static::assertNull($visibility);
58 // No query parameters
59 if (count($parameters) === 0) {
60 return SearchResult
::getSearchResult([
61 (new Bookmark())->setId(1)->setUrl('http://url.tld')->setThumbnail('thumb1'),
62 (new Bookmark())->setId(2)->setUrl('http://url2.tld'),
63 (new Bookmark())->setId(3)->setUrl('http://url3.tld')->setThumbnail('thumb2'),
69 // Make sure that PluginManager hook is triggered
70 $this->container
->pluginManager
71 ->expects(static::atLeastOnce())
72 ->method('executeHooks')
73 ->withConsecutive(['render_picwall'])
74 ->willReturnCallback(function (string $hook, array $data, array $param): array {
75 if ('render_picwall' === $hook) {
76 static::assertArrayHasKey('linksToDisplay', $data);
77 static::assertCount(2, $data['linksToDisplay']);
78 static::assertSame(1, $data['linksToDisplay'][0]['id']);
79 static::assertSame(3, $data['linksToDisplay'][1]['id']);
80 static::assertArrayHasKey('loggedin', $param);
86 $result = $this->controller
->index($request, $response);
88 static::assertSame(200, $result->getStatusCode());
89 static::assertSame('picwall', (string) $result->getBody());
90 static::assertSame('Picture wall - Shaarli', $assignedVariables['pagetitle']);
91 static::assertCount(2, $assignedVariables['linksToDisplay']);
93 $link = $assignedVariables['linksToDisplay'][0];
95 static::assertSame(1, $link['id']);
96 static::assertSame('http://url.tld', $link['url']);
97 static::assertSame('thumb1', $link['thumbnail']);
99 $link = $assignedVariables['linksToDisplay'][1];
101 static::assertSame(3, $link['id']);
102 static::assertSame('http://url3.tld', $link['url']);
103 static::assertSame('thumb2', $link['thumbnail']);
106 public function testControllerWithThumbnailsDisabled(): void
108 $this->expectException(ThumbnailsDisabledException
::class);
110 $request = $this->createMock(Request
::class);
111 $response = new Response();
113 // ConfigManager: thumbnails are disabled
114 $this->container
->conf
->method('get')->willReturnCallback(function (string $parameter, $default) {
115 if ($parameter === 'thumbnails.mode') {
116 return Thumbnailer
::MODE_NONE
;
122 $this->controller
->index($request, $response);