3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin\ShaarePublishControllerTest
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Config\ConfigManager
;
9 use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper
;
10 use Shaarli\Front\Controller\Admin\ShaarePublishController
;
11 use Shaarli\Front\Exception\WrongTokenException
;
12 use Shaarli\Http\HttpAccess
;
13 use Shaarli\Security\SessionManager
;
15 use Shaarli\Thumbnailer
;
16 use Slim\Http\Request
;
17 use Slim\Http\Response
;
19 class SaveBookmarkTest
extends TestCase
21 use FrontAdminControllerMockHelper
;
23 /** @var ShaarePublishController */
24 protected $controller;
26 public function setUp(): void
28 $this->createContainer();
30 $this->container
->httpAccess
= $this->createMock(HttpAccess
::class);
31 $this->controller
= new ShaarePublishController($this->container
);
35 * Test save a new bookmark
37 public function testSaveBookmark(): void
41 'lf_url' => 'http://url.tld/other?part=3#hash',
42 'lf_title' => 'Provided Title',
43 'lf_description' => 'Provided description.',
44 'lf_tags' => 'abc def',
46 'returnurl' => 'http://shaarli/subfolder/admin/add-shaare'
49 $request = $this->createMock(Request
::class);
52 ->willReturnCallback(function (string $key) use ($parameters): ?string {
53 return $parameters[$key] ?? null;
56 $response = new Response();
58 $checkBookmark = function (Bookmark
$bookmark) use ($parameters) {
59 static::assertSame($parameters['lf_url'], $bookmark->getUrl());
60 static::assertSame($parameters['lf_title'], $bookmark->getTitle());
61 static::assertSame($parameters['lf_description'], $bookmark->getDescription());
62 static::assertSame($parameters['lf_tags'], $bookmark->getTagsString());
63 static::assertTrue($bookmark->isPrivate());
66 $this->container
->bookmarkService
67 ->expects(static::once())
69 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($checkBookmark, $id): Bookmark
{
70 static::assertFalse($save);
72 $checkBookmark($bookmark);
74 $bookmark->setId($id);
79 $this->container
->bookmarkService
80 ->expects(static::once())
82 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($checkBookmark, $id): Bookmark
{
83 static::assertTrue($save);
85 $checkBookmark($bookmark);
87 static::assertSame($id, $bookmark->getId());
93 // Make sure that PluginManager hook is triggered
94 $this->container
->pluginManager
95 ->expects(static::atLeastOnce())
96 ->method('executeHooks')
97 ->withConsecutive(['save_link'])
98 ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
99 if ('save_link' === $hook) {
100 static::assertSame($id, $data['id']);
101 static::assertSame($parameters['lf_url'], $data['url']);
102 static::assertSame($parameters['lf_title'], $data['title']);
103 static::assertSame($parameters['lf_description'], $data['description']);
104 static::assertSame($parameters['lf_tags'], $data['tags']);
105 static::assertTrue($data['private']);
112 $result = $this->controller
->save($request, $response);
114 static::assertSame(302, $result->getStatusCode());
115 static::assertRegExp('@/subfolder/#[\w\-]{6}@', $result->getHeader('location')[0]);
120 * Test save an existing bookmark
122 public function testSaveExistingBookmark(): void
126 'lf_id' => (string) $id,
127 'lf_url' => 'http://url.tld/other?part=3#hash',
128 'lf_title' => 'Provided Title',
129 'lf_description' => 'Provided description.',
130 'lf_tags' => 'abc def',
132 'returnurl' => 'http://shaarli/subfolder/?page=2'
135 $request = $this->createMock(Request
::class);
138 ->willReturnCallback(function (string $key) use ($parameters): ?string {
139 return $parameters[$key] ?? null;
142 $response = new Response();
144 $checkBookmark = function (Bookmark
$bookmark) use ($parameters, $id) {
145 static::assertSame($id, $bookmark->getId());
146 static::assertSame($parameters['lf_url'], $bookmark->getUrl());
147 static::assertSame($parameters['lf_title'], $bookmark->getTitle());
148 static::assertSame($parameters['lf_description'], $bookmark->getDescription());
149 static::assertSame($parameters['lf_tags'], $bookmark->getTagsString());
150 static::assertTrue($bookmark->isPrivate());
153 $this->container
->bookmarkService
->expects(static::atLeastOnce())->method('exists')->willReturn(true);
154 $this->container
->bookmarkService
155 ->expects(static::once())
157 ->willReturn((new Bookmark())->setId($id)->setUrl('http://other.url'))
159 $this->container
->bookmarkService
160 ->expects(static::once())
162 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($checkBookmark, $id): Bookmark
{
163 static::assertFalse($save);
165 $checkBookmark($bookmark);
170 $this->container
->bookmarkService
171 ->expects(static::once())
173 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($checkBookmark, $id): Bookmark
{
174 static::assertTrue($save);
176 $checkBookmark($bookmark);
178 static::assertSame($id, $bookmark->getId());
184 // Make sure that PluginManager hook is triggered
185 $this->container
->pluginManager
186 ->expects(static::atLeastOnce())
187 ->method('executeHooks')
188 ->withConsecutive(['save_link'])
189 ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
190 if ('save_link' === $hook) {
191 static::assertSame($id, $data['id']);
192 static::assertSame($parameters['lf_url'], $data['url']);
193 static::assertSame($parameters['lf_title'], $data['title']);
194 static::assertSame($parameters['lf_description'], $data['description']);
195 static::assertSame($parameters['lf_tags'], $data['tags']);
196 static::assertTrue($data['private']);
203 $result = $this->controller
->save($request, $response);
205 static::assertSame(302, $result->getStatusCode());
206 static::assertRegExp('@/subfolder/\?page=2#[\w\-]{6}@', $result->getHeader('location')[0]);
210 * Test save a bookmark - try to retrieve the thumbnail
212 public function testSaveBookmarkWithThumbnailSync(): void
214 $parameters = ['lf_url' => 'http://url.tld/other?part=3#hash'];
216 $request = $this->createMock(Request
::class);
219 ->willReturnCallback(function (string $key) use ($parameters): ?string {
220 return $parameters[$key] ?? null;
223 $response = new Response();
225 $this->container
->conf
= $this->createMock(ConfigManager
::class);
226 $this->container
->conf
->method('get')->willReturnCallback(function (string $key, $default) {
227 if ($key === 'thumbnails.mode') {
228 return Thumbnailer
::MODE_ALL
;
229 } elseif ($key === 'general.enable_async_metadata') {
236 $this->container
->thumbnailer
= $this->createMock(Thumbnailer
::class);
237 $this->container
->thumbnailer
238 ->expects(static::once())
240 ->with($parameters['lf_url'])
241 ->willReturn($thumb = 'http://thumb.url')
244 $this->container
->bookmarkService
245 ->expects(static::once())
247 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($thumb): Bookmark
{
248 static::assertSame($thumb, $bookmark->getThumbnail());
254 $result = $this->controller
->save($request, $response);
256 static::assertSame(302, $result->getStatusCode());
260 * Test save a bookmark - with ID #0
262 public function testSaveBookmarkWithIdZero(): void
264 $parameters = ['lf_id' => '0'];
266 $request = $this->createMock(Request
::class);
269 ->willReturnCallback(function (string $key) use ($parameters): ?string {
270 return $parameters[$key] ?? null;
273 $response = new Response();
275 $this->container
->bookmarkService
->expects(static::once())->method('exists')->with(0)->willReturn(true);
276 $this->container
->bookmarkService
->expects(static::once())->method('get')->with(0)->willReturn(new Bookmark());
278 $result = $this->controller
->save($request, $response);
280 static::assertSame(302, $result->getStatusCode());
284 * Test save a bookmark - do not attempt to retrieve thumbnails if async mode is enabled.
286 public function testSaveBookmarkWithThumbnailAsync(): void
288 $parameters = ['lf_url' => 'http://url.tld/other?part=3#hash'];
290 $request = $this->createMock(Request
::class);
293 ->willReturnCallback(function (string $key) use ($parameters): ?string {
294 return $parameters[$key] ?? null;
297 $response = new Response();
299 $this->container
->conf
= $this->createMock(ConfigManager
::class);
300 $this->container
->conf
->method('get')->willReturnCallback(function (string $key, $default) {
301 if ($key === 'thumbnails.mode') {
302 return Thumbnailer
::MODE_ALL
;
303 } elseif ($key === 'general.enable_async_metadata') {
310 $this->container
->thumbnailer
= $this->createMock(Thumbnailer
::class);
311 $this->container
->thumbnailer
->expects(static::never())->method('get');
313 $this->container
->bookmarkService
314 ->expects(static::once())
316 ->willReturnCallback(function (Bookmark
$bookmark): Bookmark
{
317 static::assertNull($bookmark->getThumbnail());
323 $result = $this->controller
->save($request, $response);
325 static::assertSame(302, $result->getStatusCode());
329 * Change the password with a wrong existing password
331 public function testSaveBookmarkFromBookmarklet(): void
333 $parameters = ['source' => 'bookmarklet'];
335 $request = $this->createMock(Request
::class);
338 ->willReturnCallback(function (string $key) use ($parameters): ?string {
339 return $parameters[$key] ?? null;
342 $response = new Response();
344 $result = $this->controller
->save($request, $response);
346 static::assertSame(200, $result->getStatusCode());
347 static::assertSame('<script>self.close();</script>', (string) $result->getBody());
351 * Change the password with a wrong existing password
353 public function testSaveBookmarkWrongToken(): void
355 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
356 $this->container
->sessionManager
->method('checkToken')->willReturn(false);
358 $this->container
->bookmarkService
->expects(static::never())->method('addOrSet');
359 $this->container
->bookmarkService
->expects(static::never())->method('set');
361 $request = $this->createMock(Request
::class);
362 $response = new Response();
364 $this->expectException(WrongTokenException
::class);
366 $this->controller
->save($request, $response);