From 676a2113b471859a0e6472532805b56bc8c01d78 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 19 Dec 2021 18:38:42 +0100 Subject: [PATCH] Added User\License model --- db/factories/User/LicenseFactory.php | 34 ++++++++ ...2_19_000000_create_user_licenses_table.php | 87 +++++++++++++++++++ includes/controller/angeltypes_controller.php | 3 +- .../user_driver_licenses_controller.php | 52 +++++------ includes/includes.php | 1 - includes/model/User_model.php | 5 +- includes/view/AngelTypes_view.php | 23 ++--- includes/view/UserDriverLicenses_view.php | 24 ++--- resources/lang/de_DE/default.po | 8 +- resources/lang/pt_BR/default.po | 8 +- src/Controllers/Metrics/Controller.php | 3 +- src/Controllers/Metrics/Stats.php | 24 +++-- src/Models/User/License.php | 74 ++++++++++++++++ src/Models/User/User.php | 11 +++ .../Controllers/Metrics/ControllerTest.php | 6 +- tests/Unit/Controllers/Metrics/StatsTest.php | 34 +++++++- tests/Unit/Models/User/LicenseTest.php | 40 +++++++++ tests/Unit/Models/User/UserTest.php | 10 +++ 18 files changed, 361 insertions(+), 86 deletions(-) create mode 100644 db/factories/User/LicenseFactory.php create mode 100644 db/migrations/2021_12_19_000000_create_user_licenses_table.php create mode 100644 src/Models/User/License.php create mode 100644 tests/Unit/Models/User/LicenseTest.php diff --git a/db/factories/User/LicenseFactory.php b/db/factories/User/LicenseFactory.php new file mode 100644 index 00000000..0d32f2fc --- /dev/null +++ b/db/factories/User/LicenseFactory.php @@ -0,0 +1,34 @@ +faker->boolean(.8); + $drive_3_5t = $drive_car && $this->faker->boolean(.7); + $drive_7_5t = $drive_3_5t && $this->faker->boolean(); + $drive_12t = $drive_7_5t && $this->faker->boolean(.3); + $drive_forklift = ($drive_car && $this->faker->boolean(.1)) + || ($drive_12t && $this->faker->boolean(.7)); + + return [ + 'has_car' => $drive_car && $this->faker->boolean(.7), + 'drive_forklift' => $drive_forklift, + 'drive_car' => $drive_car, + 'drive_3_5t' => $drive_3_5t, + 'drive_7_5t' => $drive_7_5t, + 'drive_12t' => $drive_12t, + ]; + } +} diff --git a/db/migrations/2021_12_19_000000_create_user_licenses_table.php b/db/migrations/2021_12_19_000000_create_user_licenses_table.php new file mode 100644 index 00000000..0365fb08 --- /dev/null +++ b/db/migrations/2021_12_19_000000_create_user_licenses_table.php @@ -0,0 +1,87 @@ +schema->create('users_licenses', function (Blueprint $table) { + $this->referencesUser($table, true); + $table->boolean('has_car')->default(false); + $table->boolean('drive_forklift')->default(false); + $table->boolean('drive_car')->default(false); + $table->boolean('drive_3_5t')->default(false); + $table->boolean('drive_7_5t')->default(false); + $table->boolean('drive_12t')->default(false); + }); + + if ($this->schema->hasTable('UserDriverLicenses')) { + $licenses = $this->schema->getConnection() + ->table('UserDriverLicenses') + ->get(); + $table = $this->schema->getConnection() + ->table('users_licenses'); + + foreach ($licenses as $license) { + $table->insert([ + 'user_id' => $license->user_id, + 'has_car' => $license->has_car, + 'drive_forklift' => $license->has_license_forklift, + 'drive_car' => $license->has_license_car, + 'drive_3_5t' => $license->has_license_3_5t_transporter, + 'drive_7_5t' => $license->has_license_7_5t_truck, + 'drive_12t' => $license->has_license_12_5t_truck, + ]); + } + + $this->schema->drop('UserDriverLicenses'); + } + } + + /** + * Reverse the migration + */ + public function down(): void + { + $this->schema->create('UserDriverLicenses', function (Blueprint $table) { + $this->referencesUser($table, true); + $table->boolean('has_car'); + $table->boolean('has_license_car'); + $table->boolean('has_license_3_5t_transporter'); + $table->boolean('has_license_7_5t_truck'); + $table->boolean('has_license_12_5t_truck'); + $table->boolean('has_license_forklift'); + }); + + $licenses = $this->schema->getConnection() + ->table('users_licenses') + ->get(); + $table = $this->schema->getConnection() + ->table('UserDriverLicenses'); + + foreach ($licenses as $license) { + $table->insert([ + 'user_id' => $license->user_id, + 'has_car' => $license->has_car, + 'has_license_car' => $license->drive_car, + 'has_license_3_5t_transporter' => $license->drive_3_5t, + 'has_license_7_5t_truck' => $license->drive_7_5t, + 'has_license_12_5t_truck' => $license->drive_12t, + 'has_license_forklift' => $license->drive_forklift, + ]); + } + + $this->schema->drop('users_licenses'); + } +} diff --git a/includes/controller/angeltypes_controller.php b/includes/controller/angeltypes_controller.php index 0cfb48e2..ea083774 100644 --- a/includes/controller/angeltypes_controller.php +++ b/includes/controller/angeltypes_controller.php @@ -182,7 +182,6 @@ function angeltype_controller() $angeltype = load_angeltype(); $user_angeltype = UserAngelType_by_User_and_AngelType($user->id, $angeltype); - $user_driver_license = UserDriverLicense($user->id); $members = Users_by_angeltype($angeltype); $days = angeltype_controller_shiftsFilterDays($angeltype); @@ -209,7 +208,7 @@ function angeltype_controller() auth()->can('admin_user_angeltypes') || $isSupporter, auth()->can('admin_angel_types'), $isSupporter, - $user_driver_license, + $user->license, $user, $shiftsFilterRenderer, $shiftCalendarRenderer, diff --git a/includes/controller/user_driver_licenses_controller.php b/includes/controller/user_driver_licenses_controller.php index 55b7696a..8c83601f 100644 --- a/includes/controller/user_driver_licenses_controller.php +++ b/includes/controller/user_driver_licenses_controller.php @@ -12,14 +12,12 @@ function user_driver_license_required_hint() { $user = auth()->user(); - $angeltypes = User_angeltypes($user->id); - $user_driver_license = UserDriverLicense($user->id); - // User has already entered data, no hint needed. - if (!empty($user_driver_license)) { + if ($user->license->wantsToDrive()) { return null; } + $angeltypes = User_angeltypes($user->id); foreach ($angeltypes as $angeltype) { if ($angeltype['requires_driver_license']) { return sprintf( @@ -105,38 +103,34 @@ function user_driver_license_edit_controller() throw_redirect(user_driver_license_edit_link()); } - $user_driver_license = UserDriverLicense($user_source->id); - if (empty($user_driver_license)) { - $wants_to_drive = false; - $user_driver_license = UserDriverLicense_new(); - } else { - $wants_to_drive = true; - } - + $driverLicense = $user_source->license; if ($request->hasPostData('submit')) { - $wants_to_drive = $request->has('wants_to_drive'); - if ($wants_to_drive) { - $user_driver_license['has_car'] = $request->has('has_car'); - $user_driver_license['has_license_car'] = $request->has('has_license_car'); - $user_driver_license['has_license_3_5t_transporter'] = $request->has('has_license_3_5t_transporter'); - $user_driver_license['has_license_7_5t_truck'] = $request->has('has_license_7_5t_truck'); - $user_driver_license['has_license_12_5t_truck'] = $request->has('has_license_12_5t_truck'); - $user_driver_license['has_license_forklift'] = $request->has('has_license_forklift'); + 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(); - if (UserDriverLicense_valid($user_driver_license)) { - if (empty($user_driver_license['user_id'])) { - $user_driver_license = UserDriverLicenses_create($user_driver_license, $user_source->id); - } else { - UserDriverLicenses_update($user_driver_license); - } 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.')); } - } elseif (!empty($user_driver_license['user_id'])) { - UserDriverLicenses_delete($user_source->id); + } 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)); @@ -145,6 +139,6 @@ function user_driver_license_edit_controller() return [ sprintf(__('Edit %s driving license information'), $user_source->name), - UserDriverLicense_edit_view($user_source, $wants_to_drive, $user_driver_license) + UserDriverLicense_edit_view($user_source, $driverLicense) ]; } diff --git a/includes/includes.php b/includes/includes.php index 2756fd65..871ce3a6 100644 --- a/includes/includes.php +++ b/includes/includes.php @@ -21,7 +21,6 @@ $includeFiles = [ __DIR__ . '/../includes/model/ShiftTypes_model.php', __DIR__ . '/../includes/model/Stats.php', __DIR__ . '/../includes/model/UserAngelTypes_model.php', - __DIR__ . '/../includes/model/UserDriverLicenses_model.php', __DIR__ . '/../includes/model/UserGroups_model.php', __DIR__ . '/../includes/model/User_model.php', __DIR__ . '/../includes/model/UserWorkLog_model.php', diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 357de7c0..b51d1c59 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -91,11 +91,10 @@ function Users_by_angeltype($angeltype) 'UserAngelTypes.id AS user_angeltype_id', 'UserAngelTypes.confirm_user_id', 'UserAngelTypes.supporter', - 'UserDriverLicenses.user_id AS wants_to_drive', - 'UserDriverLicenses.*' + 'users_licenses.*' ) ->join('UserAngelTypes', 'users.id', '=', 'UserAngelTypes.user_id') - ->leftJoin('UserDriverLicenses', 'users.id', '=', 'UserDriverLicenses.user_id') + ->leftJoin('users_licenses', 'users.id', '=', 'users_licenses.user_id') ->where('UserAngelTypes.angeltype_id', '=', $angeltype['id']) ->orderBy('users.name') ->get(); diff --git a/includes/view/AngelTypes_view.php b/includes/view/AngelTypes_view.php index 692bb590..916f992e 100644 --- a/includes/view/AngelTypes_view.php +++ b/includes/view/AngelTypes_view.php @@ -1,5 +1,6 @@ wantsToDrive()) { error(__('This angeltype requires a driver license. Please enter your driver license information!')); } @@ -205,13 +206,13 @@ function AngelType_view_members($angeltype, $members, $admin_user_angeltypes, $a $member->name = User_Nick_render($member) . User_Pronoun_render($member); $member['dect'] = $member->contact->dect; if ($angeltype['requires_driver_license']) { - $member['wants_to_drive'] = icon_bool($member['wants_to_drive']); - $member['has_car'] = icon_bool($member['has_car']); - $member['has_license_car'] = icon_bool($member['has_license_car']); - $member['has_license_3_5t_transporter'] = icon_bool($member['has_license_3_5t_transporter']); - $member['has_license_7_5t_truck'] = icon_bool($member['has_license_7_5t_truck']); - $member['has_license_12_5t_truck'] = icon_bool($member['has_license_12_5t_truck']); - $member['has_license_forklift'] = icon_bool($member['has_license_forklift']); + $member['wants_to_drive'] = icon_bool($member->license->wantsToDrive()); + $member['has_car'] = icon_bool($member->license->has_car); + $member['has_license_car'] = icon_bool($member->license->drive_car); + $member['has_license_3_5t_transporter'] = icon_bool($member->license->drive_3_5t); + $member['has_license_7_5t_truck'] = icon_bool($member->license->drive_7_5t); + $member['has_license_12t_truck'] = icon_bool($member->license->drive_12t); + $member['has_license_forklift'] = icon_bool($member->license->drive_forklift); } if ($angeltype['restricted'] && empty($member['confirm_user_id'])) { @@ -302,7 +303,7 @@ function AngelType_view_table_headers($angeltype, $supporter, $admin_angeltypes) 'has_license_car' => __('Car'), 'has_license_3_5t_transporter' => __('3,5t Transporter'), 'has_license_7_5t_truck' => __('7,5t Truck'), - 'has_license_12_5t_truck' => __('12,5t Truck'), + 'has_license_12t_truck' => __('12t Truck'), 'has_license_forklift' => __('Forklift'), 'actions' => '' ]; @@ -323,7 +324,7 @@ function AngelType_view_table_headers($angeltype, $supporter, $admin_angeltypes) * @param bool $admin_user_angeltypes * @param bool $admin_angeltypes * @param bool $supporter - * @param array $user_driver_license + * @param License $user_driver_license * @param User $user * @param ShiftsFilterRenderer $shiftsFilterRenderer * @param ShiftCalendarRenderer $shiftCalendarRenderer diff --git a/includes/view/UserDriverLicenses_view.php b/includes/view/UserDriverLicenses_view.php index 46653d3a..e2e18d3c 100644 --- a/includes/view/UserDriverLicenses_view.php +++ b/includes/view/UserDriverLicenses_view.php @@ -1,16 +1,16 @@ 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'] + $user_driver_license->has_car ), heading(__('Driver license'), 3), - form_checkbox('has_license_car', __('Car'), $user_driver_license['has_license_car']), + form_checkbox('has_license_car', __('Car'), $user_driver_license->drive_car), form_checkbox( 'has_license_3_5t_transporter', __('Transporter 3,5t'), - $user_driver_license['has_license_3_5t_transporter'] + $user_driver_license->drive_3_5t ), form_checkbox( 'has_license_7_5t_truck', __('Truck 7,5t'), - $user_driver_license['has_license_7_5t_truck'] + $user_driver_license->drive_7_5t ), form_checkbox( - 'has_license_12_5t_truck', - __('Truck 12,5t'), - $user_driver_license['has_license_12_5t_truck'] + 'has_license_12t_truck', + __('Truck 12t'), + $user_driver_license->drive_12t ), form_checkbox( 'has_license_forklift', __('Forklift'), - $user_driver_license['has_license_forklift'] + $user_driver_license->drive_forklift ) ], 'driving_license'), form_submit('submit', __('Save')) diff --git a/resources/lang/de_DE/default.po b/resources/lang/de_DE/default.po index 0a7fa636..bc1e6f88 100644 --- a/resources/lang/de_DE/default.po +++ b/resources/lang/de_DE/default.po @@ -2059,8 +2059,8 @@ msgid "7,5t Truck" msgstr "7,5t LKW" #: includes/view/AngelTypes_view.php:305 -msgid "12,5t Truck" -msgstr "12,5t LKW" +msgid "12t Truck" +msgstr "12t LKW" #: includes/view/AngelTypes_view.php:306 #: includes/view/UserDriverLicenses_view.php:49 @@ -2462,8 +2462,8 @@ msgid "Truck 7,5t" msgstr "7,5t LKW" #: includes/view/UserDriverLicenses_view.php:44 -msgid "Truck 12,5t" -msgstr "12,5t LKW" +msgid "Truck 12t" +msgstr "12t LKW" #: includes/view/UserWorkLog_view.php:15 #, php-format diff --git a/resources/lang/pt_BR/default.po b/resources/lang/pt_BR/default.po index 55ea1126..852deb93 100644 --- a/resources/lang/pt_BR/default.po +++ b/resources/lang/pt_BR/default.po @@ -1821,8 +1821,8 @@ msgid "7,5t Truck" msgstr "Caminhão 7,5t" #: includes/view/AngelTypes_view.php:184 -msgid "12,5t Truck" -msgstr "Caminhão 12,5t" +msgid "12t Truck" +msgstr "Caminhão 12t" #: includes/view/AngelTypes_view.php:185 #: includes/view/UserDriverLicenses_view.php:31 @@ -2184,8 +2184,8 @@ msgid "Truck 7,5t" msgstr "Caminhão 7,5t" #: includes/view/UserDriverLicenses_view.php:30 -msgid "Truck 12,5t" -msgstr "Caminhão 12,5t" +msgid "Truck 12t" +msgstr "Caminhão 12t" #: includes/view/User_view.php:7 msgid "Please select..." diff --git a/src/Controllers/Metrics/Controller.php b/src/Controllers/Metrics/Controller.php index 73c2d242..c5d16cf8 100644 --- a/src/Controllers/Metrics/Controller.php +++ b/src/Controllers/Metrics/Controller.php @@ -110,11 +110,12 @@ class Controller extends BaseController 'licenses' => [ 'type' => 'gauge', 'help' => 'The total number of licenses', + ['labels' => ['type' => 'has_car'], 'value' => $this->stats->licenses('has_car')], ['labels' => ['type' => 'forklift'], 'value' => $this->stats->licenses('forklift')], ['labels' => ['type' => 'car'], 'value' => $this->stats->licenses('car')], ['labels' => ['type' => '3.5t'], 'value' => $this->stats->licenses('3.5t')], ['labels' => ['type' => '7.5t'], 'value' => $this->stats->licenses('7.5t')], - ['labels' => ['type' => '12.5t'], 'value' => $this->stats->licenses('12.5t')], + ['labels' => ['type' => '12t'], 'value' => $this->stats->licenses('12t')], ], 'users_email' => [ 'type' => 'gauge', diff --git a/src/Controllers/Metrics/Stats.php b/src/Controllers/Metrics/Stats.php index 620724c8..9aed2a3d 100644 --- a/src/Controllers/Metrics/Stats.php +++ b/src/Controllers/Metrics/Stats.php @@ -15,6 +15,7 @@ use Engelsystem\Models\NewsComment; use Engelsystem\Models\OAuth; use Engelsystem\Models\Question; use Engelsystem\Models\Room; +use Engelsystem\Models\User\License; use Engelsystem\Models\User\PasswordReset; use Engelsystem\Models\User\PersonalData; use Engelsystem\Models\User\Settings; @@ -232,24 +233,21 @@ class Stats /** * @param string|null $vehicle * @return int - * @codeCoverageIgnore */ - public function licenses(string $vehicle = null): int + public function licenses(string $vehicle): int { $mapping = [ - 'forklift' => 'has_license_forklift', - 'car' => 'has_license_car', - '3.5t' => 'has_license_3_5t_transporter', - '7.5t' => 'has_license_7_5t_truck', - '12.5t' => 'has_license_12_5t_truck', + 'has_car' => 'has_car', + 'forklift' => 'drive_forklift', + 'car' => 'drive_car', + '3.5t' => 'drive_3_5t', + '7.5t' => 'drive_7_5t', + '12t' => 'drive_12t', ]; - $query = $this - ->getQuery('UserDriverLicenses'); - - if (!is_null($vehicle)) { - $query->where($mapping[$vehicle], '=', true); - } + $query = (new License()) + ->getQuery() + ->where($mapping[$vehicle], true); return $query->count(); } diff --git a/src/Models/User/License.php b/src/Models/User/License.php new file mode 100644 index 00000000..bb8166c2 --- /dev/null +++ b/src/Models/User/License.php @@ -0,0 +1,74 @@ + false, + 'drive_forklift' => false, + 'drive_car' => false, + 'drive_3_5t' => false, + 'drive_7_5t' => false, + 'drive_12t' => false, + ]; + + /** The attributes that are mass assignable */ + protected $fillable = [ + 'user_id', + 'has_car', + 'drive_forklift', + 'drive_car', + 'drive_3_5t', + 'drive_7_5t', + 'drive_12t', + ]; + + /** @var string[] */ + protected $casts = [ + 'has_car' => 'boolean', + 'drive_forklift' => 'boolean', + 'drive_car' => 'boolean', + 'drive_3_5t' => 'boolean', + 'drive_7_5t' => 'boolean', + 'drive_12t' => 'boolean', + ]; + + /** + * If the user wants to drive at the event + * + * @return bool + */ + public function wantsToDrive(): bool + { + return $this->drive_forklift + || $this->drive_car + || $this->drive_3_5t + || $this->drive_7_5t + || $this->drive_12t; + } +} diff --git a/src/Models/User/User.php b/src/Models/User/User.php index 4bec75d8..2d06468d 100644 --- a/src/Models/User/User.php +++ b/src/Models/User/User.php @@ -27,6 +27,7 @@ use Illuminate\Database\Query\Builder as QueryBuilder; * @property Carbon $updated_at * * @property-read QueryBuilder|Contact $contact + * @property-read QueryBuilder|License $license * @property-read QueryBuilder|PersonalData $personalData * @property-read QueryBuilder|Settings $settings * @property-read QueryBuilder|State $state @@ -93,6 +94,16 @@ class User extends BaseModel ->withDefault(); } + /** + * @return HasOne + */ + public function license() + { + return $this + ->hasOne(License::class) + ->withDefault(); + } + /** * @return HasOne */ diff --git a/tests/Unit/Controllers/Metrics/ControllerTest.php b/tests/Unit/Controllers/Metrics/ControllerTest.php index a24ea3fa..b801f7a2 100644 --- a/tests/Unit/Controllers/Metrics/ControllerTest.php +++ b/tests/Unit/Controllers/Metrics/ControllerTest.php @@ -84,10 +84,10 @@ class ControllerTest extends TestCase ->with('metrics return') ->willReturn($response); - $stats->expects($this->exactly(5)) + $stats->expects($this->exactly(6)) ->method('licenses') - ->withConsecutive(['forklift'], ['car'], ['3.5t'], ['7.5t'], ['12.5t']) - ->willReturnOnConsecutiveCalls(3, 15, 9, 7, 1); + ->withConsecutive(['has_car'], ['forklift'], ['car'], ['3.5t'], ['7.5t'], ['12t']) + ->willReturnOnConsecutiveCalls(6, 3, 15, 9, 7, 1); $stats->expects($this->exactly(2)) ->method('arrivedUsers') ->withConsecutive([false], [true]) diff --git a/tests/Unit/Controllers/Metrics/StatsTest.php b/tests/Unit/Controllers/Metrics/StatsTest.php index e20d8286..4be3f734 100644 --- a/tests/Unit/Controllers/Metrics/StatsTest.php +++ b/tests/Unit/Controllers/Metrics/StatsTest.php @@ -12,6 +12,7 @@ use Engelsystem\Models\NewsComment; use Engelsystem\Models\OAuth; use Engelsystem\Models\Question; use Engelsystem\Models\Room; +use Engelsystem\Models\User\License; use Engelsystem\Models\User\PasswordReset; use Engelsystem\Models\User\PersonalData; use Engelsystem\Models\User\Settings; @@ -123,6 +124,22 @@ class StatsTest extends TestCase ], $themes->toArray()); } + /** + * @covers \Engelsystem\Controllers\Metrics\Stats::licenses + */ + public function testLicenses() + { + $this->addUsers(); + + $stats = new Stats($this->database); + $this->assertEquals(1, $stats->licenses('has_car')); + $this->assertEquals(1, $stats->licenses('forklift')); + $this->assertEquals(2, $stats->licenses('car')); + $this->assertEquals(0, $stats->licenses('3.5t')); + $this->assertEquals(0, $stats->licenses('7.5t')); + $this->assertEquals(1, $stats->licenses('12t')); + } + /** * @covers \Engelsystem\Controllers\Metrics\Stats::worklogSeconds */ @@ -394,11 +411,17 @@ class StatsTest extends TestCase $this->addUser(['arrived' => 1], [], ['email_human' => true, 'email_goody' => true, 'email_news' => true]); $this->addUser(['arrived' => 1], ['pronoun' => 'unicorn'], ['language' => 'lo_RM', 'email_shiftinfo' => true]); $this->addUser(['arrived' => 1, 'got_voucher' => 2], ['shirt_size' => 'XXL'], ['language' => 'lo_RM']); - $this->addUser(['arrived' => 1, 'got_voucher' => 9, 'force_active' => true], [], ['theme' => 1]); + $this->addUser( + ['arrived' => 1, 'got_voucher' => 9, 'force_active' => true], + [], + ['theme' => 1], + ['drive_car' => true, 'drive_12t' => true] + ); $this->addUser( ['arrived' => 1, 'got_voucher' => 3], ['pronoun' => 'per'], - ['theme' => 1, 'email_human' => true] + ['theme' => 1, 'email_human' => true], + ['has_car' => true, 'drive_forklift' => true, 'drive_car' => true] ); $this->addUser(['arrived' => 1, 'active' => 1, 'got_shirt' => true, 'force_active' => true]); $this->addUser(['arrived' => 1, 'active' => 1, 'got_shirt' => true], ['shirt_size' => 'L'], ['theme' => 4]); @@ -411,7 +434,7 @@ class StatsTest extends TestCase * * @return User */ - protected function addUser(array $state = [], $personalData = [], $settings = []): User + protected function addUser(array $state = [], $personalData = [], $settings = [], $license = []): User { $name = 'user_' . Str::random(5); @@ -443,6 +466,11 @@ class StatsTest extends TestCase ->associate($user) ->save(); + $license = new License($license); + $license->user() + ->associate($user) + ->save(); + return $user; } diff --git a/tests/Unit/Models/User/LicenseTest.php b/tests/Unit/Models/User/LicenseTest.php new file mode 100644 index 00000000..0215f001 --- /dev/null +++ b/tests/Unit/Models/User/LicenseTest.php @@ -0,0 +1,40 @@ +assertFalse($license->wantsToDrive()); + + $license->has_car = true; + $this->assertFalse($license->wantsToDrive()); + + $license->drive_car = true; + $this->assertTrue($license->wantsToDrive()); + + // True if a user wants to drive anything + $license = new License(['drive_forklift' => true]); + $this->assertTrue($license->wantsToDrive()); + + $license = new License(['drive_car' => true]); + $this->assertTrue($license->wantsToDrive()); + + $license = new License(['drive_3_5t' => true]); + $this->assertTrue($license->wantsToDrive()); + + $license = new License(['drive_7_5t' => true]); + $this->assertTrue($license->wantsToDrive()); + + $license = new License(['drive_12t' => true]); + $this->assertTrue($license->wantsToDrive()); + } +} diff --git a/tests/Unit/Models/User/UserTest.php b/tests/Unit/Models/User/UserTest.php index 22293fe2..664b1332 100644 --- a/tests/Unit/Models/User/UserTest.php +++ b/tests/Unit/Models/User/UserTest.php @@ -11,6 +11,7 @@ use Engelsystem\Models\OAuth; use Engelsystem\Models\Question; use Engelsystem\Models\User\Contact; use Engelsystem\Models\User\HasUserModel; +use Engelsystem\Models\User\License; use Engelsystem\Models\User\PersonalData; use Engelsystem\Models\User\Settings; use Engelsystem\Models\User\State; @@ -69,11 +70,20 @@ class UserTest extends ModelTest 'force_active' => true, ], ], + [ + License::class, + 'license', + [ + 'has_car' => true, + 'drive_car' => true, + ], + ], ]; } /** * @covers \Engelsystem\Models\User\User::contact + * @covers \Engelsystem\Models\User\User::license * @covers \Engelsystem\Models\User\User::personalData * @covers \Engelsystem\Models\User\User::settings * @covers \Engelsystem\Models\User\User::state