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
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
16 class DisplayCreateFormTest
extends TestCase
18 use FrontAdminControllerMockHelper
;
20 /** @var ManageShaareController */
21 protected $controller;
23 public function setUp(): void
25 $this->createContainer();
27 $this->container
->httpAccess
= $this->createMock(HttpAccess
::class);
28 $this->controller
= new ManageShaareController($this->container
);
32 * Test displaying bookmark create form
33 * Ensure that every step of the standard workflow works properly.
35 public function testDisplayCreateFormWithUrl(): void
37 $this->container
->environment
= [
38 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc'
41 $assignedVariables = [];
42 $this->assignTemplateVars($assignedVariables);
44 $url = 'http://url.tld/other?part=3&utm_ad=pay#hash';
45 $expectedUrl = str_replace('&utm_ad=pay', '', $url);
46 $remoteTitle = 'Remote Title';
47 $remoteDesc = 'Sometimes the meta description is relevant.';
48 $remoteTags = 'abc def';
50 $request = $this->createMock(Request
::class);
51 $request->method('getParam')->willReturnCallback(function (string $key) use ($url): ?string {
52 return $key === 'post' ? $url : null;
54 $response = new Response();
56 $this->container
->httpAccess
57 ->expects(static::once())
58 ->method('getCurlDownloadCallback')
60 function (&$charset, &$title, &$description, &$tags) use (
65 return function () use (
74 $charset = 'ISO-8859-1';
75 $title = $remoteTitle;
76 $description = $remoteDesc;
82 $this->container
->httpAccess
83 ->expects(static::once())
84 ->method('getHttpResponse')
85 ->with($expectedUrl, 30, 4194304)
86 ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void {
91 $this->container
->bookmarkService
92 ->expects(static::once())
93 ->method('bookmarksCountPerTag')
94 ->willReturn($tags = ['tag1' => 2, 'tag2' => 1])
97 // Make sure that PluginManager hook is triggered
98 $this->container
->pluginManager
99 ->expects(static::atLeastOnce())
100 ->method('executeHooks')
101 ->withConsecutive(['render_editlink'], ['render_includes'])
102 ->willReturnCallback(function (string $hook, array $data) use ($remoteTitle, $remoteDesc): array {
103 if ('render_editlink' === $hook) {
104 static::assertSame($remoteTitle, $data['link']['title']);
105 static::assertSame($remoteDesc, $data['link']['description']);
112 $result = $this->controller
->displayCreateForm($request, $response);
114 static::assertSame(200, $result->getStatusCode());
115 static::assertSame('editlink', (string) $result->getBody());
117 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
119 static::assertSame($expectedUrl, $assignedVariables['link']['url']);
120 static::assertSame($remoteTitle, $assignedVariables['link']['title']);
121 static::assertSame($remoteDesc, $assignedVariables['link']['description']);
122 static::assertSame($remoteTags, $assignedVariables['link']['tags']);
123 static::assertFalse($assignedVariables['link']['private']);
125 static::assertTrue($assignedVariables['link_is_new']);
126 static::assertSame($referer, $assignedVariables['http_referer']);
127 static::assertSame($tags, $assignedVariables['tags']);
128 static::assertArrayHasKey('source', $assignedVariables);
129 static::assertArrayHasKey('default_private_links', $assignedVariables);
133 * Test displaying bookmark create form
134 * Ensure all available query parameters are handled properly.
136 public function testDisplayCreateFormWithFullParameters(): void
138 $assignedVariables = [];
139 $this->assignTemplateVars($assignedVariables);
142 'post' => 'http://url.tld/other?part=3&utm_ad=pay#hash',
143 'title' => 'Provided Title',
144 'description' => 'Provided description.',
149 $expectedUrl = str_replace('&utm_ad=pay', '', $parameters['post']);
151 $request = $this->createMock(Request
::class);
154 ->willReturnCallback(function (string $key) use ($parameters): ?string {
155 return $parameters[$key] ?? null;
157 $response = new Response();
159 $result = $this->controller
->displayCreateForm($request, $response);
161 static::assertSame(200, $result->getStatusCode());
162 static::assertSame('editlink', (string) $result->getBody());
164 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
166 static::assertSame($expectedUrl, $assignedVariables['link']['url']);
167 static::assertSame($parameters['title'], $assignedVariables['link']['title']);
168 static::assertSame($parameters['description'], $assignedVariables['link']['description']);
169 static::assertSame($parameters['tags'], $assignedVariables['link']['tags']);
170 static::assertTrue($assignedVariables['link']['private']);
171 static::assertTrue($assignedVariables['link_is_new']);
172 static::assertSame($parameters['source'], $assignedVariables['source']);
176 * Test displaying bookmark create form
177 * Without any parameter.
179 public function testDisplayCreateFormEmpty(): void
181 $assignedVariables = [];
182 $this->assignTemplateVars($assignedVariables);
184 $request = $this->createMock(Request
::class);
185 $response = new Response();
187 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
188 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
190 $result = $this->controller
->displayCreateForm($request, $response);
192 static::assertSame(200, $result->getStatusCode());
193 static::assertSame('editlink', (string) $result->getBody());
194 static::assertSame('', $assignedVariables['link']['url']);
195 static::assertSame('Note: ', $assignedVariables['link']['title']);
196 static::assertSame('', $assignedVariables['link']['description']);
197 static::assertSame('', $assignedVariables['link']['tags']);
198 static::assertFalse($assignedVariables['link']['private']);
199 static::assertTrue($assignedVariables['link_is_new']);
203 * Test displaying bookmark create form
204 * URL not using HTTP protocol: do not try to retrieve the title
206 public function testDisplayCreateFormNotHttp(): void
208 $assignedVariables = [];
209 $this->assignTemplateVars($assignedVariables);
211 $url = 'magnet://kubuntu.torrent';
212 $request = $this->createMock(Request
::class);
215 ->willReturnCallback(function (string $key) use ($url): ?string {
216 return $key === 'post' ? $url : null;
218 $response = new Response();
220 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
221 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
223 $result = $this->controller
->displayCreateForm($request, $response);
225 static::assertSame(200, $result->getStatusCode());
226 static::assertSame('editlink', (string) $result->getBody());
227 static::assertSame($url, $assignedVariables['link']['url']);
228 static::assertTrue($assignedVariables['link_is_new']);
232 * Test displaying bookmark create form
233 * When markdown formatter is enabled, the no markdown tag should be added to existing tags.
235 public function testDisplayCreateFormWithMarkdownEnabled(): void
237 $assignedVariables = [];
238 $this->assignTemplateVars($assignedVariables);
240 $this->container
->conf
= $this->createMock(ConfigManager
::class);
241 $this->container
->conf
242 ->expects(static::atLeastOnce())
243 ->method('get')->willReturnCallback(function (string $key): ?string {
244 if ($key === 'formatter') {
252 $request = $this->createMock(Request
::class);
253 $response = new Response();
255 $result = $this->controller
->displayCreateForm($request, $response);
257 static::assertSame(200, $result->getStatusCode());
258 static::assertSame('editlink', (string) $result->getBody());
259 static::assertSame(['nomarkdown' => 1], $assignedVariables['tags']);
263 * Test displaying bookmark create form
264 * When an existing URL is submitted, we want to edit the existing link.
266 public function testDisplayCreateFormWithExistingUrl(): void
268 $assignedVariables = [];
269 $this->assignTemplateVars($assignedVariables);
271 $url = 'http://url.tld/other?part=3&utm_ad=pay#hash';
272 $expectedUrl = str_replace('&utm_ad=pay', '', $url);
274 $request = $this->createMock(Request
::class);
277 ->willReturnCallback(function (string $key) use ($url): ?string {
278 return $key === 'post' ? $url : null;
280 $response = new Response();
282 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
283 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
285 $this->container
->bookmarkService
286 ->expects(static::once())
287 ->method('findByUrl')
292 ->setUrl($expectedUrl)
293 ->setTitle($title = 'Bookmark Title')
294 ->setDescription($description = 'Bookmark description.')
295 ->setTags($tags = ['abc', 'def'])
297 ->setCreated($createdAt = new \
DateTime('2020-06-10 18:45:44'))
301 $result = $this->controller
->displayCreateForm($request, $response);
303 static::assertSame(200, $result->getStatusCode());
304 static::assertSame('editlink', (string) $result->getBody());
306 static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']);
307 static::assertFalse($assignedVariables['link_is_new']);
309 static::assertSame($id, $assignedVariables['link']['id']);
310 static::assertSame($expectedUrl, $assignedVariables['link']['url']);
311 static::assertSame($title, $assignedVariables['link']['title']);
312 static::assertSame($description, $assignedVariables['link']['description']);
313 static::assertSame(implode(' ', $tags), $assignedVariables['link']['tags']);
314 static::assertTrue($assignedVariables['link']['private']);
315 static::assertSame($createdAt, $assignedVariables['link']['created']);