refactor driving license into the user certificate settings

This commit is contained in:
Xu 2023-11-11 21:26:11 +01:00 committed by Igor Scheller
parent 24f91ce9b5
commit 6477e5dabd
13 changed files with 319 additions and 356 deletions

View File

@ -40,8 +40,9 @@ $route->addGroup(
$route->post('/theme', 'SettingsController@saveTheme');
$route->get('/language', 'SettingsController@language');
$route->post('/language', 'SettingsController@saveLanguage');
$route->get('/certificates', 'SettingsController@ifsgCertificate');
$route->post('/certificates', 'SettingsController@saveIfsgCertificate');
$route->get('/certificates', 'SettingsController@certificate');
$route->post('/certificates/ifsg', 'SettingsController@saveIfsgCertificate');
$route->post('/certificates/driving', 'SettingsController@saveDrivingLicense');
$route->get('/oauth', 'SettingsController@oauth');
$route->get('/sessions', 'SettingsController@sessions');
$route->post('/sessions', 'SettingsController@sessionsDelete');

View File

@ -1,169 +0,0 @@
<?php
use Engelsystem\Models\User\User;
/**
* Generates a hint, if user joined angeltypes that require a driving license and the user has no driver license
* information provided.
*
* @return string|null
*/
function user_driver_license_required_hint()
{
$user = auth()->user();
// User has already entered data, no hint needed.
if ($user->license->wantsToDrive()) {
return null;
}
$angeltypes = $user->userAngelTypes;
foreach ($angeltypes as $angeltype) {
if ($angeltype->requires_driver_license) {
return sprintf(
__('You joined an angeltype which requires a driving license. Please edit your driving license information here: %s.'),
'<a href="' . user_driver_license_edit_link() . '">' . __('driving license information') . '</a>'
);
}
}
return null;
}
function user_ifsg_certificate_required_hint()
{
$user = auth()->user();
// User has already entered data, no hint needed.
if (!config('ifsg_enabled') || $user->license->ifsg_light || $user->license->ifsg) {
return null;
}
$angeltypes = $user->userAngelTypes;
foreach ($angeltypes as $angeltype) {
if (
$angeltype->requires_ifsg_certificate && !(
$user->license->ifsg_certificate || $user->license->ifsg_certificate_light
)
) {
return sprintf(
__('angeltype.ifsg.required.info.here'),
'<a href="' . url('/settings/certificates') . '">' . __('ifsg.info') . '</a>'
);
}
}
return null;
}
/**
* Route user driver licenses actions.
*
* @return array
*/
function user_driver_licenses_controller()
{
$user = auth()->user();
if (!$user) {
throw_redirect(page_link_to());
}
$action = strip_request_item('action', 'edit');
return match ($action) {
'edit' => user_driver_license_edit_controller(),
default => user_driver_license_edit_controller(),
};
}
/**
* Link to user driver license edit page for given user.
*
* @param User $user
* @return string
*/
function user_driver_license_edit_link($user = null)
{
if (!$user) {
return page_link_to('user_driver_licenses');
}
return page_link_to('user_driver_licenses', ['user_id' => $user->id]);
}
/**
* Loads the user for the driver license.
*
* @return User
*/
function user_driver_license_load_user()
{
$request = request();
$user_source = auth()->user();
if ($request->has('user_id')) {
$user_source = User::find($request->input('user_id'));
if (empty($user_source)) {
throw_redirect(user_driver_license_edit_link());
}
}
return $user_source;
}
/**
* Edit a users driver license information.
*
* @return array
*/
function user_driver_license_edit_controller()
{
$user = auth()->user();
$request = request();
$user_source = user_driver_license_load_user();
// only privilege admin_user can edit other users driver license information
if ($user->id != $user_source->id && !auth()->can('admin_user')) {
throw_redirect(user_driver_license_edit_link());
}
$driverLicense = $user_source->license;
if ($request->hasPostData('submit')) {
if ($request->has('wants_to_drive')) {
$driverLicense->has_car = $request->has('has_car');
$driverLicense->drive_car = $request->has('has_license_car');
$driverLicense->drive_3_5t = $request->has('has_license_3_5t_transporter');
$driverLicense->drive_7_5t = $request->has('has_license_7_5t_truck');
$driverLicense->drive_12t = $request->has('has_license_12t_truck');
$driverLicense->drive_forklift = $request->has('has_license_forklift');
if ($driverLicense->wantsToDrive()) {
$driverLicense->save();
engelsystem_log('Driver license information updated.');
success(__('Your driver license information has been saved.'));
throw_redirect(user_link($user_source->id));
} else {
error(__('Please select at least one driving license.'));
}
} else {
$driverLicense->has_car = false;
$driverLicense->drive_car = false;
$driverLicense->drive_3_5t = false;
$driverLicense->drive_7_5t = false;
$driverLicense->drive_12t = false;
$driverLicense->drive_forklift = false;
$driverLicense->save();
engelsystem_log('Driver license information removed.');
success(__('Your driver license information has been removed.'));
throw_redirect(user_link($user_source->id));
}
}
return [
sprintf(__('Edit %s driving license information'), $user_source->displayName),
UserDriverLicense_edit_view($user_source, $driverLicense),
];
}

View File

@ -437,3 +437,57 @@ function shiftCalendarRendererByShiftFilter(ShiftsFilter $shiftsFilter)
return new ShiftCalendarRenderer($filtered_shifts, $needed_angeltypes, $shift_entries, $shiftsFilter);
}
/**
* Generates a hint, if user joined angeltypes that require a driving license and the user has no driver license
* information provided.
*
* @return string|null
*/
function user_driver_license_required_hint()
{
$user = auth()->user();
// User has already entered data, no hint needed.
if ($user->license->wantsToDrive()) {
return null;
}
$angeltypes = $user->userAngelTypes;
foreach ($angeltypes as $angeltype) {
if ($angeltype->requires_driver_license) {
return sprintf(
__('angeltype.driving_license.required.info.here'),
'<a href="' . url('/settings/certificates') . '">' . __('driving_license.info') . '</a>'
);
}
}
return null;
}
function user_ifsg_certificate_required_hint()
{
$user = auth()->user();
// User has already entered data, no hint needed.
if (!config('ifsg_enabled') || $user->license->ifsg_light || $user->license->ifsg) {
return null;
}
$angeltypes = $user->userAngelTypes;
foreach ($angeltypes as $angeltype) {
if (
$angeltype->requires_ifsg_certificate && !(
$user->license->ifsg_certificate || $user->license->ifsg_certificate_light
)
) {
return sprintf(
__('angeltype.ifsg.required.info.here'),
'<a href="' . url('/settings/certificates') . '">' . __('ifsg.info') . '</a>'
);
}
}
return null;
}

View File

@ -33,7 +33,6 @@ $includeFiles = [
__DIR__ . '/../includes/view/ShiftEntry_view.php',
__DIR__ . '/../includes/view/ShiftTypes_view.php',
__DIR__ . '/../includes/view/UserAngelTypes_view.php',
__DIR__ . '/../includes/view/UserDriverLicenses_view.php',
__DIR__ . '/../includes/view/UserHintsRenderer.php',
__DIR__ . '/../includes/view/User_view.php',
@ -46,7 +45,6 @@ $includeFiles = [
__DIR__ . '/../includes/controller/shifttypes_controller.php',
__DIR__ . '/../includes/controller/users_controller.php',
__DIR__ . '/../includes/controller/user_angeltypes_controller.php',
__DIR__ . '/../includes/controller/user_driver_licenses_controller.php',
__DIR__ . '/../includes/helper/legacy_helper.php',
__DIR__ . '/../includes/helper/message_helper.php',

View File

@ -191,7 +191,7 @@ function AngelType_view_buttons(
) {
if ($angeltype->requires_driver_license) {
$buttons[] = button(
user_driver_license_edit_link($user),
url('/settings/certificates'),
icon('person-vcard') . __('my driving license')
);
}
@ -378,11 +378,11 @@ function AngelType_view_table_headers(AngelType $angeltype, $supporter, $admin_a
$headers = array_merge($headers, [
'wants_to_drive' => __('Driver'),
'has_car' => __('Has car'),
'has_license_car' => __('Car'),
'has_license_3_5t_transporter' => __('3.5t Transporter'),
'has_license_7_5t_truck' => __('7.5t Truck'),
'has_license_12t_truck' => __('12t Truck'),
'has_license_forklift' => __('Forklift'),
'has_license_car' => __('settings.certificates.drive_car'),
'has_license_3_5t_transporter' => __('settings.certificates.drive_3_5t'),
'has_license_7_5t_truck' => __('settings.certificates.drive_7_5t'),
'has_license_12t_truck' => __('settings.certificates.drive_12t'),
'has_license_forklift' => __('settings.certificates.drive_forklift'),
]);
}

View File

@ -1,57 +0,0 @@
<?php
use Engelsystem\Models\User\License;
use Engelsystem\Models\User\User;
/**
* Edit a user's driving license information.
*
* @param User $user_source The user
* @param License $user_driver_license The user driver license
* @return string
*/
function UserDriverLicense_edit_view($user_source, $user_driver_license)
{
$link = button(user_link($user_source->id), icon('chevron-left'), 'btn-sm');
return page_with_title(
$link . ' ' . sprintf(__('Edit %s driving license information'), User_Nick_render($user_source)),
[
msg(),
form([
form_info(__('Privacy'), __('Your driving license information is only visible for supporters and admins.')),
form_checkbox('wants_to_drive', __('I am willing to drive a car for the event'), $user_driver_license->wantsToDrive()),
div('m-3', [
form_checkbox(
'has_car',
__('I have my own car with me and am willing to use it for the event (You\'ll get reimbursed for fuel)'),
$user_driver_license->has_car
),
heading(__('driving license'), 3),
form_checkbox('has_license_car', __('Car'), $user_driver_license->drive_car),
form_checkbox(
'has_license_3_5t_transporter',
__('3.5t Transporter'),
$user_driver_license->drive_3_5t
),
form_checkbox(
'has_license_7_5t_truck',
__('7.5t Truck'),
$user_driver_license->drive_7_5t
),
form_checkbox(
'has_license_12t_truck',
__('12t Truck'),
$user_driver_license->drive_12t
),
form_checkbox(
'has_license_forklift',
__('Forklift'),
$user_driver_license->drive_forklift
),
], 'driving_license'),
form_submit('submit', __('form.save')),
]),
],
true
);
}

View File

@ -557,14 +557,6 @@ function User_view(
page_link_to('admin_user', ['id' => $user_source->id]),
icon('pencil') . __('edit')
) : '',
$admin_user_privilege || ($its_me && $needs_drivers_license) ? button(
user_driver_license_edit_link($user_source),
icon('person-vcard') . __('driving license')
) : '',
config('ifsg_enabled') && ($admin_user_privilege || ($its_me && $needs_ifsg_certificate)) ? button(
page_link_to('settings/certificates'),
icon('card-checklist') . __('ifsg.certificate')
) : '',
(($admin_user_privilege || $auth->can('admin_arrive')) && !$user_source->state->arrived) ?
form([
form_hidden('action', 'arrived'),

View File

@ -430,28 +430,6 @@ msgstr "Du bist %s beigetreten."
msgid "Become a %s"
msgstr "Werde ein %s"
msgid ""
"You joined an angeltype which requires a driving license. Please edit your "
"driving license information here: %s."
msgstr ""
"Du bist einem Engeltypen beigetreten, der Führerschein-Infos benötigt. Bitte "
"trage Deine Führerschein-Infos hier ein: %s."
msgid "driving license information"
msgstr "Führerschein-Infos"
msgid "Your driver license information has been saved."
msgstr "Deine Führerschein-Infos wurden gespeichert."
msgid "Please select at least one driving license."
msgstr "Bitte wähle mindestens einen Führerschein-Typen aus."
msgid "Your driver license information has been removed."
msgstr "Deine Führerschein-Infos wurden gelöscht."
msgid "Edit %s driving license information"
msgstr "Bearbeite die Führerschein-Infos von %s"
msgid "You cannot delete yourself."
msgstr "Du kannst Dich nicht selber löschen."
@ -1015,21 +993,6 @@ msgstr "Fahrer"
msgid "Has car"
msgstr "Hat Auto"
msgid "Car"
msgstr "Auto"
msgid "3.5t Transporter"
msgstr "3,5t Transporter"
msgid "7.5t Truck"
msgstr "7,5t LKW"
msgid "12t Truck"
msgstr "12t LKW"
msgid "Forklift"
msgstr "Gabelstapler"
msgid "Info"
msgstr "Info"
@ -1243,23 +1206,6 @@ msgstr "Ausblenden bei Registrierung"
msgid "Back to profile"
msgstr "Zurück zum Profil"
msgid "Privacy"
msgstr "Privatsphäre"
msgid ""
"Your driving license information is only visible for supporters and admins."
msgstr "Deine Führerschein-Infos sind nur für Supporter und Admins sichtbar."
msgid "I am willing to drive a car for the event"
msgstr "Ich möchte für das Event Auto fahren"
msgid ""
"I have my own car with me and am willing to use it for the event (You'll get "
"reimbursed for fuel)"
msgstr ""
"Ich habe mein eigenes Auto dabei und möchte es zum Fahren für das Event "
"verwenden (Du wirst für Spritkosten entschädigt)"
msgid ""
"Do you really want to delete the user including all his shifts and every "
"other piece of his data?"
@ -1333,9 +1279,6 @@ msgstr "Aktion"
msgid "You have done enough."
msgstr "Du hast genug gemacht."
msgid "driving license"
msgstr "Führerschein"
msgid "Vouchers"
msgstr "Gutscheine"
@ -1784,18 +1727,44 @@ msgid "settings.certificates"
msgstr "Zertifikate"
msgid "settings.certificates.info"
msgstr "Hier kannst du deine gemachte Gesundheitsbelehrung eintragen"
msgstr "Diese Informationen sind nur für Supporter und Admins sichtbar."
msgid "settings.ifsg_light"
msgid "settings.certificates.title.ifsg"
msgstr "Gesundheitsbelehrungen"
msgid "settings.certificates.driving_license"
msgstr "Führerschein"
msgid "settings.certificates.has_car"
msgstr ""
"Ich habe mein eigenes Auto dabei und möchte es zum Fahren für das Event "
"verwenden (Du wirst für Spritkosten entschädigt)"
msgid "settings.certificates.drive_car"
msgstr "Auto"
msgid "settings.certificates.drive_3_5t"
msgstr "3,5t Transporter"
msgid "settings.certificates.drive_7_5t"
msgstr "7,5t LKW"
msgid "settings.certificates.drive_12t"
msgstr "12t LKW"
msgid "settings.certificates.drive_forklift"
msgstr "Gabelstapler"
msgid "settings.certificates.ifsg_light"
msgstr "Ich wurde vor Ort nach IfSG §43 (Frikadellendiplom light) belehrt."
msgid "settings.ifsg"
msgid "settings.certificates.ifsg"
msgstr "Ich habe eine Belehrung nach §43 IfSG (Frikadellendiplom) bei meinem Gesundheitsamt "
"erhalten und innerhalb von 3 Monaten die Zweitbelehrung durch uns oder meinen Arbeitgeber/Koch/Verein bekommen. "
"Zusätzlich ist die Zweitbelehrung nicht älter als zwei Jahre."
msgid "settings.certificates.success"
msgstr "Gesundheitsbelehrung wurde erfolgreich geändert."
msgstr "Zertifikate wurden erfolgreich aktualisiert."
msgid "angeltype.ifsg.required"
msgstr "Benötigt eine Gesundheitsbelehrung"
@ -1816,9 +1785,17 @@ msgid "angeltype.ifsg.required.info.here"
msgstr "Dieser Engeltyp benötigt eine Gesundheitsbelehrung. "
"Bitte trage deine Gesundheitsbelehrung hier ein: %s"
msgid "angeltype.driving_license.required.info.here"
msgstr ""
"Du bist einem Engeltypen beigetreten, der Führerschein-Infos benötigt. "
"Bitte trage Deine Führerschein-Infos hier ein: %s."
msgid "ifsg.info"
msgstr "Gesundheitsbelehrungs-Infos"
msgid "driving_license.info"
msgstr "Führerschein-Infos"
msgid "settings.language"
msgstr "Sprache"

View File

@ -345,18 +345,44 @@ msgid "settings.certificates"
msgstr "Certificates"
msgid "settings.certificates.info"
msgstr "Here you can enter your health instruction"
msgstr "This information is only visible for supporters and admins."
msgid "settings.ifsg_light"
msgid "settings.certificates.title.ifsg"
msgstr "Health instructions"
msgid "settings.certificates.driving_license"
msgstr "Driving license"
msgid "settings.certificates.has_car"
msgstr ""
"I have my own car with me and am willing to use it for the event (You'll get "
"reimbursed for fuel)"
msgid "settings.certificates.drive_car"
msgstr "Car"
msgid "settings.certificates.drive_3_5t"
msgstr "3.5t Transporter"
msgid "settings.certificates.drive_7_5t"
msgstr "7.5t Truck"
msgid "settings.certificates.drive_12t"
msgstr "12t Truck"
msgid "settings.certificates.drive_forklift"
msgstr "Forklift"
msgid "settings.certificates.ifsg_light"
msgstr "I was instructed about IfSG §43 (aka Frikadellendiplom light) on site."
msgid "settings.ifsg"
msgid "settings.certificates.ifsg"
msgstr "I have gotten the instruction about §43 IfSG (aka Frikadellendiplom) from my Health Department "
"and a second instruction from us or my employer/chef/assosiation within 3 months. "
"Additionally my second instruction is not older than 2 years."
msgid "settings.certificates.success"
msgstr "Health instruction was changed successfully."
msgstr "Certificates were updated successfully."
msgid "angeltype.ifsg.required"
msgstr "Requires health instruction"
@ -377,9 +403,17 @@ msgid "angeltype.ifsg.required.info.here"
msgstr "You joined an angeltype which requires a health instruction. "
"Please edit your health instruction information here: %s."
msgid "angeltype.driving_license.required.info.here"
msgstr ""
"You joined an angeltype which requires a driving license. "
"Please edit your driving license information here: %s."
msgid "ifsg.info"
msgstr "Health instruction information"
msgid "driving_license.info"
msgstr "driving license information"
msgid "settings.language"
msgstr "Language"

View File

@ -5,22 +5,56 @@
{% block title %}{{ __('settings.certificates') }}{% endblock %}
{% block row_content %}
<form action="" enctype="multipart/form-data" method="post">
{{ csrf() }}
<div class="row">
<div class="col-md-12">
{{ m.info(__('settings.certificates.info')) }}
{% if config('ifsg_light_enabled') %}
{{ f.checkbox('ifsg_certificate_light', __('settings.ifsg_light'), {
'checked': ifsg_certificate_light,
<div class="row">
{% if config('ifsg_enabled') %}
<form action="{{ url('/settings/certificates/ifsg') }}" enctype="multipart/form-data" method="post">
{{ csrf() }}
<div class="col-md-12 pb-3">
<h3>{{ __('settings.certificates.title.ifsg') }}</h3>
{{ m.info(__('settings.certificates.info')) }}
{% if config('ifsg_light_enabled') %}
{{ f.checkbox('ifsg_certificate_light', __('settings.certificates.ifsg_light'), {
'checked': certificates.ifsg_certificate_light,
}) }}
{% endif %}
{{ f.checkbox('ifsg_certificate', __('settings.certificates.ifsg'), {
'checked': certificates.ifsg_certificate,
}) }}
{% endif %}
{{ f.checkbox('ifsg_certificate', __('settings.ifsg'), {
'checked': ifsg_certificate,
}) }}
{{ f.submit(__('form.save')) }}
</div>
</div>
</form>
{{ f.submit(__('form.save')) }}
</div>
</form>
{% endif %}
{% if driving_license %}
<form action="{{ url('/settings/certificates/driving') }}" enctype="multipart/form-data" method="post">
{{ csrf() }}
<div class="col-md-12">
<h3>{{ __('settings.certificates.driving_license') }}</h3>
{{ m.info(__('settings.certificates.info')) }}
{{ f.checkbox('drive_car', __('settings.certificates.drive_car'), {
'checked': certificates.drive_car,
}) }}
{{ f.checkbox('drive_3_5t', __('settings.certificates.drive_3_5t'), {
'checked': certificates.drive_3_5t,
}) }}
{{ f.checkbox('drive_7_5t', __('settings.certificates.drive_7_5t'), {
'checked': certificates.drive_7_5t,
}) }}
{{ f.checkbox('drive_12t', __('settings.certificates.drive_12t'), {
'checked': certificates.drive_12t,
}) }}
{{ f.checkbox('drive_forklift', __('settings.certificates.drive_forklift'), {
'checked': certificates.drive_forklift,
}) }}
{{ f.checkbox('has_car', __('settings.certificates.has_car'), {
'checked': certificates.has_car,
}) }}
{{ f.submit(__('form.save')) }}
</div>
</form>
{% endif %}
</div>
{% endblock %}

View File

@ -11,6 +11,7 @@ use Engelsystem\Http\Response;
use Engelsystem\Http\Redirector;
use Engelsystem\Http\Request;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Models\AngelType;
use Psr\Log\LoggerInterface;
class SettingsController extends BaseController
@ -234,36 +235,36 @@ class SettingsController extends BaseController
return $this->redirect->to('/settings/language');
}
public function ifsgCertificate(): Response
public function certificate(): Response
{
$user = $this->auth->user();
if (!config('ifsg_enabled')) {
throw new HttpNotFound('ifsg.disabled');
if (!config('ifsg_enabled') && !$this->checkDrivingLicense()) {
throw new HttpNotFound();
}
return $this->response->withView(
'pages/settings/certificates',
[
'settings_menu' => $this->settingsMenu(),
'ifsg_certificate_light' => $user->license->ifsg_certificate_light,
'ifsg_certificate' => $user->license->ifsg_certificate,
'driving_license' => $this->checkDrivingLicense(),
'certificates' => $user->license,
]
);
}
public function saveIfsgCertificate(Request $request): Response
{
if (!config('ifsg_enabled')) {
throw new HttpNotFound();
}
$user = $this->auth->user();
$data = $this->validate($request, [
'ifsg_certificate_light' => 'optional|checked',
'ifsg_certificate' => 'optional|checked',
]);
if (!config('ifsg_enabled')) {
throw new HttpNotFound('ifsg.disabled');
}
if (config('ifsg_light_enabled')) {
$user->license->ifsg_certificate_light = !$data['ifsg_certificate'] && $data['ifsg_certificate_light'];
}
@ -275,6 +276,35 @@ class SettingsController extends BaseController
return $this->redirect->to('/settings/certificates');
}
public function saveDrivingLicense(Request $request): Response
{
if (!$this->checkDrivingLicense()) {
throw new HttpNotFound();
}
$user = $this->auth->user();
$data = $this->validate($request, [
'has_car' => 'optional|checked',
'drive_car' => 'optional|checked',
'drive_3_5t' => 'optional|checked',
'drive_7_5t' => 'optional|checked',
'drive_12t' => 'optional|checked',
'drive_forklift' => 'optional|checked',
]);
$user->license->has_car = (bool) $data['has_car'];
$user->license->drive_car = (bool) $data['drive_car'];
$user->license->drive_3_5t = (bool) $data['drive_3_5t'];
$user->license->drive_7_5t = (bool) $data['drive_7_5t'];
$user->license->drive_12t = (bool) $data['drive_12t'];
$user->license->drive_forklift = (bool) $data['drive_forklift'];
$user->license->save();
$this->addNotification('settings.certificates.success');
return $this->redirect->to('/settings/certificates');
}
public function oauth(): Response
{
$providers = $this->config->get('oauth');
@ -342,7 +372,7 @@ class SettingsController extends BaseController
$menu[url('/settings/theme')] = 'settings.theme';
}
if (config('ifsg_enabled')) {
if (config('ifsg_enabled') || $this->checkDrivingLicense()) {
$menu[url('/settings/certificates')] = ['title' => 'settings.certificates', 'icon' => 'card-checklist'];
}
@ -366,6 +396,13 @@ class SettingsController extends BaseController
return true;
}
protected function checkDrivingLicense(): bool
{
return $this->auth->user()->userAngelTypes->filter(function (AngelType $angelType) {
return $angelType->requires_driver_license;
})->isNotEmpty();
}
private function isRequired(string $key): string
{
$requiredFields = $this->config->get('required_user_fields');

View File

@ -24,7 +24,6 @@ class LegacyMiddleware implements MiddlewareInterface
'shift_entries',
'shifts',
'users',
'user_driver_licenses',
'admin_shifts_history',
];
@ -90,8 +89,6 @@ class LegacyMiddleware implements MiddlewareInterface
return users_controller();
case 'user_angeltypes':
return user_angeltypes_controller();
case 'user_driver_licenses':
return user_driver_licenses_controller();
case 'shifttypes':
list($title, $content) = shifttypes_controller();
return [$title, $content];

View File

@ -11,6 +11,7 @@ use Engelsystem\Controllers\NotificationType;
use Engelsystem\Controllers\SettingsController;
use Engelsystem\Http\Exceptions\HttpNotFound;
use Engelsystem\Http\Response;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Session as SessionModel;
use Engelsystem\Models\User\License;
use Engelsystem\Models\User\Settings;
@ -88,7 +89,7 @@ class SettingsControllerTest extends ControllerTest
*/
public function testProfile(): void
{
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
/** @var Response|MockObject $response */
$this->response->expects($this->once())
->method('withView')
@ -417,7 +418,7 @@ class SettingsControllerTest extends ControllerTest
*/
public function testThemeUnderNormalConditionReturnsCorrectViewAndData(): void
{
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
/** @var Response|MockObject $response */
$this->response->expects($this->once())
@ -488,7 +489,7 @@ class SettingsControllerTest extends ControllerTest
*/
public function testLanguageUnderNormalConditionReturnsCorrectViewAndData(): void
{
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
/** @var Response|MockObject $response */
$this->response->expects($this->once())
@ -593,7 +594,7 @@ class SettingsControllerTest extends ControllerTest
*/
public function testSessions(): void
{
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
$this->response->expects($this->once())
->method('withView')
@ -680,37 +681,57 @@ class SettingsControllerTest extends ControllerTest
/**
* @covers \Engelsystem\Controllers\SettingsController::__construct
* @covers \Engelsystem\Controllers\SettingsController::ifsgCertificate
* @covers \Engelsystem\Controllers\SettingsController::certificate
*/
public function testIfsgCertificate(): void
public function testCertificateIfsg(): void
{
config(['ifsg_enabled' => true, 'ifsg_light_enabled' => true]);
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
$this->response->expects($this->once())
->method('withView')
->willReturnCallback(function ($view, $data) {
$this->assertEquals('pages/settings/certificates', $view);
$this->assertArrayHasKey('ifsg_certificate_light', $data);
$this->assertArrayHasKey('ifsg_certificate', $data);
$this->assertEquals($this->user->license->ifsg_certificate_light, $data['ifsg_certificate_light']);
$this->assertEquals($this->user->license->ifsg_certificate, $data['ifsg_certificate']);
$this->assertArrayHasKey('certificates', $data);
$this->assertEquals($this->user->license, $data['certificates']);
return $this->response;
});
$this->controller->ifsgCertificate();
$this->controller->certificate();
}
/**
* @covers \Engelsystem\Controllers\SettingsController::ifsgCertificate
* @covers \Engelsystem\Controllers\SettingsController::certificate
*/
public function testIfsgCertificateNotConfigured(): void
public function testCertificateIfsgNotConfigured(): void
{
config(['ifsg_enabled' => false]);
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
$this->expectException(HttpNotFound::class);
$this->controller->ifsgCertificate();
$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();
}
/**
@ -719,7 +740,6 @@ class SettingsControllerTest extends ControllerTest
public function testSaveIfsgCertificateNotConfigured(): void
{
config(['ifsg_enabled' => false]);
$this->setExpects($this->auth, 'user', null, $this->user, $this->once());
$this->expectException(HttpNotFound::class);
$this->controller->saveIfsgCertificate($this->request);
@ -824,6 +844,49 @@ class SettingsControllerTest extends ControllerTest
$this->assertEquals(true, $this->user->license->ifsg_certificate);
}
/**
* @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);
}
/**
* @covers \Engelsystem\Controllers\SettingsController::settingsMenu
*/
@ -969,6 +1032,8 @@ class SettingsControllerTest extends ControllerTest
->has(License::factory())
->create();
$this->setExpects($this->auth, 'user', null, $this->user, $this->any());
// 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]);