2024-07-05 00:21:31 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Locations\Api;
|
|
|
|
|
|
|
|
use App\Models\Location;
|
|
|
|
use App\Models\User;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-07-05 00:29:00 -07:00
|
|
|
class CreateLocationsTest extends TestCase
|
2024-07-05 00:21:31 -07:00
|
|
|
{
|
|
|
|
|
2024-07-05 00:29:00 -07:00
|
|
|
public function testRequiresPermissionToCreateLocation()
|
|
|
|
{
|
|
|
|
$this->actingAsForApi(User::factory()->create())
|
|
|
|
->postJson(route('api.departments.store'))
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCannotCreateNewLocationsWithTheSameName()
|
2024-07-05 00:21:31 -07:00
|
|
|
{
|
|
|
|
$location = Location::factory()->create();
|
2024-07-05 00:29:00 -07:00
|
|
|
$location2 = Location::factory()->create();
|
2024-07-05 00:21:31 -07:00
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
2024-07-05 00:29:00 -07:00
|
|
|
->patchJson(route('api.locations.update', $location2), [
|
|
|
|
'name' => $location->name,
|
2024-07-05 00:21:31 -07:00
|
|
|
])
|
|
|
|
->assertOk()
|
2024-07-05 00:29:00 -07:00
|
|
|
->assertStatusMessageIs('error')
|
2024-07-05 00:21:31 -07:00
|
|
|
->assertStatus(200)
|
|
|
|
->json();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-07-05 00:29:00 -07:00
|
|
|
|
2024-07-05 03:29:30 -07:00
|
|
|
public function testUserCannotCreateLocationsThatAreTheirOwnParent()
|
|
|
|
{
|
|
|
|
$location = Location::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->patchJson(route('api.locations.update', $location), [
|
|
|
|
'parent_id' => $location->id,
|
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertStatusMessageIs('error')
|
|
|
|
->assertJson([
|
|
|
|
'messages' => [
|
|
|
|
'parent_id' => ['The parent id must not create a circular reference.'],
|
|
|
|
],
|
|
|
|
])
|
|
|
|
->json();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-05 00:21:31 -07:00
|
|
|
}
|