3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin\ShaareManageControllerTest
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
9 use Shaarli\Formatter\BookmarkFormatter
;
10 use Shaarli\Formatter\BookmarkRawFormatter
;
11 use Shaarli\Formatter\FormatterFactory
;
12 use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper
;
13 use Shaarli\Front\Controller\Admin\ShaareManageController
;
14 use Shaarli\Http\HttpAccess
;
15 use Shaarli\Security\SessionManager
;
17 use Slim\Http\Request
;
18 use Slim\Http\Response
;
20 class ChangeVisibilityBookmarkTest
extends TestCase
22 use FrontAdminControllerMockHelper
;
24 /** @var ShaareManageController */
25 protected $controller;
27 public function setUp(): void
29 $this->createContainer();
31 $this->container
->httpAccess
= $this->createMock(HttpAccess
::class);
32 $this->controller
= new ShaareManageController($this->container
);
36 * Change bookmark visibility - Set private - Single public bookmark with valid parameters
38 public function testSetSingleBookmarkPrivate(): void
40 $parameters = ['id' => '123', 'newVisibility' => 'private'];
42 $request = $this->createMock(Request
::class);
45 ->willReturnCallback(function (string $key) use ($parameters): ?string {
46 return $parameters[$key] ?? null;
49 $response = new Response();
51 $bookmark = (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123')->setPrivate(false);
53 static::assertFalse($bookmark->isPrivate());
55 $this->container
->bookmarkService
->expects(static::once())->method('get')->with(123)->willReturn($bookmark);
56 $this->container
->bookmarkService
->expects(static::once())->method('set')->with($bookmark, false);
57 $this->container
->bookmarkService
->expects(static::once())->method('save');
58 $this->container
->formatterFactory
= $this->createMock(FormatterFactory
::class);
59 $this->container
->formatterFactory
60 ->expects(static::once())
61 ->method('getFormatter')
63 ->willReturnCallback(function () use ($bookmark): BookmarkFormatter
{
64 return new BookmarkRawFormatter($this->container
->conf
, true);
68 // Make sure that PluginManager hook is triggered
69 $this->container
->pluginManager
70 ->expects(static::once())
71 ->method('executeHooks')
75 $result = $this->controller
->changeVisibility($request, $response);
77 static::assertTrue($bookmark->isPrivate());
79 static::assertSame(302, $result->getStatusCode());
80 static::assertSame(['/subfolder/'], $result->getHeader('location'));
84 * Change bookmark visibility - Set public - Single private bookmark with valid parameters
86 public function testSetSingleBookmarkPublic(): void
88 $parameters = ['id' => '123', 'newVisibility' => 'public'];
90 $request = $this->createMock(Request
::class);
93 ->willReturnCallback(function (string $key) use ($parameters): ?string {
94 return $parameters[$key] ?? null;
97 $response = new Response();
99 $bookmark = (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123')->setPrivate(true);
101 static::assertTrue($bookmark->isPrivate());
103 $this->container
->bookmarkService
->expects(static::once())->method('get')->with(123)->willReturn($bookmark);
104 $this->container
->bookmarkService
->expects(static::once())->method('set')->with($bookmark, false);
105 $this->container
->bookmarkService
->expects(static::once())->method('save');
106 $this->container
->formatterFactory
= $this->createMock(FormatterFactory
::class);
107 $this->container
->formatterFactory
108 ->expects(static::once())
109 ->method('getFormatter')
111 ->willReturn(new BookmarkRawFormatter($this->container
->conf
, true))
114 // Make sure that PluginManager hook is triggered
115 $this->container
->pluginManager
116 ->expects(static::once())
117 ->method('executeHooks')
121 $result = $this->controller
->changeVisibility($request, $response);
123 static::assertFalse($bookmark->isPrivate());
125 static::assertSame(302, $result->getStatusCode());
126 static::assertSame(['/subfolder/'], $result->getHeader('location'));
130 * Change bookmark visibility - Set private on single already private bookmark
132 public function testSetSinglePrivateBookmarkPrivate(): void
134 $parameters = ['id' => '123', 'newVisibility' => 'private'];
136 $request = $this->createMock(Request
::class);
139 ->willReturnCallback(function (string $key) use ($parameters): ?string {
140 return $parameters[$key] ?? null;
143 $response = new Response();
145 $bookmark = (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123')->setPrivate(true);
147 static::assertTrue($bookmark->isPrivate());
149 $this->container
->bookmarkService
->expects(static::once())->method('get')->with(123)->willReturn($bookmark);
150 $this->container
->bookmarkService
->expects(static::once())->method('set')->with($bookmark, false);
151 $this->container
->bookmarkService
->expects(static::once())->method('save');
152 $this->container
->formatterFactory
= $this->createMock(FormatterFactory
::class);
153 $this->container
->formatterFactory
154 ->expects(static::once())
155 ->method('getFormatter')
157 ->willReturn(new BookmarkRawFormatter($this->container
->conf
, true))
160 // Make sure that PluginManager hook is triggered
161 $this->container
->pluginManager
162 ->expects(static::once())
163 ->method('executeHooks')
167 $result = $this->controller
->changeVisibility($request, $response);
169 static::assertTrue($bookmark->isPrivate());
171 static::assertSame(302, $result->getStatusCode());
172 static::assertSame(['/subfolder/'], $result->getHeader('location'));
176 * Change bookmark visibility - Set multiple bookmarks private
178 public function testSetMultipleBookmarksPrivate(): void
180 $parameters = ['id' => '123 456 789', 'newVisibility' => 'private'];
182 $request = $this->createMock(Request
::class);
185 ->willReturnCallback(function (string $key) use ($parameters): ?string {
186 return $parameters[$key] ?? null;
189 $response = new Response();
192 (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123')->setPrivate(false),
193 (new Bookmark())->setId(456)->setUrl('http://domain.tld')->setTitle('Title 456')->setPrivate(true),
194 (new Bookmark())->setId(789)->setUrl('http://domain.tld')->setTitle('Title 789')->setPrivate(false),
197 $this->container
->bookmarkService
198 ->expects(static::exactly(3))
200 ->withConsecutive([123], [456], [789])
201 ->willReturnOnConsecutiveCalls(...$bookmarks)
203 $this->container
->bookmarkService
204 ->expects(static::exactly(3))
206 ->withConsecutive(...array_map(function (Bookmark
$bookmark): array {
207 return [$bookmark, false];
210 $this->container
->bookmarkService
->expects(static::once())->method('save');
211 $this->container
->formatterFactory
= $this->createMock(FormatterFactory
::class);
212 $this->container
->formatterFactory
213 ->expects(static::once())
214 ->method('getFormatter')
216 ->willReturn(new BookmarkRawFormatter($this->container
->conf
, true))
219 // Make sure that PluginManager hook is triggered
220 $this->container
->pluginManager
221 ->expects(static::exactly(3))
222 ->method('executeHooks')
226 $result = $this->controller
->changeVisibility($request, $response);
228 static::assertTrue($bookmarks[0]->isPrivate());
229 static::assertTrue($bookmarks[1]->isPrivate());
230 static::assertTrue($bookmarks[2]->isPrivate());
232 static::assertSame(302, $result->getStatusCode());
233 static::assertSame(['/subfolder/'], $result->getHeader('location'));
237 * Change bookmark visibility - Single bookmark not found.
239 public function testChangeVisibilitySingleBookmarkNotFound(): void
241 $parameters = ['id' => '123', 'newVisibility' => 'private'];
243 $request = $this->createMock(Request
::class);
246 ->willReturnCallback(function (string $key) use ($parameters): ?string {
247 return $parameters[$key] ?? null;
250 $response = new Response();
252 $this->container
->bookmarkService
253 ->expects(static::once())
255 ->willThrowException(new BookmarkNotFoundException())
257 $this->container
->bookmarkService
->expects(static::never())->method('set');
258 $this->container
->bookmarkService
->expects(static::never())->method('save');
259 $this->container
->formatterFactory
= $this->createMock(FormatterFactory
::class);
260 $this->container
->formatterFactory
261 ->expects(static::once())
262 ->method('getFormatter')
264 ->willReturn(new BookmarkRawFormatter($this->container
->conf
, true))
267 // Make sure that PluginManager hook is not triggered
268 $this->container
->pluginManager
269 ->expects(static::never())
270 ->method('executeHooks')
274 $result = $this->controller
->changeVisibility($request, $response);
276 static::assertSame(302, $result->getStatusCode());
277 static::assertSame(['/subfolder/'], $result->getHeader('location'));
281 * Change bookmark visibility - Multiple bookmarks with one not found.
283 public function testChangeVisibilityMultipleBookmarksOneNotFound(): void
285 $parameters = ['id' => '123 456 789', 'newVisibility' => 'public'];
287 $request = $this->createMock(Request
::class);
290 ->willReturnCallback(function (string $key) use ($parameters): ?string {
291 return $parameters[$key] ?? null;
294 $response = new Response();
297 (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123')->setPrivate(true),
298 (new Bookmark())->setId(789)->setUrl('http://domain.tld')->setTitle('Title 789')->setPrivate(false),
301 $this->container
->bookmarkService
302 ->expects(static::exactly(3))
304 ->withConsecutive([123], [456], [789])
305 ->willReturnCallback(function (int $id) use ($bookmarks): Bookmark
{
307 return $bookmarks[0];
310 return $bookmarks[1];
312 throw new BookmarkNotFoundException();
315 $this->container
->bookmarkService
316 ->expects(static::exactly(2))
318 ->withConsecutive(...array_map(function (Bookmark
$bookmark): array {
319 return [$bookmark, false];
322 $this->container
->bookmarkService
->expects(static::once())->method('save');
324 // Make sure that PluginManager hook is not triggered
325 $this->container
->pluginManager
326 ->expects(static::exactly(2))
327 ->method('executeHooks')
331 $this->container
->sessionManager
332 ->expects(static::once())
333 ->method('setSessionParameter')
334 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Bookmark with identifier 456 could not be found.'])
337 $result = $this->controller
->changeVisibility($request, $response);
339 static::assertSame(302, $result->getStatusCode());
340 static::assertSame(['/subfolder/'], $result->getHeader('location'));
344 * Change bookmark visibility - Invalid ID
346 public function testChangeVisibilityInvalidId(): void
348 $parameters = ['id' => 'nope not an ID', 'newVisibility' => 'private'];
350 $request = $this->createMock(Request
::class);
353 ->willReturnCallback(function (string $key) use ($parameters): ?string {
354 return $parameters[$key] ?? null;
357 $response = new Response();
359 $this->container
->sessionManager
360 ->expects(static::once())
361 ->method('setSessionParameter')
362 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Invalid bookmark ID provided.'])
365 $result = $this->controller
->changeVisibility($request, $response);
367 static::assertSame(302, $result->getStatusCode());
368 static::assertSame(['/subfolder/'], $result->getHeader('location'));
372 * Change bookmark visibility - Empty ID
374 public function testChangeVisibilityEmptyId(): void
376 $request = $this->createMock(Request
::class);
377 $response = new Response();
379 $this->container
->sessionManager
380 ->expects(static::once())
381 ->method('setSessionParameter')
382 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Invalid bookmark ID provided.'])
385 $result = $this->controller
->changeVisibility($request, $response);
387 static::assertSame(302, $result->getStatusCode());
388 static::assertSame(['/subfolder/'], $result->getHeader('location'));
392 * Change bookmark visibility - with invalid visibility
394 public function testChangeVisibilityWithInvalidVisibility(): void
396 $parameters = ['id' => '123', 'newVisibility' => 'invalid'];
398 $request = $this->createMock(Request
::class);
401 ->willReturnCallback(function (string $key) use ($parameters): ?string {
402 return $parameters[$key] ?? null;
405 $response = new Response();
407 $this->container
->sessionManager
408 ->expects(static::once())
409 ->method('setSessionParameter')
410 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Invalid visibility provided.'])
413 $result = $this->controller
->changeVisibility($request, $response);
415 static::assertSame(302, $result->getStatusCode());
416 static::assertSame(['/subfolder/'], $result->getHeader('location'));