2020-11-15 18:47:30 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-11-15 18:47:30 +01:00
|
|
|
namespace Engelsystem\Test\Unit\Controllers;
|
|
|
|
|
2022-10-24 14:59:34 +02:00
|
|
|
use Carbon\Carbon;
|
2020-11-15 18:47:30 +01:00
|
|
|
use Engelsystem\Config\Config;
|
2023-03-05 03:00:38 +01:00
|
|
|
use Engelsystem\Config\GoodieType;
|
2023-02-02 22:53:51 +01:00
|
|
|
use Engelsystem\Controllers\NotificationType;
|
2020-11-15 18:47:30 +01:00
|
|
|
use Engelsystem\Controllers\SettingsController;
|
|
|
|
use Engelsystem\Http\Exceptions\HttpNotFound;
|
2023-11-16 18:20:50 +01:00
|
|
|
use Engelsystem\Http\Redirector;
|
2020-11-15 18:47:30 +01:00
|
|
|
use Engelsystem\Http\Response;
|
2023-11-11 21:26:11 +01:00
|
|
|
use Engelsystem\Models\AngelType;
|
2023-09-17 20:15:49 +02:00
|
|
|
use Engelsystem\Models\Session as SessionModel;
|
2023-08-12 12:00:46 +02:00
|
|
|
use Engelsystem\Models\User\License;
|
2022-06-07 21:33:08 +02:00
|
|
|
use Engelsystem\Models\User\Settings;
|
2023-09-18 18:18:33 +02:00
|
|
|
use Illuminate\Support\Str;
|
2020-11-15 18:47:30 +01:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
2020-11-23 20:41:02 +01:00
|
|
|
use Engelsystem\Helpers\Authenticator;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
|
|
|
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
|
|
|
|
2022-10-24 14:59:34 +02:00
|
|
|
class SettingsControllerTest extends ControllerTest
|
2020-11-15 18:47:30 +01:00
|
|
|
{
|
2020-11-23 20:41:02 +01:00
|
|
|
use HasDatabase;
|
|
|
|
|
2022-12-15 19:50:56 +01:00
|
|
|
protected Authenticator|MockObject $auth;
|
2020-11-23 20:41:02 +01:00
|
|
|
|
2022-12-15 19:50:56 +01:00
|
|
|
protected User $user;
|
2020-11-23 20:41:02 +01:00
|
|
|
|
2022-12-15 19:50:56 +01:00
|
|
|
protected SettingsController $controller;
|
2022-10-03 18:58:04 +02:00
|
|
|
|
2023-09-17 20:15:49 +02:00
|
|
|
protected SessionModel $currentSession;
|
|
|
|
protected SessionModel $secondSession;
|
|
|
|
protected SessionModel $otherSession;
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function setUpProfileTest(): array
|
2022-10-15 23:08:45 +02:00
|
|
|
{
|
2022-10-03 18:58:04 +02:00
|
|
|
$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',
|
2022-10-20 21:44:58 +02:00
|
|
|
'mobile_show' => true,
|
2022-10-03 18:58:04 +02:00
|
|
|
'email' => 'a@bc.de',
|
|
|
|
'email_shiftinfo' => true,
|
|
|
|
'email_news' => true,
|
|
|
|
'email_human' => true,
|
2023-02-26 16:18:16 +01:00
|
|
|
'email_messages' => true,
|
2022-10-03 18:58:04 +02:00
|
|
|
'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,
|
2022-10-20 21:44:58 +02:00
|
|
|
'enable_mobile_show' => true,
|
2023-03-05 03:00:38 +01:00
|
|
|
'goodie_type' => GoodieType::Tshirt->value,
|
2022-10-03 18:58:04 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testProfile(): void
|
2022-10-03 18:58:04 +02:00
|
|
|
{
|
2023-11-11 21:26:11 +01:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
|
2022-10-03 18:58:04 +02:00
|
|
|
/** @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
|
2022-10-24 14:59:34 +02:00
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::getSaveProfileRules
|
2023-11-03 15:15:44 +01:00
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::isRequired
|
2022-10-03 18:58:04 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveProfile(): void
|
2022-10-03 18:58:04 +02:00
|
|
|
{
|
|
|
|
$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);
|
2022-10-15 23:08:45 +02:00
|
|
|
$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')
|
|
|
|
);
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->assertEquals($body['dect'], $this->user->contact->dect);
|
|
|
|
$this->assertEquals($body['mobile'], $this->user->contact->mobile);
|
2022-10-20 21:44:58 +02:00
|
|
|
$this->assertEquals($body['mobile_show'], $this->user->settings->mobile_show);
|
2022-10-03 18:58:04 +02:00
|
|
|
$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);
|
2023-02-26 16:18:16 +01:00
|
|
|
$this->assertEquals($body['email_messages'], $this->user->settings->email_messages);
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->assertEquals($body['email_goody'], $this->user->settings->email_goody);
|
|
|
|
$this->assertEquals($body['shirt_size'], $this->user->personalData->shirt_size);
|
|
|
|
}
|
|
|
|
|
2022-10-24 14:59:34 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveProfileThrowsErrorOnInvalidArrival(): void
|
2022-10-24 14:59:34 +02:00
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
|
|
|
config(['buildup_start' => new Carbon('2022-01-02')]); // arrival before buildup
|
|
|
|
$this->controller->saveProfile($this->request);
|
2023-02-02 22:53:51 +01:00
|
|
|
$this->assertHasNotification('settings.profile.planned_arrival_date.invalid', NotificationType::ERROR);
|
2022-10-24 14:59:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveProfileThrowsErrorOnInvalidDeparture(): void
|
2022-10-24 14:59:34 +02:00
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
|
|
|
config(['teardown_end' => new Carbon('2022-01-01')]); // departure after teardown
|
|
|
|
$this->controller->saveProfile($this->request);
|
2023-02-02 22:53:51 +01:00
|
|
|
$this->assertHasNotification('settings.profile.planned_departure_date.invalid', NotificationType::ERROR);
|
2022-10-24 14:59:34 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveProfileIgnoresPronounIfDisabled(): void
|
2022-10-03 18:58:04 +02:00
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
|
|
|
config(['enable_pronoun' => false]);
|
|
|
|
$this->controller->saveProfile($this->request);
|
|
|
|
$this->assertEquals('', $this->user->personalData->pronoun);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveProfileIgnoresFirstAndLastnameIfDisabled(): void
|
2022-10-03 18:58:04 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveProfileIgnoresArrivalDatesIfDisabled(): void
|
2022-10-03 18:58:04 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveProfileIgnoresDectIfDisabled(): void
|
2022-10-03 18:58:04 +02:00
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
|
|
|
config(['enable_dect' => false]);
|
|
|
|
$this->controller->saveProfile($this->request);
|
|
|
|
$this->assertEquals('', $this->user->contact->dect);
|
|
|
|
}
|
|
|
|
|
2022-10-20 21:44:58 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveProfileIgnoresMobileShowIfDisabled(): void
|
2022-10-20 21:44:58 +02:00
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
|
|
|
config(['enable_mobile_show' => false]);
|
|
|
|
$this->controller->saveProfile($this->request);
|
|
|
|
$this->assertFalse($this->user->settings->mobile_show);
|
|
|
|
}
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveProfileIgnoresEmailGoodyIfDisabled(): void
|
2022-10-03 18:58:04 +02:00
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
2023-03-05 03:00:38 +01:00
|
|
|
$this->config->set('goodie_type', GoodieType::None->value);
|
2022-10-03 18:58:04 +02:00
|
|
|
$this->controller->saveProfile($this->request);
|
|
|
|
$this->assertFalse($this->user->settings->email_goody);
|
|
|
|
}
|
|
|
|
|
2022-10-24 14:59:34 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveProfile
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveProfileIgnoresTShirtSizeIfDisabled(): void
|
2022-10-24 14:59:34 +02:00
|
|
|
{
|
|
|
|
$this->setUpProfileTest();
|
2023-03-05 03:00:38 +01:00
|
|
|
$this->config->set('goodie_type', GoodieType::None->value);
|
2022-10-24 14:59:34 +02:00
|
|
|
$this->controller->saveProfile($this->request);
|
|
|
|
$this->assertEquals('', $this->user->personalData->shirt_size);
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:41:02 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::password
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testPassword(): void
|
2020-11-23 20:41:02 +01:00
|
|
|
{
|
|
|
|
/** @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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSavePassword(): void
|
2020-11-23 20:41:02 +01:00
|
|
|
{
|
|
|
|
$body = [
|
|
|
|
'password' => 'password',
|
|
|
|
'new_password' => 'newpassword',
|
2023-02-05 18:03:00 +01:00
|
|
|
'new_password2' => 'newpassword',
|
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, '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');
|
2023-02-02 22:53:51 +01:00
|
|
|
$messages = $session->get('messages.' . NotificationType::MESSAGE->value);
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->assertEquals('settings.password.success', $messages[0]);
|
2023-09-17 20:47:28 +02:00
|
|
|
|
|
|
|
$this->assertCount(
|
|
|
|
1,
|
|
|
|
SessionModel::whereUserId($this->user->id)->get(),
|
|
|
|
'All other user sessions should be deleted after setting a new password'
|
|
|
|
);
|
|
|
|
$this->assertCount(2, SessionModel::all()); // Current session and another one should be still there
|
2020-11-23 20:41:02 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 23:39:46 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::savePassword
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSavePasswordWhenEmpty(): void
|
2021-12-09 23:39:46 +01:00
|
|
|
{
|
|
|
|
$this->user->password = '';
|
|
|
|
$this->user->save();
|
|
|
|
|
|
|
|
$body = [
|
|
|
|
'new_password' => 'anotherpassword',
|
2023-02-05 18:03:00 +01:00
|
|
|
'new_password2' => 'anotherpassword',
|
2021-12-09 23:39:46 +01:00
|
|
|
];
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSavePasswordWrongOldPassword(): void
|
2020-11-23 20:41:02 +01:00
|
|
|
{
|
|
|
|
$body = [
|
|
|
|
'password' => 'wrongpassword',
|
|
|
|
'new_password' => 'newpassword',
|
2023-02-05 18:03:00 +01:00
|
|
|
'new_password2' => 'newpassword',
|
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, '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
|
|
|
|
2023-02-02 22:53:51 +01:00
|
|
|
$this->assertHasNotification('auth.password.error', NotificationType::ERROR);
|
2020-11-23 20:41:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::savePassword
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSavePasswordMismatchingNewPassword(): void
|
2020-11-23 20:41:02 +01:00
|
|
|
{
|
|
|
|
$body = [
|
|
|
|
'password' => 'password',
|
|
|
|
'new_password' => 'newpassword',
|
2023-02-05 18:03:00 +01:00
|
|
|
'new_password2' => 'wrongpassword',
|
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, '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
|
|
|
|
2023-02-02 22:53:51 +01:00
|
|
|
$this->assertHasNotification('validation.password.confirmed', NotificationType::ERROR);
|
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],
|
2023-02-05 18:03:00 +01:00
|
|
|
['password', 'short', 'short'],
|
2020-11-24 17:27:21 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::savePassword
|
|
|
|
* @dataProvider savePasswordValidationProvider
|
|
|
|
*/
|
|
|
|
public function testSavePasswordValidation(
|
|
|
|
?string $password,
|
|
|
|
?string $newPassword,
|
|
|
|
?string $newPassword2
|
2022-12-14 19:15:20 +01:00
|
|
|
): void {
|
2020-11-23 20:41:02 +01:00
|
|
|
$body = [
|
2020-11-24 17:27:21 +01:00
|
|
|
'password' => $password,
|
|
|
|
'new_password' => $newPassword,
|
2023-02-05 18:03:00 +01:00
|
|
|
'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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testThemeUnderNormalConditionReturnsCorrectViewAndData(): void
|
2022-06-07 21:33:08 +02:00
|
|
|
{
|
2023-11-11 21:26:11 +01:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
|
2022-06-07 21:33:08 +02:00
|
|
|
|
|
|
|
/** @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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveThemeWithNoSelectedThemeGivenThrowsException(): void
|
2022-06-07 21:33:08 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveThemeWithUnknownSelectedThemeGivenThrowsException(): void
|
2022-06-07 21:33:08 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveThemeWithKnownSelectedThemeGivenSavesThemeAndRedirect(): void
|
2022-06-07 21:33:08 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testLanguageUnderNormalConditionReturnsCorrectViewAndData(): void
|
2022-06-20 19:03:47 +02:00
|
|
|
{
|
2023-11-11 21:26:11 +01:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
|
2022-06-20 19:03:47 +02:00
|
|
|
|
|
|
|
/** @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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveLanguageWithNoSelectedLanguageGivenThrowsException(): void
|
2022-06-20 19:03:47 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveLanguageWithUnknownSelectedLanguageGivenThrowsException(): void
|
2022-06-20 19:03:47 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveLanguageWithKnownSelectedLanguageGivenSavesLanguageAndUpdatesSessionAndRedirect(): void
|
2022-06-20 19:03:47 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testOauth(): void
|
2020-11-15 18:47:30 +01:00
|
|
|
{
|
|
|
|
$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('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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testOauthNotConfigured(): void
|
2020-11-15 18:47:30 +01:00
|
|
|
{
|
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
|
|
|
|
2023-09-17 20:15:49 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::sessions
|
|
|
|
*/
|
|
|
|
public function testSessions(): void
|
|
|
|
{
|
2023-11-11 21:26:11 +01:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
|
2023-09-17 20:15:49 +02:00
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
2023-09-18 18:18:33 +02:00
|
|
|
->willReturnCallback(function ($view, $data) {
|
2023-09-17 20:15:49 +02:00
|
|
|
$this->assertEquals('pages/settings/sessions', $view);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('sessions', $data);
|
|
|
|
$this->assertCount(3, $data['sessions']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('current_session', $data);
|
|
|
|
$this->assertEquals($this->currentSession->id, $data['current_session']);
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->controller->sessions();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::sessionsDelete
|
|
|
|
*/
|
|
|
|
public function testSessionsDelete(): void
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->setExpects($this->response, 'redirectTo', ['http://localhost/settings/sessions'], $this->response);
|
|
|
|
|
|
|
|
// Delete old user session
|
2023-09-18 18:18:33 +02:00
|
|
|
$this->request = $this->request->withParsedBody(['id' => Str::substr($this->secondSession->id, 0, 15)]);
|
2023-09-17 20:15:49 +02:00
|
|
|
$this->controller->sessionsDelete($this->request);
|
2023-08-12 12:00:46 +02:00
|
|
|
|
2023-09-17 20:15:49 +02:00
|
|
|
$this->assertHasNotification('settings.sessions.delete_success');
|
|
|
|
$this->assertCount(3, SessionModel::all());
|
|
|
|
$this->assertNull(SessionModel::find($this->secondSession->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::sessionsDelete
|
|
|
|
*/
|
|
|
|
public function testSessionsDeleteActiveSession(): void
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->setExpects($this->response, 'redirectTo', null, $this->response);
|
|
|
|
|
|
|
|
// Delete active user session
|
2023-09-18 18:18:33 +02:00
|
|
|
$this->request = $this->request->withParsedBody(['id' => Str::substr($this->currentSession->id, 0, 15)]);
|
2023-09-17 20:15:49 +02:00
|
|
|
$this->controller->sessionsDelete($this->request);
|
|
|
|
|
|
|
|
$this->assertCount(4, SessionModel::all()); // None got deleted
|
|
|
|
$this->assertNotNull(SessionModel::find($this->currentSession->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::sessionsDelete
|
|
|
|
*/
|
|
|
|
public function testSessionsDeleteOtherUsersSession(): void
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->setExpects($this->response, 'redirectTo', null, $this->response);
|
|
|
|
|
|
|
|
// Delete another users session
|
2023-09-18 18:18:33 +02:00
|
|
|
$this->request = $this->request->withParsedBody(['id' => Str::substr($this->otherSession->id, 0, 15)]);
|
2023-09-17 20:15:49 +02:00
|
|
|
$this->controller->sessionsDelete($this->request);
|
|
|
|
|
|
|
|
$this->assertCount(4, SessionModel::all()); // None got deleted
|
|
|
|
$this->assertNotNull(SessionModel::find($this->otherSession->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::sessionsDelete
|
|
|
|
*/
|
|
|
|
public function testSessionsDeleteAllSessions(): void
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->setExpects($this->response, 'redirectTo', null, $this->response);
|
|
|
|
|
|
|
|
// Delete all other user sessions
|
|
|
|
$this->request = $this->request->withParsedBody(['id' => 'all']);
|
|
|
|
$this->controller->sessionsDelete($this->request);
|
|
|
|
|
|
|
|
$this->assertCount(2, SessionModel::all()); // Two got deleted
|
|
|
|
$this->assertNotNull(SessionModel::find($this->currentSession->id));
|
|
|
|
$this->assertNull(SessionModel::find($this->secondSession->id));
|
|
|
|
}
|
2023-08-12 12:00:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::__construct
|
2023-11-11 21:26:11 +01:00
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::certificate
|
2023-08-12 12:00:46 +02:00
|
|
|
*/
|
2023-11-11 21:26:11 +01:00
|
|
|
public function testCertificateIfsg(): void
|
2023-08-12 12:00:46 +02:00
|
|
|
{
|
2023-08-30 19:54:30 +02:00
|
|
|
config(['ifsg_enabled' => true, 'ifsg_light_enabled' => true]);
|
2023-11-11 21:26:11 +01:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
|
2023-08-12 12:00:46 +02:00
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function ($view, $data) {
|
|
|
|
$this->assertEquals('pages/settings/certificates', $view);
|
2023-11-11 21:26:11 +01:00
|
|
|
$this->assertArrayHasKey('certificates', $data);
|
|
|
|
$this->assertEquals($this->user->license, $data['certificates']);
|
2023-08-12 12:00:46 +02:00
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
2023-11-11 21:26:11 +01:00
|
|
|
$this->controller->certificate();
|
2023-08-12 12:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-11 21:26:11 +01:00
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::certificate
|
2023-08-12 12:00:46 +02:00
|
|
|
*/
|
2023-11-11 21:26:11 +01:00
|
|
|
public function testCertificateIfsgNotConfigured(): void
|
2023-08-12 12:00:46 +02:00
|
|
|
{
|
|
|
|
config(['ifsg_enabled' => false]);
|
2023-11-11 21:26:11 +01:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
|
2023-08-12 12:00:46 +02:00
|
|
|
|
|
|
|
$this->expectException(HttpNotFound::class);
|
2023-11-11 21:26:11 +01:00
|
|
|
$this->controller->certificate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::certificate
|
|
|
|
*/
|
|
|
|
public function testCertificateDrivingLicense(): void
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
|
|
|
|
|
|
|
|
$angelType = AngelType::factory()->create(['requires_driver_license' => true]);
|
|
|
|
$this->user->userAngelTypes()->attach($angelType);
|
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function ($view, $data) {
|
|
|
|
$this->assertEquals('pages/settings/certificates', $view);
|
|
|
|
$this->assertArrayHasKey('certificates', $data);
|
|
|
|
$this->assertEquals($this->user->license, $data['certificates']);
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->controller->certificate();
|
2023-08-12 12:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveIfsgCertificate
|
|
|
|
*/
|
|
|
|
public function testSaveIfsgCertificateNotConfigured(): void
|
|
|
|
{
|
|
|
|
config(['ifsg_enabled' => false]);
|
|
|
|
|
|
|
|
$this->expectException(HttpNotFound::class);
|
|
|
|
$this->controller->saveIfsgCertificate($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveIfsgCertificate
|
|
|
|
*/
|
|
|
|
public function testSaveIfsgCertificateLight(): void
|
|
|
|
{
|
2023-08-30 19:54:30 +02:00
|
|
|
config(['ifsg_enabled' => true, 'ifsg_light_enabled' => true]);
|
2023-08-12 12:00:46 +02:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
|
|
|
|
$body = [
|
|
|
|
'ifsg_certificate_light' => true,
|
|
|
|
];
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
|
|
|
->with('http://localhost/settings/certificates')
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
$this->controller->saveIfsgCertificate($this->request);
|
|
|
|
|
2023-09-10 17:32:56 +02:00
|
|
|
$this->assertEquals(true, $this->user->license->ifsg_certificate_light);
|
|
|
|
$this->assertEquals(false, $this->user->license->ifsg_certificate);
|
2023-08-12 12:00:46 +02:00
|
|
|
}
|
|
|
|
|
2023-08-30 19:54:30 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveIfsgCertificate
|
|
|
|
*/
|
|
|
|
public function testSaveIfsgCertificateLightWhileDisabled(): void
|
|
|
|
{
|
|
|
|
config(['ifsg_enabled' => true, 'ifsg_light_enabled' => false]);
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
$this->user->license->ifsg_certificate_light = false;
|
|
|
|
$this->user->license->save();
|
|
|
|
|
|
|
|
$body = [
|
|
|
|
'ifsg_certificate_light' => true,
|
|
|
|
];
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
|
|
|
->with('http://localhost/settings/certificates')
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
$this->controller->saveIfsgCertificate($this->request);
|
|
|
|
|
2023-09-10 17:32:56 +02:00
|
|
|
$this->assertEquals(false, $this->user->license->ifsg_certificate_light);
|
|
|
|
$this->assertEquals(false, $this->user->license->ifsg_certificate);
|
2023-08-30 19:54:30 +02:00
|
|
|
}
|
|
|
|
|
2023-08-12 12:00:46 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveIfsgCertificate
|
|
|
|
*/
|
|
|
|
public function testSaveIfsgCertificate(): void
|
|
|
|
{
|
|
|
|
config(['ifsg_enabled' => true]);
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
|
|
|
|
$body = [
|
|
|
|
'ifsg_certificate' => true,
|
|
|
|
];
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
|
|
|
->with('http://localhost/settings/certificates')
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
$this->controller->saveIfsgCertificate($this->request);
|
|
|
|
|
2023-09-10 17:32:56 +02:00
|
|
|
$this->assertEquals(false, $this->user->license->ifsg_certificate_light);
|
|
|
|
$this->assertEquals(true, $this->user->license->ifsg_certificate);
|
2023-08-12 12:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveIfsgCertificate
|
|
|
|
*/
|
|
|
|
public function testSaveIfsgCertificateBoth(): void
|
|
|
|
{
|
|
|
|
config(['ifsg_enabled' => true]);
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
|
|
|
|
|
|
|
|
$body = [
|
|
|
|
'ifsg_certificate_light' => true,
|
|
|
|
'ifsg_certificate' => true,
|
|
|
|
];
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
|
|
|
->with('http://localhost/settings/certificates')
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
$this->controller->saveIfsgCertificate($this->request);
|
|
|
|
|
2023-09-10 17:32:56 +02:00
|
|
|
$this->assertEquals(false, $this->user->license->ifsg_certificate_light);
|
|
|
|
$this->assertEquals(true, $this->user->license->ifsg_certificate);
|
2023-08-12 12:00:46 +02:00
|
|
|
}
|
|
|
|
|
2023-11-11 21:26:11 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveDrivingLicense
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::checkDrivingLicense
|
|
|
|
*/
|
|
|
|
public function testSaveDrivingLicense(): void
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
|
|
|
|
|
|
|
|
$angelType = AngelType::factory()->create(['requires_driver_license' => true]);
|
|
|
|
$this->user->userAngelTypes()->attach($angelType);
|
|
|
|
|
|
|
|
$body = [
|
|
|
|
'has_car' => true,
|
|
|
|
'drive_forklift' => true,
|
|
|
|
'drive_12t' => true,
|
|
|
|
];
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
|
|
|
->with('http://localhost/settings/certificates')
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
$this->controller->saveDrivingLicense($this->request);
|
|
|
|
|
|
|
|
$this->assertTrue($this->user->license->has_car);
|
|
|
|
$this->assertTrue($this->user->license->drive_forklift);
|
|
|
|
$this->assertTrue($this->user->license->drive_12t);
|
|
|
|
$this->assertFalse($this->user->license->drive_car);
|
|
|
|
$this->assertFalse($this->user->license->drive_3_5t);
|
|
|
|
$this->assertFalse($this->user->license->drive_7_5t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::saveDrivingLicense
|
|
|
|
*/
|
|
|
|
public function testSaveDrivingLicenseNotAvailable(): void
|
|
|
|
{
|
|
|
|
$this->expectException(HttpNotFound::class);
|
|
|
|
$this->controller->saveDrivingLicense($this->request);
|
|
|
|
}
|
|
|
|
|
2023-11-16 18:20:50 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::api
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
|
|
|
|
*/
|
|
|
|
public function testApi(): void
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
|
|
|
|
|
|
|
|
/** @var Response|MockObject $response */
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function ($view, $data) {
|
|
|
|
$this->assertEquals('pages/settings/api', $view);
|
|
|
|
$this->assertArrayHasKey('settings_menu', $data);
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->controller = $this->app->make(SettingsController::class);
|
|
|
|
$this->controller->api();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::apiKeyReset
|
|
|
|
*/
|
|
|
|
public function testApiKeyReset(): void
|
|
|
|
{
|
|
|
|
$redirector = $this->createMock(Redirector::class);
|
|
|
|
$this->app->instance(Redirector::class, $redirector);
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
|
|
|
|
$this->setExpects($this->auth, 'resetApiKey', [$this->user], null, $this->atLeastOnce());
|
|
|
|
$this->setExpects($redirector, 'back', null, $this->response);
|
|
|
|
|
|
|
|
$this->controller = $this->app->make(SettingsController::class);
|
|
|
|
$this->controller->apiKeyReset();
|
|
|
|
}
|
2023-11-11 21:26:11 +01:00
|
|
|
|
2023-08-12 12:00:46 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
|
|
|
|
*/
|
|
|
|
public function testSettingsMenuProfile(): void
|
|
|
|
{
|
|
|
|
$menu = $this->controller->settingsMenu();
|
|
|
|
$this->assertArrayHasKey('http://localhost/settings/profile', $menu);
|
2023-09-10 17:32:56 +02:00
|
|
|
$this->assertEquals('settings.profile', $menu['http://localhost/settings/profile']);
|
2023-08-12 12:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
|
|
|
|
*/
|
|
|
|
public function testSettingsMenuPassword(): void
|
|
|
|
{
|
|
|
|
$menu = $this->controller->settingsMenu();
|
|
|
|
$this->assertArrayHasKey('http://localhost/settings/password', $menu);
|
2023-09-10 17:43:28 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
['title' => 'settings.password', 'icon' => 'key-fill'],
|
|
|
|
$menu['http://localhost/settings/password']
|
|
|
|
);
|
2023-08-12 12:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
|
|
|
|
*/
|
|
|
|
public function testSettingsMenuLanguage(): void
|
|
|
|
{
|
|
|
|
$menu = $this->controller->settingsMenu();
|
|
|
|
$this->assertArrayHasKey('http://localhost/settings/language', $menu);
|
2023-09-10 17:43:28 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
['title' => 'settings.language', 'icon' => 'translate'],
|
|
|
|
$menu['http://localhost/settings/language']
|
|
|
|
);
|
2023-08-12 12:00:46 +02: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
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSettingsMenuWithOAuth(): void
|
2020-11-24 17:27:21 +01:00
|
|
|
{
|
|
|
|
$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]);
|
|
|
|
|
2023-08-12 12:00:46 +02:00
|
|
|
$menu = $this->controller->settingsMenu();
|
|
|
|
$this->assertArrayHasKey('http://localhost/settings/oauth', $menu);
|
2023-09-10 17:32:56 +02:00
|
|
|
$this->assertEquals(['title' => 'settings.oauth', 'hidden' => false], $menu['http://localhost/settings/oauth']);
|
2021-12-03 22:27:15 +01:00
|
|
|
|
|
|
|
config(['oauth' => $providersHidden]);
|
2023-08-12 12:00:46 +02:00
|
|
|
|
|
|
|
$menu = $this->controller->settingsMenu();
|
|
|
|
$this->assertArrayHasKey('http://localhost/settings/oauth', $menu);
|
2023-09-10 17:32:56 +02:00
|
|
|
$this->assertEquals(['title' => 'settings.oauth', 'hidden' => true], $menu['http://localhost/settings/oauth']);
|
2020-11-24 17:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSettingsMenuWithoutOAuth(): void
|
2020-11-24 17:27:21 +01:00
|
|
|
{
|
|
|
|
config(['oauth' => []]);
|
2023-08-12 13:09:09 +02:00
|
|
|
|
2023-08-12 12:00:46 +02:00
|
|
|
$menu = $this->controller->settingsMenu();
|
|
|
|
$this->assertArrayNotHasKey('http://localhost/settings/oauth', $menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::checkOauthHidden
|
|
|
|
*/
|
|
|
|
public function testSettingsMenuWithIfsg(): void
|
|
|
|
{
|
|
|
|
config(['ifsg_enabled' => true]);
|
|
|
|
|
|
|
|
$menu = $this->controller->settingsMenu();
|
|
|
|
$this->assertArrayHasKey('http://localhost/settings/certificates', $menu);
|
2023-09-10 17:43:28 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
['title' => 'settings.certificates', 'icon' => 'card-checklist'],
|
|
|
|
$menu['http://localhost/settings/certificates']
|
|
|
|
);
|
2023-08-12 12:00:46 +02:00
|
|
|
}
|
2020-11-24 17:27:21 +01:00
|
|
|
|
2023-08-12 12:00:46 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
|
|
|
|
*/
|
|
|
|
public function testSettingsMenuWithoutIfsg(): void
|
|
|
|
{
|
|
|
|
config(['ifsg_enabled' => false]);
|
2023-08-12 13:09:09 +02:00
|
|
|
|
2023-08-12 12:00:46 +02:00
|
|
|
$menu = $this->controller->settingsMenu();
|
|
|
|
$this->assertArrayNotHasKey('http://localhost/settings/certificates', $menu);
|
2020-11-24 17:27:21 +01:00
|
|
|
}
|
|
|
|
|
2023-11-16 18:20:50 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
|
|
|
|
*/
|
|
|
|
public function testSettingsMenuApi(): void
|
|
|
|
{
|
|
|
|
$this->setExpects($this->auth, 'can', ['api'], true, $this->atLeastOnce());
|
|
|
|
|
|
|
|
$menu = $this->controller->settingsMenu();
|
|
|
|
$this->assertArrayHasKey('http://localhost/settings/profile', $menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
|
|
|
|
*/
|
|
|
|
public function testSettingsMenuApiNotAvailable(): void
|
|
|
|
{
|
|
|
|
$menu = $this->controller->settingsMenu();
|
|
|
|
$this->assertArrayNotHasKey('http://localhost/settings/api', $menu);
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:41:02 +01:00
|
|
|
/**
|
|
|
|
* Setup environment
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2022-06-07 21:33:08 +02:00
|
|
|
$themes = [
|
|
|
|
0 => ['name' => 'Engelsystem light'],
|
2023-02-05 18:03:00 +01:00
|
|
|
1 => ['name' => 'Engelsystem dark'],
|
2022-06-07 21:33:08 +02:00
|
|
|
];
|
2022-06-20 19:03:47 +02:00
|
|
|
$languages = [
|
|
|
|
'en_US' => 'English',
|
2023-02-05 18:03:00 +01:00
|
|
|
'de_DE' => 'Deutsch',
|
2022-06-20 19:03:47 +02:00
|
|
|
];
|
2022-10-13 20:23:22 +02:00
|
|
|
$tshirt_sizes = ['S' => 'Small'];
|
2023-11-03 15:15:44 +01:00
|
|
|
$requiredFields = [
|
|
|
|
'pronoun' => false,
|
|
|
|
'firstname' => false,
|
|
|
|
'lastname' => false,
|
|
|
|
'tshirt_size' => true,
|
|
|
|
'mobile' => false,
|
|
|
|
'dect' => false,
|
|
|
|
];
|
2022-10-13 20:23:22 +02:00
|
|
|
$this->config = new Config([
|
|
|
|
'min_password_length' => 6,
|
|
|
|
'themes' => $themes,
|
|
|
|
'locales' => $languages,
|
2023-02-05 18:03:00 +01:00
|
|
|
'tshirt_sizes' => $tshirt_sizes,
|
2023-03-05 03:00:38 +01:00
|
|
|
'goodie_type' => GoodieType::Goodie->value,
|
2023-11-12 17:40:21 +01:00
|
|
|
'required_user_fields' => $requiredFields,
|
2022-10-13 20:23:22 +02:00
|
|
|
]);
|
2020-11-23 20:41:02 +01:00
|
|
|
$this->app->instance('config', $this->config);
|
|
|
|
$this->app->instance(Config::class, $this->config);
|
|
|
|
|
2020-11-24 17:27:21 +01:00
|
|
|
$this->app->bind('http.urlGenerator', UrlGenerator::class);
|
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-20 21:44:58 +02:00
|
|
|
->has(Settings::factory([
|
|
|
|
'theme' => 1,
|
|
|
|
'language' => 'en_US',
|
|
|
|
'email_goody' => false,
|
|
|
|
'mobile_show' => false,
|
|
|
|
]))
|
2023-08-12 12:00:46 +02:00
|
|
|
->has(License::factory())
|
2022-06-07 21:33:08 +02:00
|
|
|
->create();
|
2022-10-03 18:58:04 +02:00
|
|
|
|
2023-11-11 21:26:11 +01:00
|
|
|
$this->setExpects($this->auth, 'user', null, $this->user, $this->any());
|
|
|
|
|
2023-09-17 20:15:49 +02:00
|
|
|
// Create 4 sessions, 3 for the active user
|
|
|
|
$this->otherSession = SessionModel::factory()->create()->first(); // Other users sessions
|
|
|
|
$sessions = SessionModel::factory(3)->create(['user_id' => $this->user->id]);
|
|
|
|
$this->currentSession = $sessions->first();
|
|
|
|
$this->secondSession = $sessions->last();
|
|
|
|
$this->session->setId($this->currentSession->id);
|
|
|
|
|
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
|
|
|
}
|