Implement tests

This commit is contained in:
Marcus Moore 2024-02-13 17:04:53 -08:00
parent f708b8b299
commit 9ab56fe9ca
No known key found for this signature in database
3 changed files with 96 additions and 2 deletions

View file

@ -8,7 +8,6 @@ use App\Models\Location;
use App\Models\Statuslabel;
use App\Models\Supplier;
use App\Models\User;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Factories\Factory;
@ -353,4 +352,19 @@ class AssetFactory extends Factory
{
return $this->state(['requestable' => false]);
}
/**
* This allows bypassing model level validation if you want to purposefully
* create an asset in an invalid state. Validation is turned back on
* after the model is created via the factory.
* @return AssetFactory
*/
public function canBeInvalidUponCreation()
{
return $this->afterMaking(function (Asset $asset) {
$asset->setValidating(false);
})->afterCreating(function (Asset $asset) {
$asset->setValidating(true);
});
}
}

View file

@ -3,6 +3,7 @@
namespace Database\Factories;
use App\Models\License;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class LicenseSeatFactory extends Factory
@ -13,4 +14,13 @@ class LicenseSeatFactory extends Factory
'license_id' => License::factory(),
];
}
public function assignedToUser(User $user = null)
{
return $this->state(function () use ($user) {
return [
'assigned_to' => $user->id ?? User::factory(),
];
});
}
}

View file

@ -5,6 +5,8 @@ namespace Tests\Feature\Checkins;
use App\Events\CheckoutableCheckedIn;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
use App\Models\LicenseSeat;
use App\Models\Location;
use App\Models\Statuslabel;
use App\Models\User;
use App\Notifications\CheckinAssetNotification;
@ -71,6 +73,47 @@ class AssetCheckinTest extends TestCase
$this->assertEquals($status->id, $asset->status_id);
}
public function testLocationIsSetToRTDLocationByDefaultUponCheckin()
{
$rtdLocation = Location::factory()->create();
$asset = Asset::factory()->assignedToUser()->create([
'location_id' => Location::factory()->create()->id,
'rtd_location_id' => $rtdLocation->id,
]);
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route('hardware.checkin.store', ['assetId' => $asset->id]));
$this->assertTrue($asset->refresh()->location()->is($rtdLocation));
}
public function testLocationCanBeSetUponCheckin()
{
$location = Location::factory()->create();
$asset = Asset::factory()->assignedToUser()->create();
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route('hardware.checkin.store', ['assetId' => $asset->id]), [
'location_id' => $location->id,
]);
$this->assertTrue($asset->refresh()->location()->is($location));
}
public function testDefaultLocationCanBeUpdatedUponCheckin()
{
$location = Location::factory()->create();
$asset = Asset::factory()->assignedToUser()->create();
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route('hardware.checkin.store', ['assetId' => $asset->id]), [
'location_id' => $location->id,
'update_default_location' => 0
]);
$this->assertTrue($asset->refresh()->defaultLoc()->is($location));
}
public function testLastCheckInFieldIsSetOnCheckin()
{
$admin = User::factory()->superuser()->create();
@ -82,11 +125,38 @@ class AssetCheckinTest extends TestCase
]));
$this->assertNotNull(
$asset->fresh()->last_checkin,
$asset->refresh()->last_checkin,
'last_checkin field should be set on checkin'
);
}
public function testAssetsLicenseSeatsAreClearedUponCheckin()
{
$asset = Asset::factory()->assignedToUser()->create();
LicenseSeat::factory()->assignedToUser()->for($asset)->create();
$this->assertNotNull($asset->licenseseats->first()->assigned_to);
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route('hardware.checkin.store', ['assetId' => $asset->id]));
$this->assertNull($asset->refresh()->licenseseats->first()->assigned_to);
}
public function testLegacyLocationValuesSetToZeroAreUpdated()
{
$asset = Asset::factory()->canBeInvalidUponCreation()->assignedToUser()->create([
'rtd_location_id' => 0,
'location_id' => 0,
]);
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route('hardware.checkin.store', ['assetId' => $asset->id]));
$this->assertNull($asset->refresh()->rtd_location_id);
$this->assertNull($asset->location_id);
}
public function testPendingCheckoutAcceptancesAreClearedUponCheckin()
{
$asset = Asset::factory()->assignedToUser()->create();