3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin\ShaarePublishControllerTest
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Config\ConfigManager
;
9 use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper
;
10 use Shaarli\Front\Controller\Admin\ShaarePublishController
;
11 use Shaarli\Http\HttpAccess
;
12 use Shaarli\Http\MetadataRetriever
;
14 use Slim\Http\Request
;
15 use Slim\Http\Response
;
17 class DisplayCreateFormTest
extends TestCase
19 use FrontAdminControllerMockHelper
;
21 /** @var ShaarePublishController */
22 protected $controller;
24 public function setUp(): void
26 $this->createContainer();
28 $this->container
->httpAccess
= $this->createMock(HttpAccess
::class);
29 $this->container
->metadataRetriever
= $this->createMock(MetadataRetriever
::class);
30 $this->controller
= new ShaarePublishController($this->container
);
34 * Test displaying bookmark create form
35 * Ensure that every step of the standard workflow works properly.
37 public function testDisplayCreateFormWithUrlAndWithMetadataRetrieval(): void
39 $this->container
->environment
= [
40 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc'
43 $assignedVariables = [];
44 $this->assignTemplateVars($assignedVariables);
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';
52 $request = $this->createMock(Request
::class);
53 $request->method('getParam')->willReturnCallback(function (string $key) use ($url): ?string {
54 return $key === 'post' ? $url : null;
56 $response = new Response();
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') {
67 $this->container
->metadataRetriever
->expects(static::once())->method('retrieve')->willReturn([
68 'title' => $remoteTitle,
69 'description' => $remoteDesc,
70 'tags' => $remoteTags,
73 $this->container
->bookmarkService
74 ->expects(static::once())
75 ->method('bookmarksCountPerTag')
76 ->willReturn($tags = ['tag1' => 2, 'tag2' => 1])
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']);
94 $result = $this->controller
->displayCreateForm($request, $response);
96 static::assertSame(200, $result->getStatusCode());
97 static::assertSame('editlink', (string) $result->getBody());
99 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
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']);
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);
117 * Test displaying bookmark create form without any external metadata retrieval attempt
119 public function testDisplayCreateFormWithUrlAndWithoutMetadata(): void
121 $this->container
->environment
= [
122 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc'
125 $assignedVariables = [];
126 $this->assignTemplateVars($assignedVariables);
128 $url = 'http://url.tld/other?part=3&utm_ad=pay#hash';
129 $expectedUrl = str_replace('&utm_ad=pay', '', $url);
131 $request = $this->createMock(Request
::class);
132 $request->method('getParam')->willReturnCallback(function (string $key) use ($url): ?string {
133 return $key === 'post' ? $url : null;
135 $response = new Response();
137 $this->container
->metadataRetriever
->expects(static::never())->method('retrieve');
139 $this->container
->bookmarkService
140 ->expects(static::once())
141 ->method('bookmarksCountPerTag')
142 ->willReturn($tags = ['tag1' => 2, 'tag2' => 1])
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']);
160 $result = $this->controller
->displayCreateForm($request, $response);
162 static::assertSame(200, $result->getStatusCode());
163 static::assertSame('editlink', (string) $result->getBody());
165 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
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']);
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);
183 * Test displaying bookmark create form
184 * Ensure all available query parameters are handled properly.
186 public function testDisplayCreateFormWithFullParameters(): void
188 $assignedVariables = [];
189 $this->assignTemplateVars($assignedVariables);
192 'post' => 'http://url.tld/other?part=3&utm_ad=pay#hash',
193 'title' => 'Provided Title',
194 'description' => 'Provided description.',
199 $expectedUrl = str_replace('&utm_ad=pay', '', $parameters['post']);
201 $request = $this->createMock(Request
::class);
204 ->willReturnCallback(function (string $key) use ($parameters): ?string {
205 return $parameters[$key] ?? null;
207 $response = new Response();
209 $result = $this->controller
->displayCreateForm($request, $response);
211 static::assertSame(200, $result->getStatusCode());
212 static::assertSame('editlink', (string) $result->getBody());
214 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
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']);
226 * Test displaying bookmark create form
227 * Without any parameter.
229 public function testDisplayCreateFormEmpty(): void
231 $assignedVariables = [];
232 $this->assignTemplateVars($assignedVariables);
234 $request = $this->createMock(Request
::class);
235 $response = new Response();
237 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
238 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
240 $result = $this->controller
->displayCreateForm($request, $response);
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']);
253 * Test displaying bookmark create form
254 * URL not using HTTP protocol: do not try to retrieve the title
256 public function testDisplayCreateFormNotHttp(): void
258 $assignedVariables = [];
259 $this->assignTemplateVars($assignedVariables);
261 $url = 'magnet://kubuntu.torrent';
262 $request = $this->createMock(Request
::class);
265 ->willReturnCallback(function (string $key) use ($url): ?string {
266 return $key === 'post' ? $url : null;
268 $response = new Response();
270 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
271 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
273 $result = $this->controller
->displayCreateForm($request, $response);
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']);
282 * Test displaying bookmark create form
283 * When markdown formatter is enabled, the no markdown tag should be added to existing tags.
285 public function testDisplayCreateFormWithMarkdownEnabled(): void
287 $assignedVariables = [];
288 $this->assignTemplateVars($assignedVariables);
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') {
302 $request = $this->createMock(Request
::class);
303 $response = new Response();
305 $result = $this->controller
->displayCreateForm($request, $response);
307 static::assertSame(200, $result->getStatusCode());
308 static::assertSame('editlink', (string) $result->getBody());
309 static::assertSame(['nomarkdown' => 1], $assignedVariables['tags']);
313 * Test displaying bookmark create form
314 * When an existing URL is submitted, we want to edit the existing link.
316 public function testDisplayCreateFormWithExistingUrl(): void
318 $assignedVariables = [];
319 $this->assignTemplateVars($assignedVariables);
321 $url = 'http://url.tld/other?part=3&utm_ad=pay#hash';
322 $expectedUrl = str_replace('&utm_ad=pay', '', $url);
324 $request = $this->createMock(Request
::class);
327 ->willReturnCallback(function (string $key) use ($url): ?string {
328 return $key === 'post' ? $url : null;
330 $response = new Response();
332 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
333 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
335 $this->container
->bookmarkService
336 ->expects(static::once())
337 ->method('findByUrl')
342 ->setUrl($expectedUrl)
343 ->setTitle($title = 'Bookmark Title')
344 ->setDescription($description = 'Bookmark description.')
345 ->setTags($tags = ['abc', 'def'])
347 ->setCreated($createdAt = new \
DateTime('2020-06-10 18:45:44'))
351 $result = $this->controller
->displayCreateForm($request, $response);
353 static::assertSame(200, $result->getStatusCode());
354 static::assertSame('editlink', (string) $result->getBody());
356 static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']);
357 static::assertFalse($assignedVariables['link_is_new']);
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']);