aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin
diff options
context:
space:
mode:
Diffstat (limited to 'tests/front/controller/admin')
-rw-r--r--tests/front/controller/admin/FrontAdminControllerMockHelper.php34
-rw-r--r--tests/front/controller/admin/LogoutControllerTest.php57
-rw-r--r--tests/front/controller/admin/SessionFilterControllerTest.php348
3 files changed, 439 insertions, 0 deletions
diff --git a/tests/front/controller/admin/FrontAdminControllerMockHelper.php b/tests/front/controller/admin/FrontAdminControllerMockHelper.php
new file mode 100644
index 00000000..94581c09
--- /dev/null
+++ b/tests/front/controller/admin/FrontAdminControllerMockHelper.php
@@ -0,0 +1,34 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use Shaarli\Container\ShaarliTestContainer;
8use Shaarli\Front\Controller\Visitor\FrontControllerMockHelper;
9use Shaarli\Security\LoginManager;
10
11/**
12 * Trait FrontControllerMockHelper
13 *
14 * Helper trait used to initialize the ShaarliContainer and mock its services for admin controller tests.
15 *
16 * @property ShaarliTestContainer $container
17 */
18trait FrontAdminControllerMockHelper
19{
20 use FrontControllerMockHelper {
21 FrontControllerMockHelper::createContainer as parentCreateContainer;
22 }
23
24 /**
25 * Mock the container instance
26 */
27 protected function createContainer(): void
28 {
29 $this->parentCreateContainer();
30
31 $this->container->loginManager = $this->createMock(LoginManager::class);
32 $this->container->loginManager->method('isLoggedIn')->willReturn(true);
33 }
34}
diff --git a/tests/front/controller/admin/LogoutControllerTest.php b/tests/front/controller/admin/LogoutControllerTest.php
new file mode 100644
index 00000000..239e39b2
--- /dev/null
+++ b/tests/front/controller/admin/LogoutControllerTest.php
@@ -0,0 +1,57 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7/** Override PHP builtin setcookie function in the local namespace to mock it... more or less */
8if (!function_exists('Shaarli\Front\Controller\setcookie')) {
9 function setcookie(string $name, string $value): void {
10 $_COOKIE[$name] = $value;
11 }
12}
13
14use PHPUnit\Framework\TestCase;
15use Shaarli\Security\LoginManager;
16use Shaarli\Security\SessionManager;
17use Slim\Http\Request;
18use Slim\Http\Response;
19
20class LogoutControllerTest extends TestCase
21{
22 use FrontAdminControllerMockHelper;
23
24 /** @var LogoutController */
25 protected $controller;
26
27 public function setUp(): void
28 {
29 $this->createContainer();
30
31 $this->controller = new LogoutController($this->container);
32
33 setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there');
34 }
35
36 public function testValidControllerInvoke(): void
37 {
38 $this->createValidContainerMockSet();
39
40 $request = $this->createMock(Request::class);
41 $response = new Response();
42
43 $this->container->pageCacheManager->expects(static::once())->method('invalidateCaches');
44
45 $this->container->sessionManager = $this->createMock(SessionManager::class);
46 $this->container->sessionManager->expects(static::once())->method('logout');
47
48 static::assertSame('hi there', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]);
49
50 $result = $this->controller->index($request, $response);
51
52 static::assertInstanceOf(Response::class, $result);
53 static::assertSame(302, $result->getStatusCode());
54 static::assertContains('./', $result->getHeader('Location'));
55 static::assertSame('false', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]);
56 }
57}
diff --git a/tests/front/controller/admin/SessionFilterControllerTest.php b/tests/front/controller/admin/SessionFilterControllerTest.php
new file mode 100644
index 00000000..f50f2fc2
--- /dev/null
+++ b/tests/front/controller/admin/SessionFilterControllerTest.php
@@ -0,0 +1,348 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Security\LoginManager;
9use Shaarli\Security\SessionManager;
10use Slim\Http\Request;
11use Slim\Http\Response;
12use Slim\Http\Uri;
13
14class SessionFilterControllerTest extends TestCase
15{
16 use FrontAdminControllerMockHelper;
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);
38 $request->method('getUri')->willReturnCallback(function (): Uri {
39 $uri = $this->createMock(Uri::class);
40 $uri->method('getBasePath')->willReturn('/subfolder');
41
42 return $uri;
43 });
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);
68 $request->method('getUri')->willReturnCallback(function (): Uri {
69 $uri = $this->createMock(Uri::class);
70 $uri->method('getBasePath')->willReturn('/subfolder');
71
72 return $uri;
73 });
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());
87 static::assertSame(['/subfolder'], $result->getHeader('location'));
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);
109 $request->method('getUri')->willReturnCallback(function (): Uri {
110 $uri = $this->createMock(Uri::class);
111 $uri->method('getBasePath')->willReturn('/subfolder');
112
113 return $uri;
114 });
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);
152 $request->method('getUri')->willReturnCallback(function (): Uri {
153 $uri = $this->createMock(Uri::class);
154 $uri->method('getBasePath')->willReturn('/subfolder');
155
156 return $uri;
157 });
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);
189 $request->method('getUri')->willReturnCallback(function (): Uri {
190 $uri = $this->createMock(Uri::class);
191 $uri->method('getBasePath')->willReturn('/subfolder');
192
193 return $uri;
194 });
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());
201 static::assertSame(['/subfolder'], $result->getHeader('location'));
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);
227 $request->method('getUri')->willReturnCallback(function (): Uri {
228 $uri = $this->createMock(Uri::class);
229 $uri->method('getBasePath')->willReturn('/subfolder');
230
231 return $uri;
232 });
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
253 $this->container->loginManager = $this->createMock(LoginManager::class);
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);
266 $request->method('getUri')->willReturnCallback(function (): Uri {
267 $uri = $this->createMock(Uri::class);
268 $uri->method('getBasePath')->willReturn('/subfolder');
269
270 return $uri;
271 });
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);
291 $request->method('getUri')->willReturnCallback(function (): Uri {
292 $uri = $this->createMock(Uri::class);
293 $uri->method('getBasePath')->willReturn('/subfolder');
294
295 return $uri;
296 });
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);
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
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}