3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Security\LoginManager
;
8 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 * Visibility - Default call for private filter while logged in without current value
30 public function testVisibility(): void
32 $arg = ['visibility' => 'private'];
34 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli/subfolder/controller/?searchtag=abc';
36 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
37 $this->container
->sessionManager
38 ->expects(static::once())
39 ->method('setSessionParameter')
40 ->with(SessionManager
::KEY_VISIBILITY
, 'private')
43 $request = $this->createMock(Request
::class);
44 $response = new Response();
46 $result = $this->controller
->visibility($request, $response, $arg);
48 static::assertInstanceOf(Response
::class, $result);
49 static::assertSame(302, $result->getStatusCode());
50 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
54 * Visibility - Toggle off private visibility
56 public function testVisibilityToggleOff(): void
58 $arg = ['visibility' => 'private'];
60 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli/subfolder/controller/?searchtag=abc';
62 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
63 $this->container
->sessionManager
64 ->method('getSessionParameter')
65 ->with(SessionManager
::KEY_VISIBILITY
)
66 ->willReturn('private')
68 $this->container
->sessionManager
69 ->expects(static::never())
70 ->method('setSessionParameter')
72 $this->container
->sessionManager
73 ->expects(static::once())
74 ->method('deleteSessionParameter')
75 ->with(SessionManager
::KEY_VISIBILITY
)
78 $request = $this->createMock(Request
::class);
79 $response = new Response();
81 $result = $this->controller
->visibility($request, $response, $arg);
83 static::assertInstanceOf(Response
::class, $result);
84 static::assertSame(302, $result->getStatusCode());
85 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
89 * Visibility - Change private to public
91 public function testVisibilitySwitch(): void
93 $arg = ['visibility' => 'private'];
95 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
96 $this->container
->sessionManager
97 ->method('getSessionParameter')
98 ->with(SessionManager
::KEY_VISIBILITY
)
99 ->willReturn('public')
101 $this->container
->sessionManager
102 ->expects(static::once())
103 ->method('setSessionParameter')
104 ->with(SessionManager
::KEY_VISIBILITY
, 'private')
107 $request = $this->createMock(Request
::class);
108 $response = new Response();
110 $result = $this->controller
->visibility($request, $response, $arg);
112 static::assertInstanceOf(Response
::class, $result);
113 static::assertSame(302, $result->getStatusCode());
114 static::assertSame(['/subfolder/'], $result->getHeader('location'));
118 * Visibility - With invalid value - should remove any visibility setting
120 public function testVisibilityInvalidValue(): void
122 $arg = ['visibility' => 'test'];
124 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli/subfolder/controller/?searchtag=abc';
126 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
127 $this->container
->sessionManager
128 ->expects(static::never())
129 ->method('setSessionParameter')
131 $this->container
->sessionManager
132 ->expects(static::once())
133 ->method('deleteSessionParameter')
134 ->with(SessionManager
::KEY_VISIBILITY
)
137 $request = $this->createMock(Request
::class);
138 $response = new Response();
140 $result = $this->controller
->visibility($request, $response, $arg);
142 static::assertInstanceOf(Response
::class, $result);
143 static::assertSame(302, $result->getStatusCode());
144 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));
148 * Visibility - Try to change visibility while logged out
150 public function testVisibilityLoggedOut(): void
152 $arg = ['visibility' => 'test'];
154 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli/subfolder/controller/?searchtag=abc';
156 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
157 $this->container
->loginManager
->method('isLoggedIn')->willReturn(false);
158 $this->container
->sessionManager
159 ->expects(static::never())
160 ->method('setSessionParameter')
162 $this->container
->sessionManager
163 ->expects(static::never())
164 ->method('deleteSessionParameter')
165 ->with(SessionManager
::KEY_VISIBILITY
)
168 $request = $this->createMock(Request
::class);
169 $response = new Response();
171 $result = $this->controller
->visibility($request, $response, $arg);
173 static::assertInstanceOf(Response
::class, $result);
174 static::assertSame(302, $result->getStatusCode());
175 static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location'));