3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest
;
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
;
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 ManageShaareController */
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 ManageShaareController($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::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']);
158 $result = $this->controller
->displayCreateForm($request, $response);
160 static::assertSame(200, $result->getStatusCode());
161 static::assertSame('editlink', (string) $result->getBody());
163 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
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']);
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);
181 * Test displaying bookmark create form
182 * Ensure all available query parameters are handled properly.
184 public function testDisplayCreateFormWithFullParameters(): void
186 $assignedVariables = [];
187 $this->assignTemplateVars($assignedVariables);
190 'post' => 'http://url.tld/other?part=3&utm_ad=pay#hash',
191 'title' => 'Provided Title',
192 'description' => 'Provided description.',
197 $expectedUrl = str_replace('&utm_ad=pay', '', $parameters['post']);
199 $request = $this->createMock(Request
::class);
202 ->willReturnCallback(function (string $key) use ($parameters): ?string {
203 return $parameters[$key] ?? null;
205 $response = new Response();
207 $result = $this->controller
->displayCreateForm($request, $response);
209 static::assertSame(200, $result->getStatusCode());
210 static::assertSame('editlink', (string) $result->getBody());
212 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
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']);
224 * Test displaying bookmark create form
225 * Without any parameter.
227 public function testDisplayCreateFormEmpty(): void
229 $assignedVariables = [];
230 $this->assignTemplateVars($assignedVariables);
232 $request = $this->createMock(Request
::class);
233 $response = new Response();
235 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
236 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
238 $result = $this->controller
->displayCreateForm($request, $response);
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']);
251 * Test displaying bookmark create form
252 * URL not using HTTP protocol: do not try to retrieve the title
254 public function testDisplayCreateFormNotHttp(): void
256 $assignedVariables = [];
257 $this->assignTemplateVars($assignedVariables);
259 $url = 'magnet://kubuntu.torrent';
260 $request = $this->createMock(Request
::class);
263 ->willReturnCallback(function (string $key) use ($url): ?string {
264 return $key === 'post' ? $url : null;
266 $response = new Response();
268 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
269 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
271 $result = $this->controller
->displayCreateForm($request, $response);
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']);
280 * Test displaying bookmark create form
281 * When markdown formatter is enabled, the no markdown tag should be added to existing tags.
283 public function testDisplayCreateFormWithMarkdownEnabled(): void
285 $assignedVariables = [];
286 $this->assignTemplateVars($assignedVariables);
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') {
300 $request = $this->createMock(Request
::class);
301 $response = new Response();
303 $result = $this->controller
->displayCreateForm($request, $response);
305 static::assertSame(200, $result->getStatusCode());
306 static::assertSame('editlink', (string) $result->getBody());
307 static::assertSame(['nomarkdown' => 1], $assignedVariables['tags']);
311 * Test displaying bookmark create form
312 * When an existing URL is submitted, we want to edit the existing link.
314 public function testDisplayCreateFormWithExistingUrl(): void
316 $assignedVariables = [];
317 $this->assignTemplateVars($assignedVariables);
319 $url = 'http://url.tld/other?part=3&utm_ad=pay#hash';
320 $expectedUrl = str_replace('&utm_ad=pay', '', $url);
322 $request = $this->createMock(Request
::class);
325 ->willReturnCallback(function (string $key) use ($url): ?string {
326 return $key === 'post' ? $url : null;
328 $response = new Response();
330 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
331 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
333 $this->container
->bookmarkService
334 ->expects(static::once())
335 ->method('findByUrl')
340 ->setUrl($expectedUrl)
341 ->setTitle($title = 'Bookmark Title')
342 ->setDescription($description = 'Bookmark description.')
343 ->setTags($tags = ['abc', 'def'])
345 ->setCreated($createdAt = new \
DateTime('2020-06-10 18:45:44'))
349 $result = $this->controller
->displayCreateForm($request, $response);
351 static::assertSame(200, $result->getStatusCode());
352 static::assertSame('editlink', (string) $result->getBody());
354 static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']);
355 static::assertFalse($assignedVariables['link_is_new']);
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']);