Files
Investidor-V2/tests/Feature/InvestmentTerritoryTest.php
T
2026-09-29 08:41:57 +02:00

116 lines
8.6 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Opportunity;
use App\Models\Sector;
use App\Models\User;
use App\Services\InvestmentCatalogue;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Tests\TestCase;
class InvestmentTerritoryTest extends TestCase
{
use RefreshDatabase;
public function test_confirmed_coordinates_create_a_marker_without_hiding_missing_records(): void
{
$manager = User::factory()->create();
config(['portal.managers' => [$manager->email]]);
$sector = Sector::create(['name' => 'Synthetic map', 'slug' => 'synthetic-map', 'is_active' => true]);
$record = Opportunity::create(['sector_id' => $sector->id, 'reference' => 'MAP-COORD', 'slug' => 'map-coord', 'title' => 'Synthetic marker', 'status' => 'published', 'published_at' => now()->subMinute()]);
$this->getJson('/api/v1/investment/map')->assertOk()->assertJsonPath('matched_count', 1)->assertJsonPath('complete', false)->assertJsonCount(1, 'unlocated');
$this->actingAs($manager);
$url = '/management/opportunities/'.$record->id.'/coordinates';
$payload = ['version' => 0, 'latitude' => -25.9, 'longitude' => 32.5, 'source_reference' => 'Synthetic coordinates for automated tests only', 'confirmed_site' => true];
$this->postJson($url, [...$payload, 'latitude' => 91])->assertUnprocessable();
$this->postJson($url, [...$payload, 'confirmed_site' => false])->assertUnprocessable();
$this->postJson($url, $payload)->assertOk()->assertJsonPath('version', 1);
$this->postJson($url, $payload)->assertConflict();
$this->getJson('/api/v1/investment/map')->assertOk()->assertJsonPath('complete', true)->assertJsonPath('features.0.properties.id', $record->id)->assertJsonPath('features.0.geometry.coordinates.0', 32.5);
$record->update(['status' => 'draft']);
$this->getJson('/api/v1/investment/map')->assertOk()->assertJsonCount(0, 'features')->assertJsonCount(0, 'unlocated');
}
public function test_public_cascade_filters_saved_locations_and_hides_unapproved_references(): void
{
$country = $this->location('country');
$province = $this->location('province', $country);
$district = $this->location('district', $province);
$other = $this->location('province', $country);
$private = $this->location('province', $country);
foreach ([$country, $province, $district, $other] as $id) {
DB::table('geo_locations')->where('id', $id)->update(['source_system' => 'cartografia-workbook', 'external_id' => '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:'.$id, 'data_classification' => 'public']);
}
$sector = Sector::create(['name' => 'Synthetic', 'slug' => 'synthetic', 'is_active' => true]);
$opportunity = Opportunity::create(['sector_id' => $sector->id, 'reference' => 'SYNTHETIC-PUBLIC', 'slug' => 'synthetic-public', 'title' => 'Synthetic', 'status' => 'published', 'published_at' => now()->subMinute(), 'primary_territory_id' => $district]);
$this->getJson('/api/v1/investment/territory-options?type=province')->assertOk()->assertJsonCount(2, 'data');
$this->getJson('/api/v1/investment/territory-options?type=district&parent_id='.$province)->assertOk()->assertJsonPath('data.0.id', $district);
$service = app(InvestmentCatalogue::class);
$this->assertSame([$opportunity->id], $service->query(['province_id' => $province])->pluck('id')->all());
$this->assertSame([], $service->query(['province_id' => $other])->pluck('id')->all());
$this->getJson('/oportunidades?province_id='.$other.'&district_id='.$district)->assertUnprocessable();
$this->getJson('/oportunidades?province_id='.$private)->assertUnprocessable();
$this->get('/oportunidades?province_id='.$province)->assertOk();
$this->assertCount(2, $service->context($opportunity->id)['primary_territory_path']);
$manager = User::factory()->create();
config(['portal.managers' => [$manager->email]]);
$this->actingAs($manager)->post('/management/opportunities', ['sector_id' => $sector->id, 'reference' => 'SYNTHETIC-CREATE', 'slug' => 'synthetic-create', 'title' => 'Synthetic create',
'status' => 'draft', 'is_featured' => false, 'estimated_investment_currency' => 'USD', 'province_id' => $province, 'district_id' => $district])->assertRedirect();
$this->assertDatabaseHas('opportunities', ['reference' => 'SYNTHETIC-CREATE', 'primary_territory_id' => $district, 'territory_version' => 1]);
$this->post('/management/clusters', ['code' => 'GEO-CREATE', 'name' => 'Synthetic cluster', 'sector' => 'Synthetic', 'status' => 'planned', 'province_id' => $province, 'district_id' => $district])->assertRedirect();
$this->assertDatabaseHas('economic_clusters', ['code' => 'GEO-CREATE', 'primary_territory_id' => $district]);
$manager->forceFill(['two_factor_secret' => Crypt::encryptString('SYNTHETIC'), 'two_factor_confirmed_at' => now()])->save();
$zone = $this->postJson('/api/v2/investment/admin/zones', ['code' => 'GEO-ZEE', 'name' => 'Synthetic ZEE', 'category' => 'zee', 'province_id' => $province, 'district_id' => $district])->assertCreated()->json('id');
$this->assertDatabaseHas('investment_zones', ['id' => $zone, 'primary_territory_id' => $district]);
$this->getJson('/api/v2/investment/admin/zones?province_id='.$province)->assertOk()->assertJsonPath('records.total', 1);
$this->getJson('/api/v2/investment/admin/zones?province_id='.$other)->assertOk()->assertJsonPath('records.total', 0);
$this->postJson('/management/territories/zones/'.$zone, ['version' => 1, 'path' => [$province]])->assertOk();
$this->assertDatabaseHas('investment_zones', ['id' => $zone, 'primary_territory_id' => $province, 'version_no' => 2, 'publication_status' => 'draft']);
$this->assertSame(2, DB::table('investment_zone_revisions')->where('zone_id', $zone)->count());
DB::table('geo_locations')->where('id', $province)->update(['data_classification' => 'internal']);
$this->getJson('/api/v1/investment/territory-options?type=district&parent_id='.$province)->assertOk()->assertJsonCount(0, 'data');
}
public function test_cascade_assignment_persists_and_rejects_mismatched_parent_and_stale_write(): void
{
$user = User::factory()->create();
config(['portal.managers' => [$user->email]]);
$this->actingAs($user);
$province = $this->location('province');
$other = $this->location('province');
$district = $this->location('district', $province);
$cluster = (string) Str::uuid();
DB::table('economic_clusters')->insert(['id' => $cluster, 'code' => 'SYNTHETIC', 'name' => 'Synthetic cluster', 'sector' => 'Synthetic', 'status' => 'planned']);
$this->getJson('/management/territory-subjects?kind=clusters&q=Synthetic')->assertOk()->assertJsonPath('data.0.id', $cluster);
$this->get('/management/territory-associations')->assertOk();
$this->getJson('/management/territory-options?type=district&parent_id='.$province)->assertOk()->assertJsonCount(1, 'data')->assertJsonPath('data.0.id', $district);
$this->getJson('/management/territory-options?type=district&parent_id='.$other)->assertOk()->assertJsonCount(0, 'data');
$url = '/management/territories/clusters/'.$cluster;
$this->postJson($url, ['version' => 0, 'path' => [$other, $district]])->assertUnprocessable();
$this->postJson($url, ['version' => 0, 'path' => [$province, $district]])->assertOk()->assertJsonPath('version', 1);
$this->getJson($url)->assertOk()->assertJsonPath('path.0.id', $district);
$this->assertDatabaseHas('economic_clusters', ['id' => $cluster, 'primary_territory_id' => $district]);
$this->postJson($url, ['version' => 0, 'path' => []])->assertConflict();
$this->postJson($url, ['version' => 1, 'path' => []])->assertOk();
$this->assertDatabaseHas('economic_clusters', ['id' => $cluster, 'primary_territory_id' => null]);
}
public function test_non_managers_cannot_read_internal_options(): void
{
config(['portal.managers' => []]);
$this->actingAs(User::factory()->create())->getJson('/management/territory-options?type=province')->assertForbidden();
}
private function location(string $type, ?string $parent = null): string
{
$id = (string) Str::uuid();
DB::table('geo_locations')->insert(['id' => $id, 'code' => $id, 'name' => 'Synthetic '.$id, 'location_type' => $type, 'parent_id' => $parent]);
return $id;
}
}