]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin; | |
6 | ||
7 | use PHPUnit\Framework\TestCase; | |
8 | use Shaarli\Security\LoginManager; | |
9 | use Shaarli\Security\SessionManager; | |
10 | use Slim\Http\Request; | |
11 | use Slim\Http\Response; | |
12 | ||
13 | class SessionFilterControllerTest extends TestCase | |
14 | { | |
15 | use FrontAdminControllerMockHelper; | |
16 | ||
17 | /** @var SessionFilterController */ | |
18 | protected $controller; | |
19 | ||
20 | public function setUp(): void | |
21 | { | |
22 | $this->createContainer(); | |
23 | ||
24 | $this->controller = new SessionFilterController($this->container); | |
25 | } | |
26 | ||
27 | /** | |
28 | * Link per page - Default call with valid parameter and a referer. | |
29 | */ | |
30 | public function testLinksPerPage(): void | |
31 | { | |
32 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; | |
33 | ||
34 | $request = $this->createMock(Request::class); | |
35 | $request->method('getParam')->with('nb')->willReturn('8'); | |
36 | $response = new Response(); | |
37 | ||
38 | $this->container->sessionManager | |
39 | ->expects(static::once()) | |
40 | ->method('setSessionParameter') | |
41 | ->with(SessionManager::KEY_LINKS_PER_PAGE, 8) | |
42 | ; | |
43 | ||
44 | $result = $this->controller->linksPerPage($request, $response); | |
45 | ||
46 | static::assertInstanceOf(Response::class, $result); | |
47 | static::assertSame(302, $result->getStatusCode()); | |
48 | static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location')); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Link per page - Invalid value, should use default value (20) | |
53 | */ | |
54 | public function testLinksPerPageNotValid(): void | |
55 | { | |
56 | $request = $this->createMock(Request::class); | |
57 | $request->method('getParam')->with('nb')->willReturn('test'); | |
58 | $response = new Response(); | |
59 | ||
60 | $this->container->sessionManager | |
61 | ->expects(static::once()) | |
62 | ->method('setSessionParameter') | |
63 | ->with(SessionManager::KEY_LINKS_PER_PAGE, 20) | |
64 | ; | |
65 | ||
66 | $result = $this->controller->linksPerPage($request, $response); | |
67 | ||
68 | static::assertInstanceOf(Response::class, $result); | |
69 | static::assertSame(302, $result->getStatusCode()); | |
70 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | |
71 | } | |
72 | ||
73 | /** | |
74 | * Visibility - Default call for private filter while logged in without current value | |
75 | */ | |
76 | public function testVisibility(): void | |
77 | { | |
78 | $arg = ['visibility' => 'private']; | |
79 | ||
80 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; | |
81 | ||
82 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | |
83 | $this->container->sessionManager | |
84 | ->expects(static::once()) | |
85 | ->method('setSessionParameter') | |
86 | ->with(SessionManager::KEY_VISIBILITY, 'private') | |
87 | ; | |
88 | ||
89 | $request = $this->createMock(Request::class); | |
90 | $response = new Response(); | |
91 | ||
92 | $result = $this->controller->visibility($request, $response, $arg); | |
93 | ||
94 | static::assertInstanceOf(Response::class, $result); | |
95 | static::assertSame(302, $result->getStatusCode()); | |
96 | static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location')); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Visibility - Toggle off private visibility | |
101 | */ | |
102 | public function testVisibilityToggleOff(): void | |
103 | { | |
104 | $arg = ['visibility' => 'private']; | |
105 | ||
106 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; | |
107 | ||
108 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | |
109 | $this->container->sessionManager | |
110 | ->method('getSessionParameter') | |
111 | ->with(SessionManager::KEY_VISIBILITY) | |
112 | ->willReturn('private') | |
113 | ; | |
114 | $this->container->sessionManager | |
115 | ->expects(static::never()) | |
116 | ->method('setSessionParameter') | |
117 | ; | |
118 | $this->container->sessionManager | |
119 | ->expects(static::once()) | |
120 | ->method('deleteSessionParameter') | |
121 | ->with(SessionManager::KEY_VISIBILITY) | |
122 | ; | |
123 | ||
124 | $request = $this->createMock(Request::class); | |
125 | $response = new Response(); | |
126 | ||
127 | $result = $this->controller->visibility($request, $response, $arg); | |
128 | ||
129 | static::assertInstanceOf(Response::class, $result); | |
130 | static::assertSame(302, $result->getStatusCode()); | |
131 | static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location')); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Visibility - Change private to public | |
136 | */ | |
137 | public function testVisibilitySwitch(): void | |
138 | { | |
139 | $arg = ['visibility' => 'private']; | |
140 | ||
141 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | |
142 | $this->container->sessionManager | |
143 | ->method('getSessionParameter') | |
144 | ->with(SessionManager::KEY_VISIBILITY) | |
145 | ->willReturn('public') | |
146 | ; | |
147 | $this->container->sessionManager | |
148 | ->expects(static::once()) | |
149 | ->method('setSessionParameter') | |
150 | ->with(SessionManager::KEY_VISIBILITY, 'private') | |
151 | ; | |
152 | ||
153 | $request = $this->createMock(Request::class); | |
154 | $response = new Response(); | |
155 | ||
156 | $result = $this->controller->visibility($request, $response, $arg); | |
157 | ||
158 | static::assertInstanceOf(Response::class, $result); | |
159 | static::assertSame(302, $result->getStatusCode()); | |
160 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | |
161 | } | |
162 | ||
163 | /** | |
164 | * Visibility - With invalid value - should remove any visibility setting | |
165 | */ | |
166 | public function testVisibilityInvalidValue(): void | |
167 | { | |
168 | $arg = ['visibility' => 'test']; | |
169 | ||
170 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; | |
171 | ||
172 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | |
173 | $this->container->sessionManager | |
174 | ->expects(static::never()) | |
175 | ->method('setSessionParameter') | |
176 | ; | |
177 | $this->container->sessionManager | |
178 | ->expects(static::once()) | |
179 | ->method('deleteSessionParameter') | |
180 | ->with(SessionManager::KEY_VISIBILITY) | |
181 | ; | |
182 | ||
183 | $request = $this->createMock(Request::class); | |
184 | $response = new Response(); | |
185 | ||
186 | $result = $this->controller->visibility($request, $response, $arg); | |
187 | ||
188 | static::assertInstanceOf(Response::class, $result); | |
189 | static::assertSame(302, $result->getStatusCode()); | |
190 | static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location')); | |
191 | } | |
192 | ||
193 | /** | |
194 | * Visibility - Try to change visibility while logged out | |
195 | */ | |
196 | public function testVisibilityLoggedOut(): void | |
197 | { | |
198 | $arg = ['visibility' => 'test']; | |
199 | ||
200 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; | |
201 | ||
202 | $this->container->loginManager = $this->createMock(LoginManager::class); | |
203 | $this->container->loginManager->method('isLoggedIn')->willReturn(false); | |
204 | $this->container->sessionManager | |
205 | ->expects(static::never()) | |
206 | ->method('setSessionParameter') | |
207 | ; | |
208 | $this->container->sessionManager | |
209 | ->expects(static::never()) | |
210 | ->method('deleteSessionParameter') | |
211 | ->with(SessionManager::KEY_VISIBILITY) | |
212 | ; | |
213 | ||
214 | $request = $this->createMock(Request::class); | |
215 | $response = new Response(); | |
216 | ||
217 | $result = $this->controller->visibility($request, $response, $arg); | |
218 | ||
219 | static::assertInstanceOf(Response::class, $result); | |
220 | static::assertSame(302, $result->getStatusCode()); | |
221 | static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location')); | |
222 | } | |
223 | ||
224 | /** | |
225 | * Untagged only - valid call | |
226 | */ | |
227 | public function testUntaggedOnly(): void | |
228 | { | |
229 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; | |
230 | ||
231 | $request = $this->createMock(Request::class); | |
232 | $response = new Response(); | |
233 | ||
234 | $this->container->sessionManager | |
235 | ->expects(static::once()) | |
236 | ->method('setSessionParameter') | |
237 | ->with(SessionManager::KEY_UNTAGGED_ONLY, true) | |
238 | ; | |
239 | ||
240 | $result = $this->controller->untaggedOnly($request, $response); | |
241 | ||
242 | static::assertInstanceOf(Response::class, $result); | |
243 | static::assertSame(302, $result->getStatusCode()); | |
244 | static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location')); | |
245 | } | |
246 | ||
247 | /** | |
248 | * Untagged only - toggle off | |
249 | */ | |
250 | public function testUntaggedOnlyToggleOff(): void | |
251 | { | |
252 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; | |
253 | ||
254 | $request = $this->createMock(Request::class); | |
255 | $response = new Response(); | |
256 | ||
257 | $this->container->sessionManager | |
258 | ->method('getSessionParameter') | |
259 | ->with(SessionManager::KEY_UNTAGGED_ONLY) | |
260 | ->willReturn(true) | |
261 | ; | |
262 | $this->container->sessionManager | |
263 | ->expects(static::once()) | |
264 | ->method('setSessionParameter') | |
265 | ->with(SessionManager::KEY_UNTAGGED_ONLY, false) | |
266 | ; | |
267 | ||
268 | $result = $this->controller->untaggedOnly($request, $response); | |
269 | ||
270 | static::assertInstanceOf(Response::class, $result); | |
271 | static::assertSame(302, $result->getStatusCode()); | |
272 | static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location')); | |
273 | } | |
274 | } |