snipe_it/tests/Feature/CheckoutAcceptances/Ui/AssetAcceptanceTest.php
2025-04-09 11:18:52 -07:00

139 lines
4.6 KiB
PHP

<?php
namespace Tests\Feature\CheckoutAcceptances\Ui;
use App\Events\CheckoutAccepted;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
use App\Models\User;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;
class AssetAcceptanceTest extends TestCase
{
public function testAssetCheckoutAcceptPageRenders()
{
$checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
$this->actingAs($checkoutAcceptance->assignedTo)
->get(route('account.accept.item', $checkoutAcceptance))
->assertViewIs('account.accept.create');
}
public function testCannotAcceptAssetAlreadyAccepted()
{
Event::fake([CheckoutAccepted::class]);
$checkoutAcceptance = CheckoutAcceptance::factory()->accepted()->create();
$this->assertFalse($checkoutAcceptance->isPending());
$this->actingAs($checkoutAcceptance->assignedTo)
->post(route('account.store-acceptance', $checkoutAcceptance), [
'asset_acceptance' => 'accepted',
'note' => 'my note',
])
->assertRedirectToRoute('account.accept')
->assertSessionHas('error');
Event::assertNotDispatched(CheckoutAccepted::class);
}
public function testCannotAcceptAssetForAnotherUser()
{
Event::fake([CheckoutAccepted::class]);
$checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
$this->assertTrue($checkoutAcceptance->isPending());
$anotherUser = User::factory()->create();
$this->actingAs($anotherUser)
->post(route('account.store-acceptance', $checkoutAcceptance), [
'asset_acceptance' => 'accepted',
'note' => 'my note',
])
->assertRedirectToRoute('account.accept')
->assertSessionHas('error');
$this->assertTrue($checkoutAcceptance->fresh()->isPending());
Event::assertNotDispatched(CheckoutAccepted::class);
}
public function testUserCanAcceptAsset()
{
Event::fake([CheckoutAccepted::class]);
$checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
$this->assertTrue($checkoutAcceptance->isPending());
$this->actingAs($checkoutAcceptance->assignedTo)
->post(route('account.store-acceptance', $checkoutAcceptance), [
'asset_acceptance' => 'accepted',
'note' => 'my note',
])
->assertRedirectToRoute('account.accept')
->assertSessionHas('success');
$checkoutAcceptance->refresh();
$this->assertFalse($checkoutAcceptance->isPending());
$this->assertNotNull($checkoutAcceptance->accepted_at);
$this->assertNull($checkoutAcceptance->declined_at);
Event::assertDispatched(CheckoutAccepted::class);
}
public function testUserCanDeclineAsset()
{
Event::fake([CheckoutAccepted::class]);
$checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
$this->assertTrue($checkoutAcceptance->isPending());
$this->actingAs($checkoutAcceptance->assignedTo)
->post(route('account.store-acceptance', $checkoutAcceptance), [
'asset_acceptance' => 'declined',
'note' => 'my note',
])
->assertRedirectToRoute('account.accept')
->assertSessionHas('success');
$checkoutAcceptance->refresh();
$this->assertFalse($checkoutAcceptance->isPending());
$this->assertNull($checkoutAcceptance->accepted_at);
$this->assertNotNull($checkoutAcceptance->declined_at);
Event::assertNotDispatched(CheckoutAccepted::class);
}
public function testActionLoggedWhenAcceptingAsset()
{
$checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
$this->actingAs($checkoutAcceptance->assignedTo)
->post(route('account.store-acceptance', $checkoutAcceptance), [
'asset_acceptance' => 'accepted',
'note' => 'my note',
]);
$this->assertTrue(Actionlog::query()
->where([
'action_type' => 'accepted',
'target_id' => $checkoutAcceptance->assignedTo->id,
'target_type' => User::class,
'note' => 'my note',
'item_type' => Asset::class,
'item_id' => $checkoutAcceptance->checkoutable->id,
])
->whereNotNull('action_date')
->exists()
);
}
}