]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/front/controller/PictureWallControllerTest.php
Process picwall rendering through Slim controller + UT
[github/shaarli/Shaarli.git] / tests / front / controller / PictureWallControllerTest.php
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\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;
22
23 class PictureWallControllerTest extends TestCase
24 {
25 /** @var ShaarliContainer */
26 protected $container;
27
28 /** @var PictureWallController */
29 protected $controller;
30
31 public function setUp(): void
32 {
33 $this->container = $this->createMock(ShaarliContainer::class);
34 $this->controller = new PictureWallController($this->container);
35 }
36
37 public function testValidControllerInvokeDefault(): void
38 {
39 $this->createValidContainerMockSet();
40
41 $request = $this->createMock(Request::class);
42 $request->expects(static::once())->method('getQueryParams')->willReturn([]);
43 $response = new Response();
44
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;
49 }
50
51 return $default;
52 });
53
54 // Save RainTPL assigned variables
55 $assignedVariables = [];
56 $this->container->pageBuilder
57 ->expects(static::atLeastOnce())
58 ->method('assign')
59 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
60 $assignedVariables[$key] = $value;
61
62 return $this;
63 })
64 ;
65
66 // Links dataset: 2 links with thumbnails
67 $this->container->bookmarkService
68 ->expects(static::once())
69 ->method('search')
70 ->willReturnCallback(function (array $parameters, ?string $visibility): array {
71 // Visibility is set through the container, not the call
72 static::assertNull($visibility);
73
74 // No query parameters
75 if (count($parameters) === 0) {
76 return [
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'),
80 ];
81 }
82 })
83 ;
84
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);
96
97 return $data;
98 });
99
100 $result = $this->controller->index($request, $response);
101
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']);
106
107 $link = $assignedVariables['linksToDisplay'][0];
108
109 static::assertSame(1, $link['id']);
110 static::assertSame('http://url.tld', $link['url']);
111 static::assertSame('thumb1', $link['thumbnail']);
112
113 $link = $assignedVariables['linksToDisplay'][1];
114
115 static::assertSame(3, $link['id']);
116 static::assertSame('http://url3.tld', $link['url']);
117 static::assertSame('thumb2', $link['thumbnail']);
118 }
119
120 public function testControllerWithThumbnailsDisabled(): void
121 {
122 $this->expectException(ThumbnailsDisabledException::class);
123
124 $this->createValidContainerMockSet();
125
126 $request = $this->createMock(Request::class);
127 $response = new Response();
128
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;
133 }
134
135 return $default;
136 });
137
138 $this->controller->index($request, $response);
139 }
140
141 protected function createValidContainerMockSet(): void
142 {
143 $loginManager = $this->createMock(LoginManager::class);
144 $this->container->loginManager = $loginManager;
145
146 // Config
147 $conf = $this->createMock(ConfigManager::class);
148 $this->container->conf = $conf;
149
150 // PageBuilder
151 $pageBuilder = $this->createMock(PageBuilder::class);
152 $pageBuilder
153 ->method('render')
154 ->willReturnCallback(function (string $template): string {
155 return $template;
156 })
157 ;
158 $this->container->pageBuilder = $pageBuilder;
159
160 // Plugin Manager
161 $pluginManager = $this->createMock(PluginManager::class);
162 $this->container->pluginManager = $pluginManager;
163
164 // BookmarkService
165 $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
166 $this->container->bookmarkService = $bookmarkService;
167
168 // Formatter
169 $formatterFactory = $this->createMock(FormatterFactory::class);
170 $formatterFactory
171 ->method('getFormatter')
172 ->willReturnCallback(function (string $type): BookmarkFormatter {
173 if ($type === 'raw') {
174 return new BookmarkRawFormatter($this->container->conf, true);
175 }
176 })
177 ;
178 $this->container->formatterFactory = $formatterFactory;
179 }
180 }