From ed2fc06caa80de23223a079e6e288868f6f0320b Mon Sep 17 00:00:00 2001 From: Adrian Hopek Date: Mon, 24 Jan 2022 14:57:59 +0100 Subject: [PATCH] #28 - fix --- app/Http/Requests/HolidayRequest.php | 6 +- app/Http/Requests/UserRequest.php | 2 +- app/Rules/YearPeriodExists.php | 2 +- database/factories/HolidayFactory.php | 7 +- database/factories/YearPeriodFactory.php | 2 +- resources/js/Pages/Holidays/Create.vue | 2 +- resources/js/Pages/Holidays/Edit.vue | 7 +- resources/js/Pages/Users/Create.vue | 2 +- resources/js/Pages/Users/Edit.vue | 2 +- tests/Feature/HolidayTest.php | 127 ++++++++++++++++++++++ tests/Feature/UserTest.php | 8 +- tests/Traits/InteractsWithYearPeriods.php | 2 +- 12 files changed, 150 insertions(+), 19 deletions(-) create mode 100644 tests/Feature/HolidayTest.php diff --git a/app/Http/Requests/HolidayRequest.php b/app/Http/Requests/HolidayRequest.php index 4d04dfe..747e468 100644 --- a/app/Http/Requests/HolidayRequest.php +++ b/app/Http/Requests/HolidayRequest.php @@ -6,6 +6,7 @@ namespace Toby\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Carbon; +use Illuminate\Validation\Rule; use Toby\Models\YearPeriod; use Toby\Rules\YearPeriodExists; @@ -15,7 +16,10 @@ class HolidayRequest extends FormRequest { return [ "name" => ["required", "min:3", "max:150"], - "date" => ["required", "date", new YearPeriodExists()], + "date" => ["required", + "date_format:Y-m-d", + Rule::unique("holidays", "date")->ignore($this->holiday), + new YearPeriodExists(), ], ]; } diff --git a/app/Http/Requests/UserRequest.php b/app/Http/Requests/UserRequest.php index 0c1d59e..d48873a 100644 --- a/app/Http/Requests/UserRequest.php +++ b/app/Http/Requests/UserRequest.php @@ -18,7 +18,7 @@ class UserRequest extends FormRequest "lastName" => ["required", "min:3", "max:80"], "email" => ["required", "email", Rule::unique("users", "email")->ignore($this->user)], "employmentForm" => ["required", new Enum(EmploymentForm::class)], - "employmentDate" => ["required", "date"], + "employmentDate" => ["required", "date_format:Y-m-d"], ]; } diff --git a/app/Rules/YearPeriodExists.php b/app/Rules/YearPeriodExists.php index 29ee43c..ef9ccbc 100644 --- a/app/Rules/YearPeriodExists.php +++ b/app/Rules/YearPeriodExists.php @@ -19,6 +19,6 @@ class YearPeriodExists implements Rule public function message(): string { - return "The year period for given year doesn't exist"; + return "The year period for given year doesn't exist."; } } diff --git a/database/factories/HolidayFactory.php b/database/factories/HolidayFactory.php index 0538626..70da3e8 100644 --- a/database/factories/HolidayFactory.php +++ b/database/factories/HolidayFactory.php @@ -5,17 +5,16 @@ declare(strict_types=1); namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; -use Illuminate\Support\Carbon; +use Toby\Models\YearPeriod; class HolidayFactory extends Factory { public function definition(): array { - $now = Carbon::now(); - return [ "name" => $this->faker->word, - "date" => $this->faker->dateTimeBetween($now->startOfYear(), $now->endOfYear()), + "date" => $this->faker->unique->date, + "year_period_id" => YearPeriod::current()->id, ]; } } diff --git a/database/factories/YearPeriodFactory.php b/database/factories/YearPeriodFactory.php index 2437dd3..e0768c7 100644 --- a/database/factories/YearPeriodFactory.php +++ b/database/factories/YearPeriodFactory.php @@ -11,7 +11,7 @@ class YearPeriodFactory extends Factory public function definition(): array { return [ - "year" => $this->faker->unique()->year, + "year" => (int)$this->faker->unique()->year, ]; } } diff --git a/resources/js/Pages/Holidays/Create.vue b/resources/js/Pages/Holidays/Create.vue index 322eae6..70b8bea 100644 --- a/resources/js/Pages/Holidays/Create.vue +++ b/resources/js/Pages/Holidays/Create.vue @@ -92,7 +92,7 @@ export default { setup() { const form = useForm({ name: null, - date: new Date(), + date: null, }); return { form }; diff --git a/resources/js/Pages/Holidays/Edit.vue b/resources/js/Pages/Holidays/Edit.vue index be4b095..5dbd100 100644 --- a/resources/js/Pages/Holidays/Edit.vue +++ b/resources/js/Pages/Holidays/Edit.vue @@ -85,7 +85,7 @@ import { useForm } from '@inertiajs/inertia-vue3'; import FlatPickr from 'vue-flatpickr-component'; export default { - name: 'HolidayCreate', + name: 'HolidayEdit', components: { FlatPickr, }, @@ -98,14 +98,15 @@ export default { setup(props) { const form = useForm({ name: props.holiday.name, - date: new Date(props.holiday.date), + date: props.holiday.date, }); return { form }; }, methods: { editHoliday() { - this.form.put(`/holidays/${this.holiday.id}`); + this.form + .put(`/holidays/${this.holiday.id}`); }, }, }; diff --git a/resources/js/Pages/Users/Create.vue b/resources/js/Pages/Users/Create.vue index 8f9bacd..1478b4e 100644 --- a/resources/js/Pages/Users/Create.vue +++ b/resources/js/Pages/Users/Create.vue @@ -211,7 +211,7 @@ export default { lastName: null, email: null, employmentForm: props.employmentForms[0], - employmentDate: new Date(), + employmentDate: null, }); return { form }; diff --git a/resources/js/Pages/Users/Edit.vue b/resources/js/Pages/Users/Edit.vue index 771c001..a58e49a 100644 --- a/resources/js/Pages/Users/Edit.vue +++ b/resources/js/Pages/Users/Edit.vue @@ -215,7 +215,7 @@ export default { lastName: props.user.lastName, email: props.user.email, employmentForm: props.employmentForms.find(form => form.value === props.user.employmentForm), - employmentDate: new Date(props.user.employmentDate), + employmentDate: props.user.employmentDate, }); return { form }; diff --git a/tests/Feature/HolidayTest.php b/tests/Feature/HolidayTest.php new file mode 100644 index 0000000..4cd2d92 --- /dev/null +++ b/tests/Feature/HolidayTest.php @@ -0,0 +1,127 @@ +count(10)->create(); + $user = User::factory()->create(); + + $this->assertDatabaseCount("holidays", 10); + + $this->actingAs($user) + ->get("/holidays") + ->assertInertia( + fn(Assert $page) => $page + ->component("Holidays/Index") + ->has("holidays.data", 10), + ); + } + + public function testAdminCanCreateHoliday(): void + { + $admin = User::factory()->create(); + $currentYearPeriod = YearPeriod::current(); + + $this->actingAs($admin) + ->post("/holidays", [ + "name" => "Holiday 1", + "date" => Carbon::create($currentYearPeriod->year, 5, 20)->toDateString(), + ]) + ->assertSessionHasNoErrors(); + + $this->assertDatabaseHas("holidays", [ + "name" => "Holiday 1", + "date" => Carbon::create($currentYearPeriod->year, 5, 20), + "year_period_id" => YearPeriod::current()->id, + ]); + } + + public function testAdminCannotCreateHolidayForYearPeriodThatDoesntExist(): void + { + $admin = User::factory()->create(); + $year = YearPeriod::query()->max("year") + 1; + + $this->actingAs($admin) + ->post("/holidays", [ + "name" => "Holiday 1", + "date" => Carbon::create($year, 5, 20)->toDateString(), + ]) + ->assertSessionHasErrors(["date"]); + } + + public function testAdminCannotCreateHolidayIfGivenDataIsUsed(): void + { + $admin = User::factory()->create(); + $currentYearPeriod = YearPeriod::current(); + $sameDate = Carbon::create($currentYearPeriod->year, 5, 20)->toDateString(); + + Holiday::factory()->create([ + "name" => "Holiday", + "date" => $sameDate, + ]); + + $this->actingAs($admin) + ->post("/holidays", [ + "name" => "Holiday 1", + "date" => $sameDate, + ]) + ->assertSessionHasErrors(["date"]); + } + + public function testAdminCanEditHoliday(): void + { + $admin = User::factory()->create(); + $currentYearPeriod = YearPeriod::current(); + + $holiday = Holiday::factory()->create([ + "name" => "Name to change", + "date" => Carbon::create($currentYearPeriod->year, 5, 20), + ]); + + $this->assertDatabaseHas("holidays", [ + "name" => $holiday->name, + "date" => $holiday->date->toDateString(), + "year_period_id" => $currentYearPeriod->id, + ]); + + $this->actingAs($admin) + ->put("/holidays/{$holiday->id}", [ + "name" => "Holiday 1", + "date" => Carbon::create($currentYearPeriod->year, 10, 25)->toDateString(), + ]) + ->assertSessionHasNoErrors(); + + $this->assertDatabaseHas("holidays", [ + "name" => "Holiday 1", + "date" => Carbon::create($currentYearPeriod->year, 10, 25)->toDateString(), + "year_period_id" => $currentYearPeriod->id, + ]); + } + + public function testAdminCanDeleteUser(): void + { + $admin = User::factory()->create(); + $holiday = Holiday::factory()->create(); + + $this->actingAs($admin) + ->delete("/holidays/{$holiday->id}") + ->assertSessionHasNoErrors(); + + $this->assertDeleted($holiday); + } +} diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index cbf1277..63aedc6 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -89,7 +89,7 @@ class UserTest extends FeatureTestCase "lastName" => "Doe", "email" => "john.doe@example.com", "employmentForm" => EmploymentForm::B2B_CONTRACT->value, - "employmentDate" => Carbon::now()->toDateTimeString(), + "employmentDate" => Carbon::now()->toDateString(), ]) ->assertSessionHasNoErrors(); @@ -98,7 +98,7 @@ class UserTest extends FeatureTestCase "last_name" => "Doe", "email" => "john.doe@example.com", "employment_form" => EmploymentForm::B2B_CONTRACT->value, - "employment_date" => Carbon::now()->toDateTimeString(), + "employment_date" => Carbon::now()->toDateString(), ]); } @@ -123,7 +123,7 @@ class UserTest extends FeatureTestCase "lastName" => "Doe", "email" => "john.doe@example.com", "employmentForm" => EmploymentForm::B2B_CONTRACT->value, - "employmentDate" => Carbon::now()->toDateTimeString(), + "employmentDate" => Carbon::now()->toDateString(), ]) ->assertSessionHasNoErrors(); @@ -132,7 +132,7 @@ class UserTest extends FeatureTestCase "last_name" => "Doe", "email" => "john.doe@example.com", "employment_form" => EmploymentForm::B2B_CONTRACT->value, - "employment_date" => Carbon::now()->toDateTimeString(), + "employment_date" => Carbon::now()->toDateString(), ]); } diff --git a/tests/Traits/InteractsWithYearPeriods.php b/tests/Traits/InteractsWithYearPeriods.php index fb74ab3..6ab5073 100644 --- a/tests/Traits/InteractsWithYearPeriods.php +++ b/tests/Traits/InteractsWithYearPeriods.php @@ -16,7 +16,7 @@ trait InteractsWithYearPeriods public function createYearPeriod(int $year): YearPeriod { /** @var YearPeriod $yearPeriod */ - $yearPeriod = YearPeriod::factory()->create([ + $yearPeriod = YearPeriod::factory()->createQuietly([ "year" => $year, ]);