3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
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\Front\Exception\ThumbnailsDisabledException
;
16 use Shaarli\Plugin\PluginManager
;
17 use Shaarli\Render\PageBuilder
;
18 use Shaarli\Security\LoginManager
;
19 use Shaarli\Thumbnailer
;
20 use Slim\Http\Request
;
21 use Slim\Http\Response
;
23 class PictureWallControllerTest
extends TestCase
25 /** @var ShaarliContainer */
28 /** @var PictureWallController */
29 protected $controller;
31 public function setUp(): void
33 $this->container
= $this->createMock(ShaarliContainer
::class);
34 $this->controller
= new PictureWallController($this->container
);
37 public function testValidControllerInvokeDefault(): void
39 $this->createValidContainerMockSet();
41 $request = $this->createMock(Request
::class);
42 $request->expects(static::once())->method('getQueryParams')->willReturn([]);
43 $response = new Response();
45 // ConfigManager: thumbnails are enabled
46 $this->container
->conf
->method('get')->willReturnCallback(function (string $parameter, $default) {
47 if ($parameter === 'thumbnails.mode') {
48 return Thumbnailer
::MODE_COMMON
;
54 // Save RainTPL assigned variables
55 $assignedVariables = [];
56 $this->container
->pageBuilder
57 ->expects(static::atLeastOnce())
59 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
60 $assignedVariables[$key] = $value;
66 // Links dataset: 2 links with thumbnails
67 $this->container
->bookmarkService
68 ->expects(static::once())
70 ->willReturnCallback(function (array $parameters, ?string $visibility): array {
71 // Visibility is set through the container, not the call
72 static::assertNull($visibility);
74 // No query parameters
75 if (count($parameters) === 0) {
77 (new Bookmark())->setId(1)->setUrl('http://url.tld')->setThumbnail('thumb1'),
78 (new Bookmark())->setId(2)->setUrl('http://url2.tld'),
79 (new Bookmark())->setId(3)->setUrl('http://url3.tld')->setThumbnail('thumb2'),
85 // Make sure that PluginManager hook is triggered
86 $this->container
->pluginManager
87 ->expects(static::at(0))
88 ->method('executeHooks')
89 ->willReturnCallback(function (string $hook, array $data, array $param): array {
90 static::assertSame('render_picwall', $hook);
91 static::assertArrayHasKey('linksToDisplay', $data);
92 static::assertCount(2, $data['linksToDisplay']);
93 static::assertSame(1, $data['linksToDisplay'][0]['id']);
94 static::assertSame(3, $data['linksToDisplay'][1]['id']);
95 static::assertArrayHasKey('loggedin', $param);
100 $result = $this->controller
->index($request, $response);
102 static::assertSame(200, $result->getStatusCode());
103 static::assertSame('picwall', (string) $result->getBody());
104 static::assertSame('Picture wall - Shaarli', $assignedVariables['pagetitle']);
105 static::assertCount(2, $assignedVariables['linksToDisplay']);
107 $link = $assignedVariables['linksToDisplay'][0];
109 static::assertSame(1, $link['id']);
110 static::assertSame('http://url.tld', $link['url']);
111 static::assertSame('thumb1', $link['thumbnail']);
113 $link = $assignedVariables['linksToDisplay'][1];
115 static::assertSame(3, $link['id']);
116 static::assertSame('http://url3.tld', $link['url']);
117 static::assertSame('thumb2', $link['thumbnail']);
120 public function testControllerWithThumbnailsDisabled(): void
122 $this->expectException(ThumbnailsDisabledException
::class);
124 $this->createValidContainerMockSet();
126 $request = $this->createMock(Request
::class);
127 $response = new Response();
129 // ConfigManager: thumbnails are disabled
130 $this->container
->conf
->method('get')->willReturnCallback(function (string $parameter, $default) {
131 if ($parameter === 'thumbnails.mode') {
132 return Thumbnailer
::MODE_NONE
;
138 $this->controller
->index($request, $response);
141 protected function createValidContainerMockSet(): void
143 $loginManager = $this->createMock(LoginManager
::class);
144 $this->container
->loginManager
= $loginManager;
147 $conf = $this->createMock(ConfigManager
::class);
148 $this->container
->conf
= $conf;
151 $pageBuilder = $this->createMock(PageBuilder
::class);
154 ->willReturnCallback(function (string $template): string {
158 $this->container
->pageBuilder
= $pageBuilder;
161 $pluginManager = $this->createMock(PluginManager
::class);
162 $this->container
->pluginManager
= $pluginManager;
165 $bookmarkService = $this->createMock(BookmarkServiceInterface
::class);
166 $this->container
->bookmarkService
= $bookmarkService;
169 $formatterFactory = $this->createMock(FormatterFactory
::class);
171 ->method('getFormatter')
172 ->willReturnCallback(function (string $type): BookmarkFormatter
{
173 if ($type === 'raw') {
174 return new BookmarkRawFormatter($this->container
->conf
, true);
178 $this->container
->formatterFactory
= $formatterFactory;