Ensure accessory check in emails are not sent when the setting is disabled

This commit is contained in:
Marcus Moore 2024-02-12 16:22:59 -08:00
parent 095a7d9b34
commit 728aaaab20
No known key found for this signature in database
3 changed files with 64 additions and 29 deletions

View file

@ -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';
}
}

View file

@ -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,
]);
});
}

View file

@ -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 TestCase
public 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);
},
);
}
}