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\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 ManageShaareController */
24 protected $controller;
26 public function setUp(): void
28 $this->createContainer();
30 $this->container
->httpAccess
= $this->createMock(HttpAccess
::class);
31 $this->controller
= new ManageShaareController($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): void {
70 static::assertFalse($save);
72 $checkBookmark($bookmark);
74 $bookmark->setId($id);
77 $this->container
->bookmarkService
78 ->expects(static::once())
80 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($checkBookmark, $id): void {
81 static::assertTrue($save);
83 $checkBookmark($bookmark);
85 static::assertSame($id, $bookmark->getId());
89 // Make sure that PluginManager hook is triggered
90 $this->container
->pluginManager
91 ->expects(static::atLeastOnce())
92 ->method('executeHooks')
93 ->withConsecutive(['save_link'])
94 ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
95 if ('save_link' === $hook) {
96 static::assertSame($id, $data['id']);
97 static::assertSame($parameters['lf_url'], $data['url']);
98 static::assertSame($parameters['lf_title'], $data['title']);
99 static::assertSame($parameters['lf_description'], $data['description']);
100 static::assertSame($parameters['lf_tags'], $data['tags']);
101 static::assertTrue($data['private']);
108 $result = $this->controller
->save($request, $response);
110 static::assertSame(302, $result->getStatusCode());
111 static::assertRegExp('@/subfolder/#[\w\-]{6}@', $result->getHeader('location')[0]);
116 * Test save an existing bookmark
118 public function testSaveExistingBookmark(): void
122 'lf_id' => (string) $id,
123 'lf_url' => 'http://url.tld/other?part=3#hash',
124 'lf_title' => 'Provided Title',
125 'lf_description' => 'Provided description.',
126 'lf_tags' => 'abc def',
128 'returnurl' => 'http://shaarli/subfolder/?page=2'
131 $request = $this->createMock(Request
::class);
134 ->willReturnCallback(function (string $key) use ($parameters): ?string {
135 return $parameters[$key] ?? null;
138 $response = new Response();
140 $checkBookmark = function (Bookmark
$bookmark) use ($parameters, $id) {
141 static::assertSame($id, $bookmark->getId());
142 static::assertSame($parameters['lf_url'], $bookmark->getUrl());
143 static::assertSame($parameters['lf_title'], $bookmark->getTitle());
144 static::assertSame($parameters['lf_description'], $bookmark->getDescription());
145 static::assertSame($parameters['lf_tags'], $bookmark->getTagsString());
146 static::assertTrue($bookmark->isPrivate());
149 $this->container
->bookmarkService
->expects(static::atLeastOnce())->method('exists')->willReturn(true);
150 $this->container
->bookmarkService
151 ->expects(static::once())
153 ->willReturn((new Bookmark())->setId($id)->setUrl('http://other.url'))
155 $this->container
->bookmarkService
156 ->expects(static::once())
158 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($checkBookmark, $id): void {
159 static::assertFalse($save);
161 $checkBookmark($bookmark);
164 $this->container
->bookmarkService
165 ->expects(static::once())
167 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($checkBookmark, $id): void {
168 static::assertTrue($save);
170 $checkBookmark($bookmark);
172 static::assertSame($id, $bookmark->getId());
176 // Make sure that PluginManager hook is triggered
177 $this->container
->pluginManager
178 ->expects(static::atLeastOnce())
179 ->method('executeHooks')
180 ->withConsecutive(['save_link'])
181 ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
182 if ('save_link' === $hook) {
183 static::assertSame($id, $data['id']);
184 static::assertSame($parameters['lf_url'], $data['url']);
185 static::assertSame($parameters['lf_title'], $data['title']);
186 static::assertSame($parameters['lf_description'], $data['description']);
187 static::assertSame($parameters['lf_tags'], $data['tags']);
188 static::assertTrue($data['private']);
195 $result = $this->controller
->save($request, $response);
197 static::assertSame(302, $result->getStatusCode());
198 static::assertRegExp('@/subfolder/\?page=2#[\w\-]{6}@', $result->getHeader('location')[0]);
202 * Test save a bookmark - try to retrieve the thumbnail
204 public function testSaveBookmarkWithThumbnail(): void
206 $parameters = ['lf_url' => 'http://url.tld/other?part=3#hash'];
208 $request = $this->createMock(Request
::class);
211 ->willReturnCallback(function (string $key) use ($parameters): ?string {
212 return $parameters[$key] ?? null;
215 $response = new Response();
217 $this->container
->conf
= $this->createMock(ConfigManager
::class);
218 $this->container
->conf
->method('get')->willReturnCallback(function (string $key, $default) {
219 return $key === 'thumbnails.mode' ? Thumbnailer
::MODE_ALL
: $default;
222 $this->container
->thumbnailer
= $this->createMock(Thumbnailer
::class);
223 $this->container
->thumbnailer
224 ->expects(static::once())
226 ->with($parameters['lf_url'])
227 ->willReturn($thumb = 'http://thumb.url')
230 $this->container
->bookmarkService
231 ->expects(static::once())
233 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($thumb): void {
234 static::assertSame($thumb, $bookmark->getThumbnail());
238 $result = $this->controller
->save($request, $response);
240 static::assertSame(302, $result->getStatusCode());
244 * Test save a bookmark - with ID #0
246 public function testSaveBookmarkWithIdZero(): void
248 $parameters = ['lf_id' => '0'];
250 $request = $this->createMock(Request
::class);
253 ->willReturnCallback(function (string $key) use ($parameters): ?string {
254 return $parameters[$key] ?? null;
257 $response = new Response();
259 $this->container
->bookmarkService
->expects(static::once())->method('exists')->with(0)->willReturn(true);
260 $this->container
->bookmarkService
->expects(static::once())->method('get')->with(0)->willReturn(new Bookmark());
262 $result = $this->controller
->save($request, $response);
264 static::assertSame(302, $result->getStatusCode());
268 * Change the password with a wrong existing password
270 public function testSaveBookmarkFromBookmarklet(): void
272 $parameters = ['source' => 'bookmarklet'];
274 $request = $this->createMock(Request
::class);
277 ->willReturnCallback(function (string $key) use ($parameters): ?string {
278 return $parameters[$key] ?? null;
281 $response = new Response();
283 $result = $this->controller
->save($request, $response);
285 static::assertSame(200, $result->getStatusCode());
286 static::assertSame('<script>self.close();</script>', (string) $result->getBody());
290 * Change the password with a wrong existing password
292 public function testSaveBookmarkWrongToken(): void
294 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
295 $this->container
->sessionManager
->method('checkToken')->willReturn(false);
297 $this->container
->bookmarkService
->expects(static::never())->method('addOrSet');
298 $this->container
->bookmarkService
->expects(static::never())->method('set');
300 $request = $this->createMock(Request
::class);
301 $response = new Response();
303 $this->expectException(WrongTokenException
::class);
305 $this->controller
->save($request, $response);