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