3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Security\SessionManager
;
10 use Slim\Http\Response
;
12 class SessionFilterControllerTest
extends TestCase
14 use FrontControllerMockHelper
;
16 /** @var SessionFilterController */
17 protected $controller;
19 public function setUp(): void
21 $this->createContainer();
23 $this->controller
= new SessionFilterController($this->container
);
27 * Link per page - Default call with valid parameter and a referer.
29 public function testLinksPerPage(): void
31 $this->createValidContainerMockSet();
33 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
35 $request = $this->createMock(Request
::class);
36 $request->method('getParam')->with('nb')->willReturn('8');
37 $response = new Response();
39 $this->container
->sessionManager
40 ->expects(static::once())
41 ->method('setSessionParameter')
42 ->with(SessionManager
::KEY_LINKS_PER_PAGE
, 8)
45 $result = $this->controller
->linksPerPage($request, $response);
47 static::assertInstanceOf(Response
::class, $result);
48 static::assertSame(302, $result->getStatusCode());
49 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
53 * Link per page - Invalid value, should use default value (20)
55 public function testLinksPerPageNotValid(): void
57 $this->createValidContainerMockSet();
59 $request = $this->createMock(Request
::class);
60 $request->method('getParam')->with('nb')->willReturn('test');
61 $response = new Response();
63 $this->container
->sessionManager
64 ->expects(static::once())
65 ->method('setSessionParameter')
66 ->with(SessionManager
::KEY_LINKS_PER_PAGE
, 20)
69 $result = $this->controller
->linksPerPage($request, $response);
71 static::assertInstanceOf(Response
::class, $result);
72 static::assertSame(302, $result->getStatusCode());
73 static::assertSame(['./'], $result->getHeader('location'));
77 * Visibility - Default call for private filter while logged in without current value
79 public function testVisibility(): void
81 $this->createValidContainerMockSet();
83 $arg = ['visibility' => 'private'];
85 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
87 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
88 $this->container
->sessionManager
89 ->expects(static::once())
90 ->method('setSessionParameter')
91 ->with(SessionManager
::KEY_VISIBILITY
, 'private')
94 $request = $this->createMock(Request
::class);
95 $response = new Response();
97 $result = $this->controller
->visibility($request, $response, $arg);
99 static::assertInstanceOf(Response
::class, $result);
100 static::assertSame(302, $result->getStatusCode());
101 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
105 * Visibility - Toggle off private visibility
107 public function testVisibilityToggleOff(): void
109 $this->createValidContainerMockSet();
111 $arg = ['visibility' => 'private'];
113 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
115 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
116 $this->container
->sessionManager
117 ->method('getSessionParameter')
118 ->with(SessionManager
::KEY_VISIBILITY
)
119 ->willReturn('private')
121 $this->container
->sessionManager
122 ->expects(static::never())
123 ->method('setSessionParameter')
125 $this->container
->sessionManager
126 ->expects(static::once())
127 ->method('deleteSessionParameter')
128 ->with(SessionManager
::KEY_VISIBILITY
)
131 $request = $this->createMock(Request
::class);
132 $response = new Response();
134 $result = $this->controller
->visibility($request, $response, $arg);
136 static::assertInstanceOf(Response
::class, $result);
137 static::assertSame(302, $result->getStatusCode());
138 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
142 * Visibility - Change private to public
144 public function testVisibilitySwitch(): void
146 $this->createValidContainerMockSet();
148 $arg = ['visibility' => 'private'];
150 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
151 $this->container
->sessionManager
152 ->method('getSessionParameter')
153 ->with(SessionManager
::KEY_VISIBILITY
)
154 ->willReturn('public')
156 $this->container
->sessionManager
157 ->expects(static::once())
158 ->method('setSessionParameter')
159 ->with(SessionManager
::KEY_VISIBILITY
, 'private')
162 $request = $this->createMock(Request
::class);
163 $response = new Response();
165 $result = $this->controller
->visibility($request, $response, $arg);
167 static::assertInstanceOf(Response
::class, $result);
168 static::assertSame(302, $result->getStatusCode());
169 static::assertSame(['./'], $result->getHeader('location'));
173 * Visibility - With invalid value - should remove any visibility setting
175 public function testVisibilityInvalidValue(): void
177 $this->createValidContainerMockSet();
179 $arg = ['visibility' => 'test'];
181 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
183 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
184 $this->container
->sessionManager
185 ->expects(static::never())
186 ->method('setSessionParameter')
188 $this->container
->sessionManager
189 ->expects(static::once())
190 ->method('deleteSessionParameter')
191 ->with(SessionManager
::KEY_VISIBILITY
)
194 $request = $this->createMock(Request
::class);
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/controller/?searchtag=abc'], $result->getHeader('location'));
205 * Visibility - Try to change visibility while logged out
207 public function testVisibilityLoggedOut(): 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(false);
216 $this->container
->sessionManager
217 ->expects(static::never())
218 ->method('setSessionParameter')
220 $this->container
->sessionManager
221 ->expects(static::never())
222 ->method('deleteSessionParameter')
223 ->with(SessionManager
::KEY_VISIBILITY
)
226 $request = $this->createMock(Request
::class);
227 $response = new Response();
229 $result = $this->controller
->visibility($request, $response, $arg);
231 static::assertInstanceOf(Response
::class, $result);
232 static::assertSame(302, $result->getStatusCode());
233 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
237 * Untagged only - valid call
239 public function testUntaggedOnly(): void
241 $this->createValidContainerMockSet();
243 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
245 $request = $this->createMock(Request
::class);
246 $response = new Response();
248 $this->container
->sessionManager
249 ->expects(static::once())
250 ->method('setSessionParameter')
251 ->with(SessionManager
::KEY_UNTAGGED_ONLY
, true)
254 $result = $this->controller
->untaggedOnly($request, $response);
256 static::assertInstanceOf(Response
::class, $result);
257 static::assertSame(302, $result->getStatusCode());
258 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
262 * Untagged only - toggle off
264 public function testUntaggedOnlyToggleOff(): void
266 $this->createValidContainerMockSet();
268 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
270 $request = $this->createMock(Request
::class);
271 $response = new Response();
273 $this->container
->sessionManager
274 ->method('getSessionParameter')
275 ->with(SessionManager
::KEY_UNTAGGED_ONLY
)
278 $this->container
->sessionManager
279 ->expects(static::once())
280 ->method('setSessionParameter')
281 ->with(SessionManager
::KEY_UNTAGGED_ONLY
, false)
284 $result = $this->controller
->untaggedOnly($request, $response);
286 static::assertInstanceOf(Response
::class, $result);
287 static::assertSame(302, $result->getStatusCode());
288 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));