aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin/ManageShaareControllerTest/DisplayCreateFormTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/front/controller/admin/ManageShaareControllerTest/DisplayCreateFormTest.php')
-rw-r--r--tests/front/controller/admin/ManageShaareControllerTest/DisplayCreateFormTest.php315
1 files changed, 315 insertions, 0 deletions
diff --git a/tests/front/controller/admin/ManageShaareControllerTest/DisplayCreateFormTest.php b/tests/front/controller/admin/ManageShaareControllerTest/DisplayCreateFormTest.php
new file mode 100644
index 00000000..777583d5
--- /dev/null
+++ b/tests/front/controller/admin/ManageShaareControllerTest/DisplayCreateFormTest.php
@@ -0,0 +1,315 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\Bookmark;
9use Shaarli\Config\ConfigManager;
10use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper;
11use Shaarli\Front\Controller\Admin\ManageShaareController;
12use Shaarli\Http\HttpAccess;
13use Slim\Http\Request;
14use Slim\Http\Response;
15
16class DisplayCreateFormTest extends TestCase
17{
18 use FrontAdminControllerMockHelper;
19
20 /** @var ManageShaareController */
21 protected $controller;
22
23 public function setUp(): void
24 {
25 $this->createContainer();
26
27 $this->container->httpAccess = $this->createMock(HttpAccess::class);
28 $this->controller = new ManageShaareController($this->container);
29 }
30
31 /**
32 * Test displaying bookmark create form
33 * Ensure that every step of the standard workflow works properly.
34 */
35 public function testDisplayCreateFormWithUrl(): void
36 {
37 $this->container->environment = [
38 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc'
39 ];
40
41 $assignedVariables = [];
42 $this->assignTemplateVars($assignedVariables);
43
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';
49
50 $request = $this->createMock(Request::class);
51 $request->method('getParam')->willReturnCallback(function (string $key) use ($url): ?string {
52 return $key === 'post' ? $url : null;
53 });
54 $response = new Response();
55
56 $this->container->httpAccess
57 ->expects(static::once())
58 ->method('getCurlDownloadCallback')
59 ->willReturnCallback(
60 function (&$charset, &$title, &$description, &$tags) use (
61 $remoteTitle,
62 $remoteDesc,
63 $remoteTags
64 ): callable {
65 return function () use (
66 &$charset,
67 &$title,
68 &$description,
69 &$tags,
70 $remoteTitle,
71 $remoteDesc,
72 $remoteTags
73 ): void {
74 $charset = 'ISO-8859-1';
75 $title = $remoteTitle;
76 $description = $remoteDesc;
77 $tags = $remoteTags;
78 };
79 }
80 )
81 ;
82 $this->container->httpAccess
83 ->expects(static::once())
84 ->method('getHttpResponse')
85 ->with($expectedUrl, 30, 4194304)
86 ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void {
87 $callback();
88 })
89 ;
90
91 $this->container->bookmarkService
92 ->expects(static::once())
93 ->method('bookmarksCountPerTag')
94 ->willReturn($tags = ['tag1' => 2, 'tag2' => 1])
95 ;
96
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']);
105
106 return $data;
107 })
108 ;
109
110 $result = $this->controller->displayCreateForm($request, $response);
111
112 static::assertSame(200, $result->getStatusCode());
113 static::assertSame('editlink', (string) $result->getBody());
114
115 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
116
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']);
122
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);
128 }
129
130 /**
131 * Test displaying bookmark create form
132 * Ensure all available query parameters are handled properly.
133 */
134 public function testDisplayCreateFormWithFullParameters(): void
135 {
136 $assignedVariables = [];
137 $this->assignTemplateVars($assignedVariables);
138
139 $parameters = [
140 'post' => 'http://url.tld/other?part=3&utm_ad=pay#hash',
141 'title' => 'Provided Title',
142 'description' => 'Provided description.',
143 'tags' => 'abc def',
144 'private' => '1',
145 'source' => 'apps',
146 ];
147 $expectedUrl = str_replace('&utm_ad=pay', '', $parameters['post']);
148
149 $request = $this->createMock(Request::class);
150 $request
151 ->method('getParam')
152 ->willReturnCallback(function (string $key) use ($parameters): ?string {
153 return $parameters[$key] ?? null;
154 });
155 $response = new Response();
156
157 $result = $this->controller->displayCreateForm($request, $response);
158
159 static::assertSame(200, $result->getStatusCode());
160 static::assertSame('editlink', (string) $result->getBody());
161
162 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
163
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']);
171 }
172
173 /**
174 * Test displaying bookmark create form
175 * Without any parameter.
176 */
177 public function testDisplayCreateFormEmpty(): void
178 {
179 $assignedVariables = [];
180 $this->assignTemplateVars($assignedVariables);
181
182 $request = $this->createMock(Request::class);
183 $response = new Response();
184
185 $this->container->httpAccess->expects(static::never())->method('getHttpResponse');
186 $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback');
187
188 $result = $this->controller->displayCreateForm($request, $response);
189
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']);
198 }
199
200 /**
201 * Test displaying bookmark create form
202 * URL not using HTTP protocol: do not try to retrieve the title
203 */
204 public function testDisplayCreateFormNotHttp(): void
205 {
206 $assignedVariables = [];
207 $this->assignTemplateVars($assignedVariables);
208
209 $url = 'magnet://kubuntu.torrent';
210 $request = $this->createMock(Request::class);
211 $request
212 ->method('getParam')
213 ->willReturnCallback(function (string $key) use ($url): ?string {
214 return $key === 'post' ? $url : null;
215 });
216 $response = new Response();
217
218 $this->container->httpAccess->expects(static::never())->method('getHttpResponse');
219 $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback');
220
221 $result = $this->controller->displayCreateForm($request, $response);
222
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']);
227 }
228
229 /**
230 * Test displaying bookmark create form
231 * When markdown formatter is enabled, the no markdown tag should be added to existing tags.
232 */
233 public function testDisplayCreateFormWithMarkdownEnabled(): void
234 {
235 $assignedVariables = [];
236 $this->assignTemplateVars($assignedVariables);
237
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') {
243 return 'markdown';
244 }
245
246 return $key;
247 })
248 ;
249
250 $request = $this->createMock(Request::class);
251 $response = new Response();
252
253 $result = $this->controller->displayCreateForm($request, $response);
254
255 static::assertSame(200, $result->getStatusCode());
256 static::assertSame('editlink', (string) $result->getBody());
257 static::assertSame(['nomarkdown' => 1], $assignedVariables['tags']);
258 }
259
260 /**
261 * Test displaying bookmark create form
262 * When an existing URL is submitted, we want to edit the existing link.
263 */
264 public function testDisplayCreateFormWithExistingUrl(): void
265 {
266 $assignedVariables = [];
267 $this->assignTemplateVars($assignedVariables);
268
269 $url = 'http://url.tld/other?part=3&utm_ad=pay#hash';
270 $expectedUrl = str_replace('&utm_ad=pay', '', $url);
271
272 $request = $this->createMock(Request::class);
273 $request
274 ->method('getParam')
275 ->willReturnCallback(function (string $key) use ($url): ?string {
276 return $key === 'post' ? $url : null;
277 });
278 $response = new Response();
279
280 $this->container->httpAccess->expects(static::never())->method('getHttpResponse');
281 $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback');
282
283 $this->container->bookmarkService
284 ->expects(static::once())
285 ->method('findByUrl')
286 ->with($expectedUrl)
287 ->willReturn(
288 (new Bookmark())
289 ->setId($id = 23)
290 ->setUrl($expectedUrl)
291 ->setTitle($title = 'Bookmark Title')
292 ->setDescription($description = 'Bookmark description.')
293 ->setTags($tags = ['abc', 'def'])
294 ->setPrivate(true)
295 ->setCreated($createdAt = new \DateTime('2020-06-10 18:45:44'))
296 )
297 ;
298
299 $result = $this->controller->displayCreateForm($request, $response);
300
301 static::assertSame(200, $result->getStatusCode());
302 static::assertSame('editlink', (string) $result->getBody());
303
304 static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']);
305 static::assertFalse($assignedVariables['link_is_new']);
306
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']);
314 }
315}