]>
Commit | Line | Data |
---|---|---|
baa69791 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest; | |
6 | ||
baa69791 A |
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; | |
a5a9cf23 | 12 | use Shaarli\TestCase; |
baa69791 A |
13 | use Slim\Http\Request; |
14 | use Slim\Http\Response; | |
15 | ||
16 | class 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 | |
a5a9cf23 | 99 | ->expects(static::atLeastOnce()) |
baa69791 | 100 | ->method('executeHooks') |
a5a9cf23 | 101 | ->withConsecutive(['render_editlink'], ['render_includes']) |
baa69791 | 102 | ->willReturnCallback(function (string $hook, array $data) use ($remoteTitle, $remoteDesc): array { |
a5a9cf23 A |
103 | if ('render_editlink' === $hook) { |
104 | static::assertSame($remoteTitle, $data['link']['title']); | |
105 | static::assertSame($remoteDesc, $data['link']['description']); | |
106 | } | |
baa69791 A |
107 | |
108 | return $data; | |
109 | }) | |
110 | ; | |
111 | ||
112 | $result = $this->controller->displayCreateForm($request, $response); | |
113 | ||
114 | static::assertSame(200, $result->getStatusCode()); | |
115 | static::assertSame('editlink', (string) $result->getBody()); | |
116 | ||
117 | static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']); | |
118 | ||
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']); | |
124 | ||
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); | |
130 | } | |
131 | ||
132 | /** | |
133 | * Test displaying bookmark create form | |
134 | * Ensure all available query parameters are handled properly. | |
135 | */ | |
136 | public function testDisplayCreateFormWithFullParameters(): void | |
137 | { | |
138 | $assignedVariables = []; | |
139 | $this->assignTemplateVars($assignedVariables); | |
140 | ||
141 | $parameters = [ | |
142 | 'post' => 'http://url.tld/other?part=3&utm_ad=pay#hash', | |
143 | 'title' => 'Provided Title', | |
144 | 'description' => 'Provided description.', | |
145 | 'tags' => 'abc def', | |
146 | 'private' => '1', | |
147 | 'source' => 'apps', | |
148 | ]; | |
149 | $expectedUrl = str_replace('&utm_ad=pay', '', $parameters['post']); | |
150 | ||
151 | $request = $this->createMock(Request::class); | |
152 | $request | |
153 | ->method('getParam') | |
154 | ->willReturnCallback(function (string $key) use ($parameters): ?string { | |
155 | return $parameters[$key] ?? null; | |
156 | }); | |
157 | $response = new Response(); | |
158 | ||
159 | $result = $this->controller->displayCreateForm($request, $response); | |
160 | ||
161 | static::assertSame(200, $result->getStatusCode()); | |
162 | static::assertSame('editlink', (string) $result->getBody()); | |
163 | ||
164 | static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']); | |
165 | ||
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']); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Test displaying bookmark create form | |
177 | * Without any parameter. | |
178 | */ | |
179 | public function testDisplayCreateFormEmpty(): void | |
180 | { | |
181 | $assignedVariables = []; | |
182 | $this->assignTemplateVars($assignedVariables); | |
183 | ||
184 | $request = $this->createMock(Request::class); | |
185 | $response = new Response(); | |
186 | ||
187 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | |
188 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | |
189 | ||
190 | $result = $this->controller->displayCreateForm($request, $response); | |
191 | ||
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']); | |
200 | } | |
201 | ||
202 | /** | |
203 | * Test displaying bookmark create form | |
204 | * URL not using HTTP protocol: do not try to retrieve the title | |
205 | */ | |
206 | public function testDisplayCreateFormNotHttp(): void | |
207 | { | |
208 | $assignedVariables = []; | |
209 | $this->assignTemplateVars($assignedVariables); | |
210 | ||
211 | $url = 'magnet://kubuntu.torrent'; | |
212 | $request = $this->createMock(Request::class); | |
213 | $request | |
214 | ->method('getParam') | |
215 | ->willReturnCallback(function (string $key) use ($url): ?string { | |
216 | return $key === 'post' ? $url : null; | |
217 | }); | |
218 | $response = new Response(); | |
219 | ||
220 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | |
221 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | |
222 | ||
223 | $result = $this->controller->displayCreateForm($request, $response); | |
224 | ||
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']); | |
229 | } | |
230 | ||
231 | /** | |
232 | * Test displaying bookmark create form | |
233 | * When markdown formatter is enabled, the no markdown tag should be added to existing tags. | |
234 | */ | |
235 | public function testDisplayCreateFormWithMarkdownEnabled(): void | |
236 | { | |
237 | $assignedVariables = []; | |
238 | $this->assignTemplateVars($assignedVariables); | |
239 | ||
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') { | |
245 | return 'markdown'; | |
246 | } | |
247 | ||
248 | return $key; | |
249 | }) | |
250 | ; | |
251 | ||
252 | $request = $this->createMock(Request::class); | |
253 | $response = new Response(); | |
254 | ||
255 | $result = $this->controller->displayCreateForm($request, $response); | |
256 | ||
257 | static::assertSame(200, $result->getStatusCode()); | |
258 | static::assertSame('editlink', (string) $result->getBody()); | |
259 | static::assertSame(['nomarkdown' => 1], $assignedVariables['tags']); | |
260 | } | |
261 | ||
262 | /** | |
263 | * Test displaying bookmark create form | |
264 | * When an existing URL is submitted, we want to edit the existing link. | |
265 | */ | |
266 | public function testDisplayCreateFormWithExistingUrl(): void | |
267 | { | |
268 | $assignedVariables = []; | |
269 | $this->assignTemplateVars($assignedVariables); | |
270 | ||
271 | $url = 'http://url.tld/other?part=3&utm_ad=pay#hash'; | |
272 | $expectedUrl = str_replace('&utm_ad=pay', '', $url); | |
273 | ||
274 | $request = $this->createMock(Request::class); | |
275 | $request | |
276 | ->method('getParam') | |
277 | ->willReturnCallback(function (string $key) use ($url): ?string { | |
278 | return $key === 'post' ? $url : null; | |
279 | }); | |
280 | $response = new Response(); | |
281 | ||
282 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | |
283 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | |
284 | ||
285 | $this->container->bookmarkService | |
286 | ->expects(static::once()) | |
287 | ->method('findByUrl') | |
288 | ->with($expectedUrl) | |
289 | ->willReturn( | |
290 | (new Bookmark()) | |
291 | ->setId($id = 23) | |
292 | ->setUrl($expectedUrl) | |
293 | ->setTitle($title = 'Bookmark Title') | |
294 | ->setDescription($description = 'Bookmark description.') | |
295 | ->setTags($tags = ['abc', 'def']) | |
296 | ->setPrivate(true) | |
297 | ->setCreated($createdAt = new \DateTime('2020-06-10 18:45:44')) | |
298 | ) | |
299 | ; | |
300 | ||
301 | $result = $this->controller->displayCreateForm($request, $response); | |
302 | ||
303 | static::assertSame(200, $result->getStatusCode()); | |
304 | static::assertSame('editlink', (string) $result->getBody()); | |
305 | ||
306 | static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']); | |
307 | static::assertFalse($assignedVariables['link_is_new']); | |
308 | ||
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']); | |
316 | } | |
317 | } |