diff --git a/database/factories/AssetFactory.php b/database/factories/AssetFactory.php index e6108ce94..5461303c8 100644 --- a/database/factories/AssetFactory.php +++ b/database/factories/AssetFactory.php @@ -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); + }); + } } diff --git a/database/factories/LicenseSeatFactory.php b/database/factories/LicenseSeatFactory.php index 3c6cc4246..cd9acfee1 100644 --- a/database/factories/LicenseSeatFactory.php +++ b/database/factories/LicenseSeatFactory.php @@ -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(), + ]; + }); + } } diff --git a/tests/Feature/Checkins/AssetCheckinTest.php b/tests/Feature/Checkins/AssetCheckinTest.php index 4af551208..433e824dd 100644 --- a/tests/Feature/Checkins/AssetCheckinTest.php +++ b/tests/Feature/Checkins/AssetCheckinTest.php @@ -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();