]>
Commit | Line | Data |
---|---|---|
baa69791 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5d8de758 | 5 | namespace Shaarli\Front\Controller\Admin\ShaarePublishControllerTest; |
baa69791 | 6 | |
baa69791 A |
7 | use Shaarli\Bookmark\Bookmark; |
8 | use Shaarli\Config\ConfigManager; | |
9 | use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; | |
5d8de758 | 10 | use Shaarli\Front\Controller\Admin\ShaarePublishController; |
baa69791 | 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 | ||
5d8de758 | 21 | /** @var ShaarePublishController */ |
baa69791 A |
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); |
5d8de758 | 30 | $this->controller = new ShaarePublishController($this->container); |
baa69791 A |
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']); | |
b3bd8c3e | 104 | static::assertSame($remoteTags . ' ', $assignedVariables['link']['tags']); |
baa69791 A |
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 | |
21e72da9 | 147 | ->expects(static::atLeastOnce()) |
4cf3564d | 148 | ->method('executeHooks') |
21e72da9 | 149 | ->withConsecutive(['render_editlink'], ['render_includes']) |
4cf3564d | 150 | ->willReturnCallback(function (string $hook, array $data): array { |
21e72da9 A |
151 | if ('render_editlink' === $hook) { |
152 | static::assertSame('', $data['link']['title']); | |
153 | static::assertSame('', $data['link']['description']); | |
154 | } | |
4cf3564d A |
155 | |
156 | return $data; | |
157 | }) | |
158 | ; | |
159 | ||
160 | $result = $this->controller->displayCreateForm($request, $response); | |
161 | ||
162 | static::assertSame(200, $result->getStatusCode()); | |
163 | static::assertSame('editlink', (string) $result->getBody()); | |
164 | ||
165 | static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']); | |
166 | ||
167 | static::assertSame($expectedUrl, $assignedVariables['link']['url']); | |
168 | static::assertSame('', $assignedVariables['link']['title']); | |
169 | static::assertSame('', $assignedVariables['link']['description']); | |
170 | static::assertSame('', $assignedVariables['link']['tags']); | |
171 | static::assertFalse($assignedVariables['link']['private']); | |
172 | ||
173 | static::assertTrue($assignedVariables['link_is_new']); | |
174 | static::assertSame($referer, $assignedVariables['http_referer']); | |
175 | static::assertSame($tags, $assignedVariables['tags']); | |
176 | static::assertArrayHasKey('source', $assignedVariables); | |
177 | static::assertArrayHasKey('default_private_links', $assignedVariables); | |
178 | static::assertArrayHasKey('async_metadata', $assignedVariables); | |
179 | static::assertArrayHasKey('retrieve_description', $assignedVariables); | |
baa69791 A |
180 | } |
181 | ||
182 | /** | |
183 | * Test displaying bookmark create form | |
184 | * Ensure all available query parameters are handled properly. | |
185 | */ | |
186 | public function testDisplayCreateFormWithFullParameters(): void | |
187 | { | |
188 | $assignedVariables = []; | |
189 | $this->assignTemplateVars($assignedVariables); | |
190 | ||
191 | $parameters = [ | |
192 | 'post' => 'http://url.tld/other?part=3&utm_ad=pay#hash', | |
193 | 'title' => 'Provided Title', | |
194 | 'description' => 'Provided description.', | |
b3bd8c3e | 195 | 'tags' => 'abc@def', |
baa69791 A |
196 | 'private' => '1', |
197 | 'source' => 'apps', | |
198 | ]; | |
199 | $expectedUrl = str_replace('&utm_ad=pay', '', $parameters['post']); | |
200 | ||
201 | $request = $this->createMock(Request::class); | |
202 | $request | |
203 | ->method('getParam') | |
204 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | |
205 | return $parameters[$key] ?? null; | |
206 | }); | |
207 | $response = new Response(); | |
208 | ||
209 | $result = $this->controller->displayCreateForm($request, $response); | |
210 | ||
211 | static::assertSame(200, $result->getStatusCode()); | |
212 | static::assertSame('editlink', (string) $result->getBody()); | |
213 | ||
214 | static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']); | |
215 | ||
216 | static::assertSame($expectedUrl, $assignedVariables['link']['url']); | |
217 | static::assertSame($parameters['title'], $assignedVariables['link']['title']); | |
218 | static::assertSame($parameters['description'], $assignedVariables['link']['description']); | |
b3bd8c3e | 219 | static::assertSame($parameters['tags'] . '@', $assignedVariables['link']['tags']); |
baa69791 A |
220 | static::assertTrue($assignedVariables['link']['private']); |
221 | static::assertTrue($assignedVariables['link_is_new']); | |
222 | static::assertSame($parameters['source'], $assignedVariables['source']); | |
223 | } | |
224 | ||
225 | /** | |
226 | * Test displaying bookmark create form | |
227 | * Without any parameter. | |
228 | */ | |
229 | public function testDisplayCreateFormEmpty(): void | |
230 | { | |
231 | $assignedVariables = []; | |
232 | $this->assignTemplateVars($assignedVariables); | |
233 | ||
234 | $request = $this->createMock(Request::class); | |
235 | $response = new Response(); | |
236 | ||
237 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | |
238 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | |
239 | ||
240 | $result = $this->controller->displayCreateForm($request, $response); | |
241 | ||
242 | static::assertSame(200, $result->getStatusCode()); | |
243 | static::assertSame('editlink', (string) $result->getBody()); | |
244 | static::assertSame('', $assignedVariables['link']['url']); | |
245 | static::assertSame('Note: ', $assignedVariables['link']['title']); | |
246 | static::assertSame('', $assignedVariables['link']['description']); | |
247 | static::assertSame('', $assignedVariables['link']['tags']); | |
248 | static::assertFalse($assignedVariables['link']['private']); | |
249 | static::assertTrue($assignedVariables['link_is_new']); | |
250 | } | |
251 | ||
252 | /** | |
253 | * Test displaying bookmark create form | |
254 | * URL not using HTTP protocol: do not try to retrieve the title | |
255 | */ | |
256 | public function testDisplayCreateFormNotHttp(): void | |
257 | { | |
258 | $assignedVariables = []; | |
259 | $this->assignTemplateVars($assignedVariables); | |
260 | ||
261 | $url = 'magnet://kubuntu.torrent'; | |
262 | $request = $this->createMock(Request::class); | |
263 | $request | |
264 | ->method('getParam') | |
265 | ->willReturnCallback(function (string $key) use ($url): ?string { | |
266 | return $key === 'post' ? $url : null; | |
267 | }); | |
268 | $response = new Response(); | |
269 | ||
270 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | |
271 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | |
272 | ||
273 | $result = $this->controller->displayCreateForm($request, $response); | |
274 | ||
275 | static::assertSame(200, $result->getStatusCode()); | |
276 | static::assertSame('editlink', (string) $result->getBody()); | |
277 | static::assertSame($url, $assignedVariables['link']['url']); | |
278 | static::assertTrue($assignedVariables['link_is_new']); | |
279 | } | |
280 | ||
281 | /** | |
282 | * Test displaying bookmark create form | |
283 | * When markdown formatter is enabled, the no markdown tag should be added to existing tags. | |
284 | */ | |
285 | public function testDisplayCreateFormWithMarkdownEnabled(): void | |
286 | { | |
287 | $assignedVariables = []; | |
288 | $this->assignTemplateVars($assignedVariables); | |
289 | ||
290 | $this->container->conf = $this->createMock(ConfigManager::class); | |
291 | $this->container->conf | |
292 | ->expects(static::atLeastOnce()) | |
293 | ->method('get')->willReturnCallback(function (string $key): ?string { | |
294 | if ($key === 'formatter') { | |
295 | return 'markdown'; | |
296 | } | |
297 | ||
298 | return $key; | |
299 | }) | |
300 | ; | |
301 | ||
302 | $request = $this->createMock(Request::class); | |
303 | $response = new Response(); | |
304 | ||
305 | $result = $this->controller->displayCreateForm($request, $response); | |
306 | ||
307 | static::assertSame(200, $result->getStatusCode()); | |
308 | static::assertSame('editlink', (string) $result->getBody()); | |
309 | static::assertSame(['nomarkdown' => 1], $assignedVariables['tags']); | |
310 | } | |
311 | ||
312 | /** | |
313 | * Test displaying bookmark create form | |
314 | * When an existing URL is submitted, we want to edit the existing link. | |
315 | */ | |
316 | public function testDisplayCreateFormWithExistingUrl(): void | |
317 | { | |
318 | $assignedVariables = []; | |
319 | $this->assignTemplateVars($assignedVariables); | |
320 | ||
321 | $url = 'http://url.tld/other?part=3&utm_ad=pay#hash'; | |
322 | $expectedUrl = str_replace('&utm_ad=pay', '', $url); | |
323 | ||
324 | $request = $this->createMock(Request::class); | |
325 | $request | |
326 | ->method('getParam') | |
327 | ->willReturnCallback(function (string $key) use ($url): ?string { | |
328 | return $key === 'post' ? $url : null; | |
329 | }); | |
330 | $response = new Response(); | |
331 | ||
332 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | |
333 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | |
334 | ||
335 | $this->container->bookmarkService | |
336 | ->expects(static::once()) | |
337 | ->method('findByUrl') | |
338 | ->with($expectedUrl) | |
339 | ->willReturn( | |
340 | (new Bookmark()) | |
341 | ->setId($id = 23) | |
342 | ->setUrl($expectedUrl) | |
343 | ->setTitle($title = 'Bookmark Title') | |
344 | ->setDescription($description = 'Bookmark description.') | |
345 | ->setTags($tags = ['abc', 'def']) | |
346 | ->setPrivate(true) | |
347 | ->setCreated($createdAt = new \DateTime('2020-06-10 18:45:44')) | |
348 | ) | |
349 | ; | |
350 | ||
351 | $result = $this->controller->displayCreateForm($request, $response); | |
352 | ||
353 | static::assertSame(200, $result->getStatusCode()); | |
354 | static::assertSame('editlink', (string) $result->getBody()); | |
355 | ||
356 | static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']); | |
357 | static::assertFalse($assignedVariables['link_is_new']); | |
358 | ||
359 | static::assertSame($id, $assignedVariables['link']['id']); | |
360 | static::assertSame($expectedUrl, $assignedVariables['link']['url']); | |
361 | static::assertSame($title, $assignedVariables['link']['title']); | |
362 | static::assertSame($description, $assignedVariables['link']['description']); | |
b3bd8c3e | 363 | static::assertSame(implode('@', $tags) . '@', $assignedVariables['link']['tags']); |
baa69791 A |
364 | static::assertTrue($assignedVariables['link']['private']); |
365 | static::assertSame($createdAt, $assignedVariables['link']['created']); | |
366 | } | |
367 | } |