A free open source IT asset/license management system
Implement some tests
Marcus Moore530291f81c2b
diff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.phpindex a096f16678..46f8c5b8b5 100644--- a/app/Http/Controllers/Assets/AssetCheckoutController.php+++ b/app/Http/Controllers/Assets/AssetCheckoutController.php@@ -64,6 +64,7 @@ class AssetCheckoutController extends Controller$target = $this->determineCheckoutTarget($asset);+ // this seems to be handled by the checkOut method$asset = $this->updateAssetLocation($asset, $target);$checkout_at = date('Y-m-d H:i:s');@@ -92,6 +93,7 @@ class AssetCheckoutController extends Controller$settings = \App\Models\Setting::getSettings();// We have to check whether $target->company_id is null here since locations don't have a company yet+ // @todo: test thisif (($settings->full_multiple_companies_support) && ((!is_null($target->company_id)) && (!is_null($asset->company_id)))) {if ($target->company_id != $asset->company_id){return redirect()->to("hardware/$assetId/checkout")->with('error', trans('general.error_user_company'));diff --git a/tests/Feature/Checkouts/AssetCheckoutTest.php b/tests/Feature/Checkouts/AssetCheckoutTest.phpindex 1f67422c63..b24d83dbf7 100644--- a/tests/Feature/Checkouts/AssetCheckoutTest.php+++ b/tests/Feature/Checkouts/AssetCheckoutTest.php@@ -4,6 +4,7 @@ namespace Tests\Feature\Checkouts;use App\Events\CheckoutableCheckedOut;use App\Models\Asset;+use App\Models\Location;use App\Models\Statuslabel;use App\Models\User;use Illuminate\Support\Facades\Event;@@ -24,6 +25,46 @@ class AssetCheckoutTest extends TestCase->assertForbidden();}+ public function testNonExistentAssetCannotBeCheckedOut()+ {+ Event::fake([CheckoutableCheckedOut::class]);++ $this->actingAs(User::factory()->checkoutAssets()->create())+ ->post(route('hardware.checkout.store', 1000), [+ 'checkout_to_type' => 'user',+ 'assigned_user' => User::factory()->create()->id,+ 'name' => 'Changed Name',+ 'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,+ 'checkout_at' => '2024-03-18',+ 'expected_checkin' => '2024-03-28',+ 'note' => 'An awesome note',+ ])+ ->assertSessionHas('error');++ Event::assertNotDispatched(CheckoutableCheckedOut::class);+ }++ public function testAssetNotAvailableForCheckoutCannotBeCheckedOut()+ {+ Event::fake([CheckoutableCheckedOut::class]);++ $asset = Asset::factory()->assignedToUser()->create();++ $this->actingAs(User::factory()->checkoutAssets()->create())+ ->post(route('hardware.checkout.store', $asset), [+ 'checkout_to_type' => 'user',+ 'assigned_user' => User::factory()->create()->id,+ 'name' => 'Changed Name',+ 'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,+ 'checkout_at' => '2024-03-18',+ 'expected_checkin' => '2024-03-28',+ 'note' => 'An awesome note',+ ])+ ->assertSessionHas('error');++ Event::assertNotDispatched(CheckoutableCheckedOut::class);+ }+public function testValidationWhenCheckingOutAsset(){$this->actingAs(User::factory()->create())@@ -35,27 +76,34 @@ class AssetCheckoutTest extends TestCasepublic function testAnAssetCanBeCheckedOutToAUser(){- // $this->markTestIncomplete();+ $this->markTestIncomplete();Event::fake([CheckoutableCheckedOut::class]);- $status = Statuslabel::factory()->readyToDeploy()->create();- $asset = Asset::factory()->create();+ $originalStatus = Statuslabel::factory()->readyToDeploy()->create();+ $updatedStatus = Statuslabel::factory()->readyToDeploy()->create();+ $asset = Asset::factory()->create(['status_id' => $originalStatus->id]);$admin = User::factory()->checkoutAssets()->create();- $user = User::factory()->create();+ $userLocation = Location::factory()->create();++ $user = User::factory()->for($userLocation)->create();$this->actingAs($admin)->post(route('hardware.checkout.store', $asset), ['checkout_to_type' => 'user','assigned_user' => $user->id,'name' => 'Changed Name',- 'status_id' => $status->id,+ 'status_id' => $updatedStatus->id,'checkout_at' => '2024-03-18','expected_checkin' => '2024-03-28','note' => 'An awesome note',]);// @todo: ensure asset updated+ $asset->refresh();+ $this->assertTrue($asset->location->is($userLocation));+ $this->assertEquals('2024-03-28 00:00:00', (string) $asset->expected_checkin);+ $this->assertTrue($asset->assetstatus->is($updatedStatus));Event::assertDispatched(function (CheckoutableCheckedOut $event) use ($admin, $asset, $user) {return $event->checkoutable->is($asset)@@ -67,6 +115,11 @@ class AssetCheckoutTest extends TestCase// @todo: assert action log entry created}+ public function testLicenseSeatsAreAssignedToUserUponCheckout()+ {+ $this->markTestIncomplete();+ }+public function testAnAssetCanBeCheckedOutToAnAsset(){$this->markTestIncomplete();@@ -76,4 +129,9 @@ class AssetCheckoutTest extends TestCase{$this->markTestIncomplete();}++ public function testLastCheckoutUsesCurrentDateIfNotProvided()+ {+ $this->markTestIncomplete();+ }}