238 lines
7.7 KiB
PHP
238 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Api\V1\SuperAdmin;
|
|
|
|
use App\Models\Barangay;
|
|
use App\Models\CityMunicipality;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use Database\Seeders\RoleSeeder;
|
|
use Database\Seeders\SamplePsgcSeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Laravel\Sanctum\Sanctum;
|
|
use MatanYadaev\EloquentSpatial\Objects\LineString;
|
|
use MatanYadaev\EloquentSpatial\Objects\Point;
|
|
use MatanYadaev\EloquentSpatial\Objects\Polygon;
|
|
use Tests\TestCase;
|
|
|
|
class SuperAdminBarangayTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private User $superAdmin;
|
|
|
|
private User $admin;
|
|
|
|
private Tenant $tenant;
|
|
|
|
private CityMunicipality $city;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->seed(RoleSeeder::class);
|
|
$this->seed(SamplePsgcSeeder::class);
|
|
|
|
$this->superAdmin = User::factory()->create([
|
|
'role' => User::ROLE_SUPER_ADMIN,
|
|
'status' => User::STATUS_ACTIVE,
|
|
]);
|
|
$this->superAdmin->assignRole(User::ROLE_SUPER_ADMIN);
|
|
|
|
$this->city = CityMunicipality::first();
|
|
|
|
// Create Tenant LGU with boundary polygon
|
|
$boundary = new Polygon([
|
|
new LineString([
|
|
new Point(14.0, 120.0, 4326),
|
|
new Point(15.0, 120.0, 4326),
|
|
new Point(15.0, 121.0, 4326),
|
|
new Point(14.0, 121.0, 4326),
|
|
new Point(14.0, 120.0, 4326),
|
|
]),
|
|
], 4326);
|
|
|
|
$this->tenant = $this->defaultTenant;
|
|
$this->tenant->update([
|
|
'city_municipality_id' => $this->city->id,
|
|
'boundary_polygon' => $boundary,
|
|
'contact_email' => 'admin@testlgu.gov.ph',
|
|
'contact_phone' => '+639171112233',
|
|
]);
|
|
|
|
$this->admin = User::factory()->create([
|
|
'tenant_id' => $this->tenant->id,
|
|
'role' => User::ROLE_ADMIN,
|
|
'status' => User::STATUS_ACTIVE,
|
|
]);
|
|
$this->admin->assignRole(User::ROLE_ADMIN);
|
|
}
|
|
|
|
public function test_guests_cannot_access_super_admin_barangay_endpoints(): void
|
|
{
|
|
$this->getJson('/api/v1/super-admin/barangays')->assertStatus(401);
|
|
}
|
|
|
|
public function test_lgu_admins_cannot_access_super_admin_barangay_endpoints(): void
|
|
{
|
|
Sanctum::actingAs($this->admin);
|
|
$this->getJson('/api/v1/super-admin/barangays')->assertStatus(403);
|
|
}
|
|
|
|
public function test_super_admin_can_list_barangays(): void
|
|
{
|
|
Sanctum::actingAs($this->superAdmin);
|
|
|
|
$response = $this->getJson('/api/v1/super-admin/barangays');
|
|
$response->assertStatus(200);
|
|
}
|
|
|
|
public function test_super_admin_can_create_barangay_inside_lgu_boundary_with_auto_codes(): void
|
|
{
|
|
Sanctum::actingAs($this->superAdmin);
|
|
|
|
$payload = [
|
|
'name' => 'New Barangay',
|
|
'city_municipality_id' => $this->city->id,
|
|
'urban_rural' => 'urban',
|
|
'population' => 4500,
|
|
// Drawn polygon boundary completely inside LGU (14.0 to 15.0 lat, 120.0 to 121.0 lng)
|
|
'boundary' => [
|
|
['lat' => 14.1, 'lng' => 120.1],
|
|
['lat' => 14.2, 'lng' => 120.1],
|
|
['lat' => 14.2, 'lng' => 120.2],
|
|
['lat' => 14.1, 'lng' => 120.2],
|
|
['lat' => 14.1, 'lng' => 120.1],
|
|
],
|
|
];
|
|
|
|
$response = $this->postJson('/api/v1/super-admin/barangays', $payload);
|
|
|
|
$response->assertStatus(201)
|
|
->assertJsonPath('data.name', 'New Barangay')
|
|
->assertJsonPath('data.urban_rural', 'urban')
|
|
->assertJsonPath('data.population', 4500);
|
|
|
|
$this->assertDatabaseHas('barangays', [
|
|
'name' => 'New Barangay',
|
|
'city_municipality_id' => $this->city->id,
|
|
]);
|
|
|
|
$barangay = Barangay::where('name', 'New Barangay')->first();
|
|
$this->assertNotEmpty($barangay->psgc_code);
|
|
$this->assertNotEmpty($barangay->code);
|
|
$this->assertNotNull($barangay->centroid);
|
|
}
|
|
|
|
public function test_super_admin_cannot_create_barangay_outside_lgu_boundary(): void
|
|
{
|
|
Sanctum::actingAs($this->superAdmin);
|
|
|
|
$payload = [
|
|
'name' => 'Outside Barangay',
|
|
'city_municipality_id' => $this->city->id,
|
|
'urban_rural' => 'rural',
|
|
'population' => 1000,
|
|
// Drawn polygon boundary completely outside LGU boundary (14.0 to 15.0 lat, 120.0 to 121.0 lng)
|
|
'boundary' => [
|
|
['lat' => 16.0, 'lng' => 122.0],
|
|
['lat' => 16.1, 'lng' => 122.0],
|
|
['lat' => 16.1, 'lng' => 122.1],
|
|
['lat' => 16.0, 'lng' => 122.1],
|
|
['lat' => 16.0, 'lng' => 122.0],
|
|
],
|
|
];
|
|
|
|
$response = $this->postJson('/api/v1/super-admin/barangays', $payload);
|
|
|
|
$response->assertStatus(422)
|
|
->assertJsonValidationErrors(['boundary']);
|
|
}
|
|
|
|
public function test_super_admin_can_update_barangay(): void
|
|
{
|
|
Sanctum::actingAs($this->superAdmin);
|
|
|
|
// Pre-create a barangay
|
|
$boundary = new Polygon([
|
|
new LineString([
|
|
new Point(14.1, 120.1, 4326),
|
|
new Point(14.2, 120.1, 4326),
|
|
new Point(14.2, 120.2, 4326),
|
|
new Point(14.1, 120.2, 4326),
|
|
new Point(14.1, 120.1, 4326),
|
|
]),
|
|
], 4326);
|
|
|
|
$barangay = Barangay::create([
|
|
'name' => 'Old Barangay',
|
|
'city_municipality_id' => $this->city->id,
|
|
'psgc_code' => '999999999999',
|
|
'code' => '9999999999999999',
|
|
'urban_rural' => 'unknown',
|
|
'population' => 500,
|
|
'boundary' => $boundary,
|
|
'centroid' => new Point(14.15, 120.15, 4326),
|
|
]);
|
|
|
|
$payload = [
|
|
'name' => 'Updated Barangay Name',
|
|
'city_municipality_id' => $this->city->id,
|
|
'urban_rural' => 'rural',
|
|
'population' => 800,
|
|
'boundary' => [
|
|
['lat' => 14.1, 'lng' => 120.1],
|
|
['lat' => 14.3, 'lng' => 120.1],
|
|
['lat' => 14.3, 'lng' => 120.3],
|
|
['lat' => 14.1, 'lng' => 120.3],
|
|
['lat' => 14.1, 'lng' => 120.1],
|
|
],
|
|
];
|
|
|
|
$response = $this->putJson("/api/v1/super-admin/barangays/{$barangay->id}", $payload);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonPath('data.name', 'Updated Barangay Name')
|
|
->assertJsonPath('data.urban_rural', 'rural')
|
|
->assertJsonPath('data.population', 800);
|
|
|
|
$this->assertDatabaseHas('barangays', [
|
|
'id' => $barangay->id,
|
|
'name' => 'Updated Barangay Name',
|
|
]);
|
|
}
|
|
|
|
public function test_super_admin_can_delete_barangay(): void
|
|
{
|
|
Sanctum::actingAs($this->superAdmin);
|
|
|
|
// Pre-create a barangay
|
|
$boundary = new Polygon([
|
|
new LineString([
|
|
new Point(14.1, 120.1, 4326),
|
|
new Point(14.2, 120.1, 4326),
|
|
new Point(14.2, 120.2, 4326),
|
|
new Point(14.1, 120.2, 4326),
|
|
new Point(14.1, 120.1, 4326),
|
|
]),
|
|
], 4326);
|
|
|
|
$barangay = Barangay::create([
|
|
'name' => 'To Delete Barangay',
|
|
'city_municipality_id' => $this->city->id,
|
|
'psgc_code' => '888888888888',
|
|
'code' => '8888888888888888',
|
|
'urban_rural' => 'unknown',
|
|
'population' => 100,
|
|
'boundary' => $boundary,
|
|
'centroid' => new Point(14.15, 120.15, 4326),
|
|
]);
|
|
|
|
$this->deleteJson("/api/v1/super-admin/barangays/{$barangay->id}")->assertStatus(200);
|
|
|
|
$this->assertSoftDeleted('barangays', [
|
|
'id' => $barangay->id,
|
|
]);
|
|
}
|
|
}
|