Skip to content

Commit dc5dedd

Browse files
authored
Merge pull request #15975 from marcusmoore/testing/license-checkin
Added tests around license checkin
2 parents f90dd9d + d49bfb5 commit dc5dedd

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

app/Http/Controllers/Licenses/LicenseCheckinController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function store(Request $request, $seatId = null, $backTo = null)
7171

7272
if (! $license->reassignable) {
7373
// Not allowed to checkin
74-
Session::flash('error', 'License not reassignable.');
74+
Session::flash('error', trans('admin/licenses/message.checkin.not_reassignable') . '.');
7575

7676
return redirect()->back()->withInput();
7777
}

database/factories/LicenseSeatFactory.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Asset;
66
use App\Models\License;
7+
use App\Models\LicenseSeat;
78
use App\Models\User;
89
use Illuminate\Database\Eloquent\Factories\Factory;
910

@@ -33,4 +34,18 @@ public function assignedToUser(User $user = null)
3334
];
3435
});
3536
}
37+
38+
public function reassignable()
39+
{
40+
return $this->afterMaking(function (LicenseSeat $seat) {
41+
$seat->license->update(['reassignable' => true]);
42+
});
43+
}
44+
45+
public function notReassignable()
46+
{
47+
return $this->afterMaking(function (LicenseSeat $seat) {
48+
$seat->license->update(['reassignable' => false]);
49+
});
50+
}
3651
}

resources/lang/en-US/admin/licenses/message.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
'checkin' => array(
5252
'error' => 'There was an issue checking in the license. Please try again.',
53+
'not_reassignable' => 'License not reassignable',
5354
'success' => 'The license was checked in successfully'
5455
),
5556

tests/Feature/Checkins/Ui/LicenseCheckinTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Tests\Feature\Checkins\Ui;
44

5+
use App\Events\CheckoutableCheckedIn;
6+
use App\Models\Asset;
57
use App\Models\LicenseSeat;
68
use App\Models\User;
9+
use Illuminate\Support\Facades\Event;
710
use Tests\TestCase;
811

912
class LicenseCheckinTest extends TestCase
@@ -17,10 +20,112 @@ public function testCheckingInLicenseRequiresCorrectPermission()
1720
->assertForbidden();
1821
}
1922

23+
public function testCannotCheckinNonReassignableLicense()
24+
{
25+
$licenseSeat = LicenseSeat::factory()
26+
->notReassignable()
27+
->assignedToUser()
28+
->create();
29+
30+
$this->actingAs(User::factory()->checkoutLicenses()->create())
31+
->post(route('licenses.checkin.save', $licenseSeat), [
32+
'notes' => 'my note',
33+
'redirect_option' => 'index',
34+
])
35+
->assertSessionHas('error', trans('admin/licenses/message.checkin.not_reassignable') . '.');
36+
37+
$this->assertNotNull($licenseSeat->fresh()->assigned_to);
38+
}
39+
40+
public function testCannotCheckinLicenseThatIsNotAssigned()
41+
{
42+
$licenseSeat = LicenseSeat::factory()
43+
->reassignable()
44+
->create();
45+
46+
$this->assertNull($licenseSeat->assigned_to);
47+
$this->assertNull($licenseSeat->asset_id);
48+
49+
$this->actingAs(User::factory()->checkoutLicenses()->create())
50+
->post(route('licenses.checkin.save', $licenseSeat), [
51+
'notes' => 'my note',
52+
'redirect_option' => 'index',
53+
])
54+
->assertSessionHas('error', trans('admin/licenses/message.checkin.error'));
55+
}
56+
57+
public function testCanCheckInLicenseAssignedToAsset()
58+
{
59+
Event::fake([CheckoutableCheckedIn::class]);
60+
61+
$asset = Asset::factory()->create();
62+
63+
$licenseSeat = LicenseSeat::factory()
64+
->reassignable()
65+
->assignedToAsset($asset)
66+
->create();
67+
68+
$actor = User::factory()->checkoutLicenses()->create();
69+
70+
$this->actingAs($actor)
71+
->post(route('licenses.checkin.save', $licenseSeat), [
72+
'notes' => 'my note',
73+
'redirect_option' => 'index',
74+
])
75+
->assertRedirect(route('licenses.index'));
76+
77+
$this->assertNull($licenseSeat->fresh()->asset_id);
78+
$this->assertNull($licenseSeat->fresh()->assigned_to);
79+
$this->assertEquals('my note', $licenseSeat->fresh()->notes);
80+
81+
Event::assertDispatchedTimes(CheckoutableCheckedIn::class, 1);
82+
Event::assertDispatched(CheckoutableCheckedIn::class, function (CheckoutableCheckedIn $event) use ($actor, $asset, $licenseSeat) {
83+
return $event->checkoutable->is($licenseSeat)
84+
&& $event->checkedOutTo->is($asset)
85+
&& $event->checkedInBy->is($actor)
86+
&& $event->note === 'my note';
87+
});
88+
}
89+
90+
public function testCanCheckInLicenseAssignedToUser()
91+
{
92+
Event::fake([CheckoutableCheckedIn::class]);
93+
94+
$user = User::factory()->create();
95+
96+
$licenseSeat = LicenseSeat::factory()
97+
->reassignable()
98+
->assignedToUser($user)
99+
->create();
100+
101+
$actor = User::factory()->checkoutLicenses()->create();
102+
103+
$this->actingAs($actor)
104+
->post(route('licenses.checkin.save', $licenseSeat), [
105+
'notes' => 'my note',
106+
'redirect_option' => 'index',
107+
])
108+
->assertRedirect(route('licenses.index'));
109+
110+
$this->assertNull($licenseSeat->fresh()->asset_id);
111+
$this->assertNull($licenseSeat->fresh()->assigned_to);
112+
$this->assertEquals('my note', $licenseSeat->fresh()->notes);
113+
114+
Event::assertDispatchedTimes(CheckoutableCheckedIn::class, 1);
115+
Event::assertDispatched(CheckoutableCheckedIn::class, function (CheckoutableCheckedIn $event) use ($actor, $licenseSeat, $user) {
116+
return $event->checkoutable->is($licenseSeat)
117+
&& $event->checkedOutTo->is($user)
118+
&& $event->checkedInBy->is($actor)
119+
&& $event->note === 'my note';
120+
});
121+
122+
}
123+
20124
public function testPageRenders()
21125
{
22126
$this->actingAs(User::factory()->superuser()->create())
23127
->get(route('licenses.checkin', LicenseSeat::factory()->assignedToUser()->create()->id))
24128
->assertOk();
129+
25130
}
26131
}

0 commit comments

Comments
 (0)