snipe-it/tests/unit/AssetTest.php

324 lines
12 KiB
PHP
Raw Normal View History

2016-03-25 01:18:05 -07:00
<?php
use App\Exceptions\CheckoutNotAllowed;
2016-03-25 01:18:05 -07:00
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Company;
use App\Models\Location;
use App\Models\User;
use Carbon\Carbon;
2016-03-25 01:18:05 -07:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Hash;
2016-03-25 01:18:05 -07:00
class AssetTest extends BaseTest
2016-03-25 01:18:05 -07:00
{
/**
* @var \UnitTester
*/
protected $tester;
public function testFailsEmptyValidation()
{
// An Asset requires a name, a qty, and a category_id.
$a = Asset::create();
$this->assertFalse($a->isValid());
$fields = [
'model_id' => 'model id',
'status_id' => 'status id',
'asset_tag' => 'asset tag',
];
$errors = $a->getErrors();
foreach ($fields as $field => $fieldTitle) {
$this->assertEquals($errors->get($field)[0], "The ${fieldTitle} field is required.");
}
}
2017-06-15 19:42:43 -07:00
public function testAutoIncrementMixed()
{
$expected = '123411';
$next = Asset::nextAutoIncrement(
collect([
['asset_tag' => '0012345'],
['asset_tag' => 'WTF00134'],
['asset_tag' => 'WTF-745'],
['asset_tag' => '0012346'],
['asset_tag' => '00123410'],
['asset_tag' => 'U8T7597h77'],
2017-06-15 19:42:43 -07:00
])
);
2017-06-15 19:42:43 -07:00
\Log::debug(print_r($next));
$this->assertEquals($expected, $next);
}
public function testAutoIncrementMixedFullTagNumber()
{
$expected = '123411';
$next = Asset::nextAutoIncrement(
[
['asset_tag' => '0012345'],
['asset_tag' => 'WTF00134'],
['asset_tag' => 'WTF-745'],
['asset_tag' => '0012346'],
['asset_tag' => '00123410'],
['asset_tag' => 'U8T7597h77'],
]
);
$this->assertEquals($expected, $next);
}
/**
* @test
*/
public function testWarrantyExpiresAttribute()
{
2021-06-10 13:17:44 -07:00
$asset = Asset::factory()->laptopMbp()->create([
'model_id' => $this->createValidAssetModel()->id,
'supplier_id' => $this->createvalidSupplier()->id,
'rtd_location_id' => $this->createValidLocation()->id,
]);
$asset->purchase_date = Carbon::createFromDate(2017, 1, 1)->hour(0)->minute(0)->second(0);
$asset->warranty_months = 24;
$asset->save();
$saved_asset = Asset::find($asset->id);
$this->tester->assertInstanceOf(\DateTime::class, $saved_asset->purchase_date);
$this->tester->assertEquals(
Carbon::createFromDate(2017, 1, 1)->format('Y-m-d'),
$saved_asset->purchase_date->format('Y-m-d')
);
$this->tester->assertEquals(
Carbon::createFromDate(2017, 1, 1)->setTime(0, 0, 0),
$saved_asset->purchase_date
);
$this->tester->assertEquals(24, $saved_asset->warranty_months);
$this->tester->assertInstanceOf(\DateTime::class, $saved_asset->warranty_expires);
$this->tester->assertEquals(
Carbon::createFromDate(2019, 1, 1)->format('Y-m-d'),
$saved_asset->warranty_expires->format('Y-m-d')
);
$this->tester->assertEquals(
Carbon::createFromDate(2019, 1, 1)->setTime(0, 0, 0),
$saved_asset->warranty_expires
);
}
public function testModelIdMustExist()
{
$model = $this->createValidAssetModel();
2021-06-10 13:17:44 -07:00
$asset = Asset::factory()->make([
'model_id' => $model->id,
'supplier_id' => $this->createValidSupplier()->id,
'rtd_location_id' => $this->createValidLocation()->id,
]);
$asset->save();
$this->assertTrue($asset->isValid());
$newId = $model->id + 1;
2021-06-10 13:17:44 -07:00
$asset = Asset::factory()->make(['model_id' => $newId]);
$asset->save();
$this->assertFalse($asset->isValid());
}
public function testAnAssetHasRelationships()
{
2021-06-10 13:17:44 -07:00
$asset = Asset::factory()->laptopMbp()
->create([
'model_id' => $this->createValidAssetModel()->id,
'company_id' => $this->createValidCompany()->id,
'supplier_id' => $this->createValidSupplier()->id,
]);
$this->assertInstanceOf(AssetModel::class, $asset->model);
$this->assertInstanceOf(Company::class, $asset->company);
$this->assertInstanceOf(App\Models\Depreciation::class, $asset->depreciation);
$this->assertInstanceOf(App\Models\Statuslabel::class, $asset->assetstatus);
$this->assertInstanceOf(App\Models\Supplier::class, $asset->supplier);
}
public function testAnAssetCanBeAvailableForCheckout()
{
// Logic: If the asset is not assigned to anyone,
// and the statuslabel type is "deployable"
// and the asset is not deleted
// Then it is available for checkout
// An asset assigned to someone should not be available for checkout.
2021-06-10 13:17:44 -07:00
$assetAssigned = Asset::factory()->laptopMbp()->assignedToUser()
->create(['model_id' => $this->createValidAssetModel()]);
$this->assertFalse($assetAssigned->availableForCheckout());
// An asset with a non deployable statuslabel should not be available for checkout.
2021-06-10 13:17:44 -07:00
$assetUndeployable = Asset::factory()->create([
'status_id' => $this->createValidStatuslabel('archived')->id,
'model_id' => $this->createValidAssetModel(),
]);
$this->assertFalse($assetUndeployable->availableForCheckout());
// An asset that has been deleted is not avaiable for checkout.
2021-06-10 13:17:44 -07:00
$assetDeleted = Asset::factory()->deleted()->create([
'model_id' => $this->createValidAssetModel(),
]);
$this->assertFalse($assetDeleted->availableForCheckout());
// A ready to deploy asset that isn't assigned to anyone is available for checkout
2021-06-10 13:17:44 -07:00
$asset = Asset::factory()->create([
'status_id' => $this->createValidStatuslabel('rtd')->id,
'model_id' => $this->createValidAssetModel(),
]);
$this->assertTrue($asset->availableForCheckout());
}
public function testAnAssetCanHaveComponents()
{
$asset = $this->createValidAsset();
2021-06-10 13:17:44 -07:00
$components = \App\Models\Component::factory()->count(5)->ramCrucial4()->create([
'category_id' => $this->createValidCategory('component-hdd-category')->id,
]);
$components->each(function ($component) use ($asset) {
$component->assets()->attach($component, [
'asset_id'=>$asset->id,
]);
});
$this->assertInstanceOf(App\Models\Component::class, $asset->components()->first());
$this->assertCount(5, $asset->components);
}
public function testAnAssetCanHaveUploads()
{
$asset = $this->createValidAsset([
'supplier_id' => $this->createValidSupplier()->id,
]);
$this->assertCount(0, $asset->uploads);
2021-06-10 13:17:44 -07:00
\App\Models\Actionlog::factory()->count('asset-upload')->create(['item_id' => $asset->id]);
$this->assertCount(1, $asset->fresh()->uploads);
}
// Helper Method for checking in assets.... We should extract this to the model or a trait.
private function checkin($asset, $target)
{
$asset->expected_checkin = null;
$asset->last_checkout = null;
$asset->assigned_to = null;
$asset->assigned_type = null;
$asset->location_id = $asset->rtd_location_id;
$asset->assignedTo()->disassociate($asset);
$asset->accepted = null;
$asset->save();
$asset->logCheckin($target, 'Test Checkin');
}
public function testAnAssetCanBeCheckedOut()
{
// This tests Asset::checkOut(), Asset::assignedTo(), Asset::assignedAssets(), Asset::assetLoc(), Asset::assignedType(), defaultLoc()
$asset = $this->createValidAsset();
$adminUser = $this->signIn();
2021-06-10 13:17:44 -07:00
$target = \App\Models\User::factory()->create([
'location_id' => \App\Models\Location::factory()->create(),
]);
// An Asset Can be checked out to a user, and this should be logged.
$asset->checkOut($target, $adminUser);
$asset->save();
$this->assertInstanceOf(App\Models\User::class, $asset->assignedTo);
$this->assertEquals($asset->location->id, $target->userLoc->id);
$this->assertEquals('user', $asset->assignedType());
$this->assertEquals($asset->defaultLoc->id, $asset->rtd_location_id);
$this->tester->seeRecord('action_logs', [
'action_type' => 'checkout',
'target_type' => get_class($target),
'target_id' => $target->id,
]);
$this->tester->seeRecord('assets', [
'id' => $asset->id,
'assigned_to' => $target->id,
'assigned_type' => User::class,
]);
$this->checkin($asset, $target);
$this->assertNull($asset->fresh()->assignedTo);
$this->tester->seeRecord('action_logs', [
'action_type' => 'checkin from',
'target_type' => get_class($target),
'target_id' => $target->id,
]);
$this->tester->seeRecord('assets', [
'id' => $asset->id,
'assigned_to' => null,
'assigned_type' => null,
]);
// An Asset Can be checked out to a asset, and this should be logged.
$target = $this->createValidAsset();
$asset->checkOut($target, $adminUser);
$asset->save();
$this->assertInstanceOf(App\Models\Asset::class, $asset->fresh()->assignedTo);
$this->assertEquals($asset->fresh()->location->id, $target->fresh()->location->id);
$this->assertEquals('asset', $asset->assignedType());
$this->assertEquals($asset->defaultLoc->id, $asset->rtd_location_id);
$this->tester->seeRecord('action_logs', [
'action_type' => 'checkout',
'target_type' => get_class($target),
'target_id' => $target->id,
]);
$this->assertCount(1, $target->assignedAssets);
$this->checkin($asset, $target);
$this->assertNull($asset->fresh()->assignedTo);
$this->tester->seeRecord('action_logs', [
'action_type' => 'checkin from',
'target_type' => get_class($target),
'target_id' => $target->id,
]);
// An Asset cannot be checked out to itself.
$target = $this->createValidAsset();
$this->expectException(CheckoutNotAllowed::class);
$target->checkOut($target, $adminUser);
2020-04-29 08:01:52 -07:00
// An Asset Can be checked out to a location, and this should be logged.
$target = $this->createValidLocation();
$asset->checkOut($target, $adminUser);
$asset->save();
$this->assertInstanceOf(App\Models\Location::class, $asset->fresh()->assignedTo);
$this->assertEquals($asset->fresh()->location->id, $target->fresh()->id);
$this->assertEquals('location', $asset->assignedType());
$this->assertEquals($asset->defaultLoc->id, $asset->rtd_location_id);
$this->tester->seeRecord('action_logs', [
'action_type' => 'checkout',
'target_type' => get_class($target),
'target_id' => $target->id,
]);
$this->checkin($asset, $target);
$this->assertNull($asset->fresh()->assignedTo);
$this->tester->seeRecord('action_logs', [
'action_type' => 'checkin from',
'target_type' => get_class($target),
'target_id' => $target->id,
]);
}
public function testAnAssetHasMaintenances()
{
$asset = $this->createValidAsset();
2021-06-10 13:17:44 -07:00
\App\Models\AssetMaintenance::factory()->create(['asset_id' => $asset->id]);
$this->assertCount(1, $asset->assetmaintenances);
}
2016-03-25 01:18:05 -07:00
}