snipe-it/tests/Feature/Notifications/Email/AssetAcceptanceReminderTest.php

109 lines
3.4 KiB
PHP
Raw Normal View History

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
{
protected function setUp(): void
{
parent::setUp();
Mail::fake();
}
public function testMustHavePermissionToSendReminder()
{
2025-01-29 16:15:27 -08:00
$checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
2025-01-29 15:21:10 -08:00
$userWithoutPermission = User::factory()->create();
$this->actingAs($userWithoutPermission)
2025-01-29 16:15:27 -08:00
->post($this->routeFor($checkoutAcceptance))
2025-01-29 15:21:10 -08:00
->assertForbidden();
2025-01-30 11:48:55 -08:00
Mail::assertNotSent(CheckoutAssetMail::class);
2025-01-29 15:21:10 -08:00
}
public function testReminderNotSentIfAcceptanceDoesNotExist()
{
2025-01-29 16:16:47 -08:00
$this->actingAs(User::factory()->canViewReports()->create())
2025-01-29 15:21:10 -08:00
->post(route('reports/unaccepted_assets_sent_reminder', [
'acceptance_id' => 999999,
]));
Mail::assertNotSent(CheckoutAssetMail::class);
}
public function testReminderNotSentIfAcceptanceAlreadyAccepted()
{
$checkoutAcceptanceAlreadyAccepted = CheckoutAcceptance::factory()->accepted()->create();
2025-01-29 16:16:47 -08:00
$this->actingAs(User::factory()->canViewReports()->create())
2025-01-29 15:21:10 -08:00
->post($this->routeFor($checkoutAcceptanceAlreadyAccepted));
Mail::assertNotSent(CheckoutAssetMail::class);
}
2025-01-29 16:24:43 -08:00
public static function CheckoutAcceptancesToUsersWithoutEmailAddresses()
2025-01-29 15:21:10 -08:00
{
2025-01-29 16:24:43 -08:00
yield 'User with null email address' => [
function () {
return CheckoutAcceptance::factory()
->pending()
->forAssignedTo(['email' => null])
->create();
}
];
yield 'User with empty string email address' => [
function () {
return CheckoutAcceptance::factory()
->pending()
->forAssignedTo(['email' => ''])
->create();
}
];
}
#[DataProvider('CheckoutAcceptancesToUsersWithoutEmailAddresses')]
public function testUserWithoutEmailAddressHandledGracefully($callback)
{
$checkoutAcceptance = $callback();
2025-01-29 15:21:10 -08:00
2025-01-29 16:16:47 -08:00
$this->actingAs(User::factory()->canViewReports()->create())
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-30 11:47:43 -08:00
public function testReminderIsSentToUser()
2025-01-29 15:21:10 -08:00
{
2025-01-30 11:47:43 -08:00
$checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
2025-01-29 15:36:45 -08:00
2025-01-29 16:16:47 -08:00
$this->actingAs(User::factory()->canViewReports()->create())
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)
&& $mail->hasSubject(trans('mail.unaccepted_asset_reminder'));
2025-01-29 15:21:10 -08:00
});
}
private function routeFor(CheckoutAcceptance $checkoutAcceptance): string
{
return route('reports/unaccepted_assets_sent_reminder', [
'acceptance_id' => $checkoutAcceptance->id,
]);
}
}