]>
Commit | Line | Data |
---|---|---|
baa69791 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest; | |
6 | ||
baa69791 A |
7 | use Shaarli\Bookmark\Bookmark; |
8 | use Shaarli\Config\ConfigManager; | |
9 | use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; | |
10 | use Shaarli\Front\Controller\Admin\ManageShaareController; | |
11 | use Shaarli\Http\HttpAccess; | |
4cf3564d | 12 | use Shaarli\Http\MetadataRetriever; |
a5a9cf23 | 13 | use Shaarli\TestCase; |
baa69791 A |
14 | use Slim\Http\Request; |
15 | use Slim\Http\Response; | |
16 | ||
17 | class DisplayCreateFormTest extends TestCase | |
18 | { | |
19 | use FrontAdminControllerMockHelper; | |
20 | ||
21 | /** @var ManageShaareController */ | |
22 | protected $controller; | |
23 | ||
24 | public function setUp(): void | |
25 | { | |
26 | $this->createContainer(); | |
27 | ||
28 | $this->container->httpAccess = $this->createMock(HttpAccess::class); | |
4cf3564d | 29 | $this->container->metadataRetriever = $this->createMock(MetadataRetriever::class); |
baa69791 A |
30 | $this->controller = new ManageShaareController($this->container); |
31 | } | |
32 | ||
33 | /** | |
34 | * Test displaying bookmark create form | |
35 | * Ensure that every step of the standard workflow works properly. | |
36 | */ | |
4cf3564d | 37 | public function testDisplayCreateFormWithUrlAndWithMetadataRetrieval(): void |
baa69791 A |
38 | { |
39 | $this->container->environment = [ | |
40 | 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc' | |
41 | ]; | |
42 | ||
43 | $assignedVariables = []; | |
44 | $this->assignTemplateVars($assignedVariables); | |
45 | ||
46 | $url = 'http://url.tld/other?part=3&utm_ad=pay#hash'; | |
47 | $expectedUrl = str_replace('&utm_ad=pay', '', $url); | |
48 | $remoteTitle = 'Remote Title'; | |
49 | $remoteDesc = 'Sometimes the meta description is relevant.'; | |
50 | $remoteTags = 'abc def'; | |
51 | ||
52 | $request = $this->createMock(Request::class); | |
53 | $request->method('getParam')->willReturnCallback(function (string $key) use ($url): ?string { | |
54 | return $key === 'post' ? $url : null; | |
55 | }); | |
56 | $response = new Response(); | |
57 | ||
4cf3564d A |
58 | $this->container->conf = $this->createMock(ConfigManager::class); |
59 | $this->container->conf->method('get')->willReturnCallback(function (string $param, $default) { | |
60 | if ($param === 'general.enable_async_metadata') { | |
61 | return false; | |
62 | } | |
63 | ||
64 | return $default; | |
65 | }); | |
66 | ||
67 | $this->container->metadataRetriever->expects(static::once())->method('retrieve')->willReturn([ | |
68 | 'title' => $remoteTitle, | |
69 | 'description' => $remoteDesc, | |
70 | 'tags' => $remoteTags, | |
71 | ]); | |
baa69791 A |
72 | |
73 | $this->container->bookmarkService | |
74 | ->expects(static::once()) | |
75 | ->method('bookmarksCountPerTag') | |
76 | ->willReturn($tags = ['tag1' => 2, 'tag2' => 1]) | |
77 | ; | |
78 | ||
79 | // Make sure that PluginManager hook is triggered | |
80 | $this->container->pluginManager | |
a5a9cf23 | 81 | ->expects(static::atLeastOnce()) |
baa69791 | 82 | ->method('executeHooks') |
a5a9cf23 | 83 | ->withConsecutive(['render_editlink'], ['render_includes']) |
baa69791 | 84 | ->willReturnCallback(function (string $hook, array $data) use ($remoteTitle, $remoteDesc): array { |
a5a9cf23 A |
85 | if ('render_editlink' === $hook) { |
86 | static::assertSame($remoteTitle, $data['link']['title']); | |
87 | static::assertSame($remoteDesc, $data['link']['description']); | |
88 | } | |
baa69791 A |
89 | |
90 | return $data; | |
91 | }) | |
92 | ; | |
93 | ||
94 | $result = $this->controller->displayCreateForm($request, $response); | |
95 | ||
96 | static::assertSame(200, $result->getStatusCode()); | |
97 | static::assertSame('editlink', (string) $result->getBody()); | |
98 | ||
99 | static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']); | |
100 | ||
101 | static::assertSame($expectedUrl, $assignedVariables['link']['url']); | |
102 | static::assertSame($remoteTitle, $assignedVariables['link']['title']); | |
103 | static::assertSame($remoteDesc, $assignedVariables['link']['description']); | |
104 | static::assertSame($remoteTags, $assignedVariables['link']['tags']); | |
105 | static::assertFalse($assignedVariables['link']['private']); | |
106 | ||
107 | static::assertTrue($assignedVariables['link_is_new']); | |
108 | static::assertSame($referer, $assignedVariables['http_referer']); | |
109 | static::assertSame($tags, $assignedVariables['tags']); | |
110 | static::assertArrayHasKey('source', $assignedVariables); | |
111 | static::assertArrayHasKey('default_private_links', $assignedVariables); | |
4cf3564d A |
112 | static::assertArrayHasKey('async_metadata', $assignedVariables); |
113 | static::assertArrayHasKey('retrieve_description', $assignedVariables); | |
114 | } | |
115 | ||
116 | /** | |
117 | * Test displaying bookmark create form without any external metadata retrieval attempt | |
118 | */ | |
119 | public function testDisplayCreateFormWithUrlAndWithoutMetadata(): void | |
120 | { | |
121 | $this->container->environment = [ | |
122 | 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc' | |
123 | ]; | |
124 | ||
125 | $assignedVariables = []; | |
126 | $this->assignTemplateVars($assignedVariables); | |
127 | ||
128 | $url = 'http://url.tld/other?part=3&utm_ad=pay#hash'; | |
129 | $expectedUrl = str_replace('&utm_ad=pay', '', $url); | |
130 | ||
131 | $request = $this->createMock(Request::class); | |
132 | $request->method('getParam')->willReturnCallback(function (string $key) use ($url): ?string { | |
133 | return $key === 'post' ? $url : null; | |
134 | }); | |
135 | $response = new Response(); | |
136 | ||
137 | $this->container->metadataRetriever->expects(static::never())->method('retrieve'); | |
138 | ||
139 | $this->container->bookmarkService | |
140 | ->expects(static::once()) | |
141 | ->method('bookmarksCountPerTag') | |
142 | ->willReturn($tags = ['tag1' => 2, 'tag2' => 1]) | |
143 | ; | |
144 | ||
145 | // Make sure that PluginManager hook is triggered | |
146 | $this->container->pluginManager | |
147 | ->expects(static::at(0)) | |
148 | ->method('executeHooks') | |
149 | ->willReturnCallback(function (string $hook, array $data): array { | |
150 | static::assertSame('render_editlink', $hook); | |
151 | static::assertSame('', $data['link']['title']); | |
152 | static::assertSame('', $data['link']['description']); | |
153 | ||
154 | return $data; | |
155 | }) | |
156 | ; | |
157 | ||
158 | $result = $this->controller->displayCreateForm($request, $response); | |
159 | ||
160 | static::assertSame(200, $result->getStatusCode()); | |
161 | static::assertSame('editlink', (string) $result->getBody()); | |
162 | ||
163 | static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']); | |
164 | ||
165 | static::assertSame($expectedUrl, $assignedVariables['link']['url']); | |
166 | static::assertSame('', $assignedVariables['link']['title']); | |
167 | static::assertSame('', $assignedVariables['link']['description']); | |
168 | static::assertSame('', $assignedVariables['link']['tags']); | |
169 | static::assertFalse($assignedVariables['link']['private']); | |
170 | ||
171 | static::assertTrue($assignedVariables['link_is_new']); | |
172 | static::assertSame($referer, $assignedVariables['http_referer']); | |
173 | static::assertSame($tags, $assignedVariables['tags']); | |
174 | static::assertArrayHasKey('source', $assignedVariables); | |
175 | static::assertArrayHasKey('default_private_links', $assignedVariables); | |
176 | static::assertArrayHasKey('async_metadata', $assignedVariables); | |
177 | static::assertArrayHasKey('retrieve_description', $assignedVariables); | |
baa69791 A |
178 | } |
179 | ||
180 | /** | |
181 | * Test displaying bookmark create form | |
182 | * Ensure all available query parameters are handled properly. | |
183 | */ | |
184 | public function testDisplayCreateFormWithFullParameters(): void | |
185 | { | |
186 | $assignedVariables = []; | |
187 | $this->assignTemplateVars($assignedVariables); | |
188 | ||
189 | $parameters = [ | |
190 | 'post' => 'http://url.tld/other?part=3&utm_ad=pay#hash', | |
191 | 'title' => 'Provided Title', | |
192 | 'description' => 'Provided description.', | |
193 | 'tags' => 'abc def', | |
194 | 'private' => '1', | |
195 | 'source' => 'apps', | |
196 | ]; | |
197 | $expectedUrl = str_replace('&utm_ad=pay', '', $parameters['post']); | |
198 | ||
199 | $request = $this->createMock(Request::class); | |
200 | $request | |
201 | ->method('getParam') | |
202 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | |
203 | return $parameters[$key] ?? null; | |
204 | }); | |
205 | $response = new Response(); | |
206 | ||
207 | $result = $this->controller->displayCreateForm($request, $response); | |
208 | ||
209 | static::assertSame(200, $result->getStatusCode()); | |
210 | static::assertSame('editlink', (string) $result->getBody()); | |
211 | ||
212 | static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']); | |
213 | ||
214 | static::assertSame($expectedUrl, $assignedVariables['link']['url']); | |
215 | static::assertSame($parameters['title'], $assignedVariables['link']['title']); | |
216 | static::assertSame($parameters['description'], $assignedVariables['link']['description']); | |
217 | static::assertSame($parameters['tags'], $assignedVariables['link']['tags']); | |
218 | static::assertTrue($assignedVariables['link']['private']); | |
219 | static::assertTrue($assignedVariables['link_is_new']); | |
220 | static::assertSame($parameters['source'], $assignedVariables['source']); | |
221 | } | |
222 | ||
223 | /** | |
224 | * Test displaying bookmark create form | |
225 | * Without any parameter. | |
226 | */ | |
227 | public function testDisplayCreateFormEmpty(): void | |
228 | { | |
229 | $assignedVariables = []; | |
230 | $this->assignTemplateVars($assignedVariables); | |
231 | ||
232 | $request = $this->createMock(Request::class); | |
233 | $response = new Response(); | |
234 | ||
235 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | |
236 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | |
237 | ||
238 | $result = $this->controller->displayCreateForm($request, $response); | |
239 | ||
240 | static::assertSame(200, $result->getStatusCode()); | |
241 | static::assertSame('editlink', (string) $result->getBody()); | |
242 | static::assertSame('', $assignedVariables['link']['url']); | |
243 | static::assertSame('Note: ', $assignedVariables['link']['title']); | |
244 | static::assertSame('', $assignedVariables['link']['description']); | |
245 | static::assertSame('', $assignedVariables['link']['tags']); | |
246 | static::assertFalse($assignedVariables['link']['private']); | |
247 | static::assertTrue($assignedVariables['link_is_new']); | |
248 | } | |
249 | ||
250 | /** | |
251 | * Test displaying bookmark create form | |
252 | * URL not using HTTP protocol: do not try to retrieve the title | |
253 | */ | |
254 | public function testDisplayCreateFormNotHttp(): void | |
255 | { | |
256 | $assignedVariables = []; | |
257 | $this->assignTemplateVars($assignedVariables); | |
258 | ||
259 | $url = 'magnet://kubuntu.torrent'; | |
260 | $request = $this->createMock(Request::class); | |
261 | $request | |
262 | ->method('getParam') | |
263 | ->willReturnCallback(function (string $key) use ($url): ?string { | |
264 | return $key === 'post' ? $url : null; | |
265 | }); | |
266 | $response = new Response(); | |
267 | ||
268 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | |
269 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | |
270 | ||
271 | $result = $this->controller->displayCreateForm($request, $response); | |
272 | ||
273 | static::assertSame(200, $result->getStatusCode()); | |
274 | static::assertSame('editlink', (string) $result->getBody()); | |
275 | static::assertSame($url, $assignedVariables['link']['url']); | |
276 | static::assertTrue($assignedVariables['link_is_new']); | |
277 | } | |
278 | ||
279 | /** | |
280 | * Test displaying bookmark create form | |
281 | * When markdown formatter is enabled, the no markdown tag should be added to existing tags. | |
282 | */ | |
283 | public function testDisplayCreateFormWithMarkdownEnabled(): void | |
284 | { | |
285 | $assignedVariables = []; | |
286 | $this->assignTemplateVars($assignedVariables); | |
287 | ||
288 | $this->container->conf = $this->createMock(ConfigManager::class); | |
289 | $this->container->conf | |
290 | ->expects(static::atLeastOnce()) | |
291 | ->method('get')->willReturnCallback(function (string $key): ?string { | |
292 | if ($key === 'formatter') { | |
293 | return 'markdown'; | |
294 | } | |
295 | ||
296 | return $key; | |
297 | }) | |
298 | ; | |
299 | ||
300 | $request = $this->createMock(Request::class); | |
301 | $response = new Response(); | |
302 | ||
303 | $result = $this->controller->displayCreateForm($request, $response); | |
304 | ||
305 | static::assertSame(200, $result->getStatusCode()); | |
306 | static::assertSame('editlink', (string) $result->getBody()); | |
307 | static::assertSame(['nomarkdown' => 1], $assignedVariables['tags']); | |
308 | } | |
309 | ||
310 | /** | |
311 | * Test displaying bookmark create form | |
312 | * When an existing URL is submitted, we want to edit the existing link. | |
313 | */ | |
314 | public function testDisplayCreateFormWithExistingUrl(): void | |
315 | { | |
316 | $assignedVariables = []; | |
317 | $this->assignTemplateVars($assignedVariables); | |
318 | ||
319 | $url = 'http://url.tld/other?part=3&utm_ad=pay#hash'; | |
320 | $expectedUrl = str_replace('&utm_ad=pay', '', $url); | |
321 | ||
322 | $request = $this->createMock(Request::class); | |
323 | $request | |
324 | ->method('getParam') | |
325 | ->willReturnCallback(function (string $key) use ($url): ?string { | |
326 | return $key === 'post' ? $url : null; | |
327 | }); | |
328 | $response = new Response(); | |
329 | ||
330 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | |
331 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | |
332 | ||
333 | $this->container->bookmarkService | |
334 | ->expects(static::once()) | |
335 | ->method('findByUrl') | |
336 | ->with($expectedUrl) | |
337 | ->willReturn( | |
338 | (new Bookmark()) | |
339 | ->setId($id = 23) | |
340 | ->setUrl($expectedUrl) | |
341 | ->setTitle($title = 'Bookmark Title') | |
342 | ->setDescription($description = 'Bookmark description.') | |
343 | ->setTags($tags = ['abc', 'def']) | |
344 | ->setPrivate(true) | |
345 | ->setCreated($createdAt = new \DateTime('2020-06-10 18:45:44')) | |
346 | ) | |
347 | ; | |
348 | ||
349 | $result = $this->controller->displayCreateForm($request, $response); | |
350 | ||
351 | static::assertSame(200, $result->getStatusCode()); | |
352 | static::assertSame('editlink', (string) $result->getBody()); | |
353 | ||
354 | static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']); | |
355 | static::assertFalse($assignedVariables['link_is_new']); | |
356 | ||
357 | static::assertSame($id, $assignedVariables['link']['id']); | |
358 | static::assertSame($expectedUrl, $assignedVariables['link']['url']); | |
359 | static::assertSame($title, $assignedVariables['link']['title']); | |
360 | static::assertSame($description, $assignedVariables['link']['description']); | |
361 | static::assertSame(implode(' ', $tags), $assignedVariables['link']['tags']); | |
362 | static::assertTrue($assignedVariables['link']['private']); | |
363 | static::assertSame($createdAt, $assignedVariables['link']['created']); | |
364 | } | |
365 | } |