Skip to content

Commit 425ad93

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents 8a44144 + 4b9f442 commit 425ad93

File tree

9 files changed

+459
-164
lines changed

9 files changed

+459
-164
lines changed

app/Listeners/CheckoutableListener.php

Lines changed: 190 additions & 152 deletions
Large diffs are not rendered by default.

app/Notifications/AuditNotification.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Notifications;
44

5+
use AllowDynamicProperties;
56
use App\Models\Setting;
67
use Illuminate\Bus\Queueable;
78
use Illuminate\Notifications\Channels\SlackWebhookChannel;
@@ -12,7 +13,7 @@
1213
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
1314
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;
1415

15-
class AuditNotification extends Notification
16+
#[AllowDynamicProperties] class AuditNotification extends Notification
1617
{
1718
use Queueable;
1819
/**

database/factories/CategoryFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function definition()
2828
'checkin_email' => true,
2929
'eula_text' => $this->faker->paragraph(),
3030
'require_acceptance' => false,
31-
'use_default_eula' => $this->faker->boolean(),
31+
'use_default_eula' => false,
3232
'created_by' => User::factory()->superuser(),
3333
'notes' => 'Created by DB seeder',
3434
];

resources/views/groups/view.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<div class="box-body">
2323
<div class="row">
2424
<div class="col-md-12">
25-
<div class="table table-responsive">
25+
2626

2727
<table
2828
data-columns="{{ \App\Presenters\UserPresenter::dataTableLayout() }}"
@@ -41,7 +41,7 @@ class="table table-striped snipe-table"
4141
"ignoreColumn": ["actions","image","change","checkbox","checkincheckout","icon"]
4242
}'>
4343
</table>
44-
</div>
44+
4545
</div>
4646
</div>
4747
</div>

tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use App\Mail\CheckinAssetMail;
66
use App\Models\Accessory;
7+
use App\Models\AssetModel;
8+
use App\Models\Category;
79
use App\Models\Consumable;
810
use App\Models\LicenseSeat;
911
use Illuminate\Support\Facades\Mail;
1012
use PHPUnit\Framework\Attributes\Group;
1113
use App\Events\CheckoutableCheckedIn;
1214
use App\Models\Asset;
1315
use App\Models\User;
14-
use App\Notifications\CheckinAssetNotification;
15-
use Illuminate\Support\Facades\Notification;
1616
use Tests\TestCase;
1717

1818
#[Group('notifications')]
@@ -25,10 +25,8 @@ protected function setUp(): void
2525
Mail::fake();
2626
}
2727

28-
public function testCheckInEmailSentToUserIfSettingEnabled()
28+
public function test_check_in_email_sent_to_user_if_setting_enabled()
2929
{
30-
Mail::fake();
31-
3230
$user = User::factory()->create();
3331
$asset = Asset::factory()->assignedToUser($user)->create();
3432

@@ -39,13 +37,11 @@ public function testCheckInEmailSentToUserIfSettingEnabled()
3937
Mail::assertSent(CheckinAssetMail::class, function($mail) use ($user) {
4038
return $mail->hasTo($user->email);
4139
});
42-
4340
}
4441

45-
public function testCheckInEmailNotSentToUserIfSettingDisabled()
42+
public function test_check_in_email_not_sent_to_user_if_setting_disabled()
4643
{
4744
$this->settings->disableAdminCC();
48-
Mail::fake();
4945

5046
$user = User::factory()->create();
5147
$checkoutables = collect([
@@ -93,6 +89,69 @@ public function testCheckInEmailNotSentToUserIfSettingDisabled()
9389
});
9490
}
9591

92+
public function test_handles_user_not_having_email_address_set()
93+
{
94+
$user = User::factory()->create(['email' => null]);
95+
$asset = Asset::factory()->assignedToUser($user)->create();
96+
97+
$asset->model->category->update(['checkin_email' => true]);
98+
99+
$this->fireCheckInEvent($asset, $user);
100+
101+
Mail::assertNothingSent();
102+
}
103+
104+
public function test_admin_alert_email_sends()
105+
{
106+
$this->settings->enableAdminCC('[email protected]');
107+
108+
$user = User::factory()->create();
109+
$asset = Asset::factory()->assignedToUser($user)->create();
110+
111+
$asset->model->category->update(['checkin_email' => true]);
112+
113+
$this->fireCheckInEvent($asset, $user);
114+
115+
Mail::assertSent(CheckinAssetMail::class, function ($mail) use ($user) {
116+
return $mail->hasTo($user->email) && $mail->hasCc('[email protected]');
117+
});
118+
}
119+
120+
public function test_admin_alert_email_still_sent_when_category_email_is_not_set_to_send_email_to_user()
121+
{
122+
$this->settings->enableAdminCC('[email protected]');
123+
124+
$category = Category::factory()->create([
125+
'checkin_email' => false,
126+
'eula_text' => null,
127+
'use_default_eula' => false,
128+
]);
129+
$assetModel = AssetModel::factory()->create(['category_id' => $category->id]);
130+
$asset = Asset::factory()->create(['model_id' => $assetModel->id]);
131+
132+
$this->fireCheckInEvent($asset, User::factory()->create());
133+
134+
Mail::assertSent(CheckinAssetMail::class, function ($mail) {
135+
return $mail->hasTo('[email protected]');
136+
});
137+
}
138+
139+
public function test_admin_alert_email_still_sent_when_user_has_no_email_address()
140+
{
141+
$this->settings->enableAdminCC('[email protected]');
142+
143+
$user = User::factory()->create(['email' => null]);
144+
$asset = Asset::factory()->assignedToUser($user)->create();
145+
146+
$asset->model->category->update(['checkin_email' => true]);
147+
148+
$this->fireCheckInEvent($asset, $user);
149+
150+
Mail::assertSent(CheckinAssetMail::class, function ($mail) {
151+
return $mail->hasTo('[email protected]');
152+
});
153+
}
154+
96155
private function fireCheckInEvent($asset, $user): void
97156
{
98157
event(new CheckoutableCheckedIn(
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace Tests\Feature\Notifications\Email;
4+
5+
use App\Events\CheckoutableCheckedOut;
6+
use App\Mail\CheckoutAssetMail;
7+
use App\Models\Asset;
8+
use App\Models\AssetModel;
9+
use App\Models\Category;
10+
use App\Models\User;
11+
use Illuminate\Support\Facades\Mail;
12+
use PHPUnit\Framework\Attributes\Group;
13+
use Tests\TestCase;
14+
15+
#[Group('notifications')]
16+
class EmailNotificationsUponCheckoutTest extends TestCase
17+
{
18+
private Asset $asset;
19+
private AssetModel $assetModel;
20+
private Category $category;
21+
private User $user;
22+
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
27+
Mail::fake();
28+
29+
$this->category = Category::factory()->create([
30+
'checkin_email' => false,
31+
'eula_text' => null,
32+
'require_acceptance' => false,
33+
'use_default_eula' => false,
34+
]);
35+
36+
$this->assetModel = AssetModel::factory()->for($this->category)->create();
37+
$this->asset = Asset::factory()->for($this->assetModel, 'model')->create();
38+
39+
$this->user = User::factory()->create();
40+
}
41+
42+
public function test_email_sent_to_user_when_category_requires_acceptance()
43+
{
44+
$this->category->update(['require_acceptance' => true]);
45+
46+
$this->fireCheckoutEvent();
47+
48+
$this->assertUserSentEmail();
49+
}
50+
51+
public function test_email_sent_to_user_when_category_using_default_eula()
52+
{
53+
$this->settings->setEula();
54+
55+
$this->category->update(['use_default_eula' => true]);
56+
57+
$this->fireCheckoutEvent();
58+
59+
$this->assertUserSentEmail();
60+
}
61+
62+
public function test_email_sent_to_user_when_category_using_local_eula()
63+
{
64+
$this->category->update(['eula_text' => 'Some EULA text']);
65+
66+
$this->fireCheckoutEvent();
67+
68+
$this->assertUserSentEmail();
69+
}
70+
71+
public function test_email_sent_to_user_when_category_set_to_explicitly_send_email()
72+
{
73+
$this->category->update(['checkin_email' => true]);
74+
75+
$this->fireCheckoutEvent();
76+
77+
$this->assertUserSentEmail();
78+
}
79+
80+
public function test_handles_user_not_having_email_address_set()
81+
{
82+
$this->category->update(['checkin_email' => true]);
83+
$this->user->update(['email' => null]);
84+
85+
$this->fireCheckoutEvent();
86+
87+
Mail::assertNothingSent();
88+
}
89+
90+
public function test_admin_alert_email_sends()
91+
{
92+
$this->settings->enableAdminCC('[email protected]');
93+
94+
$this->category->update(['checkin_email' => true]);
95+
96+
$this->fireCheckoutEvent();
97+
98+
Mail::assertSent(CheckoutAssetMail::class, function (CheckoutAssetMail $mail) {
99+
return $mail->hasCc('[email protected]');
100+
});
101+
}
102+
103+
public function test_admin_alert_email_still_sent_when_category_is_not_set_to_send_email_to_user()
104+
{
105+
$this->settings->enableAdminCC('[email protected]');
106+
107+
$this->fireCheckoutEvent();
108+
109+
Mail::assertSent(CheckoutAssetMail::class, function ($mail) {
110+
return $mail->hasTo('[email protected]');
111+
});
112+
}
113+
114+
public function test_admin_alert_email_still_sent_when_user_has_no_email_address()
115+
{
116+
$this->settings->enableAdminCC('[email protected]');
117+
118+
$this->category->update(['checkin_email' => true]);
119+
$this->user->update(['email' => null]);
120+
121+
$this->fireCheckoutEvent();
122+
123+
Mail::assertSent(CheckoutAssetMail::class, function ($mail) {
124+
return $mail->hasTo('[email protected]');
125+
});
126+
}
127+
128+
private function fireCheckoutEvent(): void
129+
{
130+
event(new CheckoutableCheckedOut(
131+
$this->asset,
132+
$this->user,
133+
User::factory()->superuser()->create(),
134+
'',
135+
));
136+
}
137+
138+
private function assertUserSentEmail(): void
139+
{
140+
Mail::assertSent(CheckoutAssetMail::class, function (CheckoutAssetMail $mail) {
141+
return $mail->hasTo($this->user->email);
142+
});
143+
}
144+
}

tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tests\Feature\Notifications\Webhooks;
44

5+
use App\Models\AssetModel;
6+
use App\Models\Category;
57
use PHPUnit\Framework\Attributes\DataProvider;
68
use PHPUnit\Framework\Attributes\Group;
79
use App\Events\CheckoutableCheckedIn;
@@ -95,6 +97,27 @@ public function testAssetCheckinDoesNotSendSlackNotificationWhenSettingDisabled(
9597
$this->assertNoSlackNotificationSent(CheckinAssetNotification::class);
9698
}
9799

100+
public function testSlackNotificationIsStillSentWhenCategoryEmailIsNotSetToSendEmails()
101+
{
102+
$this->settings->enableSlackWebhook();
103+
104+
$category = Category::factory()->create([
105+
'checkin_email' => false,
106+
'eula_text' => null,
107+
'require_acceptance' => false,
108+
'use_default_eula' => false,
109+
]);
110+
$assetModel = AssetModel::factory()->for($category)->create();
111+
$asset = Asset::factory()->for($assetModel, 'model')->assignedToUser()->create();
112+
113+
$this->fireCheckInEvent(
114+
$asset,
115+
User::factory()->create(),
116+
);
117+
118+
$this->assertSlackNotificationSent(CheckinAssetNotification::class);
119+
}
120+
98121
public function testComponentCheckinDoesNotSendSlackNotification()
99122
{
100123
$this->settings->enableSlackWebhook();

tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tests\Feature\Notifications\Webhooks;
44

5+
use App\Models\AssetModel;
6+
use App\Models\Category;
57
use PHPUnit\Framework\Attributes\DataProvider;
68
use PHPUnit\Framework\Attributes\Group;
79
use App\Events\CheckoutableCheckedOut;
@@ -97,6 +99,27 @@ public function testAssetCheckoutDoesNotSendSlackNotificationWhenSettingDisabled
9799
$this->assertNoSlackNotificationSent(CheckoutAssetNotification::class);
98100
}
99101

102+
public function testSlackNotificationIsStillSentWhenCategoryEmailIsNotSetToSendEmails()
103+
{
104+
$this->settings->enableSlackWebhook();
105+
106+
$category = Category::factory()->create([
107+
'checkin_email' => false,
108+
'eula_text' => null,
109+
'require_acceptance' => false,
110+
'use_default_eula' => false,
111+
]);
112+
$assetModel = AssetModel::factory()->for($category)->create();
113+
$asset = Asset::factory()->for($assetModel, 'model')->create();
114+
115+
$this->fireCheckOutEvent(
116+
$asset,
117+
User::factory()->create(),
118+
);
119+
120+
$this->assertSlackNotificationSent(CheckoutAssetNotification::class);
121+
}
122+
100123
public function testComponentCheckoutDoesNotSendSlackNotification()
101124
{
102125
$this->settings->enableSlackWebhook();

tests/Support/Settings.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public function disableAlertEmail(): Settings
4646
]);
4747
}
4848

49+
public function enableAdminCC(string $email = '[email protected]'): Settings
50+
{
51+
return $this->update([
52+
'admin_cc_email' => $email,
53+
]);
54+
}
55+
4956
public function disableAdminCC(): Settings
5057
{
5158
return $this->update([

0 commit comments

Comments
 (0)