varias alteracoes
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-10-06 14:50:12 +02:00
parent 4f35313d06
commit e65cb26901
333 changed files with 54656 additions and 2058 deletions
+136 -3
View File
@@ -3,9 +3,12 @@
namespace Tests\Feature;
use App\Models\Opportunity;
use App\Models\InvestmentDossier;
use App\Models\Project;
use App\Models\Sector;
use App\Models\User;
use App\Services\InvestmentCatalogue;
use App\Services\InvestmentGeography;
use Database\Seeders\InvestmentAreaSeeder;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -25,7 +28,10 @@ class InvestmentTerritoryTest extends TestCase
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->getJson('/api/v1/investment/search')->assertOk()->assertJsonPath('total', 1)->assertJsonPath('data.0.id', $record->id);
$this->get('/oportunidades')->assertInertia(fn (AssertableInertia $page) => $page->where('results.total', 1)->where('results.data.0.id', $record->id));
$this->getJson('/api/v1/investment/map')->assertOk()->assertJsonPath('matched_count', 1)
->assertJsonPath('georeferenced_count', 0)->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];
@@ -33,9 +39,16 @@ class InvestmentTerritoryTest extends TestCase
$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);
$this->getJson('/api/v1/investment/search')->assertOk()->assertJsonPath('total', 1)->assertJsonPath('data.0.id', $record->id);
$this->get('/oportunidades')->assertInertia(fn (AssertableInertia $page) => $page->where('results.total', 1)->where('results.data.0.id', $record->id));
$this->getJson('/api/v1/investment/map')->assertOk()->assertJsonPath('matched_count', 1)
->assertJsonPath('georeferenced_count', 1)->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');
$this->getJson('/api/v1/investment/search')->assertOk()->assertJsonPath('total', 0)->assertJsonCount(0, 'data');
$this->get('/oportunidades')->assertInertia(fn (AssertableInertia $page) => $page->where('results.total', 0)->has('results.data', 0));
$this->getJson('/api/v1/investment/map')->assertOk()->assertJsonPath('matched_count', 0)->assertJsonPath('complete', true)
->assertJsonCount(0, 'features')->assertJsonCount(0, 'unlocated');
}
public function test_public_cascade_filters_saved_locations_and_hides_unapproved_references(): void
@@ -78,6 +91,126 @@ class InvestmentTerritoryTest extends TestCase
$this->getJson('/api/v1/investment/territory-options?type=district&parent_id='.$province)->assertOk()->assertJsonCount(0, 'data');
}
public function test_opportunities_match_hierarchical_and_multiple_public_locations_without_expanding(): void
{
$country = $this->location('country');
$provinceA = $this->location('province', $country);
$districtA = $this->location('district', $provinceA);
$postA = $this->location('admin_post', $districtA);
$localityA = $this->location('locality', $postA);
$provinceB = $this->location('province', $country);
$districtB = $this->location('district', $provinceB);
$postB = $this->location('admin_post', $districtB);
$localityB = $this->location('locality', $postB);
$ids = [$country, $provinceA, $districtA, $postA, $localityA, $provinceB, $districtB, $postB, $localityB];
foreach ($ids 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' => 'Territorial test', 'slug' => 'territorial-test', 'is_active' => true]);
$make = function (string $code, ?string $primary, string $scope = 'specific') use ($sector): Opportunity {
return Opportunity::create(['sector_id' => $sector->id, 'reference' => $code, 'slug' => strtolower($code), 'title' => $code,
'status' => 'published', 'published_at' => now()->subMinute(), 'primary_territory_id' => $primary, 'geography_scope' => $scope]);
};
$a = $make('AREA-A', $districtA);
$a->update(['estimated_investment_amount' => '1234.50', 'estimated_investment_currency' => 'USD']);
$multi = $make('AREA-MULTI', $localityB);
DB::table('opportunity_locations')->insert(['opportunity_id' => $multi->id, 'geo_location_id' => $localityA]);
$b = $make('AREA-B', $localityB);
$national = $make('AREA-NATIONAL', null, 'national');
$unlocated = $make('AREA-UNKNOWN', null, 'unspecified');
$service = app(InvestmentCatalogue::class);
$this->assertEqualsCanonicalizing([$a->id, $multi->id], $service->query(['province_id' => $provinceA])->pluck('id')->all());
$this->assertEqualsCanonicalizing([$a->id, $multi->id], $service->query(['province_id' => $provinceA, 'district_id' => $districtA])->pluck('id')->all());
$this->assertEqualsCanonicalizing([$multi->id], $service->query(['province_id' => $provinceA, 'district_id' => $districtA,
'admin_post_id' => $postA, 'locality_id' => $localityA])->pluck('id')->all());
$this->assertEqualsCanonicalizing([$a->id, $multi->id, $national->id], $service->query(['province_id' => $provinceA, 'include_national' => true])->pluck('id')->all());
$this->assertSame([], $service->query(['province_id' => $provinceB, 'sector' => 'no-such-sector'])->pluck('id')->all());
$this->assertEqualsCanonicalizing([$a->id, $multi->id, $b->id, $unlocated->id], $service->query([])->pluck('id')->all());
$this->assertSame(2, $service->query(['province_id' => $provinceA])->reorder()->count());
$map = $this->getJson('/api/v1/investment/map?province_id='.$provinceA)->assertOk()->assertJsonPath('matched_count', 2);
$this->assertEqualsCanonicalizing([$a->id, $multi->id], array_column($map->json('unlocated'), 'id'));
$export = $this->get('/api/v1/investment/export?province_id='.$provinceA)->assertOk();
$csv = $export->streamedContent();
$this->assertStringContainsString('Referência;Título;', $csv);
$this->assertStringContainsString('AREA-A', $csv);
$this->assertStringContainsString('AREA-MULTI', $csv);
$this->assertStringNotContainsString('AREA-B', $csv);
$this->assertStringContainsString('1234.50', $csv);
$this->get('/oportunidades?province_id='.$provinceA.'&page=1')->assertInertia(fn (AssertableInertia $page) => $page
->where('results.total', 2)->has('results.data', 2)->where('filters.province_id', $provinceA));
$this->getJson('/api/v1/investment/search?province_id='.$provinceA)->assertOk()->assertJsonPath('total', 2);
$this->assertNotContains($national->id, $service->query(['province_id' => $provinceA])->pluck('id')->all());
$this->assertNotContains($unlocated->id, $service->query(['province_id' => $provinceA])->pluck('id')->all());
$this->assertNotContains($national->id, $service->query([])->pluck('id')->all());
}
public function test_cluster_territory_uses_only_public_validated_project_links_and_project_locations(): void
{
$country = $this->location('country');
$provinceA = $this->location('province', $country);
$provinceB = $this->location('province', $country);
foreach ([$country, $provinceA, $provinceB] as $id) {
DB::table('geo_locations')->where('id', $id)->update([
'source_system' => 'cartografia-workbook',
'external_id' => '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:'.$id,
'data_classification' => 'public',
]);
}
$clusterIds = [];
foreach ([['MATCH', $provinceA, 'validated'], ['PENDING', $provinceB, 'pending']] as [$code, $province, $status]) {
$clusterId = (string) Str::uuid();
$projectId = Project::create(['code' => 'TERR-'.$code, 'name' => 'Project '.$code, 'status' => 'active'])->id;
DB::table('project_locations')->insert(['project_id' => $projectId, 'geo_location_id' => $province,
'role' => 'primary', 'data_classification' => 'public']);
DB::table('economic_clusters')->insert(['id' => $clusterId, 'code' => 'TERR-'.$code, 'name' => 'Cluster '.$code,
'sector' => 'Teste', 'status' => 'active']);
DB::table('cluster_projects')->insert(['cluster_id' => $clusterId, 'project_id' => $projectId, 'role' => 'anchor',
'data_classification' => 'public', 'association_type' => 'potential', 'source_reference' => 'Fonte de teste para ligação territorial',
'evidence_reference' => 'fixture:territory:'.$code, 'valid_from' => today()->toDateString(), 'validation_status' => $status]);
InvestmentDossier::create(['cluster_id' => $clusterId, 'title' => 'Dossier '.$code, 'content' => '{}', 'source' => 'fixture de teste']);
$clusterIds[] = $clusterId;
}
$ids = app(InvestmentGeography::class)->descendants($provinceA);
$matches = app(InvestmentGeography::class)->constrainDossiersToTerritories(InvestmentDossier::query(), $ids)
->pluck('cluster_id')->all();
$this->assertSame([$clusterIds[0]], $matches);
}
public function test_opportunity_pagination_retains_the_selected_territory(): void
{
$country = $this->location('country');
$province = $this->location('province', $country);
foreach ([$country, $province] 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' => 'Pagination area', 'slug' => 'pagination-area', 'is_active' => true]);
$ids = [];
for ($index = 1; $index <= 13; $index++) {
$record = Opportunity::create(['sector_id' => $sector->id, 'reference' => 'PAGE-'.$index,
'slug' => 'page-'.$index, 'title' => 'Page opportunity '.$index, 'status' => 'published',
'published_at' => now()->subMinute(), 'primary_territory_id' => $province, 'geography_scope' => 'specific']);
$ids[] = $record->id;
}
$this->get('/oportunidades?province_id='.$province.'&page=2')->assertInertia(fn (AssertableInertia $page) => $page
->where('results.total', 13)->has('results.data', 1)->where('results.data.0.id', $ids[0])
->where('filters.province_id', $province)
->where('results.prev_page_url', fn ($url) => str_contains($url, 'province_id='.$province)));
$this->getJson('/api/v1/investment/search?province_id='.$province.'&page=2')->assertOk()
->assertJsonPath('total', 13)->assertJsonCount(1, 'data')->assertJsonPath('data.0.id', $ids[0]);
}
public function test_cascade_assignment_persists_and_rejects_mismatched_parent_and_stale_write(): void
{
$user = User::factory()->create();