3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Bookmark\Bookmark
;
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper
;
11 use Shaarli\Front\Controller\Admin\ManageShaareController
;
12 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::at(0))
100 ->method('executeHooks')
101 ->willReturnCallback(function (string $hook, array $data) use ($remoteTitle, $remoteDesc): array {
102 static::assertSame('render_editlink', $hook);
103 static::assertSame($remoteTitle, $data['link']['title']);
104 static::assertSame($remoteDesc, $data['link']['description']);
110 $result = $this->controller
->displayCreateForm($request, $response);
112 static::assertSame(200, $result->getStatusCode());
113 static::assertSame('editlink', (string) $result->getBody());
115 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
117 static::assertSame($expectedUrl, $assignedVariables['link']['url']);
118 static::assertSame($remoteTitle, $assignedVariables['link']['title']);
119 static::assertSame($remoteDesc, $assignedVariables['link']['description']);
120 static::assertSame($remoteTags, $assignedVariables['link']['tags']);
121 static::assertFalse($assignedVariables['link']['private']);
123 static::assertTrue($assignedVariables['link_is_new']);
124 static::assertSame($referer, $assignedVariables['http_referer']);
125 static::assertSame($tags, $assignedVariables['tags']);
126 static::assertArrayHasKey('source', $assignedVariables);
127 static::assertArrayHasKey('default_private_links', $assignedVariables);
131 * Test displaying bookmark create form
132 * Ensure all available query parameters are handled properly.
134 public function testDisplayCreateFormWithFullParameters(): void
136 $assignedVariables = [];
137 $this->assignTemplateVars($assignedVariables);
140 'post' => 'http://url.tld/other?part=3&utm_ad=pay#hash',
141 'title' => 'Provided Title',
142 'description' => 'Provided description.',
147 $expectedUrl = str_replace('&utm_ad=pay', '', $parameters['post']);
149 $request = $this->createMock(Request
::class);
152 ->willReturnCallback(function (string $key) use ($parameters): ?string {
153 return $parameters[$key] ?? null;
155 $response = new Response();
157 $result = $this->controller
->displayCreateForm($request, $response);
159 static::assertSame(200, $result->getStatusCode());
160 static::assertSame('editlink', (string) $result->getBody());
162 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
164 static::assertSame($expectedUrl, $assignedVariables['link']['url']);
165 static::assertSame($parameters['title'], $assignedVariables['link']['title']);
166 static::assertSame($parameters['description'], $assignedVariables['link']['description']);
167 static::assertSame($parameters['tags'], $assignedVariables['link']['tags']);
168 static::assertTrue($assignedVariables['link']['private']);
169 static::assertTrue($assignedVariables['link_is_new']);
170 static::assertSame($parameters['source'], $assignedVariables['source']);
174 * Test displaying bookmark create form
175 * Without any parameter.
177 public function testDisplayCreateFormEmpty(): void
179 $assignedVariables = [];
180 $this->assignTemplateVars($assignedVariables);
182 $request = $this->createMock(Request
::class);
183 $response = new Response();
185 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
186 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
188 $result = $this->controller
->displayCreateForm($request, $response);
190 static::assertSame(200, $result->getStatusCode());
191 static::assertSame('editlink', (string) $result->getBody());
192 static::assertSame('', $assignedVariables['link']['url']);
193 static::assertSame('Note: ', $assignedVariables['link']['title']);
194 static::assertSame('', $assignedVariables['link']['description']);
195 static::assertSame('', $assignedVariables['link']['tags']);
196 static::assertFalse($assignedVariables['link']['private']);
197 static::assertTrue($assignedVariables['link_is_new']);
201 * Test displaying bookmark create form
202 * URL not using HTTP protocol: do not try to retrieve the title
204 public function testDisplayCreateFormNotHttp(): void
206 $assignedVariables = [];
207 $this->assignTemplateVars($assignedVariables);
209 $url = 'magnet://kubuntu.torrent';
210 $request = $this->createMock(Request
::class);
213 ->willReturnCallback(function (string $key) use ($url): ?string {
214 return $key === 'post' ? $url : null;
216 $response = new Response();
218 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
219 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
221 $result = $this->controller
->displayCreateForm($request, $response);
223 static::assertSame(200, $result->getStatusCode());
224 static::assertSame('editlink', (string) $result->getBody());
225 static::assertSame($url, $assignedVariables['link']['url']);
226 static::assertTrue($assignedVariables['link_is_new']);
230 * Test displaying bookmark create form
231 * When markdown formatter is enabled, the no markdown tag should be added to existing tags.
233 public function testDisplayCreateFormWithMarkdownEnabled(): void
235 $assignedVariables = [];
236 $this->assignTemplateVars($assignedVariables);
238 $this->container
->conf
= $this->createMock(ConfigManager
::class);
239 $this->container
->conf
240 ->expects(static::atLeastOnce())
241 ->method('get')->willReturnCallback(function (string $key): ?string {
242 if ($key === 'formatter') {
250 $request = $this->createMock(Request
::class);
251 $response = new Response();
253 $result = $this->controller
->displayCreateForm($request, $response);
255 static::assertSame(200, $result->getStatusCode());
256 static::assertSame('editlink', (string) $result->getBody());
257 static::assertSame(['nomarkdown' => 1], $assignedVariables['tags']);
261 * Test displaying bookmark create form
262 * When an existing URL is submitted, we want to edit the existing link.
264 public function testDisplayCreateFormWithExistingUrl(): void
266 $assignedVariables = [];
267 $this->assignTemplateVars($assignedVariables);
269 $url = 'http://url.tld/other?part=3&utm_ad=pay#hash';
270 $expectedUrl = str_replace('&utm_ad=pay', '', $url);
272 $request = $this->createMock(Request
::class);
275 ->willReturnCallback(function (string $key) use ($url): ?string {
276 return $key === 'post' ? $url : null;
278 $response = new Response();
280 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
281 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
283 $this->container
->bookmarkService
284 ->expects(static::once())
285 ->method('findByUrl')
290 ->setUrl($expectedUrl)
291 ->setTitle($title = 'Bookmark Title')
292 ->setDescription($description = 'Bookmark description.')
293 ->setTags($tags = ['abc', 'def'])
295 ->setCreated($createdAt = new \
DateTime('2020-06-10 18:45:44'))
299 $result = $this->controller
->displayCreateForm($request, $response);
301 static::assertSame(200, $result->getStatusCode());
302 static::assertSame('editlink', (string) $result->getBody());
304 static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']);
305 static::assertFalse($assignedVariables['link_is_new']);
307 static::assertSame($id, $assignedVariables['link']['id']);
308 static::assertSame($expectedUrl, $assignedVariables['link']['url']);
309 static::assertSame($title, $assignedVariables['link']['title']);
310 static::assertSame($description, $assignedVariables['link']['description']);
311 static::assertSame(implode(' ', $tags), $assignedVariables['link']['tags']);
312 static::assertTrue($assignedVariables['link']['private']);
313 static::assertSame($createdAt, $assignedVariables['link']['created']);