provincias
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-09-29 14:14:06 +02:00
parent e780e67e98
commit 67dad461d9
35 changed files with 1046 additions and 42 deletions
+94
View File
@@ -6,10 +6,13 @@ use App\Models\Opportunity;
use App\Models\Sector;
use App\Models\User;
use App\Services\InvestmentCatalogue;
use Database\Seeders\InvestmentAreaSeeder;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Inertia\Testing\AssertableInertia;
use Tests\TestCase;
class InvestmentTerritoryTest extends TestCase
@@ -105,6 +108,97 @@ class InvestmentTerritoryTest extends TestCase
$this->actingAs(User::factory()->create())->getJson('/management/territory-options?type=province')->assertForbidden();
}
public function test_opportunity_edit_saves_the_geographic_fields_with_the_record(): void
{
$manager = User::factory()->create();
config(['portal.managers' => [$manager->email]]);
$country = $this->location('country');
$province = $this->location('province', $country);
$sector = Sector::create(['name' => 'Synthetic edit', 'slug' => 'synthetic-edit', 'is_active' => true]);
$record = Opportunity::create(['sector_id' => $sector->id, 'reference' => 'EDIT-GEO', 'slug' => 'edit-geo', 'title' => 'Synthetic edit', 'status' => 'draft']);
$this->actingAs($manager)->put('/management/opportunities/'.$record->id, [
'sector_id' => $sector->id, 'reference' => 'EDIT-GEO', 'slug' => 'edit-geo', 'title' => 'Synthetic edit',
'status' => 'draft', 'is_featured' => false, 'estimated_investment_currency' => 'USD',
'province_id' => $province, 'territory_version' => 0,
])->assertRedirect();
$this->assertDatabaseHas('opportunities', ['id' => $record->id, 'primary_territory_id' => $province]);
$this->assertSame($province, $record->fresh()->primaryTerritory->id);
$this->get('/management/opportunities/'.$record->id.'/edit')->assertOk();
$this->getJson('/management/territory-options?type=locality&parent_id='.$province)->assertUnprocessable();
$this->getJson('/management/territory-options?type=district&parent_id=00000000-0000-4000-8000-000000000001')->assertUnprocessable();
$this->getJson('/management/territory-options?type=province&q='.$province)->assertOk()->assertJsonPath('data.0.id', $province);
$other = $this->location('province', $country);
$district = $this->location('district', $other);
$payload = ['sector_id' => $sector->id, 'reference' => 'EDIT-GEO', 'slug' => 'edit-geo', 'title' => 'Must not persist',
'status' => 'draft', 'is_featured' => false, 'estimated_investment_currency' => 'USD',
'province_id' => $province, 'district_id' => $district, 'territory_version' => 1];
$this->putJson('/management/opportunities/'.$record->id, $payload)->assertUnprocessable()->assertJsonValidationErrors('district_id');
$this->assertDatabaseHas('opportunities', ['id' => $record->id, 'title' => 'Synthetic edit', 'primary_territory_id' => $province]);
$this->putJson('/management/opportunities/'.$record->id, [...$payload, 'district_id' => null, 'territory_version' => 0])->assertConflict();
}
public function test_database_rejects_a_cycle_wrong_parent_and_duplicate_source_code(): void
{
$country = $this->location('country');
$province = $this->location('province', $country);
$district = $this->location('district', $province);
foreach ([
fn () => DB::table('geo_locations')->where('id', $province)->update(['parent_id' => $district]),
fn () => DB::table('geo_locations')->where('id', $district)->update(['parent_id' => $country]),
fn () => DB::table('geo_locations')->where('id', $country)->update(['parent_id' => $country]),
] as $invalid) {
try {
DB::transaction($invalid);
$this->fail('Invalid hierarchy accepted');
} catch (QueryException $error) {
$this->assertStringContainsString('ERROR', $error->getMessage());
}
}
DB::table('geo_locations')->where('id', $province)->update(['source_code' => '01']);
$other = $this->location('province', $country);
try {
DB::transaction(fn () => DB::table('geo_locations')->where('id', $other)->update(['source_code' => '01']));
$this->fail('Duplicate source code accepted');
} catch (QueryException $error) {
$this->assertStringContainsString('geo_source_parent_code_uq', $error->getMessage());
}
}
public function test_integrity_migration_reverses_without_data_and_preserves_imported_codes(): void
{
$migration = require database_path('migrations/2026_09_29_100000_territory_integrity.php');
$migration->down();
$migration->up();
$country = $this->location('country');
DB::table('geo_locations')->where('id', $country)->update(['source_code' => '508']);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Preserve imported source codes');
$migration->down();
}
public function test_background_options_do_not_consume_form_validation_feedback(): void
{
$manager = User::factory()->create();
config(['portal.managers' => [$manager->email]]);
$this->actingAs($manager)->from('/management/opportunities/create')
->post('/management/opportunities', [])->assertSessionHasErrors('reference');
$this->withCookie(config('session.cookie'), session()->getId())->withCredentials();
$this->getJson('/management/territory-options?type=province')->assertOk()->assertSessionHasErrors('reference');
$this->get('/management/opportunities/create')->assertInertia(fn (AssertableInertia $page) => $page->has('errors.reference'));
}
public function test_investment_areas_are_idempotent_and_preserve_legacy_sectors(): void
{
$old = Sector::create(['name' => 'Energia e Mineração', 'slug' => 'energia', 'is_active' => true]);
$this->seed(InvestmentAreaSeeder::class);
$count = Sector::count();
$this->seed(InvestmentAreaSeeder::class);
$this->assertSame($count, Sector::count());
$this->assertSame('Energia e Mineração', $old->fresh()->name);
$this->assertDatabaseHas('sectors', ['name' => 'Energia', 'slug' => 'energia-especifica']);
$this->assertDatabaseHas('sectors', ['name' => 'Mineração']);
}
private function location(string $type, ?string $parent = null): string
{
$id = (string) Str::uuid();