2024-09-16 16:40:10 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Checkins\Api;
|
|
|
|
|
|
|
|
use App\Models\Accessory;
|
2024-09-17 09:11:58 -07:00
|
|
|
use App\Models\Company;
|
2024-09-16 16:40:10 -07:00
|
|
|
use App\Models\User;
|
2024-09-17 09:11:58 -07:00
|
|
|
use Tests\Concerns\TestsFullMultipleCompaniesSupport;
|
2024-09-16 16:40:10 -07:00
|
|
|
use Tests\Concerns\TestsPermissionsRequirement;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-09-17 09:11:58 -07:00
|
|
|
class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement
|
2024-09-16 16:40:10 -07:00
|
|
|
{
|
|
|
|
public function testRequiresPermission()
|
|
|
|
{
|
|
|
|
$accessory = Accessory::factory()->checkedOutToUser()->create();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->create())
|
|
|
|
->postJson(route('api.accessories.checkin', $accessory))
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
2024-09-17 09:11:58 -07:00
|
|
|
|
|
|
|
public function testAdheresToFullMultipleCompaniesSupportScoping()
|
|
|
|
{
|
|
|
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
|
|
|
|
2024-09-18 11:18:21 -07:00
|
|
|
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
|
2024-09-17 09:11:58 -07:00
|
|
|
$userInCompanyA = User::factory()->for($companyA)->checkinAccessories()->create();
|
|
|
|
$accessoryForCompanyB = Accessory::factory()->for($companyB)->checkedOutToUser()->create();
|
2024-09-18 11:18:21 -07:00
|
|
|
$anotherAccessoryForCompanyB = Accessory::factory()->for($companyB)->checkedOutToUser()->create();
|
|
|
|
|
|
|
|
$this->assertEquals(1, $accessoryForCompanyB->checkouts->count());
|
|
|
|
$this->assertEquals(1, $anotherAccessoryForCompanyB->checkouts->count());
|
2024-09-17 09:11:58 -07:00
|
|
|
|
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
|
|
|
|
$this->actingAsForApi($userInCompanyA)
|
|
|
|
->postJson(route('api.accessories.checkin', $accessoryForCompanyB))
|
2024-09-18 11:18:21 -07:00
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
$this->actingAsForApi($superUser)
|
|
|
|
->postJson(route('api.accessories.checkin', $anotherAccessoryForCompanyB))
|
|
|
|
->assertStatusMessageIs('success');
|
2024-09-17 09:11:58 -07:00
|
|
|
|
2024-09-18 11:18:21 -07:00
|
|
|
$this->assertEquals(1, $accessoryForCompanyB->fresh()->checkouts->count(), 'Accessory should not be checked in');
|
|
|
|
$this->assertEquals(0, $anotherAccessoryForCompanyB->fresh()->checkouts->count(), 'Accessory should be checked in');
|
2024-09-17 09:11:58 -07:00
|
|
|
}
|
2024-09-18 11:19:41 -07:00
|
|
|
|
|
|
|
public function testCanCheckinAccessory()
|
|
|
|
{
|
|
|
|
$accessory = Accessory::factory()->checkedOutToUser()->create();
|
|
|
|
|
|
|
|
$this->assertEquals(1, $accessory->checkouts->count());
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->checkinAccessories()->create())
|
|
|
|
->postJson(route('api.accessories.checkin', $accessory))
|
|
|
|
->assertStatusMessageIs('success');
|
|
|
|
|
|
|
|
$this->assertEquals(0, $accessory->fresh()->checkouts->count(), 'Accessory should be checked in');
|
|
|
|
}
|
2024-09-16 16:40:10 -07:00
|
|
|
}
|