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\Front\Controller\Admin\FrontAdminControllerMockHelper
;
10 use Shaarli\Front\Controller\Admin\ShaareManageController
;
11 use Shaarli\Http\HttpAccess
;
12 use Shaarli\Security\SessionManager
;
14 use Slim\Http\Request
;
15 use Slim\Http\Response
;
17 class PinBookmarkTest
extends TestCase
19 use FrontAdminControllerMockHelper
;
21 /** @var ShaareManageController */
22 protected $controller;
24 public function setUp(): void
26 $this->createContainer();
28 $this->container
->httpAccess
= $this->createMock(HttpAccess
::class);
29 $this->controller
= new ShaareManageController($this->container
);
33 * Test pin bookmark - with valid input
35 * @dataProvider initialStickyValuesProvider()
37 public function testPinBookmarkIsStickyNull(?bool $sticky, bool $expectedValue): void
41 $request = $this->createMock(Request
::class);
42 $response = new Response();
44 $bookmark = (new Bookmark())
46 ->setUrl('http://domain.tld')
47 ->setTitle('Title 123')
51 $this->container
->bookmarkService
->expects(static::once())->method('get')->with(123)->willReturn($bookmark);
52 $this->container
->bookmarkService
->expects(static::once())->method('set')->with($bookmark, true);
54 // Make sure that PluginManager hook is triggered
55 $this->container
->pluginManager
56 ->expects(static::once())
57 ->method('executeHooks')
61 $result = $this->controller
->pinBookmark($request, $response, ['id' => (string) $id]);
63 static::assertSame(302, $result->getStatusCode());
64 static::assertSame(['/subfolder/'], $result->getHeader('location'));
66 static::assertSame($expectedValue, $bookmark->isSticky());
69 public function initialStickyValuesProvider(): array
71 // [initialStickyState, isStickyAfterPin]
72 return [[null, true], [false, true], [true, false]];
76 * Test pin bookmark - invalid bookmark ID
78 public function testDisplayEditFormInvalidId(): void
82 $request = $this->createMock(Request
::class);
83 $response = new Response();
85 $this->container
->sessionManager
86 ->expects(static::once())
87 ->method('setSessionParameter')
88 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Bookmark with identifier invalid could not be found.'])
91 $result = $this->controller
->pinBookmark($request, $response, ['id' => $id]);
93 static::assertSame(302, $result->getStatusCode());
94 static::assertSame(['/subfolder/'], $result->getHeader('location'));
98 * Test pin bookmark - Bookmark ID not provided
100 public function testDisplayEditFormIdNotProvided(): void
102 $request = $this->createMock(Request
::class);
103 $response = new Response();
105 $this->container
->sessionManager
106 ->expects(static::once())
107 ->method('setSessionParameter')
108 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Bookmark with identifier could not be found.'])
111 $result = $this->controller
->pinBookmark($request, $response, []);
113 static::assertSame(302, $result->getStatusCode());
114 static::assertSame(['/subfolder/'], $result->getHeader('location'));
118 * Test pin bookmark - bookmark not found
120 public function testDisplayEditFormBookmarkNotFound(): void
124 $request = $this->createMock(Request
::class);
125 $response = new Response();
127 $this->container
->bookmarkService
128 ->expects(static::once())
131 ->willThrowException(new BookmarkNotFoundException())
134 $this->container
->sessionManager
135 ->expects(static::once())
136 ->method('setSessionParameter')
137 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Bookmark with identifier 123 could not be found.'])
140 $result = $this->controller
->pinBookmark($request, $response, ['id' => (string) $id]);
142 static::assertSame(302, $result->getStatusCode());
143 static::assertSame(['/subfolder/'], $result->getHeader('location'));