diff options
Diffstat (limited to 'tests/front/controller/admin/SessionFilterControllerTest.php')
-rw-r--r-- | tests/front/controller/admin/SessionFilterControllerTest.php | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/tests/front/controller/admin/SessionFilterControllerTest.php b/tests/front/controller/admin/SessionFilterControllerTest.php new file mode 100644 index 00000000..d306c6e9 --- /dev/null +++ b/tests/front/controller/admin/SessionFilterControllerTest.php | |||
@@ -0,0 +1,177 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin; | ||
6 | |||
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; | ||
12 | |||
13 | class SessionFilterControllerTest extends TestCase | ||
14 | { | ||
15 | use FrontAdminControllerMockHelper; | ||
16 | |||
17 | /** @var SessionFilterController */ | ||
18 | protected $controller; | ||
19 | |||
20 | public function setUp(): void | ||
21 | { | ||
22 | $this->createContainer(); | ||
23 | |||
24 | $this->controller = new SessionFilterController($this->container); | ||
25 | } | ||
26 | |||
27 | /** | ||
28 | * Visibility - Default call for private filter while logged in without current value | ||
29 | */ | ||
30 | public function testVisibility(): void | ||
31 | { | ||
32 | $arg = ['visibility' => 'private']; | ||
33 | |||
34 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; | ||
35 | |||
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') | ||
41 | ; | ||
42 | |||
43 | $request = $this->createMock(Request::class); | ||
44 | $response = new Response(); | ||
45 | |||
46 | $result = $this->controller->visibility($request, $response, $arg); | ||
47 | |||
48 | static::assertInstanceOf(Response::class, $result); | ||
49 | static::assertSame(302, $result->getStatusCode()); | ||
50 | static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location')); | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Visibility - Toggle off private visibility | ||
55 | */ | ||
56 | public function testVisibilityToggleOff(): void | ||
57 | { | ||
58 | $arg = ['visibility' => 'private']; | ||
59 | |||
60 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; | ||
61 | |||
62 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | ||
63 | $this->container->sessionManager | ||
64 | ->method('getSessionParameter') | ||
65 | ->with(SessionManager::KEY_VISIBILITY) | ||
66 | ->willReturn('private') | ||
67 | ; | ||
68 | $this->container->sessionManager | ||
69 | ->expects(static::never()) | ||
70 | ->method('setSessionParameter') | ||
71 | ; | ||
72 | $this->container->sessionManager | ||
73 | ->expects(static::once()) | ||
74 | ->method('deleteSessionParameter') | ||
75 | ->with(SessionManager::KEY_VISIBILITY) | ||
76 | ; | ||
77 | |||
78 | $request = $this->createMock(Request::class); | ||
79 | $response = new Response(); | ||
80 | |||
81 | $result = $this->controller->visibility($request, $response, $arg); | ||
82 | |||
83 | static::assertInstanceOf(Response::class, $result); | ||
84 | static::assertSame(302, $result->getStatusCode()); | ||
85 | static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location')); | ||
86 | } | ||
87 | |||
88 | /** | ||
89 | * Visibility - Change private to public | ||
90 | */ | ||
91 | public function testVisibilitySwitch(): void | ||
92 | { | ||
93 | $arg = ['visibility' => 'private']; | ||
94 | |||
95 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | ||
96 | $this->container->sessionManager | ||
97 | ->method('getSessionParameter') | ||
98 | ->with(SessionManager::KEY_VISIBILITY) | ||
99 | ->willReturn('public') | ||
100 | ; | ||
101 | $this->container->sessionManager | ||
102 | ->expects(static::once()) | ||
103 | ->method('setSessionParameter') | ||
104 | ->with(SessionManager::KEY_VISIBILITY, 'private') | ||
105 | ; | ||
106 | |||
107 | $request = $this->createMock(Request::class); | ||
108 | $response = new Response(); | ||
109 | |||
110 | $result = $this->controller->visibility($request, $response, $arg); | ||
111 | |||
112 | static::assertInstanceOf(Response::class, $result); | ||
113 | static::assertSame(302, $result->getStatusCode()); | ||
114 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | ||
115 | } | ||
116 | |||
117 | /** | ||
118 | * Visibility - With invalid value - should remove any visibility setting | ||
119 | */ | ||
120 | public function testVisibilityInvalidValue(): void | ||
121 | { | ||
122 | $arg = ['visibility' => 'test']; | ||
123 | |||
124 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; | ||
125 | |||
126 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | ||
127 | $this->container->sessionManager | ||
128 | ->expects(static::never()) | ||
129 | ->method('setSessionParameter') | ||
130 | ; | ||
131 | $this->container->sessionManager | ||
132 | ->expects(static::once()) | ||
133 | ->method('deleteSessionParameter') | ||
134 | ->with(SessionManager::KEY_VISIBILITY) | ||
135 | ; | ||
136 | |||
137 | $request = $this->createMock(Request::class); | ||
138 | $response = new Response(); | ||
139 | |||
140 | $result = $this->controller->visibility($request, $response, $arg); | ||
141 | |||
142 | static::assertInstanceOf(Response::class, $result); | ||
143 | static::assertSame(302, $result->getStatusCode()); | ||
144 | static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location')); | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * Visibility - Try to change visibility while logged out | ||
149 | */ | ||
150 | public function testVisibilityLoggedOut(): void | ||
151 | { | ||
152 | $arg = ['visibility' => 'test']; | ||
153 | |||
154 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; | ||
155 | |||
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') | ||
161 | ; | ||
162 | $this->container->sessionManager | ||
163 | ->expects(static::never()) | ||
164 | ->method('deleteSessionParameter') | ||
165 | ->with(SessionManager::KEY_VISIBILITY) | ||
166 | ; | ||
167 | |||
168 | $request = $this->createMock(Request::class); | ||
169 | $response = new Response(); | ||
170 | |||
171 | $result = $this->controller->visibility($request, $response, $arg); | ||
172 | |||
173 | static::assertInstanceOf(Response::class, $result); | ||
174 | static::assertSame(302, $result->getStatusCode()); | ||
175 | static::assertSame(['/subfolder/controller/?searchtag=abc'], $result->getHeader('location')); | ||
176 | } | ||
177 | } | ||