From 04e96b8f20095568aa37dad8eca2d0b1effff07e Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 11 Sep 2024 16:43:41 +0100 Subject: [PATCH] Added tests Signed-off-by: snipe --- tests/Feature/Assets/Ui/EditAssetTest.php | 43 ++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Assets/Ui/EditAssetTest.php b/tests/Feature/Assets/Ui/EditAssetTest.php index befe64a864..c17366bed5 100644 --- a/tests/Feature/Assets/Ui/EditAssetTest.php +++ b/tests/Feature/Assets/Ui/EditAssetTest.php @@ -2,16 +2,20 @@ namespace Tests\Feature\Assets\Ui; +use App\Events\CheckoutableCheckedIn; use App\Models\Asset; use App\Models\AssetModel; +use App\Models\Location; use App\Models\StatusLabel; use App\Models\User; +use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Event; use Tests\TestCase; class EditAssetTest extends TestCase { - public function testPermissionRequiredToViewLicense() + public function testPermissionRequiredToViewAsset() { $asset = Asset::factory()->create(); $this->actingAs(User::factory()->create()) @@ -64,4 +68,41 @@ class EditAssetTest extends TestCase $this->assertDatabaseHas('assets', ['asset_tag' => 'New Asset Tag']); } + public function testNewCheckinIsLoggedIfStatusChangedToUndeployable() + { + Event::fake([CheckoutableCheckedIn::class]); + + $user = User::factory()->create(); + $location = Location::factory()->create(); + $deployable_status = Statuslabel::factory()->rtd()->create(); + $achived_status = Statuslabel::factory()->archived()->create(); + + $asset = Asset::factory()->assignedToUser($user)->create(['status_id' => $deployable_status->id]); + + $this->assertTrue($asset->assignedTo->is($user)); + + $currentTimestamp = now(); + + $this->actingAs(User::factory()->viewAssets()->editAssets()->create()) + ->post( + route('hardware.update', ['hardware' => $asset->id]), + [ + 'status_id' => $achived_status->id, + ], + ); + + $asset->refresh(); + \Log::error('AssignedTo: '.$asset->refresh()->assignedTo); + \Log::error('Assigned_to: '.$asset->refresh()->assigned_to); + \Log::error('Assigned_type: '.$asset->refresh()->assigned_type); + $this->assertNull($asset->refresh()->assignedTo); + $this->assertNull($asset->refresh()->assigned_type); + $this->assertEquals($achived_status->id, $asset->refresh()->status_id); + + Event::assertDispatched(function (CheckoutableCheckedIn $event) use ($currentTimestamp) { + // this could be better mocked but is ok for now. + return Carbon::parse($event->action_date)->diffInSeconds($currentTimestamp) < 2; + }, 1); + } + }