2023-06-07 14:22:22 -07:00
|
|
|
<?php
|
|
|
|
|
2024-06-04 10:48:53 -07:00
|
|
|
namespace Tests\Feature\Consumables\Api;
|
2023-06-07 14:22:22 -07:00
|
|
|
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Consumable;
|
|
|
|
use App\Models\User;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-06-03 16:54:59 -07:00
|
|
|
class ConsumableIndexTest extends TestCase
|
2023-06-07 14:22:22 -07:00
|
|
|
{
|
|
|
|
public function testConsumableIndexAdheresToCompanyScoping()
|
|
|
|
{
|
|
|
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
|
|
|
|
|
|
|
$consumableA = Consumable::factory()->for($companyA)->create();
|
|
|
|
$consumableB = Consumable::factory()->for($companyB)->create();
|
|
|
|
|
|
|
|
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
|
|
|
|
$userInCompanyA = $companyA->users()->save(User::factory()->viewConsumables()->make());
|
|
|
|
$userInCompanyB = $companyB->users()->save(User::factory()->viewConsumables()->make());
|
|
|
|
|
|
|
|
$this->settings->disableMultipleFullCompanySupport();
|
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($superUser)
|
|
|
|
->getJson(route('api.consumables.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseContainsInRows($consumableA)
|
|
|
|
->assertResponseContainsInRows($consumableB);
|
2023-06-07 14:22:22 -07:00
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($userInCompanyA)
|
|
|
|
->getJson(route('api.consumables.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseContainsInRows($consumableA)
|
|
|
|
->assertResponseContainsInRows($consumableB);
|
2023-06-07 14:22:22 -07:00
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($userInCompanyB)
|
|
|
|
->getJson(route('api.consumables.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseContainsInRows($consumableA)
|
|
|
|
->assertResponseContainsInRows($consumableB);
|
2023-06-07 14:22:22 -07:00
|
|
|
|
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($superUser)
|
|
|
|
->getJson(route('api.consumables.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseContainsInRows($consumableA)
|
|
|
|
->assertResponseContainsInRows($consumableB);
|
2023-06-07 14:22:22 -07:00
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($userInCompanyA)
|
|
|
|
->getJson(route('api.consumables.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseContainsInRows($consumableA)
|
|
|
|
->assertResponseDoesNotContainInRows($consumableB);
|
2023-06-07 14:22:22 -07:00
|
|
|
|
2023-06-22 17:37:30 -07:00
|
|
|
$this->actingAsForApi($userInCompanyB)
|
|
|
|
->getJson(route('api.consumables.index'))
|
2023-06-22 14:41:56 -07:00
|
|
|
->assertResponseDoesNotContainInRows($consumableA)
|
|
|
|
->assertResponseContainsInRows($consumableB);
|
2023-06-07 14:22:22 -07:00
|
|
|
}
|
2024-07-11 07:25:26 -07:00
|
|
|
|
|
|
|
public function testConsumableIndexReturnsExpectedSearchResults()
|
|
|
|
{
|
|
|
|
Consumable::factory()->count(10)->create();
|
|
|
|
Consumable::factory()->count(1)->create(['name' => 'My Test Consumable']);
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->getJson(
|
|
|
|
route('api.consumables.index', [
|
|
|
|
'search' => 'My Test Consumable',
|
|
|
|
'sort' => 'name',
|
|
|
|
'order' => 'asc',
|
|
|
|
'offset' => '0',
|
|
|
|
'limit' => '20',
|
|
|
|
]))
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'total',
|
|
|
|
'rows',
|
|
|
|
])
|
|
|
|
->assertJson([
|
|
|
|
'total' => 1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
2023-06-07 14:22:22 -07:00
|
|
|
}
|