Add tests for license checkin
This commit is contained in:
parent
699476da90
commit
7aa5195e87
3 changed files with 54 additions and 12 deletions
|
@ -63,7 +63,6 @@ class LicenseCheckinController extends Controller
|
||||||
$license = License::find($licenseSeat->license_id);
|
$license = License::find($licenseSeat->license_id);
|
||||||
|
|
||||||
// LicenseSeat is not assigned, it can't be checked in
|
// LicenseSeat is not assigned, it can't be checked in
|
||||||
// @todo:
|
|
||||||
if (is_null($licenseSeat->assigned_to) && is_null($licenseSeat->asset_id)) {
|
if (is_null($licenseSeat->assigned_to) && is_null($licenseSeat->asset_id)) {
|
||||||
return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkin.error'));
|
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);
|
return redirect()->back()->withInput()->withErrors($validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @todo:
|
|
||||||
if($licenseSeat->assigned_to != null){
|
if($licenseSeat->assigned_to != null){
|
||||||
$return_to = User::find($licenseSeat->assigned_to);
|
$return_to = User::find($licenseSeat->assigned_to);
|
||||||
} else {
|
} else {
|
||||||
$return_to = Asset::find($licenseSeat->asset_id);
|
$return_to = Asset::find($licenseSeat->asset_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @todo:
|
|
||||||
// Update the asset data
|
// Update the asset data
|
||||||
$licenseSeat->assigned_to = null;
|
$licenseSeat->assigned_to = null;
|
||||||
$licenseSeat->asset_id = null;
|
$licenseSeat->asset_id = null;
|
||||||
|
@ -109,7 +106,6 @@ class LicenseCheckinController extends Controller
|
||||||
|
|
||||||
// Was the asset updated?
|
// Was the asset updated?
|
||||||
if ($licenseSeat->save()) {
|
if ($licenseSeat->save()) {
|
||||||
// @todo:
|
|
||||||
event(new CheckoutableCheckedIn($licenseSeat, $return_to, auth()->user(), $request->input('notes')));
|
event(new CheckoutableCheckedIn($licenseSeat, $return_to, auth()->user(), $request->input('notes')));
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -37,14 +37,14 @@ class LicenseSeatFactory extends Factory
|
||||||
|
|
||||||
public function reassignable()
|
public function reassignable()
|
||||||
{
|
{
|
||||||
return $this->afterCreating(function (LicenseSeat $seat) {
|
return $this->afterMaking(function (LicenseSeat $seat) {
|
||||||
$seat->license->update(['reassignable' => true]);
|
$seat->license->update(['reassignable' => true]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function notReassignable()
|
public function notReassignable()
|
||||||
{
|
{
|
||||||
return $this->afterCreating(function (LicenseSeat $seat) {
|
return $this->afterMaking(function (LicenseSeat $seat) {
|
||||||
$seat->license->update(['reassignable' => false]);
|
$seat->license->update(['reassignable' => false]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,11 @@
|
||||||
|
|
||||||
namespace Tests\Feature\Checkins\Ui;
|
namespace Tests\Feature\Checkins\Ui;
|
||||||
|
|
||||||
|
use App\Events\CheckoutableCheckedIn;
|
||||||
|
use App\Models\Asset;
|
||||||
use App\Models\LicenseSeat;
|
use App\Models\LicenseSeat;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
class LicenseCheckinTest extends TestCase
|
class LicenseCheckinTest extends TestCase
|
||||||
|
@ -34,16 +37,37 @@ class LicenseCheckinTest extends TestCase
|
||||||
$this->assertNotNull($licenseSeat->fresh()->assigned_to);
|
$this->assertNotNull($licenseSeat->fresh()->assigned_to);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCanCheckInLicenseAssignedToAsset()
|
public function testCannotCheckinLicenseThatIsNotAssigned()
|
||||||
{
|
{
|
||||||
$licenseSeat = LicenseSeat::factory()
|
$licenseSeat = LicenseSeat::factory()
|
||||||
->reassignable()
|
->reassignable()
|
||||||
->assignedToAsset()
|
|
||||||
->create();
|
->create();
|
||||||
|
|
||||||
$this->assertNotNull($licenseSeat->asset_id);
|
$this->assertNull($licenseSeat->assigned_to);
|
||||||
|
$this->assertNull($licenseSeat->asset_id);
|
||||||
|
|
||||||
$this->actingAs(User::factory()->checkoutLicenses()->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.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), [
|
->post(route('licenses.checkin.save', $licenseSeat), [
|
||||||
'notes' => 'my note',
|
'notes' => 'my note',
|
||||||
'redirect_option' => 'index',
|
'redirect_option' => 'index',
|
||||||
|
@ -52,18 +76,31 @@ class LicenseCheckinTest extends TestCase
|
||||||
|
|
||||||
$this->assertNull($licenseSeat->fresh()->asset_id);
|
$this->assertNull($licenseSeat->fresh()->asset_id);
|
||||||
$this->assertNull($licenseSeat->fresh()->assigned_to);
|
$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()
|
public function testCanCheckInLicenseAssignedToUser()
|
||||||
{
|
{
|
||||||
|
Event::fake([CheckoutableCheckedIn::class]);
|
||||||
|
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
$licenseSeat = LicenseSeat::factory()
|
$licenseSeat = LicenseSeat::factory()
|
||||||
->reassignable()
|
->reassignable()
|
||||||
->assignedToUser()
|
->assignedToUser($user)
|
||||||
->create();
|
->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), [
|
->post(route('licenses.checkin.save', $licenseSeat), [
|
||||||
'notes' => 'my note',
|
'notes' => 'my note',
|
||||||
'redirect_option' => 'index',
|
'redirect_option' => 'index',
|
||||||
|
@ -72,5 +109,14 @@ class LicenseCheckinTest extends TestCase
|
||||||
|
|
||||||
$this->assertNull($licenseSeat->fresh()->asset_id);
|
$this->assertNull($licenseSeat->fresh()->asset_id);
|
||||||
$this->assertNull($licenseSeat->fresh()->assigned_to);
|
$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';
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue