A free open source IT asset/license management system
Ensure accessory check in emails are not sent when the setting is disabled
Marcus Moore728aaaab20a8
diff --git a/app/Notifications/CheckinAccessoryNotification.php b/app/Notifications/CheckinAccessoryNotification.phpindex 04a1d07168..e368f161e1 100644--- a/app/Notifications/CheckinAccessoryNotification.php+++ b/app/Notifications/CheckinAccessoryNotification.php@@ -67,30 +67,8 @@ class CheckinAccessoryNotification extends Notification* 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.phpindex acef32c2bd..c30be883e7 100644--- a/database/factories/AccessoryFactory.php+++ b/database/factories/AccessoryFactory.php@@ -142,14 +142,16 @@ class AccessoryFactory extends Factory});}- public function checkedOut()+ public function checkedOut(User $user = null){- return $this->afterCreating(function (Accessory $accessory) {+ return $this->afterCreating(function (Accessory $accessory) use ($user) {+ $accessory->decrement('qty');+$accessory->users()->attach($accessory->id, ['accessory_id' => $accessory->id,'created_at' => Carbon::now(),'user_id' => 1,- 'assigned_to' => User::factory()->create()->id,+ 'assigned_to' => $user->id ?? User::factory()->create()->id,]);});}diff --git a/tests/Feature/Checkins/AccessoryCheckinTest.php b/tests/Feature/Checkins/AccessoryCheckinTest.phpindex 6d9d1ecf2e..c312c0da83 100644--- a/tests/Feature/Checkins/AccessoryCheckinTest.php+++ b/tests/Feature/Checkins/AccessoryCheckinTest.php@@ -2,8 +2,13 @@namespace Tests\Feature\Checkins;+use App\Events\CheckoutableCheckedIn;use App\Models\Accessory;use App\Models\User;+use App\Notifications\CheckinAccessoryNotification;+use App\Notifications\CheckinAssetNotification;+use Illuminate\Support\Facades\Event;+use Illuminate\Support\Facades\Notification;use Tests\Support\InteractsWithSettings;use Tests\TestCase;@@ -20,16 +25,66 @@ class AccessoryCheckinTest extends TestCasepublic function testAccessoryCanBeCheckedIn(){- $this->markTestIncomplete();+ Event::fake([CheckoutableCheckedIn::class]);++ $user = User::factory()->create();+ $accessory = Accessory::factory()->checkedOut($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(){- $this->markTestIncomplete();+ Notification::fake();++ $user = User::factory()->create();+ $accessory = Accessory::factory()->checkedOut($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(){- $this->markTestIncomplete();+ Notification::fake();++ $user = User::factory()->create();+ $accessory = Accessory::factory()->checkedOut($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);+ },+ );}}