2020-04-30 06:50:12 -07:00
|
|
|
<?php
|
2021-11-30 20:09:29 -08:00
|
|
|
namespace Tests\Unit;
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2024-08-06 12:07:34 -07:00
|
|
|
use App\Models\Asset;
|
2020-04-30 06:50:12 -07:00
|
|
|
use App\Models\Category;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Component;
|
|
|
|
use App\Models\Location;
|
2024-08-06 12:07:34 -07:00
|
|
|
use App\Models\User;
|
2023-03-07 16:57:55 -08:00
|
|
|
use Tests\TestCase;
|
2020-04-30 06:50:12 -07:00
|
|
|
|
2023-03-07 16:57:55 -08:00
|
|
|
class ComponentTest extends TestCase
|
2020-04-30 06:50:12 -07:00
|
|
|
{
|
2021-12-02 19:43:15 -08:00
|
|
|
public function testAComponentBelongsToACompany()
|
2020-04-30 06:50:12 -07:00
|
|
|
{
|
2021-06-10 13:17:44 -07:00
|
|
|
$component = Component::factory()
|
2021-12-02 19:43:15 -08:00
|
|
|
->create(
|
|
|
|
[
|
|
|
|
'company_id' => Company::factory()->create()->id
|
|
|
|
]
|
|
|
|
);
|
2020-04-30 06:50:12 -07:00
|
|
|
$this->assertInstanceOf(Company::class, $component->company);
|
|
|
|
}
|
|
|
|
|
2021-12-02 19:43:15 -08:00
|
|
|
public function testAComponentHasALocation()
|
2020-04-30 06:50:12 -07:00
|
|
|
{
|
2021-06-10 13:17:44 -07:00
|
|
|
$component = Component::factory()
|
|
|
|
->create(['location_id' => Location::factory()->create()->id]);
|
2020-04-30 06:50:12 -07:00
|
|
|
$this->assertInstanceOf(Location::class, $component->location);
|
|
|
|
}
|
|
|
|
|
2021-12-02 19:43:15 -08:00
|
|
|
public function testAComponentBelongsToACategory()
|
2020-04-30 06:50:12 -07:00
|
|
|
{
|
2021-12-02 19:43:15 -08:00
|
|
|
$component = Component::factory()->ramCrucial4()
|
|
|
|
->create(
|
|
|
|
[
|
|
|
|
'category_id' =>
|
|
|
|
Category::factory()->create(
|
|
|
|
[
|
|
|
|
'category_type' => 'component'
|
|
|
|
]
|
|
|
|
)->id]);
|
2020-04-30 06:50:12 -07:00
|
|
|
$this->assertInstanceOf(Category::class, $component->category);
|
|
|
|
$this->assertEquals('component', $component->category->category_type);
|
|
|
|
}
|
2024-08-06 12:07:34 -07:00
|
|
|
|
|
|
|
public function test_num_checked_out_takes_does_not_scope_by_company()
|
|
|
|
{
|
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
|
|
|
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
|
|
|
|
|
|
|
$componentForCompanyA = Component::factory()->for($companyA)->create(['qty' => 5]);
|
|
|
|
$assetForCompanyB = Asset::factory()->for($companyB)->create();
|
|
|
|
|
|
|
|
// Ideally, we shouldn't have a component attached to an
|
|
|
|
// asset from a different company but alas...
|
|
|
|
$componentForCompanyA->assets()->attach($componentForCompanyA->id, [
|
|
|
|
'component_id' => $componentForCompanyA->id,
|
|
|
|
'assigned_qty' => 4,
|
|
|
|
'asset_id' => $assetForCompanyB->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->superuser()->create());
|
|
|
|
$this->assertEquals(4, $componentForCompanyA->fresh()->numCheckedOut());
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create());
|
|
|
|
$this->assertEquals(4, $componentForCompanyA->fresh()->numCheckedOut());
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->for($companyA)->create());
|
|
|
|
$this->assertEquals(4, $componentForCompanyA->fresh()->numCheckedOut());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_num_remaining_takes_company_scoping_into_account()
|
|
|
|
{
|
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
|
|
|
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
|
|
|
|
|
|
|
$componentForCompanyA = Component::factory()->for($companyA)->create(['qty' => 5]);
|
|
|
|
$assetForCompanyB = Asset::factory()->for($companyB)->create();
|
|
|
|
|
|
|
|
// Ideally, we shouldn't have a component attached to an
|
|
|
|
// asset from a different company but alas...
|
|
|
|
$componentForCompanyA->assets()->attach($componentForCompanyA->id, [
|
|
|
|
'component_id' => $componentForCompanyA->id,
|
|
|
|
'assigned_qty' => 4,
|
|
|
|
'asset_id' => $assetForCompanyB->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->superuser()->create());
|
|
|
|
$this->assertEquals(1, $componentForCompanyA->fresh()->numRemaining());
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create());
|
|
|
|
$this->assertEquals(1, $componentForCompanyA->fresh()->numRemaining());
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->for($companyA)->create());
|
|
|
|
$this->assertEquals(1, $componentForCompanyA->fresh()->numRemaining());
|
|
|
|
}
|
2020-04-30 06:50:12 -07:00
|
|
|
}
|