3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Config\ConfigManager
;
9 use Shaarli\Front\Exception\WrongTokenException
;
10 use Shaarli\Security\SessionManager
;
11 use Slim\Http\Request
;
12 use Slim\Http\Response
;
14 class PasswordControllerTest
extends TestCase
16 use FrontAdminControllerMockHelper
;
18 /** @var PasswordController */
19 protected $controller;
21 /** @var mixed[] Variables assigned to the template */
22 protected $assignedVariables = [];
24 public function setUp(): void
26 $this->createContainer();
27 $this->assignTemplateVars($this->assignedVariables
);
29 $this->controller
= new PasswordController($this->container
);
33 * Test displaying the change password page.
35 public function testGetPage(): void
37 $request = $this->createMock(Request
::class);
38 $response = new Response();
40 $result = $this->controller
->index($request, $response);
42 static::assertSame(200, $result->getStatusCode());
43 static::assertSame('changepassword', (string) $result->getBody());
44 static::assertSame('Change password - Shaarli', $this->assignedVariables
['pagetitle']);
48 * Change the password with valid parameters
50 public function testPostNewPasswordDefault(): void
52 $request = $this->createMock(Request
::class);
53 $request->method('getParam')->willReturnCallback(function (string $key): string {
54 if ('oldpassword' === $key) {
57 if ('setpassword' === $key) {
63 $response = new Response();
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');
71 return strpos($key, 'credentials') !== false ? $key : $default;
73 $this->container
->conf
->expects(static::once())->method('write')->with(true);
75 $this->container
->conf
77 ->willReturnCallback(function (string $key, string $value) {
78 if ('credentials.hash' === $key) {
79 static::assertSame(sha1('new' . 'credentials.login' . 'credentials.salt'), $value);
84 $result = $this->controller
->change($request, $response);
86 static::assertSame(200, $result->getStatusCode());
87 static::assertSame('changepassword', (string) $result->getBody());
88 static::assertSame('Change password - Shaarli', $this->assignedVariables
['pagetitle']);
92 * Change the password with a wrong existing password
94 public function testPostNewPasswordWrongOldPassword(): void
96 $request = $this->createMock(Request
::class);
97 $request->method('getParam')->willReturnCallback(function (string $key): string {
98 if ('oldpassword' === $key) {
101 if ('setpassword' === $key) {
107 $response = new Response();
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');
115 return strpos($key, 'credentials') !== false ? $key : $default;
118 $this->container
->conf
->expects(static::never())->method('set');
119 $this->container
->conf
->expects(static::never())->method('write');
121 $this->container
->sessionManager
122 ->expects(static::once())
123 ->method('setSessionParameter')
124 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['The old password is not correct.'])
127 $result = $this->controller
->change($request, $response);
129 static::assertSame(400, $result->getStatusCode());
130 static::assertSame('changepassword', (string) $result->getBody());
131 static::assertSame('Change password - Shaarli', $this->assignedVariables
['pagetitle']);
135 * Change the password with a wrong existing password
137 public function testPostNewPasswordWrongToken(): void
139 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
140 $this->container
->sessionManager
->method('checkToken')->willReturn(false);
142 $this->container
->conf
->expects(static::never())->method('set');
143 $this->container
->conf
->expects(static::never())->method('write');
145 $request = $this->createMock(Request
::class);
146 $response = new Response();
148 $this->expectException(WrongTokenException
::class);
150 $this->controller
->change($request, $response);
154 * Change the password with an empty new password
156 public function testPostNewEmptyPassword(): void
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.'])
164 $this->container
->conf
->expects(static::never())->method('set');
165 $this->container
->conf
->expects(static::never())->method('write');
167 $request = $this->createMock(Request
::class);
168 $request->method('getParam')->willReturnCallback(function (string $key): string {
169 if ('oldpassword' === $key) {
172 if ('setpassword' === $key) {
178 $response = new Response();
180 $result = $this->controller
->change($request, $response);
182 static::assertSame(400, $result->getStatusCode());
183 static::assertSame('changepassword', (string) $result->getBody());
184 static::assertSame('Change password - Shaarli', $this->assignedVariables
['pagetitle']);