]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest; | |
6 | ||
7 | use Shaarli\Bookmark\Bookmark; | |
8 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | |
9 | use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; | |
10 | use Shaarli\Front\Controller\Admin\ManageShaareController; | |
11 | use Shaarli\Http\HttpAccess; | |
12 | use Shaarli\Security\SessionManager; | |
13 | use Shaarli\TestCase; | |
14 | use Slim\Http\Request; | |
15 | use Slim\Http\Response; | |
16 | ||
17 | class PinBookmarkTest extends TestCase | |
18 | { | |
19 | use FrontAdminControllerMockHelper; | |
20 | ||
21 | /** @var ManageShaareController */ | |
22 | protected $controller; | |
23 | ||
24 | public function setUp(): void | |
25 | { | |
26 | $this->createContainer(); | |
27 | ||
28 | $this->container->httpAccess = $this->createMock(HttpAccess::class); | |
29 | $this->controller = new ManageShaareController($this->container); | |
30 | } | |
31 | ||
32 | /** | |
33 | * Test pin bookmark - with valid input | |
34 | * | |
35 | * @dataProvider initialStickyValuesProvider() | |
36 | */ | |
37 | public function testPinBookmarkIsStickyNull(?bool $sticky, bool $expectedValue): void | |
38 | { | |
39 | $id = 123; | |
40 | ||
41 | $request = $this->createMock(Request::class); | |
42 | $response = new Response(); | |
43 | ||
44 | $bookmark = (new Bookmark()) | |
45 | ->setId(123) | |
46 | ->setUrl('http://domain.tld') | |
47 | ->setTitle('Title 123') | |
48 | ->setSticky($sticky) | |
49 | ; | |
50 | ||
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); | |
53 | ||
54 | // Make sure that PluginManager hook is triggered | |
55 | $this->container->pluginManager | |
56 | ->expects(static::once()) | |
57 | ->method('executeHooks') | |
58 | ->with('save_link') | |
59 | ; | |
60 | ||
61 | $result = $this->controller->pinBookmark($request, $response, ['id' => (string) $id]); | |
62 | ||
63 | static::assertSame(302, $result->getStatusCode()); | |
64 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | |
65 | ||
66 | static::assertSame($expectedValue, $bookmark->isSticky()); | |
67 | } | |
68 | ||
69 | public function initialStickyValuesProvider(): array | |
70 | { | |
71 | // [initialStickyState, isStickyAfterPin] | |
72 | return [[null, true], [false, true], [true, false]]; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Test pin bookmark - invalid bookmark ID | |
77 | */ | |
78 | public function testDisplayEditFormInvalidId(): void | |
79 | { | |
80 | $id = 'invalid'; | |
81 | ||
82 | $request = $this->createMock(Request::class); | |
83 | $response = new Response(); | |
84 | ||
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.']) | |
89 | ; | |
90 | ||
91 | $result = $this->controller->pinBookmark($request, $response, ['id' => $id]); | |
92 | ||
93 | static::assertSame(302, $result->getStatusCode()); | |
94 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Test pin bookmark - Bookmark ID not provided | |
99 | */ | |
100 | public function testDisplayEditFormIdNotProvided(): void | |
101 | { | |
102 | $request = $this->createMock(Request::class); | |
103 | $response = new Response(); | |
104 | ||
105 | $this->container->sessionManager | |
106 | ->expects(static::once()) | |
107 | ->method('setSessionParameter') | |
108 | ->with(SessionManager::KEY_ERROR_MESSAGES, ['Bookmark with identifier could not be found.']) | |
109 | ; | |
110 | ||
111 | $result = $this->controller->pinBookmark($request, $response, []); | |
112 | ||
113 | static::assertSame(302, $result->getStatusCode()); | |
114 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Test pin bookmark - bookmark not found | |
119 | */ | |
120 | public function testDisplayEditFormBookmarkNotFound(): void | |
121 | { | |
122 | $id = 123; | |
123 | ||
124 | $request = $this->createMock(Request::class); | |
125 | $response = new Response(); | |
126 | ||
127 | $this->container->bookmarkService | |
128 | ->expects(static::once()) | |
129 | ->method('get') | |
130 | ->with($id) | |
131 | ->willThrowException(new BookmarkNotFoundException()) | |
132 | ; | |
133 | ||
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.']) | |
138 | ; | |
139 | ||
140 | $result = $this->controller->pinBookmark($request, $response, ['id' => (string) $id]); | |
141 | ||
142 | static::assertSame(302, $result->getStatusCode()); | |
143 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | |
144 | } | |
145 | } |