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
;
13 class SessionFilterControllerTest
extends TestCase
15 use FrontAdminControllerMockHelper
;
17 /** @var SessionFilterController */
18 protected $controller;
20 public function setUp(): void
22 $this->createContainer();
24 $this->controller
= new SessionFilterController($this->container
);
28 * Link per page - Default call with valid parameter and a referer.
30 public function testLinksPerPage(): void
32 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
34 $request = $this->createMock(Request
::class);
35 $request->method('getParam')->with('nb')->willReturn('8');
36 $response = new Response();
38 $this->container
->sessionManager
39 ->expects(static::once())
40 ->method('setSessionParameter')
41 ->with(SessionManager
::KEY_LINKS_PER_PAGE
, 8)
44 $result = $this->controller
->linksPerPage($request, $response);
46 static::assertInstanceOf(Response
::class, $result);
47 static::assertSame(302, $result->getStatusCode());
48 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
52 * Link per page - Invalid value, should use default value (20)
54 public function testLinksPerPageNotValid(): void
56 $request = $this->createMock(Request
::class);
57 $request->method('getParam')->with('nb')->willReturn('test');
58 $response = new Response();
60 $this->container
->sessionManager
61 ->expects(static::once())
62 ->method('setSessionParameter')
63 ->with(SessionManager
::KEY_LINKS_PER_PAGE
, 20)
66 $result = $this->controller
->linksPerPage($request, $response);
68 static::assertInstanceOf(Response
::class, $result);
69 static::assertSame(302, $result->getStatusCode());
70 static::assertSame(['/subfolder/'], $result->getHeader('location'));
74 * Visibility - Default call for private filter while logged in without current value
76 public function testVisibility(): void
78 $arg = ['visibility' => 'private'];
80 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
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')
89 $request = $this->createMock(Request
::class);
90 $response = new Response();
92 $result = $this->controller
->visibility($request, $response, $arg);
94 static::assertInstanceOf(Response
::class, $result);
95 static::assertSame(302, $result->getStatusCode());
96 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
100 * Visibility - Toggle off private visibility
102 public function testVisibilityToggleOff(): void
104 $arg = ['visibility' => 'private'];
106 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
108 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
109 $this->container
->sessionManager
110 ->method('getSessionParameter')
111 ->with(SessionManager
::KEY_VISIBILITY
)
112 ->willReturn('private')
114 $this->container
->sessionManager
115 ->expects(static::never())
116 ->method('setSessionParameter')
118 $this->container
->sessionManager
119 ->expects(static::once())
120 ->method('deleteSessionParameter')
121 ->with(SessionManager
::KEY_VISIBILITY
)
124 $request = $this->createMock(Request
::class);
125 $response = new Response();
127 $result = $this->controller
->visibility($request, $response, $arg);
129 static::assertInstanceOf(Response
::class, $result);
130 static::assertSame(302, $result->getStatusCode());
131 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
135 * Visibility - Change private to public
137 public function testVisibilitySwitch(): void
139 $arg = ['visibility' => 'private'];
141 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
142 $this->container
->sessionManager
143 ->method('getSessionParameter')
144 ->with(SessionManager
::KEY_VISIBILITY
)
145 ->willReturn('public')
147 $this->container
->sessionManager
148 ->expects(static::once())
149 ->method('setSessionParameter')
150 ->with(SessionManager
::KEY_VISIBILITY
, 'private')
153 $request = $this->createMock(Request
::class);
154 $response = new Response();
156 $result = $this->controller
->visibility($request, $response, $arg);
158 static::assertInstanceOf(Response
::class, $result);
159 static::assertSame(302, $result->getStatusCode());
160 static::assertSame(['/subfolder/'], $result->getHeader('location'));
164 * Visibility - With invalid value - should remove any visibility setting
166 public function testVisibilityInvalidValue(): void
168 $arg = ['visibility' => 'test'];
170 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
172 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
173 $this->container
->sessionManager
174 ->expects(static::never())
175 ->method('setSessionParameter')
177 $this->container
->sessionManager
178 ->expects(static::once())
179 ->method('deleteSessionParameter')
180 ->with(SessionManager
::KEY_VISIBILITY
)
183 $request = $this->createMock(Request
::class);
184 $response = new Response();
186 $result = $this->controller
->visibility($request, $response, $arg);
188 static::assertInstanceOf(Response
::class, $result);
189 static::assertSame(302, $result->getStatusCode());
190 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
194 * Visibility - Try to change visibility while logged out
196 public function testVisibilityLoggedOut(): void
198 $arg = ['visibility' => 'test'];
200 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
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')
208 $this->container
->sessionManager
209 ->expects(static::never())
210 ->method('deleteSessionParameter')
211 ->with(SessionManager
::KEY_VISIBILITY
)
214 $request = $this->createMock(Request
::class);
215 $response = new Response();
217 $result = $this->controller
->visibility($request, $response, $arg);
219 static::assertInstanceOf(Response
::class, $result);
220 static::assertSame(302, $result->getStatusCode());
221 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
225 * Untagged only - valid call
227 public function testUntaggedOnly(): void
229 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
231 $request = $this->createMock(Request
::class);
232 $response = new Response();
234 $this->container
->sessionManager
235 ->expects(static::once())
236 ->method('setSessionParameter')
237 ->with(SessionManager
::KEY_UNTAGGED_ONLY
, true)
240 $result = $this->controller
->untaggedOnly($request, $response);
242 static::assertInstanceOf(Response
::class, $result);
243 static::assertSame(302, $result->getStatusCode());
244 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
248 * Untagged only - toggle off
250 public function testUntaggedOnlyToggleOff(): void
252 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
254 $request = $this->createMock(Request
::class);
255 $response = new Response();
257 $this->container
->sessionManager
258 ->method('getSessionParameter')
259 ->with(SessionManager
::KEY_UNTAGGED_ONLY
)
262 $this->container
->sessionManager
263 ->expects(static::once())
264 ->method('setSessionParameter')
265 ->with(SessionManager
::KEY_UNTAGGED_ONLY
, false)
268 $result = $this->controller
->untaggedOnly($request, $response);
270 static::assertInstanceOf(Response
::class, $result);
271 static::assertSame(302, $result->getStatusCode());
272 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));