Merge pull request #16717 from Godmartinz/fix_checkin_mail_test

Adds a check for category email alert boolean, bolster Check in Test
This commit is contained in:
snipe 2025-04-16 18:10:29 +01:00 committed by GitHub
commit 1464f80425
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 60 additions and 12 deletions

View file

@ -385,9 +385,25 @@ class CheckoutableListener
private function shouldNotSendAnyNotifications($checkoutable): bool
{
return in_array(get_class($checkoutable), $this->skipNotificationsFor);
if(in_array(get_class($checkoutable), $this->skipNotificationsFor)) {
return true;
}
//runs a check if the category wants to send checkin/checkout emails to users
$category = match (true) {
$checkoutable instanceof Asset => $checkoutable->model->category,
$checkoutable instanceof Accessory,
$checkoutable instanceof Consumable => $checkoutable->category,
$checkoutable instanceof LicenseSeat => $checkoutable->license->category,
default => null,
};
if (!$category->checkin_email) {
return true;
}
return false;
}
private function shouldSendWebhookNotification(): bool
{
return Setting::getSettings() && Setting::getSettings()->webhook_endpoint;

View file

@ -25,7 +25,7 @@ class CategoryFactory extends Factory
return [
'name' => $this->faker->catchPhrase(),
'category_type' => 'asset',
'checkin_email' => $this->faker->boolean(),
'checkin_email' => true,
'eula_text' => $this->faker->paragraph(),
'require_acceptance' => false,
'use_default_eula' => $this->faker->boolean(),

View file

@ -3,6 +3,9 @@
namespace Tests\Feature\Notifications\Email;
use App\Mail\CheckinAssetMail;
use App\Models\Accessory;
use App\Models\Consumable;
use App\Models\LicenseSeat;
use Illuminate\Support\Facades\Mail;
use PHPUnit\Framework\Attributes\Group;
use App\Events\CheckoutableCheckedIn;
@ -45,20 +48,49 @@ class EmailNotificationsUponCheckinTest extends TestCase
Mail::fake();
$user = User::factory()->create();
$asset = Asset::factory()->assignedToUser($user)->create();
$asset->model->category->update([
'checkin_email' => false,
'eula_text' => null,
'require_acceptance' => false,
$checkoutables = collect([
Asset::factory()->assignedToUser($user)->create(),
LicenseSeat::factory()->assignedToUser($user)->create(),
Accessory::factory()->checkedOutToUser($user)->create(),
Consumable::factory()->checkedOutToUser($user)->create(),
]);
$this->fireCheckInEvent($asset, $user);
foreach ($checkoutables as $checkoutable) {
Mail::assertNotSent(CheckinAssetMail::class, function($mail) use ($user) {
return $mail->hasTo($user->email);
if ($checkoutable instanceof Asset) {
$checkoutable->model->category->update([
'checkin_email' => false,
'eula_text' => null,
'require_acceptance' => false,
]);
$checkoutable = $checkoutable->fresh(['model.category']);
}
);
if ($checkoutable instanceof Accessory || $checkoutable instanceof Consumable) {
$checkoutable->category->update([
'checkin_email' => false,
'eula_text' => null,
'require_acceptance' => false,
]);
$checkoutable = $checkoutable->fresh(['category']);
}
if ($checkoutable instanceof LicenseSeat) {
$checkoutable->license->category->update([
'checkin_email' => false,
'eula_text' => null,
'require_acceptance' => false,
]);
$checkoutable = $checkoutable->fresh(['license.category']);
}
// Fire event manually
$this->fireCheckInEvent($checkoutable, $user);
}
Mail::assertNotSent(CheckinAssetMail::class, function ($mail) use ($user) {
return $mail->hasTo($user->email);
});
}
private function fireCheckInEvent($asset, $user): void