aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin
diff options
context:
space:
mode:
Diffstat (limited to 'tests/front/controller/admin')
-rw-r--r--tests/front/controller/admin/FrontAdminControllerMockHelper.php3
-rw-r--r--tests/front/controller/admin/LogoutControllerTest.php2
-rw-r--r--tests/front/controller/admin/PasswordControllerTest.php186
-rw-r--r--tests/front/controller/admin/SessionFilterControllerTest.php18
-rw-r--r--tests/front/controller/admin/ToolsControllerTest.php4
5 files changed, 187 insertions, 26 deletions
diff --git a/tests/front/controller/admin/FrontAdminControllerMockHelper.php b/tests/front/controller/admin/FrontAdminControllerMockHelper.php
index 94581c09..bd40c0c7 100644
--- a/tests/front/controller/admin/FrontAdminControllerMockHelper.php
+++ b/tests/front/controller/admin/FrontAdminControllerMockHelper.php
@@ -6,7 +6,6 @@ namespace Shaarli\Front\Controller\Admin;
6 6
7use Shaarli\Container\ShaarliTestContainer; 7use Shaarli\Container\ShaarliTestContainer;
8use Shaarli\Front\Controller\Visitor\FrontControllerMockHelper; 8use Shaarli\Front\Controller\Visitor\FrontControllerMockHelper;
9use Shaarli\Security\LoginManager;
10 9
11/** 10/**
12 * Trait FrontControllerMockHelper 11 * Trait FrontControllerMockHelper
@@ -28,7 +27,7 @@ trait FrontAdminControllerMockHelper
28 { 27 {
29 $this->parentCreateContainer(); 28 $this->parentCreateContainer();
30 29
31 $this->container->loginManager = $this->createMock(LoginManager::class);
32 $this->container->loginManager->method('isLoggedIn')->willReturn(true); 30 $this->container->loginManager->method('isLoggedIn')->willReturn(true);
31 $this->container->sessionManager->method('checkToken')->willReturn(true);
33 } 32 }
34} 33}
diff --git a/tests/front/controller/admin/LogoutControllerTest.php b/tests/front/controller/admin/LogoutControllerTest.php
index ba681b16..78a0fe73 100644
--- a/tests/front/controller/admin/LogoutControllerTest.php
+++ b/tests/front/controller/admin/LogoutControllerTest.php
@@ -35,8 +35,6 @@ class LogoutControllerTest extends TestCase
35 35
36 public function testValidControllerInvoke(): void 36 public function testValidControllerInvoke(): void
37 { 37 {
38 $this->createValidContainerMockSet();
39
40 $request = $this->createMock(Request::class); 38 $request = $this->createMock(Request::class);
41 $response = new Response(); 39 $response = new Response();
42 40
diff --git a/tests/front/controller/admin/PasswordControllerTest.php b/tests/front/controller/admin/PasswordControllerTest.php
new file mode 100644
index 00000000..7262243e
--- /dev/null
+++ b/tests/front/controller/admin/PasswordControllerTest.php
@@ -0,0 +1,186 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Front\Exception\WrongTokenException;
10use Shaarli\Security\SessionManager;
11use Slim\Http\Request;
12use Slim\Http\Response;
13
14class PasswordControllerTest extends TestCase
15{
16 use FrontAdminControllerMockHelper;
17
18 /** @var PasswordController */
19 protected $controller;
20
21 /** @var mixed[] Variables assigned to the template */
22 protected $assignedVariables = [];
23
24 public function setUp(): void
25 {
26 $this->createContainer();
27 $this->assignTemplateVars($this->assignedVariables);
28
29 $this->controller = new PasswordController($this->container);
30 }
31
32 /**
33 * Test displaying the change password page.
34 */
35 public function testGetPage(): void
36 {
37 $request = $this->createMock(Request::class);
38 $response = new Response();
39
40 $result = $this->controller->index($request, $response);
41
42 static::assertSame(200, $result->getStatusCode());
43 static::assertSame('changepassword', (string) $result->getBody());
44 static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']);
45 }
46
47 /**
48 * Change the password with valid parameters
49 */
50 public function testPostNewPasswordDefault(): void
51 {
52 $request = $this->createMock(Request::class);
53 $request->method('getParam')->willReturnCallback(function (string $key): string {
54 if ('oldpassword' === $key) {
55 return 'old';
56 }
57 if ('setpassword' === $key) {
58 return 'new';
59 }
60
61 return $key;
62 });
63 $response = new Response();
64
65 $this->container->conf = $this->createMock(ConfigManager::class);
66 $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) {
67 if ('credentials.hash' === $key) {
68 return sha1('old' . 'credentials.login' . 'credentials.salt');
69 }
70
71 return strpos($key, 'credentials') !== false ? $key : $default;
72 });
73 $this->container->conf->expects(static::once())->method('write')->with(true);
74
75 $this->container->conf
76 ->method('set')
77 ->willReturnCallback(function (string $key, string $value) {
78 if ('credentials.hash' === $key) {
79 static::assertSame(sha1('new' . 'credentials.login' . 'credentials.salt'), $value);
80 }
81 })
82 ;
83
84 $result = $this->controller->change($request, $response);
85
86 static::assertSame(200, $result->getStatusCode());
87 static::assertSame('changepassword', (string) $result->getBody());
88 static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']);
89 }
90
91 /**
92 * Change the password with a wrong existing password
93 */
94 public function testPostNewPasswordWrongOldPassword(): void
95 {
96 $request = $this->createMock(Request::class);
97 $request->method('getParam')->willReturnCallback(function (string $key): string {
98 if ('oldpassword' === $key) {
99 return 'wrong';
100 }
101 if ('setpassword' === $key) {
102 return 'new';
103 }
104
105 return $key;
106 });
107 $response = new Response();
108
109 $this->container->conf = $this->createMock(ConfigManager::class);
110 $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) {
111 if ('credentials.hash' === $key) {
112 return sha1('old' . 'credentials.login' . 'credentials.salt');
113 }
114
115 return strpos($key, 'credentials') !== false ? $key : $default;
116 });
117
118 $this->container->conf->expects(static::never())->method('set');
119 $this->container->conf->expects(static::never())->method('write');
120
121 $this->container->sessionManager
122 ->expects(static::once())
123 ->method('setSessionParameter')
124 ->with(SessionManager::KEY_ERROR_MESSAGES, ['The old password is not correct.'])
125 ;
126
127 $result = $this->controller->change($request, $response);
128
129 static::assertSame(400, $result->getStatusCode());
130 static::assertSame('changepassword', (string) $result->getBody());
131 static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']);
132 }
133
134 /**
135 * Change the password with a wrong existing password
136 */
137 public function testPostNewPasswordWrongToken(): void
138 {
139 $this->container->sessionManager = $this->createMock(SessionManager::class);
140 $this->container->sessionManager->method('checkToken')->willReturn(false);
141
142 $this->container->conf->expects(static::never())->method('set');
143 $this->container->conf->expects(static::never())->method('write');
144
145 $request = $this->createMock(Request::class);
146 $response = new Response();
147
148 $this->expectException(WrongTokenException::class);
149
150 $this->controller->change($request, $response);
151 }
152
153 /**
154 * Change the password with an empty new password
155 */
156 public function testPostNewEmptyPassword(): void
157 {
158 $this->container->sessionManager
159 ->expects(static::once())
160 ->method('setSessionParameter')
161 ->with(SessionManager::KEY_ERROR_MESSAGES, ['You must provide the current and new password to change it.'])
162 ;
163
164 $this->container->conf->expects(static::never())->method('set');
165 $this->container->conf->expects(static::never())->method('write');
166
167 $request = $this->createMock(Request::class);
168 $request->method('getParam')->willReturnCallback(function (string $key): string {
169 if ('oldpassword' === $key) {
170 return 'old';
171 }
172 if ('setpassword' === $key) {
173 return '';
174 }
175
176 return $key;
177 });
178 $response = new Response();
179
180 $result = $this->controller->change($request, $response);
181
182 static::assertSame(400, $result->getStatusCode());
183 static::assertSame('changepassword', (string) $result->getBody());
184 static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']);
185 }
186}
diff --git a/tests/front/controller/admin/SessionFilterControllerTest.php b/tests/front/controller/admin/SessionFilterControllerTest.php
index f50f2fc2..096963cf 100644
--- a/tests/front/controller/admin/SessionFilterControllerTest.php
+++ b/tests/front/controller/admin/SessionFilterControllerTest.php
@@ -30,8 +30,6 @@ class SessionFilterControllerTest extends TestCase
30 */ 30 */
31 public function testLinksPerPage(): void 31 public function testLinksPerPage(): void
32 { 32 {
33 $this->createValidContainerMockSet();
34
35 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; 33 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
36 34
37 $request = $this->createMock(Request::class); 35 $request = $this->createMock(Request::class);
@@ -62,8 +60,6 @@ class SessionFilterControllerTest extends TestCase
62 */ 60 */
63 public function testLinksPerPageNotValid(): void 61 public function testLinksPerPageNotValid(): void
64 { 62 {
65 $this->createValidContainerMockSet();
66
67 $request = $this->createMock(Request::class); 63 $request = $this->createMock(Request::class);
68 $request->method('getUri')->willReturnCallback(function (): Uri { 64 $request->method('getUri')->willReturnCallback(function (): Uri {
69 $uri = $this->createMock(Uri::class); 65 $uri = $this->createMock(Uri::class);
@@ -92,8 +88,6 @@ class SessionFilterControllerTest extends TestCase
92 */ 88 */
93 public function testVisibility(): void 89 public function testVisibility(): void
94 { 90 {
95 $this->createValidContainerMockSet();
96
97 $arg = ['visibility' => 'private']; 91 $arg = ['visibility' => 'private'];
98 92
99 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; 93 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
@@ -126,8 +120,6 @@ class SessionFilterControllerTest extends TestCase
126 */ 120 */
127 public function testVisibilityToggleOff(): void 121 public function testVisibilityToggleOff(): void
128 { 122 {
129 $this->createValidContainerMockSet();
130
131 $arg = ['visibility' => 'private']; 123 $arg = ['visibility' => 'private'];
132 124
133 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; 125 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
@@ -169,8 +161,6 @@ class SessionFilterControllerTest extends TestCase
169 */ 161 */
170 public function testVisibilitySwitch(): void 162 public function testVisibilitySwitch(): void
171 { 163 {
172 $this->createValidContainerMockSet();
173
174 $arg = ['visibility' => 'private']; 164 $arg = ['visibility' => 'private'];
175 165
176 $this->container->loginManager->method('isLoggedIn')->willReturn(true); 166 $this->container->loginManager->method('isLoggedIn')->willReturn(true);
@@ -206,8 +196,6 @@ class SessionFilterControllerTest extends TestCase
206 */ 196 */
207 public function testVisibilityInvalidValue(): void 197 public function testVisibilityInvalidValue(): void
208 { 198 {
209 $this->createValidContainerMockSet();
210
211 $arg = ['visibility' => 'test']; 199 $arg = ['visibility' => 'test'];
212 200
213 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; 201 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
@@ -244,8 +232,6 @@ class SessionFilterControllerTest extends TestCase
244 */ 232 */
245 public function testVisibilityLoggedOut(): void 233 public function testVisibilityLoggedOut(): void
246 { 234 {
247 $this->createValidContainerMockSet();
248
249 $arg = ['visibility' => 'test']; 235 $arg = ['visibility' => 'test'];
250 236
251 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; 237 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
@@ -283,8 +269,6 @@ class SessionFilterControllerTest extends TestCase
283 */ 269 */
284 public function testUntaggedOnly(): void 270 public function testUntaggedOnly(): void
285 { 271 {
286 $this->createValidContainerMockSet();
287
288 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; 272 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
289 273
290 $request = $this->createMock(Request::class); 274 $request = $this->createMock(Request::class);
@@ -314,8 +298,6 @@ class SessionFilterControllerTest extends TestCase
314 */ 298 */
315 public function testUntaggedOnlyToggleOff(): void 299 public function testUntaggedOnlyToggleOff(): void
316 { 300 {
317 $this->createValidContainerMockSet();
318
319 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc']; 301 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/subfolder/controller/?searchtag=abc'];
320 302
321 $request = $this->createMock(Request::class); 303 $request = $this->createMock(Request::class);
diff --git a/tests/front/controller/admin/ToolsControllerTest.php b/tests/front/controller/admin/ToolsControllerTest.php
index 47c5746e..fc756f0f 100644
--- a/tests/front/controller/admin/ToolsControllerTest.php
+++ b/tests/front/controller/admin/ToolsControllerTest.php
@@ -24,8 +24,6 @@ class ToolsControllerTestControllerTest extends TestCase
24 24
25 public function testDefaultInvokeWithHttps(): void 25 public function testDefaultInvokeWithHttps(): void
26 { 26 {
27 $this->createValidContainerMockSet();
28
29 $request = $this->createMock(Request::class); 27 $request = $this->createMock(Request::class);
30 $response = new Response(); 28 $response = new Response();
31 29
@@ -49,8 +47,6 @@ class ToolsControllerTestControllerTest extends TestCase
49 47
50 public function testDefaultInvokeWithoutHttps(): void 48 public function testDefaultInvokeWithoutHttps(): void
51 { 49 {
52 $this->createValidContainerMockSet();
53
54 $request = $this->createMock(Request::class); 50 $request = $this->createMock(Request::class);
55 $response = new Response(); 51 $response = new Response();
56 52