2025-01-29 15:21:10 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Notifications\Email;
|
|
|
|
|
|
|
|
use App\Mail\CheckoutAssetMail;
|
|
|
|
use App\Models\CheckoutAcceptance;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
2025-01-29 15:36:45 -08:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2025-01-29 15:21:10 -08:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class AssetAcceptanceReminderTest extends TestCase
|
|
|
|
{
|
|
|
|
private CheckoutAcceptance $checkoutAcceptance;
|
|
|
|
private User $actor;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Mail::fake();
|
|
|
|
|
|
|
|
$this->actor = User::factory()->canViewReports()->create();
|
|
|
|
$this->checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMustHavePermissionToSendReminder()
|
|
|
|
{
|
|
|
|
$userWithoutPermission = User::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs($userWithoutPermission)
|
|
|
|
->post($this->routeFor($this->checkoutAcceptance))
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReminderNotSentIfAcceptanceDoesNotExist()
|
|
|
|
{
|
|
|
|
$this->actingAs($this->actor)
|
|
|
|
->post(route('reports/unaccepted_assets_sent_reminder', [
|
|
|
|
'acceptance_id' => 999999,
|
|
|
|
]));
|
|
|
|
|
|
|
|
Mail::assertNotSent(CheckoutAssetMail::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReminderNotSentIfAcceptanceAlreadyAccepted()
|
|
|
|
{
|
|
|
|
$checkoutAcceptanceAlreadyAccepted = CheckoutAcceptance::factory()->accepted()->create();
|
|
|
|
|
|
|
|
$this->actingAs($this->actor)
|
|
|
|
->post($this->routeFor($checkoutAcceptanceAlreadyAccepted));
|
|
|
|
|
|
|
|
Mail::assertNotSent(CheckoutAssetMail::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserWithoutEmailAddressHandledGracefully()
|
|
|
|
{
|
2025-01-29 16:14:09 -08:00
|
|
|
$checkoutAcceptance = CheckoutAcceptance::factory()
|
|
|
|
->pending()
|
|
|
|
->forAssignedTo(['email' => null])
|
|
|
|
->create();
|
2025-01-29 15:21:10 -08:00
|
|
|
|
|
|
|
$this->actingAs($this->actor)
|
2025-01-29 16:14:09 -08:00
|
|
|
->post($this->routeFor($checkoutAcceptance))
|
2025-01-29 15:21:10 -08:00
|
|
|
// check we didn't crash...
|
|
|
|
->assertRedirect();
|
|
|
|
|
|
|
|
Mail::assertNotSent(CheckoutAssetMail::class);
|
|
|
|
}
|
|
|
|
|
2025-01-29 15:56:20 -08:00
|
|
|
public static function acceptances()
|
2025-01-29 15:21:10 -08:00
|
|
|
{
|
2025-01-29 15:36:45 -08:00
|
|
|
yield 'User with locale set' => [
|
|
|
|
function () {
|
|
|
|
return CheckoutAcceptance::factory()->pending()->create();
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'User without locale set' => [
|
|
|
|
function () {
|
|
|
|
$checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
|
|
|
|
$checkoutAcceptance->assignedTo->update(['locale' => null]);
|
|
|
|
|
|
|
|
return $checkoutAcceptance;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2025-01-29 15:56:20 -08:00
|
|
|
#[DataProvider('acceptances')]
|
|
|
|
public function testReminderIsSentToUser($callback)
|
2025-01-29 15:36:45 -08:00
|
|
|
{
|
2025-01-29 15:56:20 -08:00
|
|
|
$checkoutAcceptance = $callback();
|
2025-01-29 15:36:45 -08:00
|
|
|
|
2025-01-29 15:21:10 -08:00
|
|
|
$this->actingAs($this->actor)
|
2025-01-29 15:36:45 -08:00
|
|
|
->post($this->routeFor($checkoutAcceptance))
|
2025-01-29 15:21:10 -08:00
|
|
|
->assertRedirect(route('reports/unaccepted_assets'));
|
|
|
|
|
|
|
|
Mail::assertSent(CheckoutAssetMail::class, 1);
|
2025-01-29 15:36:45 -08:00
|
|
|
Mail::assertSent(CheckoutAssetMail::class, function (CheckoutAssetMail $mail) use ($checkoutAcceptance) {
|
|
|
|
return $mail->hasTo($checkoutAcceptance->assignedTo->email)
|
2025-01-29 15:21:10 -08:00
|
|
|
&& $mail->hasSubject('Reminder: ' . trans('mail.Asset_Checkout_Notification'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private function routeFor(CheckoutAcceptance $checkoutAcceptance): string
|
|
|
|
{
|
|
|
|
return route('reports/unaccepted_assets_sent_reminder', [
|
|
|
|
'acceptance_id' => $checkoutAcceptance->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|