aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateFormTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-11-12 13:02:36 +0100
committerArthurHoaro <arthur@hoa.ro>2020-11-12 13:02:36 +0100
commit1409f1c89a7ca01456ae2dcd6357d296e2b99f5a (patch)
treeffa30a9358e82d27be75d8fc5e57f3c8820dc6d3 /tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateFormTest.php
parent054e03f37fa29da8066f1a637919f13c7e7dc5d2 (diff)
parenta6935feb22df8d9634189ee87d257da9f03eedbd (diff)
downloadShaarli-v0.12.tar.gz
Shaarli-v0.12.tar.zst
Shaarli-v0.12.zip
Merge branch 'master' into v0.12v0.12.1v0.12
Diffstat (limited to 'tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateFormTest.php')
-rw-r--r--tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateFormTest.php367
1 files changed, 367 insertions, 0 deletions
diff --git a/tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateFormTest.php b/tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateFormTest.php
new file mode 100644
index 00000000..964773da
--- /dev/null
+++ b/tests/front/controller/admin/ShaarePublishControllerTest/DisplayCreateFormTest.php
@@ -0,0 +1,367 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin\ShaarePublishControllerTest;
6
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper;
10use Shaarli\Front\Controller\Admin\ShaarePublishController;
11use Shaarli\Http\HttpAccess;
12use Shaarli\Http\MetadataRetriever;
13use Shaarli\TestCase;
14use Slim\Http\Request;
15use Slim\Http\Response;
16
17class DisplayCreateFormTest extends TestCase
18{
19 use FrontAdminControllerMockHelper;
20
21 /** @var ShaarePublishController */
22 protected $controller;
23
24 public function setUp(): void
25 {
26 $this->createContainer();
27
28 $this->container->httpAccess = $this->createMock(HttpAccess::class);
29 $this->container->metadataRetriever = $this->createMock(MetadataRetriever::class);
30 $this->controller = new ShaarePublishController($this->container);
31 }
32
33 /**
34 * Test displaying bookmark create form
35 * Ensure that every step of the standard workflow works properly.
36 */
37 public function testDisplayCreateFormWithUrlAndWithMetadataRetrieval(): void
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
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 ]);
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
81 ->expects(static::atLeastOnce())
82 ->method('executeHooks')
83 ->withConsecutive(['render_editlink'], ['render_includes'])
84 ->willReturnCallback(function (string $hook, array $data) use ($remoteTitle, $remoteDesc): array {
85 if ('render_editlink' === $hook) {
86 static::assertSame($remoteTitle, $data['link']['title']);
87 static::assertSame($remoteDesc, $data['link']['description']);
88 }
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);
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::atLeastOnce())
148 ->method('executeHooks')
149 ->withConsecutive(['render_editlink'], ['render_includes'])
150 ->willReturnCallback(function (string $hook, array $data): array {
151 if ('render_editlink' === $hook) {
152 static::assertSame('', $data['link']['title']);
153 static::assertSame('', $data['link']['description']);
154 }
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);
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.',
195 'tags' => 'abc@def',
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']);
219 static::assertSame($parameters['tags'] . '@', $assignedVariables['link']['tags']);
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']);
363 static::assertSame(implode('@', $tags) . '@', $assignedVariables['link']['tags']);
364 static::assertTrue($assignedVariables['link']['private']);
365 static::assertSame($createdAt, $assignedVariables['link']['created']);
366 }
367}