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->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
35 $request = $this->createMock(Request
::class);
36 $request->method('getUri')->willReturnCallback(function (): Uri
{
37 $uri = $this->createMock(Uri
::class);
38 $uri->method('getBasePath')->willReturn('/subfolder');
42 $request->method('getParam')->with('nb')->willReturn('8');
43 $response = new Response();
45 $this->container
->sessionManager
46 ->expects(static::once())
47 ->method('setSessionParameter')
48 ->with(SessionManager
::KEY_LINKS_PER_PAGE
, 8)
51 $result = $this->controller
->linksPerPage($request, $response);
53 static::assertInstanceOf(Response
::class, $result);
54 static::assertSame(302, $result->getStatusCode());
55 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
59 * Link per page - Invalid value, should use default value (20)
61 public function testLinksPerPageNotValid(): void
63 $request = $this->createMock(Request
::class);
64 $request->method('getUri')->willReturnCallback(function (): Uri
{
65 $uri = $this->createMock(Uri
::class);
66 $uri->method('getBasePath')->willReturn('/subfolder');
70 $request->method('getParam')->with('nb')->willReturn('test');
71 $response = new Response();
73 $this->container
->sessionManager
74 ->expects(static::once())
75 ->method('setSessionParameter')
76 ->with(SessionManager
::KEY_LINKS_PER_PAGE
, 20)
79 $result = $this->controller
->linksPerPage($request, $response);
81 static::assertInstanceOf(Response
::class, $result);
82 static::assertSame(302, $result->getStatusCode());
83 static::assertSame(['/subfolder'], $result->getHeader('location'));
87 * Visibility - Default call for private filter while logged in without current value
89 public function testVisibility(): void
91 $arg = ['visibility' => 'private'];
93 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
95 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
96 $this->container
->sessionManager
97 ->expects(static::once())
98 ->method('setSessionParameter')
99 ->with(SessionManager
::KEY_VISIBILITY
, 'private')
102 $request = $this->createMock(Request
::class);
103 $request->method('getUri')->willReturnCallback(function (): Uri
{
104 $uri = $this->createMock(Uri
::class);
105 $uri->method('getBasePath')->willReturn('/subfolder');
109 $response = new Response();
111 $result = $this->controller
->visibility($request, $response, $arg);
113 static::assertInstanceOf(Response
::class, $result);
114 static::assertSame(302, $result->getStatusCode());
115 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
119 * Visibility - Toggle off private visibility
121 public function testVisibilityToggleOff(): void
123 $arg = ['visibility' => 'private'];
125 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
127 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
128 $this->container
->sessionManager
129 ->method('getSessionParameter')
130 ->with(SessionManager
::KEY_VISIBILITY
)
131 ->willReturn('private')
133 $this->container
->sessionManager
134 ->expects(static::never())
135 ->method('setSessionParameter')
137 $this->container
->sessionManager
138 ->expects(static::once())
139 ->method('deleteSessionParameter')
140 ->with(SessionManager
::KEY_VISIBILITY
)
143 $request = $this->createMock(Request
::class);
144 $request->method('getUri')->willReturnCallback(function (): Uri
{
145 $uri = $this->createMock(Uri
::class);
146 $uri->method('getBasePath')->willReturn('/subfolder');
150 $response = new Response();
152 $result = $this->controller
->visibility($request, $response, $arg);
154 static::assertInstanceOf(Response
::class, $result);
155 static::assertSame(302, $result->getStatusCode());
156 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
160 * Visibility - Change private to public
162 public function testVisibilitySwitch(): void
164 $arg = ['visibility' => 'private'];
166 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
167 $this->container
->sessionManager
168 ->method('getSessionParameter')
169 ->with(SessionManager
::KEY_VISIBILITY
)
170 ->willReturn('public')
172 $this->container
->sessionManager
173 ->expects(static::once())
174 ->method('setSessionParameter')
175 ->with(SessionManager
::KEY_VISIBILITY
, 'private')
178 $request = $this->createMock(Request
::class);
179 $request->method('getUri')->willReturnCallback(function (): Uri
{
180 $uri = $this->createMock(Uri
::class);
181 $uri->method('getBasePath')->willReturn('/subfolder');
185 $response = new Response();
187 $result = $this->controller
->visibility($request, $response, $arg);
189 static::assertInstanceOf(Response
::class, $result);
190 static::assertSame(302, $result->getStatusCode());
191 static::assertSame(['/subfolder'], $result->getHeader('location'));
195 * Visibility - With invalid value - should remove any visibility setting
197 public function testVisibilityInvalidValue(): void
199 $arg = ['visibility' => 'test'];
201 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
203 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
204 $this->container
->sessionManager
205 ->expects(static::never())
206 ->method('setSessionParameter')
208 $this->container
->sessionManager
209 ->expects(static::once())
210 ->method('deleteSessionParameter')
211 ->with(SessionManager
::KEY_VISIBILITY
)
214 $request = $this->createMock(Request
::class);
215 $request->method('getUri')->willReturnCallback(function (): Uri
{
216 $uri = $this->createMock(Uri
::class);
217 $uri->method('getBasePath')->willReturn('/subfolder');
221 $response = new Response();
223 $result = $this->controller
->visibility($request, $response, $arg);
225 static::assertInstanceOf(Response
::class, $result);
226 static::assertSame(302, $result->getStatusCode());
227 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
231 * Visibility - Try to change visibility while logged out
233 public function testVisibilityLoggedOut(): void
235 $arg = ['visibility' => 'test'];
237 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
239 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
240 $this->container
->loginManager
->method('isLoggedIn')->willReturn(false);
241 $this->container
->sessionManager
242 ->expects(static::never())
243 ->method('setSessionParameter')
245 $this->container
->sessionManager
246 ->expects(static::never())
247 ->method('deleteSessionParameter')
248 ->with(SessionManager
::KEY_VISIBILITY
)
251 $request = $this->createMock(Request
::class);
252 $request->method('getUri')->willReturnCallback(function (): Uri
{
253 $uri = $this->createMock(Uri
::class);
254 $uri->method('getBasePath')->willReturn('/subfolder');
258 $response = new Response();
260 $result = $this->controller
->visibility($request, $response, $arg);
262 static::assertInstanceOf(Response
::class, $result);
263 static::assertSame(302, $result->getStatusCode());
264 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
268 * Untagged only - valid call
270 public function testUntaggedOnly(): void
272 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
274 $request = $this->createMock(Request
::class);
275 $request->method('getUri')->willReturnCallback(function (): Uri
{
276 $uri = $this->createMock(Uri
::class);
277 $uri->method('getBasePath')->willReturn('/subfolder');
281 $response = new Response();
283 $this->container
->sessionManager
284 ->expects(static::once())
285 ->method('setSessionParameter')
286 ->with(SessionManager
::KEY_UNTAGGED_ONLY
, true)
289 $result = $this->controller
->untaggedOnly($request, $response);
291 static::assertInstanceOf(Response
::class, $result);
292 static::assertSame(302, $result->getStatusCode());
293 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
297 * Untagged only - toggle off
299 public function testUntaggedOnlyToggleOff(): void
301 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
303 $request = $this->createMock(Request
::class);
304 $request->method('getUri')->willReturnCallback(function (): Uri
{
305 $uri = $this->createMock(Uri
::class);
306 $uri->method('getBasePath')->willReturn('/subfolder');
311 $response = new Response();
313 $this->container
->sessionManager
314 ->method('getSessionParameter')
315 ->with(SessionManager
::KEY_UNTAGGED_ONLY
)
318 $this->container
->sessionManager
319 ->expects(static::once())
320 ->method('setSessionParameter')
321 ->with(SessionManager
::KEY_UNTAGGED_ONLY
, false)
324 $result = $this->controller
->untaggedOnly($request, $response);
326 static::assertInstanceOf(Response
::class, $result);
327 static::assertSame(302, $result->getStatusCode());
328 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));