3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
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
;
14 class SessionFilterControllerTest
extends TestCase
16 use FrontAdminControllerMockHelper
;
18 /** @var SessionFilterController */
19 protected $controller;
21 public function setUp(): void
23 $this->createContainer();
25 $this->controller
= new SessionFilterController($this->container
);
29 * Link per page - Default call with valid parameter and a referer.
31 public function testLinksPerPage(): void
33 $this->createValidContainerMockSet();
35 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
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');
44 $request->method('getParam')->with('nb')->willReturn('8');
45 $response = new Response();
47 $this->container
->sessionManager
48 ->expects(static::once())
49 ->method('setSessionParameter')
50 ->with(SessionManager
::KEY_LINKS_PER_PAGE
, 8)
53 $result = $this->controller
->linksPerPage($request, $response);
55 static::assertInstanceOf(Response
::class, $result);
56 static::assertSame(302, $result->getStatusCode());
57 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
61 * Link per page - Invalid value, should use default value (20)
63 public function testLinksPerPageNotValid(): void
65 $this->createValidContainerMockSet();
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');
74 $request->method('getParam')->with('nb')->willReturn('test');
75 $response = new Response();
77 $this->container
->sessionManager
78 ->expects(static::once())
79 ->method('setSessionParameter')
80 ->with(SessionManager
::KEY_LINKS_PER_PAGE
, 20)
83 $result = $this->controller
->linksPerPage($request, $response);
85 static::assertInstanceOf(Response
::class, $result);
86 static::assertSame(302, $result->getStatusCode());
87 static::assertSame(['/subfolder'], $result->getHeader('location'));
91 * Visibility - Default call for private filter while logged in without current value
93 public function testVisibility(): void
95 $this->createValidContainerMockSet();
97 $arg = ['visibility' => 'private'];
99 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
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')
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');
115 $response = new Response();
117 $result = $this->controller
->visibility($request, $response, $arg);
119 static::assertInstanceOf(Response
::class, $result);
120 static::assertSame(302, $result->getStatusCode());
121 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
125 * Visibility - Toggle off private visibility
127 public function testVisibilityToggleOff(): void
129 $this->createValidContainerMockSet();
131 $arg = ['visibility' => 'private'];
133 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
135 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
136 $this->container
->sessionManager
137 ->method('getSessionParameter')
138 ->with(SessionManager
::KEY_VISIBILITY
)
139 ->willReturn('private')
141 $this->container
->sessionManager
142 ->expects(static::never())
143 ->method('setSessionParameter')
145 $this->container
->sessionManager
146 ->expects(static::once())
147 ->method('deleteSessionParameter')
148 ->with(SessionManager
::KEY_VISIBILITY
)
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');
158 $response = new Response();
160 $result = $this->controller
->visibility($request, $response, $arg);
162 static::assertInstanceOf(Response
::class, $result);
163 static::assertSame(302, $result->getStatusCode());
164 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
168 * Visibility - Change private to public
170 public function testVisibilitySwitch(): void
172 $this->createValidContainerMockSet();
174 $arg = ['visibility' => 'private'];
176 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
177 $this->container
->sessionManager
178 ->method('getSessionParameter')
179 ->with(SessionManager
::KEY_VISIBILITY
)
180 ->willReturn('public')
182 $this->container
->sessionManager
183 ->expects(static::once())
184 ->method('setSessionParameter')
185 ->with(SessionManager
::KEY_VISIBILITY
, 'private')
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');
195 $response = new Response();
197 $result = $this->controller
->visibility($request, $response, $arg);
199 static::assertInstanceOf(Response
::class, $result);
200 static::assertSame(302, $result->getStatusCode());
201 static::assertSame(['/subfolder'], $result->getHeader('location'));
205 * Visibility - With invalid value - should remove any visibility setting
207 public function testVisibilityInvalidValue(): void
209 $this->createValidContainerMockSet();
211 $arg = ['visibility' => 'test'];
213 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
215 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
216 $this->container
->sessionManager
217 ->expects(static::never())
218 ->method('setSessionParameter')
220 $this->container
->sessionManager
221 ->expects(static::once())
222 ->method('deleteSessionParameter')
223 ->with(SessionManager
::KEY_VISIBILITY
)
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');
233 $response = new Response();
235 $result = $this->controller
->visibility($request, $response, $arg);
237 static::assertInstanceOf(Response
::class, $result);
238 static::assertSame(302, $result->getStatusCode());
239 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
243 * Visibility - Try to change visibility while logged out
245 public function testVisibilityLoggedOut(): void
247 $this->createValidContainerMockSet();
249 $arg = ['visibility' => 'test'];
251 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
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')
259 $this->container
->sessionManager
260 ->expects(static::never())
261 ->method('deleteSessionParameter')
262 ->with(SessionManager
::KEY_VISIBILITY
)
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');
272 $response = new Response();
274 $result = $this->controller
->visibility($request, $response, $arg);
276 static::assertInstanceOf(Response
::class, $result);
277 static::assertSame(302, $result->getStatusCode());
278 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
282 * Untagged only - valid call
284 public function testUntaggedOnly(): void
286 $this->createValidContainerMockSet();
288 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
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');
297 $response = new Response();
299 $this->container
->sessionManager
300 ->expects(static::once())
301 ->method('setSessionParameter')
302 ->with(SessionManager
::KEY_UNTAGGED_ONLY
, true)
305 $result = $this->controller
->untaggedOnly($request, $response);
307 static::assertInstanceOf(Response
::class, $result);
308 static::assertSame(302, $result->getStatusCode());
309 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
313 * Untagged only - toggle off
315 public function testUntaggedOnlyToggleOff(): void
317 $this->createValidContainerMockSet();
319 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
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');
329 $response = new Response();
331 $this->container
->sessionManager
332 ->method('getSessionParameter')
333 ->with(SessionManager
::KEY_UNTAGGED_ONLY
)
336 $this->container
->sessionManager
337 ->expects(static::once())
338 ->method('setSessionParameter')
339 ->with(SessionManager
::KEY_UNTAGGED_ONLY
, false)
342 $result = $this->controller
->untaggedOnly($request, $response);
344 static::assertInstanceOf(Response
::class, $result);
345 static::assertSame(302, $result->getStatusCode());
346 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));