aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-26 11:15:15 +0100
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit485b168a9677d160b0c0426e4f282b9bd0c632c1 (patch)
treec7c21ea01096bfe28622b3a23c5c76d1cb2435fa /tests/front/controller
parentbee33239ed444f9724422fe5234cd79997500519 (diff)
downloadShaarli-485b168a9677d160b0c0426e4f282b9bd0c632c1.tar.gz
Shaarli-485b168a9677d160b0c0426e4f282b9bd0c632c1.tar.zst
Shaarli-485b168a9677d160b0c0426e4f282b9bd0c632c1.zip
Process picwall rendering through Slim controller + UT
Diffstat (limited to 'tests/front/controller')
-rw-r--r--tests/front/controller/PictureWallControllerTest.php180
1 files changed, 180 insertions, 0 deletions
diff --git a/tests/front/controller/PictureWallControllerTest.php b/tests/front/controller/PictureWallControllerTest.php
new file mode 100644
index 00000000..63802abd
--- /dev/null
+++ b/tests/front/controller/PictureWallControllerTest.php
@@ -0,0 +1,180 @@
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\Front\Exception\ThumbnailsDisabledException;
16use Shaarli\Plugin\PluginManager;
17use Shaarli\Render\PageBuilder;
18use Shaarli\Security\LoginManager;
19use Shaarli\Thumbnailer;
20use Slim\Http\Request;
21use Slim\Http\Response;
22
23class 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}