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): 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 testSaveBookmarkWithThumbnail(): 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 return $key === 'thumbnails.mode' ? Thumbnailer
::MODE_ALL
: $default;
230 $this->container
->thumbnailer
= $this->createMock(Thumbnailer
::class);
231 $this->container
->thumbnailer
232 ->expects(static::once())
234 ->with($parameters['lf_url'])
235 ->willReturn($thumb = 'http://thumb.url')
238 $this->container
->bookmarkService
239 ->expects(static::once())
241 ->willReturnCallback(function (Bookmark
$bookmark, bool $save) use ($thumb): Bookmark
{
242 static::assertSame($thumb, $bookmark->getThumbnail());
248 $result = $this->controller
->save($request, $response);
250 static::assertSame(302, $result->getStatusCode());
254 * Test save a bookmark - with ID #0
256 public function testSaveBookmarkWithIdZero(): void
258 $parameters = ['lf_id' => '0'];
260 $request = $this->createMock(Request
::class);
263 ->willReturnCallback(function (string $key) use ($parameters): ?string {
264 return $parameters[$key] ?? null;
267 $response = new Response();
269 $this->container
->bookmarkService
->expects(static::once())->method('exists')->with(0)->willReturn(true);
270 $this->container
->bookmarkService
->expects(static::once())->method('get')->with(0)->willReturn(new Bookmark());
272 $result = $this->controller
->save($request, $response);
274 static::assertSame(302, $result->getStatusCode());
278 * Change the password with a wrong existing password
280 public function testSaveBookmarkFromBookmarklet(): void
282 $parameters = ['source' => 'bookmarklet'];
284 $request = $this->createMock(Request
::class);
287 ->willReturnCallback(function (string $key) use ($parameters): ?string {
288 return $parameters[$key] ?? null;
291 $response = new Response();
293 $result = $this->controller
->save($request, $response);
295 static::assertSame(200, $result->getStatusCode());
296 static::assertSame('<script>self.close();</script>', (string) $result->getBody());
300 * Change the password with a wrong existing password
302 public function testSaveBookmarkWrongToken(): void
304 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
305 $this->container
->sessionManager
->method('checkToken')->willReturn(false);
307 $this->container
->bookmarkService
->expects(static::never())->method('addOrSet');
308 $this->container
->bookmarkService
->expects(static::never())->method('set');
310 $request = $this->createMock(Request
::class);
311 $response = new Response();
313 $this->expectException(WrongTokenException
::class);
315 $this->controller
->save($request, $response);