2020-11-15 18:47:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers;
|
|
|
|
|
|
|
|
use Engelsystem\Config\Config;
|
2022-10-03 18:58:04 +02:00
|
|
|
use Engelsystem\Controllers\Admin\UserShirtController;
|
2020-11-15 18:47:30 +01:00
|
|
|
use Engelsystem\Controllers\SettingsController;
|
|
|
|
use Engelsystem\Http\Exceptions\HttpNotFound;
|
2022-10-03 18:58:04 +02:00
|
|
|
use Engelsystem\Http\Redirector;
|
2020-11-15 18:47:30 +01:00
|
|
|
use Engelsystem\Http\Response;
|
2022-10-03 18:58:04 +02:00
|
|
|
use Engelsystem\Models\User\Contact;
|
|
|
|
use Engelsystem\Models\User\PersonalData;
|
2022-06-07 21:33:08 +02:00
|
|
|
use Engelsystem\Models\User\Settings;
|
2022-10-03 18:58:04 +02:00
|
|
|
use Engelsystem\Models\User\State;
|
2020-11-15 18:47:30 +01:00
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
2020-11-23 20:41:02 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Engelsystem\Helpers\Authenticator;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
|
|
|
use Engelsystem\Http\Request;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Log\Test\TestLogger;
|
|
|
|
use Engelsystem\Http\UrlGeneratorInterface;
|
|
|
|
use Engelsystem\Http\UrlGenerator;
|
|
|
|
use Engelsystem\Models\User\User;
|
2020-11-24 17:27:21 +01:00
|
|
|
use Engelsystem\Http\Validation\Validator;
|
|
|
|
use Engelsystem\Http\Exceptions\ValidationException;
|
2020-11-15 18:47:30 +01:00
|
|
|
|
|
|
|
class SettingsControllerTest extends TestCase
|
|
|
|
{
|
2020-11-23 20:41:02 +01:00
|
|
|
use HasDatabase;
|
|
|
|
|
|
|
|
/** @var Authenticator|MockObject */
|
|
|
|
protected $auth;
|
|
|
|
|
|
|
|
/** @var Config */
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/** @var TestLogger */
|
|
|
|
protected $log;
|
|
|
|
|
|
|
|
/** @var Response|MockObject */
|
|
|
|
protected $response;
|
|
|
|
|
|
|
|
/** @var Request */
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/** @var User */
|
|
|
|
protected $user;
|
|
|
|
|
2022-06-20 19:03:47 +02:00
|
|
|
/** @var Session */
|
|
|
|
protected $session;
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
/** @var SettingsController */
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
protected function setUpProfileTest() {
|
|
|
|
$body = [
|
|
|
|
'pronoun' => 'Herr',
|
|
|
|
'first_name' => 'John',
|
|
|
|
'last_name' => 'Doe',
|
|
|
|
'planned_arrival_date' => '2022-01-01',
|
|
|
|
'planned_departure_date' => '2022-01-02',
|
|
|
|
'dect' => '1234',
|
|
|
|
'mobile' => '0123456789',
|
|
|
|
'email' => 'a@bc.de',
|
|
|
|
'email_shiftinfo' => true,
|
|
|
|
'email_news' => true,
|
|
|
|
'email_human' => true,
|
|
|
|
'email_goody' => true,
|
|
|
|
'shirt_size' => 'S',
|
|
|
|
];
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
|
|
|
$this->setExpects(
|
|
|
|
$this->response,
|
|
|
|
'redirectTo',
|
|
|
|
['http://localhost/settings/profile'],
|
|
|
|
$this->response,
|
|
|
|
$this->atLeastOnce()
|
|
|
|
);
|
|
|
|
|
|
|
|
config([
|
|
|
|
'enable_pronoun' => true,
|
|
|
|
'enable_user_name' => true,
|
|
|
|
'enable_planned_arrival' => true,
|
|
|
|
'enable_dect' => true,
|
|
|
|
'enable_goody' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
|
|
|
|
|
|
|
|
$this->controller = $this->app->make(SettingsController::class);
|
|
|
|
$this->controller->setValidator(new Validator());
|
|
|
|
|
|
|
|
return $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::profile
|
|
|
|
*/
|
|
|
|
public function testProfile()
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
/** @var Response|MockObject $response */
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function ($view, $data) {
|
|
|
|
$this->assertEquals('pages/settings/profile', $view);
|
|
|
|
$this->assertArrayHasKey('user', $data);
|
|
|
|
$this->assertEquals($this->user, $data['user']);
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->controller = $this->app->make(SettingsController::class);
|
|
|
|
$this->controller->profile();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
|
|
|
public function testSaveProfile()
|
|
|
|
{
|
|
|
|
$body = $this->setUpProfileTest();
|
|
|
|
$this->controller->saveProfile($this->request);
|
|
|
|
|
|
|
|
$this->assertEquals($body['pronoun'], $this->user->personalData->pronoun);
|
|
|
|
$this->assertEquals($body['first_name'], $this->user->personalData->first_name);
|
|
|
|
$this->assertEquals($body['last_name'], $this->user->personalData->last_name);
|
|
|
|
$this->assertEquals($body['planned_arrival_date'], $this->user->personalData->planned_arrival_date->format('Y-m-d'));
|
|
|
|
$this->assertEquals($body['planned_departure_date'], $this->user->personalData->planned_departure_date->format('Y-m-d'));
|
|
|
|
$this->assertEquals($body['dect'], $this->user->contact->dect);
|
|
|
|
$this->assertEquals($body['mobile'], $this->user->contact->mobile);
|
|
|
|
$this->assertEquals($body['email'], $this->user->email);
|
|
|
|
$this->assertEquals($body['email_shiftinfo'], $this->user->settings->email_shiftinfo);
|
|
|
|
$this->assertEquals($body['email_news'], $this->user->settings->email_news);
|
|
|
|
$this->assertEquals($body['email_human'], $this->user->settings->email_human);
|
|
|
|
$this->assertEquals($body['email_goody'], $this->user->settings->email_goody);
|
|
|
|
$this->assertEquals($body['shirt_size'], $this->user->personalData->shirt_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
|
|
|
public function testSaveProfileIgnoresPronounIfDisabled()
|
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
|
|
|
config(['enable_pronoun' => false]);
|
|
|
|
$this->controller->saveProfile($this->request);
|
|
|
|
$this->assertEquals('', $this->user->personalData->pronoun);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
|
|
|
public function testSaveProfileIgnoresFirstAndLastnameIfDisabled()
|
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
|
|
|
config(['enable_user_name' => false]);
|
|
|
|
$this->controller->saveProfile($this->request);
|
|
|
|
$this->assertEquals('', $this->user->personalData->first_name);
|
|
|
|
$this->assertEquals('', $this->user->personalData->last_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
|
|
|
public function testSaveProfileIgnoresArrivalDatesIfDisabled()
|
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
|
|
|
config(['enable_planned_arrival' => false]);
|
|
|
|
$this->controller->saveProfile($this->request);
|
|
|
|
$this->assertEquals('', $this->user->personalData->planned_arrival_date);
|
|
|
|
$this->assertEquals('', $this->user->personalData->planned_departure_date);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
|
|
|
public function testSaveProfileIgnoresDectIfDisabled()
|
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
|
|
|
config(['enable_dect' => false]);
|
|
|
|
$this->controller->saveProfile($this->request);
|
|
|
|
$this->assertEquals('', $this->user->contact->dect);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
|
|
|
public function testSaveProfileIgnoresEmailGoodyIfDisabled()
|
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
|
|
|
config(['enable_goody' => false]);
|
|
|
|
$this->controller->saveProfile($this->request);
|
|
|
|
$this->assertFalse($this->user->settings->email_goody);
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:41:02 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::password
|
|
|
|
*/
|
|
|
|
public function testPassword()
|
|
|
|
{
|
|
|
|
/** @var Response|MockObject $response */
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function ($view, $data) {
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->assertEquals('pages/settings/password', $view);
|
2020-11-23 20:41:02 +01:00
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->password();
|
2020-11-23 20:41:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::savePassword
|
|
|
|
*/
|
|
|
|
public function testSavePassword()
|
|
|
|
{
|
|
|
|
$body = [
|
|
|
|
'password' => 'password',
|
|
|
|
'new_password' => 'newpassword',
|
|
|
|
'new_password2' => 'newpassword'
|
|
|
|
];
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
|
|
|
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->setExpects($this->auth, 'verifyPassword', [$this->user, 'password'], true, $this->once());
|
|
|
|
$this->setExpects($this->auth, 'setPassword', [$this->user, 'newpassword'], null, $this->once());
|
|
|
|
$this->setExpects(
|
|
|
|
$this->response,
|
|
|
|
'redirectTo',
|
|
|
|
['http://localhost/settings/password'],
|
|
|
|
$this->response,
|
|
|
|
$this->once()
|
|
|
|
);
|
2020-11-23 20:41:02 +01:00
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->savePassword($this->request);
|
2020-11-23 20:41:02 +01:00
|
|
|
|
|
|
|
$this->assertTrue($this->log->hasInfoThatContains('User set new password.'));
|
|
|
|
|
|
|
|
/** @var Session $session */
|
|
|
|
$session = $this->app->get('session');
|
|
|
|
$messages = $session->get('messages');
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->assertEquals('settings.password.success', $messages[0]);
|
2020-11-23 20:41:02 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 23:39:46 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::savePassword
|
|
|
|
*/
|
|
|
|
public function testSavePasswordWhenEmpty()
|
|
|
|
{
|
|
|
|
$this->user->password = '';
|
|
|
|
$this->user->save();
|
|
|
|
|
|
|
|
$body = [
|
|
|
|
'new_password' => 'anotherpassword',
|
|
|
|
'new_password2' => 'anotherpassword'
|
|
|
|
];
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
|
|
|
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->setExpects($this->auth, 'setPassword', [$this->user, 'anotherpassword'], null, $this->once());
|
|
|
|
$this->setExpects(
|
|
|
|
$this->response,
|
|
|
|
'redirectTo',
|
|
|
|
['http://localhost/settings/password'],
|
|
|
|
$this->response,
|
|
|
|
$this->once()
|
|
|
|
);
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->savePassword($this->request);
|
2021-12-09 23:39:46 +01:00
|
|
|
}
|
|
|
|
|
2020-11-23 20:41:02 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::savePassword
|
|
|
|
*/
|
|
|
|
public function testSavePasswordWrongOldPassword()
|
|
|
|
{
|
|
|
|
$body = [
|
|
|
|
'password' => 'wrongpassword',
|
|
|
|
'new_password' => 'newpassword',
|
|
|
|
'new_password2' => 'newpassword'
|
|
|
|
];
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
|
|
|
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->setExpects($this->auth, 'verifyPassword', [$this->user, 'wrongpassword'], false, $this->once());
|
|
|
|
$this->setExpects($this->auth, 'setPassword', null, null, $this->never());
|
|
|
|
$this->setExpects(
|
|
|
|
$this->response,
|
|
|
|
'redirectTo',
|
|
|
|
['http://localhost/settings/password'],
|
|
|
|
$this->response,
|
|
|
|
$this->once()
|
|
|
|
);
|
2020-11-23 20:41:02 +01:00
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->savePassword($this->request);
|
2020-11-23 20:41:02 +01:00
|
|
|
|
|
|
|
/** @var Session $session */
|
|
|
|
$session = $this->app->get('session');
|
|
|
|
$errors = $session->get('errors');
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->assertEquals('auth.password.error', $errors[0]);
|
2020-11-23 20:41:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::savePassword
|
|
|
|
*/
|
|
|
|
public function testSavePasswordMismatchingNewPassword()
|
|
|
|
{
|
|
|
|
$body = [
|
|
|
|
'password' => 'password',
|
|
|
|
'new_password' => 'newpassword',
|
|
|
|
'new_password2' => 'wrongpassword'
|
|
|
|
];
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
|
|
|
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->setExpects($this->auth, 'verifyPassword', [$this->user, 'password'], true, $this->once());
|
|
|
|
$this->setExpects($this->auth, 'setPassword', null, null, $this->never());
|
|
|
|
$this->setExpects(
|
|
|
|
$this->response,
|
|
|
|
'redirectTo',
|
|
|
|
['http://localhost/settings/password'],
|
|
|
|
$this->response,
|
|
|
|
$this->once()
|
|
|
|
);
|
2020-11-23 20:41:02 +01:00
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->savePassword($this->request);
|
2020-11-23 20:41:02 +01:00
|
|
|
|
|
|
|
/** @var Session $session */
|
|
|
|
$session = $this->app->get('session');
|
|
|
|
$errors = $session->get('errors');
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->assertEquals('validation.password.confirmed', $errors[0]);
|
2020-11-23 20:41:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-24 17:27:21 +01:00
|
|
|
* @return array
|
2020-11-23 20:41:02 +01:00
|
|
|
*/
|
2020-11-24 17:27:21 +01:00
|
|
|
public function savePasswordValidationProvider(): array
|
2020-11-23 20:41:02 +01:00
|
|
|
{
|
2020-11-24 17:27:21 +01:00
|
|
|
return [
|
|
|
|
[null, 'newpassword', 'newpassword'],
|
|
|
|
['password', null, 'newpassword'],
|
|
|
|
['password', 'newpassword', null],
|
|
|
|
['password', 'short', 'short']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::savePassword
|
|
|
|
* @dataProvider savePasswordValidationProvider
|
|
|
|
* @param string $password
|
|
|
|
* @param string $new_password
|
|
|
|
* @param string $new_password2
|
|
|
|
*/
|
|
|
|
public function testSavePasswordValidation(
|
|
|
|
?string $password,
|
|
|
|
?string $newPassword,
|
|
|
|
?string $newPassword2
|
|
|
|
) {
|
2020-11-23 20:41:02 +01:00
|
|
|
$body = [
|
2020-11-24 17:27:21 +01:00
|
|
|
'password' => $password,
|
|
|
|
'new_password' => $newPassword,
|
|
|
|
'new_password2' => $newPassword2
|
2020-11-23 20:41:02 +01:00
|
|
|
];
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
|
|
|
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->setExpects($this->auth, 'setPassword', null, null, $this->never());
|
2020-11-23 20:41:02 +01:00
|
|
|
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->expectException(ValidationException::class);
|
2020-11-23 20:41:02 +01:00
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->savePassword($this->request);
|
2020-11-23 20:41:02 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 21:33:08 +02:00
|
|
|
/**
|
|
|
|
* @testdox theme: underNormalConditions -> returnsCorrectViewAndData
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::theme
|
|
|
|
*/
|
|
|
|
public function testThemeUnderNormalConditionReturnsCorrectViewAndData()
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
|
|
|
|
/** @var Response|MockObject $response */
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function ($view, $data) {
|
|
|
|
$this->assertEquals('pages/settings/theme', $view);
|
|
|
|
$this->assertArrayHasKey('settings_menu', $data);
|
|
|
|
$this->assertArrayHasKey('themes', $data);
|
|
|
|
$this->assertArrayHasKey('current_theme', $data);
|
|
|
|
$this->assertEquals([0 => 'Engelsystem light', 1 => 'Engelsystem dark'], $data['themes']);
|
|
|
|
$this->assertEquals(1, $data['current_theme']);
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->theme();
|
2022-06-07 21:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox saveTheme: withNoSelectedThemeGiven -> throwsException
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveTheme
|
|
|
|
*/
|
|
|
|
public function testSaveThemeWithNoSelectedThemeGivenThrowsException()
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->saveTheme($this->request);
|
2022-06-07 21:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox saveTheme: withUnknownSelectedThemeGiven -> throwsException
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveTheme
|
|
|
|
*/
|
|
|
|
public function testSaveThemeWithUnknownSelectedThemeGivenThrowsException()
|
|
|
|
{
|
|
|
|
$this->request = $this->request->withParsedBody(['select_theme' => 2]);
|
|
|
|
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->expectException(HttpNotFound::class);
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->saveTheme($this->request);
|
2022-06-07 21:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox saveTheme: withKnownSelectedThemeGiven -> savesThemeAndRedirect
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveTheme
|
|
|
|
*/
|
|
|
|
public function testSaveThemeWithKnownSelectedThemeGivenSavesThemeAndRedirect()
|
|
|
|
{
|
|
|
|
$this->assertEquals(1, $this->user->settings->theme);
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
|
|
|
->with('http://localhost/settings/theme')
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
$this->request = $this->request->withParsedBody(['select_theme' => 0]);
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->saveTheme($this->request);
|
2022-06-07 21:33:08 +02:00
|
|
|
|
|
|
|
$this->assertEquals(0, $this->user->settings->theme);
|
|
|
|
}
|
|
|
|
|
2022-06-20 19:03:47 +02:00
|
|
|
/**
|
|
|
|
* @testdox language: underNormalConditions -> returnsCorrectViewAndData
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::language
|
|
|
|
*/
|
|
|
|
public function testLanguageUnderNormalConditionReturnsCorrectViewAndData()
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
|
|
|
|
/** @var Response|MockObject $response */
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function ($view, $data) {
|
|
|
|
$this->assertEquals('pages/settings/language', $view);
|
|
|
|
$this->assertArrayHasKey('settings_menu', $data);
|
|
|
|
$this->assertArrayHasKey('languages', $data);
|
|
|
|
$this->assertArrayHasKey('current_language', $data);
|
|
|
|
$this->assertEquals(['en_US' => 'English', 'de_DE' => 'Deutsch'], $data['languages']);
|
|
|
|
$this->assertEquals('en_US', $data['current_language']);
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->language();
|
2022-06-20 19:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox saveLanguage: withNoSelectedLanguageGiven -> throwsException
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveLanguage
|
|
|
|
*/
|
|
|
|
public function testSaveLanguageWithNoSelectedLanguageGivenThrowsException()
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->saveLanguage($this->request);
|
2022-06-20 19:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox saveLanguage: withUnknownSelectedLanguageGiven -> throwsException
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveLanguage
|
|
|
|
*/
|
|
|
|
public function testSaveLanguageWithUnknownSelectedLanguageGivenThrowsException()
|
|
|
|
{
|
|
|
|
$this->request = $this->request->withParsedBody(['select_language' => 'unknown']);
|
|
|
|
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->expectException(HttpNotFound::class);
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->saveLanguage($this->request);
|
2022-06-20 19:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox saveLanguage: withKnownSelectedLanguageGiven -> savesLanguageAndUpdatesSessionAndRedirect
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveLanguage
|
|
|
|
*/
|
|
|
|
public function testSaveLanguageWithKnownSelectedLanguageGivenSavesLanguageAndUpdatesSessionAndRedirect()
|
|
|
|
{
|
|
|
|
$this->assertEquals('en_US', $this->user->settings->language);
|
|
|
|
$this->session->set('locale', 'en_US');
|
|
|
|
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
|
|
|
->with('http://localhost/settings/language')
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
$this->request = $this->request->withParsedBody(['select_language' => 'de_DE']);
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->saveLanguage($this->request);
|
2022-06-20 19:03:47 +02:00
|
|
|
|
|
|
|
$this->assertEquals('de_DE', $this->user->settings->language);
|
|
|
|
$this->assertEquals('de_DE', $this->session->get('locale'));
|
|
|
|
}
|
|
|
|
|
2020-11-15 18:47:30 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::__construct
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::oauth
|
|
|
|
*/
|
|
|
|
public function testOauth()
|
|
|
|
{
|
|
|
|
$providers = ['foo' => ['lorem' => 'ipsum']];
|
2020-11-23 20:41:02 +01:00
|
|
|
config(['oauth' => $providers]);
|
|
|
|
$this->response->expects($this->once())
|
2020-11-15 18:47:30 +01:00
|
|
|
->method('withView')
|
2020-11-23 20:41:02 +01:00
|
|
|
->willReturnCallback(function ($view, $data) use ($providers) {
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->assertEquals('pages/settings/oauth', $view);
|
2020-11-15 18:47:30 +01:00
|
|
|
$this->assertArrayHasKey('information', $data);
|
|
|
|
$this->assertArrayHasKey('providers', $data);
|
|
|
|
$this->assertEquals($providers, $data['providers']);
|
|
|
|
|
2020-11-23 20:41:02 +01:00
|
|
|
return $this->response;
|
2020-11-15 18:47:30 +01:00
|
|
|
});
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->oauth();
|
2020-11-15 18:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::oauth
|
|
|
|
*/
|
|
|
|
public function testOauthNotConfigured()
|
|
|
|
{
|
2020-11-23 20:41:02 +01:00
|
|
|
config(['oauth' => []]);
|
2020-11-15 18:47:30 +01:00
|
|
|
|
|
|
|
$this->expectException(HttpNotFound::class);
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->oauth();
|
2020-11-15 18:47:30 +01:00
|
|
|
}
|
2020-11-23 20:41:02 +01:00
|
|
|
|
2020-11-24 17:27:21 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
|
2021-12-03 22:27:15 +01:00
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::checkOauthHidden
|
2020-11-24 17:27:21 +01:00
|
|
|
*/
|
|
|
|
public function testSettingsMenuWithOAuth()
|
|
|
|
{
|
|
|
|
$providers = ['foo' => ['lorem' => 'ipsum']];
|
2021-12-03 22:27:15 +01:00
|
|
|
$providersHidden = ['foo' => ['lorem' => 'ipsum', 'hidden' => true]];
|
2020-11-24 17:27:21 +01:00
|
|
|
config(['oauth' => $providers]);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'http://localhost/user-settings' => 'settings.profile',
|
|
|
|
'http://localhost/settings/password' => 'settings.password',
|
2022-06-20 19:03:47 +02:00
|
|
|
'http://localhost/settings/language' => 'settings.language',
|
2022-06-07 21:33:08 +02:00
|
|
|
'http://localhost/settings/theme' => 'settings.theme',
|
2021-12-03 22:27:15 +01:00
|
|
|
'http://localhost/settings/oauth' => ['title' => 'settings.oauth', 'hidden' => false]
|
2022-10-03 18:58:04 +02:00
|
|
|
], $this->controller->settingsMenu());
|
2021-12-03 22:27:15 +01:00
|
|
|
|
|
|
|
config(['oauth' => $providersHidden]);
|
|
|
|
$this->assertEquals([
|
|
|
|
'http://localhost/user-settings' => 'settings.profile',
|
|
|
|
'http://localhost/settings/password' => 'settings.password',
|
2022-06-20 19:03:47 +02:00
|
|
|
'http://localhost/settings/language' => 'settings.language',
|
2022-06-07 21:33:08 +02:00
|
|
|
'http://localhost/settings/theme' => 'settings.theme',
|
2021-12-03 22:27:15 +01:00
|
|
|
'http://localhost/settings/oauth' => ['title' => 'settings.oauth', 'hidden' => true]
|
2022-10-03 18:58:04 +02:00
|
|
|
], $this->controller->settingsMenu());
|
2020-11-24 17:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
|
|
|
|
*/
|
|
|
|
public function testSettingsMenuWithoutOAuth()
|
|
|
|
{
|
|
|
|
config(['oauth' => []]);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'http://localhost/user-settings' => 'settings.profile',
|
2022-06-07 21:33:08 +02:00
|
|
|
'http://localhost/settings/password' => 'settings.password',
|
2022-06-20 19:03:47 +02:00
|
|
|
'http://localhost/settings/language' => 'settings.language',
|
2022-06-07 21:33:08 +02:00
|
|
|
'http://localhost/settings/theme' => 'settings.theme'
|
2022-10-03 18:58:04 +02:00
|
|
|
], $this->controller->settingsMenu());
|
2020-11-24 17:27:21 +01:00
|
|
|
}
|
|
|
|
|
2020-11-23 20:41:02 +01:00
|
|
|
/**
|
|
|
|
* Setup environment
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->initDatabase();
|
|
|
|
|
2022-06-07 21:33:08 +02:00
|
|
|
$themes = [
|
|
|
|
0 => ['name' => 'Engelsystem light'],
|
|
|
|
1 => ['name' => 'Engelsystem dark']
|
|
|
|
];
|
2022-06-20 19:03:47 +02:00
|
|
|
$languages = [
|
|
|
|
'en_US' => 'English',
|
|
|
|
'de_DE' => 'Deutsch'
|
|
|
|
];
|
2022-10-13 20:23:22 +02:00
|
|
|
$tshirt_sizes = ['S' => 'Small'];
|
|
|
|
$this->config = new Config([
|
|
|
|
'min_password_length' => 6,
|
|
|
|
'themes' => $themes,
|
|
|
|
'locales' => $languages,
|
|
|
|
'tshirt_sizes' => $tshirt_sizes
|
|
|
|
]);
|
2020-11-23 20:41:02 +01:00
|
|
|
$this->app->instance('config', $this->config);
|
|
|
|
$this->app->instance(Config::class, $this->config);
|
|
|
|
|
|
|
|
$this->request = Request::create('http://localhost');
|
|
|
|
$this->app->instance('request', $this->request);
|
|
|
|
$this->app->instance(Request::class, $this->request);
|
|
|
|
$this->app->instance(ServerRequestInterface::class, $this->request);
|
|
|
|
|
|
|
|
$this->response = $this->createMock(Response::class);
|
|
|
|
$this->app->instance(Response::class, $this->response);
|
|
|
|
|
|
|
|
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->app->bind('http.urlGenerator', UrlGenerator::class);
|
2020-11-23 20:41:02 +01:00
|
|
|
|
|
|
|
$this->log = new TestLogger();
|
|
|
|
$this->app->instance(LoggerInterface::class, $this->log);
|
|
|
|
|
2022-06-20 19:03:47 +02:00
|
|
|
$this->session = new Session(new MockArraySessionStorage());
|
|
|
|
$this->app->instance('session', $this->session);
|
2020-11-23 20:41:02 +01:00
|
|
|
|
|
|
|
$this->auth = $this->createMock(Authenticator::class);
|
|
|
|
$this->app->instance(Authenticator::class, $this->auth);
|
|
|
|
|
2022-06-07 21:33:08 +02:00
|
|
|
$this->user = User::factory()
|
2022-10-03 18:58:04 +02:00
|
|
|
->has(Settings::factory(['theme' => 1, 'language' => 'en_US', 'email_goody' => false]))
|
2022-06-07 21:33:08 +02:00
|
|
|
->create();
|
2022-10-03 18:58:04 +02:00
|
|
|
|
|
|
|
$this->controller = $this->app->make(SettingsController::class);
|
|
|
|
$this->controller->setValidator(new Validator());
|
2020-11-23 20:41:02 +01:00
|
|
|
}
|
2020-11-15 18:47:30 +01:00
|
|
|
}
|