diff --git a/app/Notifications/CheckinAccessoryNotification.php b/app/Notifications/CheckinAccessoryNotification.php index 04a1d07168..8cfca23e59 100644 --- a/app/Notifications/CheckinAccessoryNotification.php +++ b/app/Notifications/CheckinAccessoryNotification.php @@ -63,34 +63,8 @@ class CheckinAccessoryNotification extends Notification if ($this->target instanceof User && $this->target->email != '') { \Log::debug('The target is a user'); - /** - * Send an email if the asset requires acceptance, - * so the user can accept or decline the asset - */ - if (($this->item->requireAcceptance()) || ($this->item->getEula()) || ($this->item->checkin_email())) { - $notifyBy[] = 'mail'; - } - - /** - * Send an email if the asset requires acceptance, - * so the user can accept or decline the asset - */ - if ($this->item->requireAcceptance()) { - \Log::debug('This accessory requires acceptance'); - } - - /** - * Send an email if the item has a EULA, since the user should always receive it - */ - if ($this->item->getEula()) { - \Log::debug('This accessory has a EULA'); - } - - /** - * Send an email if an email should be sent at checkin/checkout - */ if ($this->item->checkin_email()) { - \Log::debug('This accessory has a checkin_email()'); + $notifyBy[] = 'mail'; } } diff --git a/database/factories/AccessoryFactory.php b/database/factories/AccessoryFactory.php index 4066526770..ce0d60cc18 100644 --- a/database/factories/AccessoryFactory.php +++ b/database/factories/AccessoryFactory.php @@ -8,6 +8,7 @@ use App\Models\Location; use App\Models\Manufacturer; use App\Models\Supplier; use App\Models\User; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\Factory; class AccessoryFactory extends Factory @@ -140,4 +141,16 @@ class AccessoryFactory extends Factory $accessory->category->update(['require_acceptance' => 1]); }); } + + public function checkedOutToUser(User $user = null) + { + return $this->afterCreating(function (Accessory $accessory) use ($user) { + $accessory->users()->attach($accessory->id, [ + 'accessory_id' => $accessory->id, + 'created_at' => Carbon::now(), + 'user_id' => 1, + 'assigned_to' => $user->id ?? User::factory()->create()->id, + ]); + }); + } } diff --git a/tests/Feature/Checkins/AccessoryCheckinTest.php b/tests/Feature/Checkins/AccessoryCheckinTest.php new file mode 100644 index 0000000000..25cd5d0d87 --- /dev/null +++ b/tests/Feature/Checkins/AccessoryCheckinTest.php @@ -0,0 +1,91 @@ +checkedOutToUser()->create(); + + $this->actingAs(User::factory()->create()) + ->post(route('accessories.checkin.store', $accessory->users->first()->pivot->id)) + ->assertForbidden(); + } + + public function testAccessoryCanBeCheckedIn() + { + Event::fake([CheckoutableCheckedIn::class]); + + $user = User::factory()->create(); + $accessory = Accessory::factory()->checkedOutToUser($user)->create(); + + $this->assertTrue($accessory->users->contains($user)); + + $this->actingAs(User::factory()->checkinAccessories()->create()) + ->post(route('accessories.checkin.store', $accessory->users->first()->pivot->id)); + + $this->assertFalse($accessory->fresh()->users->contains($user)); + + Event::assertDispatched(CheckoutableCheckedIn::class, 1); + } + + public function testEmailSentToUserIfSettingEnabled() + { + Notification::fake(); + + $user = User::factory()->create(); + $accessory = Accessory::factory()->checkedOutToUser($user)->create(); + + $accessory->category->update(['checkin_email' => true]); + + event(new CheckoutableCheckedIn( + $accessory, + $user, + User::factory()->checkinAccessories()->create(), + '', + )); + + Notification::assertSentTo( + [$user], + function (CheckinAccessoryNotification $notification, $channels) { + return in_array('mail', $channels); + }, + ); + } + + public function testEmailNotSentToUserIfSettingDisabled() + { + Notification::fake(); + + $user = User::factory()->create(); + $accessory = Accessory::factory()->checkedOutToUser($user)->create(); + + $accessory->category->update(['checkin_email' => false]); + + event(new CheckoutableCheckedIn( + $accessory, + $user, + User::factory()->checkinAccessories()->create(), + '', + )); + + Notification::assertNotSentTo( + [$user], + function (CheckinAccessoryNotification $notification, $channels) { + return in_array('mail', $channels); + }, + ); + } +}