A free open source IT asset/license management system
added a coulple test adjustments and moved mail send logic to listener
Godfrey M285d6897cf83
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.phpindex 44987e20d1..3e23259fb6 100644--- a/app/Listeners/CheckoutableListener.php+++ b/app/Listeners/CheckoutableListener.php@@ -57,8 +57,21 @@ class CheckoutableListenerif (!$event->checkedOutTo->locale){$mailable->locale($event->checkedOutTo->locale);}++ /**+ * Send an email if any of the following conditions are met:+ * 1. The asset requires acceptance+ * 2. The item has a EULA+ * 3. The item should send an email at check-in/check-out+ */++ if ($event->checkoutable->requireAcceptance() || $event->checkoutable->getEula() ||+ (method_exists($event->checkoutable, 'checkin_email') && $event->checkoutable->checkin_email())) {+Mail::to($notifiable)->send($mailable);Log::info('Sending email, Locale: ' .($event->checkedOutTo->locale ?? 'default'));+ }+// Send Webhook notificationif ($this->shouldSendWebhookNotification()) {Notification::route(Setting::getSettings()->webhook_selected, Setting::getSettings()->webhook_endpoint)diff --git a/app/Notifications/CheckoutAssetNotification.php b/app/Notifications/CheckoutAssetNotification.phpindex 1ca329ed80..97706cec94 100644--- a/app/Notifications/CheckoutAssetNotification.php+++ b/app/Notifications/CheckoutAssetNotification.php@@ -9,7 +9,6 @@ use App\Models\User;use Exception;use Illuminate\Bus\Queueable;use Illuminate\Notifications\Channels\SlackWebhookChannel;-use Illuminate\Notifications\Messages\MailMessage;use Illuminate\Notifications\Messages\SlackMessage;use Illuminate\Notifications\Notification;use NotificationChannels\GoogleChat\Card;@@ -154,12 +153,4 @@ public function toGoogleChat());}-- /**- * Get the mail representation of the notification.- *- * @param mixed $notifiable- * @return \Illuminate\Notifications\Messages\MailMessage- */-}diff --git a/app/Notifications/CheckoutConsumableNotification.php b/app/Notifications/CheckoutConsumableNotification.phpindex 0a2733689b..3f2adc3331 100644--- a/app/Notifications/CheckoutConsumableNotification.php+++ b/app/Notifications/CheckoutConsumableNotification.php@@ -65,33 +65,33 @@ class CheckoutConsumableNotification extends Notification$notifyBy[] = SlackWebhookChannel::class;}-// /**-// * Only send notifications to users that have email addresses-// */-// if ($this->target instanceof User && $this->target->email != '') {-//-// /**-// * Send an email if the asset requires acceptance,-// * so the user can accept or decline the asset-// */-// if ($this->item->requireAcceptance()) {-// $notifyBy[1] = 'mail';-// }-//-// /**-// * Send an email if the item has a EULA, since the user should always receive it-// */-// if ($this->item->getEula()) {-// $notifyBy[1] = 'mail';-// }-//-// /**-// * Send an email if an email should be sent at checkin/checkout-// */-// if ((method_exists($this->item, 'checkin_email')) && ($this->item->checkin_email())) {-// $notifyBy[1] = 'mail';-// }-// }+ /**+ * Only send notifications to users that have email addresses+ */+ if ($this->target instanceof User && $this->target->email != '') {++ /**+ * Send an email if the asset requires acceptance,+ * so the user can accept or decline the asset+ */+ if ($this->item->requireAcceptance()) {+ $notifyBy[1] = 'mail';+ }++ /**+ * Send an email if the item has a EULA, since the user should always receive it+ */+ if ($this->item->getEula()) {+ $notifyBy[1] = 'mail';+ }++ /**+ * Send an email if an email should be sent at checkin/checkout+ */+ if ((method_exists($this->item, 'checkin_email')) && ($this->item->checkin_email())) {+ $notifyBy[1] = 'mail';+ }+ }return $notifyBy;}diff --git a/resources/views/notifications/markdown/checkout-asset.blade.php b/resources/views/notifications/markdown/checkout-asset.blade.phpdeleted file mode 100644index 5b4c811dd7..0000000000--- a/resources/views/notifications/markdown/checkout-asset.blade.php+++ /dev/null@@ -1,76 +0,0 @@-@component('mail::message')- # {{ trans('mail.hello') }} {{ $target->present()->fullName() }},-- {{ trans('mail.new_item_checked') }}-- @if (($snipeSettings->show_images_in_email =='1') && $item->getImageUrl())- <center><img src="{{ $item->getImageUrl() }}" alt="Asset" style="max-width: 570px;"></center>- @endif-- @component('mail::table')- | | |- | ------------- | ------------- |- @if ((isset($item->name)) && ($item->name!=''))- | **{{ trans('mail.asset_name') }}** | {{ $item->name }} |- @endif- @if (($item->name!=$item->asset_tag))- | **{{ trans('mail.asset_tag') }}** | {{ $item->asset_tag }} |- @endif- @if (isset($item->manufacturer))- | **{{ trans('general.manufacturer') }}** | {{ $item->manufacturer->name }} |- @endif- @if (isset($item->model))- | **{{ trans('general.asset_model') }}** | {{ $item->model->name }} |- @endif- @if ((isset($item->model->model_number)) && ($item->model->name!=$item->model->model_number))- | **{{ trans('general.model_no') }}** | {{ $item->model->model_number }} |- @endif- @if (isset($item->serial))- | **{{ trans('mail.serial') }}** | {{ $item->serial }} |- @endif- @if (isset($last_checkout))- | **{{ trans('mail.checkout_date') }}** | {{ $last_checkout }} |- @endif- @if (isset($status))- | **{{ trans('general.status') }}** | {{ $status }} |- @endif- @if ((isset($expected_checkin)) && ($expected_checkin!=''))- | **{{ trans('mail.expecting_checkin_date') }}** | {{ $expected_checkin }} |- @endif- @foreach($fields as $field)- @if (($item->{ $field->db_column_name() }!='') && ($field->show_in_email) && ($field->field_encrypted=='0'))- | **{{ $field->name }}** | {{ $item->{ $field->db_column_name() } }} |- @endif- @endforeach- @if ($admin)- | **{{ trans('general.administrator') }}** | {{ $admin->present()->fullName() }} |- @endif- @if ($note)- | **{{ trans('mail.additional_notes') }}** | {{ $note }} |- @endif- @endcomponent-- @if (($req_accept == 1) && ($eula!=''))- {{ trans('mail.read_the_terms_and_click') }}- @elseif (($req_accept == 1) && ($eula==''))- {{ trans('mail.click_on_the_link_asset') }}- @elseif (($req_accept == 0) && ($eula!=''))- {{ trans('mail.read_the_terms') }}- @endif-- @if ($eula)- @component('mail::panel')- {!! $eula !!}- @endcomponent- @endif-- @if ($req_accept == 1)- **[✔ {{ trans('mail.i_have_read') }}]({{ $accept_url }})**- @endif--- {{ trans('mail.best_regards') }}-- {{ $snipeSettings->site_name }}--@endcomponent\ No newline at end of filediff --git a/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php b/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.phpindex 484b7e72cb..46c34aedd7 100644--- a/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php@@ -8,6 +8,7 @@ use App\Models\Component;use App\Models\Consumable;use App\Models\User;use App\Notifications\CheckoutConsumableNotification;+use Illuminate\Support\Facades\Mail;use Illuminate\Support\Facades\Notification;use Tests\TestCase;@@ -53,7 +54,7 @@ class ConsumableCheckoutTest extends TestCasepublic function testUserSentNotificationUponCheckout(){- Notification::fake();+ Mail::fake();$consumable = Consumable::factory()->create();$user = User::factory()->create();@@ -63,7 +64,9 @@ class ConsumableCheckoutTest extends TestCase'assigned_to' => $user->id,]);- Notification::assertSentTo($user, CheckoutConsumableNotification::class);+ Mail::assertSent(CheckoutConsumableNotification::class, function ($mail) use ($consumable, $user) {+ return $mail->hasTo($user) && $mail->consumables->contains($consumable);+ });}public function testActionLogCreatedUponCheckout()diff --git a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.phpindex 449f65c7ab..c7c239b7ab 100644--- a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php+++ b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php@@ -2,6 +2,8 @@namespace Tests\Feature\Notifications\Email;+use App\Mail\CheckinAssetMail;+use Illuminate\Support\Facades\Mail;use PHPUnit\Framework\Attributes\Group;use App\Events\CheckoutableCheckedIn;use App\Models\Asset;@@ -22,6 +24,8 @@ class EmailNotificationsUponCheckinTest extends TestCasepublic function testCheckInEmailSentToUserIfSettingEnabled(){+ Mail::fake();+$user = User::factory()->create();$asset = Asset::factory()->assignedToUser($user)->create();@@ -29,16 +33,16 @@ class EmailNotificationsUponCheckinTest extends TestCase$this->fireCheckInEvent($asset, $user);- Notification::assertSentTo(- $user,- function (CheckinAssetNotification $notification, $channels) {- return in_array('mail', $channels);- },- );+ Mail::assertSent(CheckinAssetMail::class, function($mail) use ($user, $asset) {+ return $mail->hasTo($user->email) && $mail->event->checkoutable->id === $asset->id;+ });+}public function testCheckInEmailNotSentToUserIfSettingDisabled(){+ Mail::fake();+$user = User::factory()->create();$asset = Asset::factory()->assignedToUser($user)->create();@@ -46,10 +50,8 @@ class EmailNotificationsUponCheckinTest extends TestCase$this->fireCheckInEvent($asset, $user);- Notification::assertNotSentTo(- $user,- function (CheckinAssetNotification $notification, $channels) {- return in_array('mail', $channels);+ Mail::assertNotSent(CheckinAssetMail::class, function($mail) use ($user, $asset) {+ return $mail->hasTo($user->email) && $mail->event->checkoutable->id === $asset->id;});}