diff --git a/database/factories/CheckoutAcceptanceFactory.php b/database/factories/CheckoutAcceptanceFactory.php index 6bb87d18e1..8dbb6712c6 100644 --- a/database/factories/CheckoutAcceptanceFactory.php +++ b/database/factories/CheckoutAcceptanceFactory.php @@ -27,6 +27,17 @@ class CheckoutAcceptanceFactory extends Factory public function configure(): static { return $this->afterCreating(function (CheckoutAcceptance $acceptance) { + // @todo: add comment + if ($acceptance->checkoutable instanceof Asset) { + $acceptance->checkoutable->assetlog()->create([ + 'action_type' => 'checkout', + 'target_id' => $acceptance->assigned_to_id, + 'target_type' => get_class($acceptance->assignedTo), + 'item_id' => $acceptance->checkoutable_id, + 'item_type' => $acceptance->checkoutable_type, + ]); + } + if ($acceptance->checkoutable instanceof Asset && $acceptance->assignedTo instanceof User) { $acceptance->checkoutable->update([ 'assigned_to' => $acceptance->assigned_to_id, @@ -51,4 +62,12 @@ class CheckoutAcceptanceFactory extends Factory 'declined_at' => null, ]); } + + public function accepted() + { + return $this->state([ + 'accepted_at' => now()->subDay(), + 'declined_at' => null, + ]); + } } diff --git a/tests/Feature/Notifications/Email/AssetAcceptanceReminderTest.php b/tests/Feature/Notifications/Email/AssetAcceptanceReminderTest.php new file mode 100644 index 0000000000..3809b826e6 --- /dev/null +++ b/tests/Feature/Notifications/Email/AssetAcceptanceReminderTest.php @@ -0,0 +1,90 @@ +actor = User::factory()->canViewReports()->create(); + $this->checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create(); + } + + public function testMustHavePermissionToSendReminder() + { + $userWithoutPermission = User::factory()->create(); + + $this->actingAs($userWithoutPermission) + ->post($this->routeFor($this->checkoutAcceptance)) + ->assertForbidden(); + } + + public function testReminderNotSentIfAcceptanceDoesNotExist() + { + $this->actingAs($this->actor) + ->post(route('reports/unaccepted_assets_sent_reminder', [ + 'acceptance_id' => 999999, + ])); + + Mail::assertNotSent(CheckoutAssetMail::class); + } + + public function testReminderNotSentIfAcceptanceAlreadyAccepted() + { + $checkoutAcceptanceAlreadyAccepted = CheckoutAcceptance::factory()->accepted()->create(); + + $this->actingAs($this->actor) + ->post($this->routeFor($checkoutAcceptanceAlreadyAccepted)); + + Mail::assertNotSent(CheckoutAssetMail::class); + } + + public function testUserWithoutEmailAddressHandledGracefully() + { + $userWithoutEmailAddress = User::factory()->create(['email' => null]); + + $this->checkoutAcceptance->assigned_to_id = $userWithoutEmailAddress->id; + $this->checkoutAcceptance->save(); + + $this->actingAs($this->actor) + ->post($this->routeFor($this->checkoutAcceptance)) + // check we didn't crash... + ->assertRedirect(); + + Mail::assertNotSent(CheckoutAssetMail::class); + } + + public function testReminderIsSentToUser() + { + $this->actingAs($this->actor) + ->post($this->routeFor($this->checkoutAcceptance)) + ->assertRedirect(route('reports/unaccepted_assets')); + + Mail::assertSent(CheckoutAssetMail::class, 1); + Mail::assertSent(CheckoutAssetMail::class, function (CheckoutAssetMail $mail) { + return $mail->hasTo($this->checkoutAcceptance->assignedTo->email) + // @todo: + && $mail->hasSubject('Reminder: ' . trans('mail.Asset_Checkout_Notification')); + }); + } + + private function routeFor(CheckoutAcceptance $checkoutAcceptance): string + { + return route('reports/unaccepted_assets_sent_reminder', [ + 'acceptance_id' => $checkoutAcceptance->id, + ]); + } +}