From 699476da9092232fbf629201ab5a3a937800f1ad Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Dec 2024 17:33:34 -0800 Subject: [PATCH 1/2] Scaffold and implement some license checkin tests --- .../Licenses/LicenseCheckinController.php | 6 +- database/factories/LicenseSeatFactory.php | 15 +++++ .../lang/en-US/admin/licenses/message.php | 1 + .../Checkins/Ui/LicenseCheckinTest.php | 55 +++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Licenses/LicenseCheckinController.php b/app/Http/Controllers/Licenses/LicenseCheckinController.php index dd83d0154c..4496eb3bc9 100644 --- a/app/Http/Controllers/Licenses/LicenseCheckinController.php +++ b/app/Http/Controllers/Licenses/LicenseCheckinController.php @@ -63,6 +63,7 @@ class LicenseCheckinController extends Controller $license = License::find($licenseSeat->license_id); // LicenseSeat is not assigned, it can't be checked in + // @todo: if (is_null($licenseSeat->assigned_to) && is_null($licenseSeat->asset_id)) { return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkin.error')); } @@ -71,7 +72,7 @@ class LicenseCheckinController extends Controller if (! $license->reassignable) { // Not allowed to checkin - Session::flash('error', 'License not reassignable.'); + Session::flash('error', trans('admin/licenses/message.checkin.not_reassignable') . '.'); return redirect()->back()->withInput(); } @@ -90,12 +91,14 @@ class LicenseCheckinController extends Controller return redirect()->back()->withInput()->withErrors($validator); } + // @todo: if($licenseSeat->assigned_to != null){ $return_to = User::find($licenseSeat->assigned_to); } else { $return_to = Asset::find($licenseSeat->asset_id); } + // @todo: // Update the asset data $licenseSeat->assigned_to = null; $licenseSeat->asset_id = null; @@ -106,6 +109,7 @@ class LicenseCheckinController extends Controller // Was the asset updated? if ($licenseSeat->save()) { + // @todo: event(new CheckoutableCheckedIn($licenseSeat, $return_to, auth()->user(), $request->input('notes'))); diff --git a/database/factories/LicenseSeatFactory.php b/database/factories/LicenseSeatFactory.php index f9560af472..cc3b7521a0 100644 --- a/database/factories/LicenseSeatFactory.php +++ b/database/factories/LicenseSeatFactory.php @@ -4,6 +4,7 @@ namespace Database\Factories; use App\Models\Asset; use App\Models\License; +use App\Models\LicenseSeat; use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; @@ -33,4 +34,18 @@ class LicenseSeatFactory extends Factory ]; }); } + + public function reassignable() + { + return $this->afterCreating(function (LicenseSeat $seat) { + $seat->license->update(['reassignable' => true]); + }); + } + + public function notReassignable() + { + return $this->afterCreating(function (LicenseSeat $seat) { + $seat->license->update(['reassignable' => false]); + }); + } } diff --git a/resources/lang/en-US/admin/licenses/message.php b/resources/lang/en-US/admin/licenses/message.php index 7f5981aa05..74e1d7af5a 100644 --- a/resources/lang/en-US/admin/licenses/message.php +++ b/resources/lang/en-US/admin/licenses/message.php @@ -50,6 +50,7 @@ return array( 'checkin' => array( 'error' => 'There was an issue checking in the license. Please try again.', + 'not_reassignable' => 'License not reassignable', 'success' => 'The license was checked in successfully' ), diff --git a/tests/Feature/Checkins/Ui/LicenseCheckinTest.php b/tests/Feature/Checkins/Ui/LicenseCheckinTest.php index e087cb442d..5842e0c931 100644 --- a/tests/Feature/Checkins/Ui/LicenseCheckinTest.php +++ b/tests/Feature/Checkins/Ui/LicenseCheckinTest.php @@ -17,5 +17,60 @@ class LicenseCheckinTest extends TestCase ->assertForbidden(); } + public function testCannotCheckinNonReassignableLicense() + { + $licenseSeat = LicenseSeat::factory() + ->notReassignable() + ->assignedToUser() + ->create(); + $this->actingAs(User::factory()->checkoutLicenses()->create()) + ->post(route('licenses.checkin.save', $licenseSeat), [ + 'notes' => 'my note', + 'redirect_option' => 'index', + ]) + ->assertSessionHas('error', trans('admin/licenses/message.checkin.not_reassignable') . '.'); + + $this->assertNotNull($licenseSeat->fresh()->assigned_to); + } + + public function testCanCheckInLicenseAssignedToAsset() + { + $licenseSeat = LicenseSeat::factory() + ->reassignable() + ->assignedToAsset() + ->create(); + + $this->assertNotNull($licenseSeat->asset_id); + + $this->actingAs(User::factory()->checkoutLicenses()->create()) + ->post(route('licenses.checkin.save', $licenseSeat), [ + 'notes' => 'my note', + 'redirect_option' => 'index', + ]) + ->assertRedirect(route('licenses.index')); + + $this->assertNull($licenseSeat->fresh()->asset_id); + $this->assertNull($licenseSeat->fresh()->assigned_to); + } + + public function testCanCheckInLicenseAssignedToUser() + { + $licenseSeat = LicenseSeat::factory() + ->reassignable() + ->assignedToUser() + ->create(); + + $this->assertNotNull($licenseSeat->assigned_to); + + $this->actingAs(User::factory()->checkoutLicenses()->create()) + ->post(route('licenses.checkin.save', $licenseSeat), [ + 'notes' => 'my note', + 'redirect_option' => 'index', + ]) + ->assertRedirect(route('licenses.index')); + + $this->assertNull($licenseSeat->fresh()->asset_id); + $this->assertNull($licenseSeat->fresh()->assigned_to); + } } From 7aa5195e87d5a7060fd4165e60ac06a6ab8aa8b4 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Dec 2024 14:39:24 -0800 Subject: [PATCH 2/2] Add tests for license checkin --- .../Licenses/LicenseCheckinController.php | 4 -- database/factories/LicenseSeatFactory.php | 4 +- .../Checkins/Ui/LicenseCheckinTest.php | 58 +++++++++++++++++-- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/Licenses/LicenseCheckinController.php b/app/Http/Controllers/Licenses/LicenseCheckinController.php index 4496eb3bc9..373a167642 100644 --- a/app/Http/Controllers/Licenses/LicenseCheckinController.php +++ b/app/Http/Controllers/Licenses/LicenseCheckinController.php @@ -63,7 +63,6 @@ class LicenseCheckinController extends Controller $license = License::find($licenseSeat->license_id); // LicenseSeat is not assigned, it can't be checked in - // @todo: if (is_null($licenseSeat->assigned_to) && is_null($licenseSeat->asset_id)) { return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkin.error')); } @@ -91,14 +90,12 @@ class LicenseCheckinController extends Controller return redirect()->back()->withInput()->withErrors($validator); } - // @todo: if($licenseSeat->assigned_to != null){ $return_to = User::find($licenseSeat->assigned_to); } else { $return_to = Asset::find($licenseSeat->asset_id); } - // @todo: // Update the asset data $licenseSeat->assigned_to = null; $licenseSeat->asset_id = null; @@ -109,7 +106,6 @@ class LicenseCheckinController extends Controller // Was the asset updated? if ($licenseSeat->save()) { - // @todo: event(new CheckoutableCheckedIn($licenseSeat, $return_to, auth()->user(), $request->input('notes'))); diff --git a/database/factories/LicenseSeatFactory.php b/database/factories/LicenseSeatFactory.php index cc3b7521a0..aaf75bdc2d 100644 --- a/database/factories/LicenseSeatFactory.php +++ b/database/factories/LicenseSeatFactory.php @@ -37,14 +37,14 @@ class LicenseSeatFactory extends Factory public function reassignable() { - return $this->afterCreating(function (LicenseSeat $seat) { + return $this->afterMaking(function (LicenseSeat $seat) { $seat->license->update(['reassignable' => true]); }); } public function notReassignable() { - return $this->afterCreating(function (LicenseSeat $seat) { + return $this->afterMaking(function (LicenseSeat $seat) { $seat->license->update(['reassignable' => false]); }); } diff --git a/tests/Feature/Checkins/Ui/LicenseCheckinTest.php b/tests/Feature/Checkins/Ui/LicenseCheckinTest.php index 5842e0c931..7e6a33853e 100644 --- a/tests/Feature/Checkins/Ui/LicenseCheckinTest.php +++ b/tests/Feature/Checkins/Ui/LicenseCheckinTest.php @@ -2,8 +2,11 @@ namespace Tests\Feature\Checkins\Ui; +use App\Events\CheckoutableCheckedIn; +use App\Models\Asset; use App\Models\LicenseSeat; use App\Models\User; +use Illuminate\Support\Facades\Event; use Tests\TestCase; class LicenseCheckinTest extends TestCase @@ -34,16 +37,37 @@ class LicenseCheckinTest extends TestCase $this->assertNotNull($licenseSeat->fresh()->assigned_to); } - public function testCanCheckInLicenseAssignedToAsset() + public function testCannotCheckinLicenseThatIsNotAssigned() { $licenseSeat = LicenseSeat::factory() ->reassignable() - ->assignedToAsset() ->create(); - $this->assertNotNull($licenseSeat->asset_id); + $this->assertNull($licenseSeat->assigned_to); + $this->assertNull($licenseSeat->asset_id); $this->actingAs(User::factory()->checkoutLicenses()->create()) + ->post(route('licenses.checkin.save', $licenseSeat), [ + 'notes' => 'my note', + 'redirect_option' => 'index', + ]) + ->assertSessionHas('error', trans('admin/licenses/message.checkin.error')); + } + + public function testCanCheckInLicenseAssignedToAsset() + { + Event::fake([CheckoutableCheckedIn::class]); + + $asset = Asset::factory()->create(); + + $licenseSeat = LicenseSeat::factory() + ->reassignable() + ->assignedToAsset($asset) + ->create(); + + $actor = User::factory()->checkoutLicenses()->create(); + + $this->actingAs($actor) ->post(route('licenses.checkin.save', $licenseSeat), [ 'notes' => 'my note', 'redirect_option' => 'index', @@ -52,18 +76,31 @@ class LicenseCheckinTest extends TestCase $this->assertNull($licenseSeat->fresh()->asset_id); $this->assertNull($licenseSeat->fresh()->assigned_to); + $this->assertEquals('my note', $licenseSeat->fresh()->notes); + + Event::assertDispatchedTimes(CheckoutableCheckedIn::class, 1); + Event::assertDispatched(CheckoutableCheckedIn::class, function (CheckoutableCheckedIn $event) use ($actor, $asset, $licenseSeat) { + return $event->checkoutable->is($licenseSeat) + && $event->checkedOutTo->is($asset) + && $event->checkedInBy->is($actor) + && $event->note === 'my note'; + }); } public function testCanCheckInLicenseAssignedToUser() { + Event::fake([CheckoutableCheckedIn::class]); + + $user = User::factory()->create(); + $licenseSeat = LicenseSeat::factory() ->reassignable() - ->assignedToUser() + ->assignedToUser($user) ->create(); - $this->assertNotNull($licenseSeat->assigned_to); + $actor = User::factory()->checkoutLicenses()->create(); - $this->actingAs(User::factory()->checkoutLicenses()->create()) + $this->actingAs($actor) ->post(route('licenses.checkin.save', $licenseSeat), [ 'notes' => 'my note', 'redirect_option' => 'index', @@ -72,5 +109,14 @@ class LicenseCheckinTest extends TestCase $this->assertNull($licenseSeat->fresh()->asset_id); $this->assertNull($licenseSeat->fresh()->assigned_to); + $this->assertEquals('my note', $licenseSeat->fresh()->notes); + + Event::assertDispatchedTimes(CheckoutableCheckedIn::class, 1); + Event::assertDispatched(CheckoutableCheckedIn::class, function (CheckoutableCheckedIn $event) use ($actor, $licenseSeat, $user) { + return $event->checkoutable->is($licenseSeat) + && $event->checkedOutTo->is($user) + && $event->checkedInBy->is($actor) + && $event->note === 'my note'; + }); } }