fix(portal): preserve territorial totals and legacy redirect context
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Services\InvestmentGeography;
|
||||
use App\Support\IsolatedQaDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
/** Temporary tables shadow QA tables on this connection only; no migrations or permanent fixture writes. */
|
||||
class HomeTerritorialStatisticsTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
if (! IsolatedQaDatabase::allowsSchemaChanges()) {
|
||||
throw new \LogicException('Explicit isolated QA identity required for temporary fixtures.');
|
||||
}
|
||||
DB::beginTransaction();
|
||||
foreach ([
|
||||
'opportunities' => 'id bigint, status text, published_at timestamptz, deleted_at timestamptz, owner_user_id bigint, workflow_context jsonb, primary_territory_id uuid, province text, geography_scope text',
|
||||
'opportunity_locations' => 'opportunity_id bigint, geo_location_id uuid',
|
||||
'geo_locations' => 'id uuid, parent_id uuid, name text, location_type text, deleted_at timestamptz, data_classification text, source_system text, external_id text',
|
||||
'geo_territory_profiles' => 'geo_location_id uuid, source_id uuid, status text, valid_from date, valid_to date',
|
||||
'geo_sources' => 'id uuid, status text',
|
||||
'audit_events' => 'id bigint, source_system text, after_data jsonb, occurred_at timestamptz',
|
||||
] as $table => $columns) {
|
||||
DB::statement('CREATE TEMP TABLE '.$table.' ('.$columns.') ON COMMIT DROP');
|
||||
}
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if (DB::transactionLevel() > 0) {
|
||||
DB::rollBack();
|
||||
}
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
private function territory(string $name, string $type, ?string $parent = null, array $attributes = []): string
|
||||
{
|
||||
$id = (string) Str::uuid();
|
||||
DB::table('geo_locations')->insert(array_merge([
|
||||
'id' => $id, 'parent_id' => $parent, 'name' => $name, 'location_type' => $type,
|
||||
'deleted_at' => null, 'data_classification' => 'public', 'source_system' => 'cartografia-workbook',
|
||||
'external_id' => '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:'.$id,
|
||||
], $attributes));
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
private function opportunity(int $id, array $attributes = []): void
|
||||
{
|
||||
DB::table('opportunities')->insert(array_merge([
|
||||
'id' => $id, 'status' => 'published', 'published_at' => now()->subMinute(),
|
||||
'deleted_at' => null, 'owner_user_id' => null, 'workflow_context' => null,
|
||||
'primary_territory_id' => null, 'province' => null, 'geography_scope' => 'unspecified',
|
||||
], $attributes));
|
||||
}
|
||||
|
||||
private function statistics(): array
|
||||
{
|
||||
return app(InvestmentGeography::class)->opportunityStatistics();
|
||||
}
|
||||
|
||||
public function test_multiple_territories_and_repeated_links_do_not_multiply_the_opportunity_total(): void
|
||||
{
|
||||
$country = $this->territory('QA country', 'country');
|
||||
$first = $this->territory('QA province A', 'province', $country);
|
||||
$other = $this->territory('QA province B', 'province', $country);
|
||||
$district = $this->territory('QA district', 'district', $first);
|
||||
$this->opportunity(1, ['primary_territory_id' => $district, 'province' => 'Stale declaration']);
|
||||
foreach ([$district, $district, $first, $other, $other] as $id) {
|
||||
DB::table('opportunity_locations')->insert(['opportunity_id' => 1, 'geo_location_id' => $id]);
|
||||
}
|
||||
$this->assertSame(['opportunities' => 1, 'provinces' => 2], $this->statistics());
|
||||
$this->opportunity(2, ['primary_territory_id' => $first]);
|
||||
$this->opportunity(3);
|
||||
$this->assertSame(['opportunities' => 3, 'provinces' => 2], $this->statistics());
|
||||
}
|
||||
|
||||
public function test_publication_guards_apply_to_both_counts(): void
|
||||
{
|
||||
$country = $this->territory('QA country', 'country');
|
||||
$public = $this->territory('QA public province', 'province', $country);
|
||||
$hidden = $this->territory('QA unpublished coverage', 'province', $country);
|
||||
$this->opportunity(1, ['primary_territory_id' => $public]);
|
||||
foreach ([
|
||||
['status' => 'draft'], ['published_at' => now()->addDay()], ['published_at' => null],
|
||||
['deleted_at' => now()], ['owner_user_id' => 9, 'workflow_context' => '{"publication":{"visibility":"restricted"}}'],
|
||||
] as $index => $attributes) {
|
||||
$this->opportunity($index + 2, ['primary_territory_id' => $hidden, ...$attributes]);
|
||||
}
|
||||
$this->opportunity(20, ['primary_territory_id' => $hidden]);
|
||||
DB::table('audit_events')->insert(['id' => 1, 'source_system' => 'opportunity-origin-review', 'occurred_at' => now(),
|
||||
'after_data' => '{"opportunity_id":20,"classification":"example"}']);
|
||||
$this->assertSame(['opportunities' => 1, 'provinces' => 1], $this->statistics());
|
||||
}
|
||||
|
||||
public function test_no_territory_and_national_scope_do_not_invent_provincial_coverage(): void
|
||||
{
|
||||
$country = $this->territory('QA country', 'country');
|
||||
$this->territory('QA known province', 'province', $country);
|
||||
$this->opportunity(1);
|
||||
$this->opportunity(2, ['province' => 'QA known province', 'geography_scope' => 'national']);
|
||||
$this->opportunity(3, ['workflow_context' => '{"corridor_agribusiness":{"corridor":"Corredor de Maputo"}}']);
|
||||
$this->assertSame(['opportunities' => 3, 'provinces' => 0], $this->statistics());
|
||||
}
|
||||
|
||||
public function test_legacy_names_require_a_public_province_and_cannot_override_canonical_links(): void
|
||||
{
|
||||
$country = $this->territory('QA country', 'country');
|
||||
$first = $this->territory('QA province A', 'province', $country);
|
||||
$this->territory('QA province B', 'province', $country);
|
||||
$this->opportunity(1, ['province' => ' qa PROVINCE A ']);
|
||||
$this->opportunity(2, ['province' => 'Unknown province']);
|
||||
$this->opportunity(3, ['province' => ' ']);
|
||||
$this->opportunity(4, ['province' => 'QA province B', 'primary_territory_id' => $first]);
|
||||
$this->opportunity(5, ['province' => 'QA province B']);
|
||||
DB::table('opportunity_locations')->insert(['opportunity_id' => 5, 'geo_location_id' => $first]);
|
||||
$this->assertSame(['opportunities' => 5, 'provinces' => 1], $this->statistics());
|
||||
}
|
||||
|
||||
public function test_private_removed_broken_and_cyclic_ancestry_are_not_counted(): void
|
||||
{
|
||||
$country = $this->territory('QA country', 'country');
|
||||
$private = $this->territory('QA private', 'province', $country, ['data_classification' => 'restricted']);
|
||||
$child = $this->territory('QA private child', 'district', $private);
|
||||
$removed = $this->territory('QA removed', 'province', $country, ['deleted_at' => now()]);
|
||||
$broken = $this->territory('QA broken', 'province', (string) Str::uuid());
|
||||
$cycle = $this->territory('QA cycle', 'province');
|
||||
DB::table('geo_locations')->where('id', $cycle)->update(['parent_id' => $cycle]);
|
||||
foreach ([$private, $child, $removed, $broken, $cycle] as $index => $id) {
|
||||
$this->opportunity($index + 1, ['primary_territory_id' => $id]);
|
||||
}
|
||||
$this->assertSame(['opportunities' => 5, 'provinces' => 0], $this->statistics());
|
||||
}
|
||||
|
||||
public function test_city_and_province_are_distinct_but_duplicate_province_names_are_not_added(): void
|
||||
{
|
||||
$country = $this->territory('QA country', 'country');
|
||||
foreach (['Maputo Cidade', 'Maputo Província', ' maputo cidade '] as $index => $name) {
|
||||
$this->opportunity($index + 1, ['primary_territory_id' => $this->territory($name, 'province', $country)]);
|
||||
}
|
||||
$this->assertSame(['opportunities' => 3, 'provinces' => 2], $this->statistics());
|
||||
}
|
||||
|
||||
public function test_empty_catalogue_returns_zeroes_and_query_count_does_not_grow_with_links(): void
|
||||
{
|
||||
DB::enableQueryLog();
|
||||
DB::flushQueryLog();
|
||||
$this->assertSame(['opportunities' => 0, 'provinces' => 0], $this->statistics());
|
||||
$emptyCount = count(DB::getQueryLog());
|
||||
DB::disableQueryLog();
|
||||
$country = $this->territory('QA country', 'country');
|
||||
$province = $this->territory('QA province', 'province', $country);
|
||||
for ($id = 1; $id <= 120; $id++) {
|
||||
$this->opportunity($id, ['primary_territory_id' => $province]);
|
||||
DB::table('opportunity_locations')->insert(['opportunity_id' => $id, 'geo_location_id' => $province]);
|
||||
}
|
||||
DB::enableQueryLog();
|
||||
DB::flushQueryLog();
|
||||
$this->assertSame(['opportunities' => 120, 'provinces' => 1], $this->statistics());
|
||||
$this->assertSame($emptyCount, count(DB::getQueryLog()));
|
||||
$this->assertLessThanOrEqual(2, $emptyCount);
|
||||
DB::disableQueryLog();
|
||||
}
|
||||
}
|
||||
@@ -20,15 +20,15 @@ class PublicPortalNavigationTest extends TestCase
|
||||
Opportunity::create(['sector_id' => $sector->id, 'reference' => 'CACHE-1', 'title' => 'Solar',
|
||||
'slug' => 'solar', 'status' => 'published', 'published_at' => now()->subMinute(), 'is_featured' => true]);
|
||||
$first = $this->getJson('/api/v1/home')->assertOk()->json();
|
||||
$cached = Cache::get('portal_home_sector_organization_v1_pt');
|
||||
$cached = Cache::get('portal_home_territorial_v2_pt');
|
||||
$this->assertIsArray($cached['sectors']);
|
||||
$this->assertIsArray($cached['featuredOpportunities']);
|
||||
$restored = unserialize(serialize($cached), ['allowed_classes' => false]);
|
||||
$this->assertSame($cached, $restored);
|
||||
Cache::put('portal_home_sector_organization_v1_pt', $restored);
|
||||
Cache::put('portal_home_territorial_v2_pt', $restored);
|
||||
$this->assertSame($first, $this->getJson('/api/v1/home')->assertOk()->json());
|
||||
$sector->update(['name' => 'Energia actualizada']);
|
||||
$this->assertNull(Cache::get('portal_home_sector_organization_v1_pt'));
|
||||
$this->assertNull(Cache::get('portal_home_territorial_v2_pt'));
|
||||
$this->getJson('/api/v1/home')->assertJsonPath('data.sectors.0.name', 'Energia actualizada');
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Http\Controllers\Api\V1\HomeController;
|
||||
use App\Models\Opportunity;
|
||||
use App\Models\Sector;
|
||||
use Illuminate\Foundation\Testing\TestCase;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
|
||||
class HomeTerritorialCacheTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
config(['cache.default' => 'array']);
|
||||
DB::shouldReceive('connection')->never();
|
||||
}
|
||||
|
||||
public function test_controller_reads_the_new_plain_data_cache_instead_of_the_legacy_metric(): void
|
||||
{
|
||||
$current = ['stats' => ['opportunities' => 1, 'provinces' => 2, 'sectors' => 1], 'sectors' => [], 'featuredOpportunities' => []];
|
||||
Cache::put('portal_home_sector_organization_v1_pt', ['stats' => ['opportunities' => 99, 'provinces' => 1]]);
|
||||
Cache::put('portal_home_territorial_v2_pt', unserialize(serialize($current), ['allowed_classes' => false]));
|
||||
$response = app(HomeController::class)->show(Request::create('/api/v1/home?locale=pt'));
|
||||
|
||||
$this->assertSame($current, $response->getData(true)['data']);
|
||||
}
|
||||
|
||||
public function test_existing_model_events_invalidate_both_locales_of_the_new_cache(): void
|
||||
{
|
||||
foreach ([Opportunity::class, Sector::class] as $model) {
|
||||
foreach (['saved', 'deleted'] as $event) {
|
||||
Cache::put('portal_home_territorial_v2_pt', ['stats' => []]);
|
||||
Cache::put('portal_home_territorial_v2_en', ['stats' => []]);
|
||||
Event::dispatch('eloquent.'.$event.': '.$model, [new $model]);
|
||||
$this->assertNull(Cache::get('portal_home_territorial_v2_pt'));
|
||||
$this->assertNull(Cache::get('portal_home_territorial_v2_en'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Illuminate\Foundation\Testing\TestCase;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
|
||||
class JointDeliveryContextTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
DB::shouldReceive('connection')->never();
|
||||
}
|
||||
|
||||
/** @return array<string, array{string, string, string, string}> */
|
||||
public static function entries(): array
|
||||
{
|
||||
$entries = [];
|
||||
foreach (['GET', 'HEAD'] as $method) {
|
||||
foreach ([
|
||||
'/simulador' => ['/simulador-fiscal', 'services.simulator'],
|
||||
'/simulador/' => ['/simulador-fiscal', 'services.simulator'],
|
||||
'/contactos' => ['/apoio', 'services.support'],
|
||||
'/contactos/' => ['/apoio', 'services.support'],
|
||||
] as $path => [$destination, $name]) {
|
||||
$entries[$method.' '.$path] = [$method, $path, $destination, $name];
|
||||
}
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
|
||||
#[DataProvider('entries')]
|
||||
public function test_both_aliases_preserve_context_and_resolve_the_existing_main_destination(string $method, string $path, string $destination, string $name): void
|
||||
{
|
||||
$query = [
|
||||
'locale' => 'en', 'dossier' => 'qa-existing-dossier', 'opportunity_id' => '42',
|
||||
'context' => ['territory_id' => 'qa-territory', 'origin' => 'carteira', 'return_to' => '/investidor/dossiers/qa-existing-dossier?stage=S08'],
|
||||
'q' => 'Maputo & Zambézia / investimento',
|
||||
'redirect' => 'https://example.invalid/untrusted-destination',
|
||||
];
|
||||
$request = Request::create('https://portal-investidor.test'.$path.'?'.http_build_query($query, '', '&', PHP_QUERY_RFC3986), $method);
|
||||
app()->instance('request', $request);
|
||||
$routes = app('router')->getRoutes();
|
||||
$response = $routes->match($request)->run();
|
||||
$location = $response->headers->get('Location');
|
||||
|
||||
$this->assertSame(301, $response->getStatusCode());
|
||||
$this->assertSame('https', parse_url($location, PHP_URL_SCHEME));
|
||||
$this->assertSame('portal-investidor.test', parse_url($location, PHP_URL_HOST));
|
||||
$this->assertSame($destination, parse_url($location, PHP_URL_PATH));
|
||||
parse_str(parse_url($location, PHP_URL_QUERY), $actual);
|
||||
$this->assertEquals($query, $actual);
|
||||
$this->assertSame($name, $routes->match(Request::create($location, $method))->getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Illuminate\Foundation\Testing\TestCase;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
|
||||
class PortalLegacyNavigationTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
DB::shouldReceive('connection')->never();
|
||||
}
|
||||
|
||||
public static function aliases(): array
|
||||
{
|
||||
return [
|
||||
'simulator' => ['/simulador', '/simulador-fiscal'],
|
||||
'simulator trailing slash' => ['/simulador/', '/simulador-fiscal'],
|
||||
'contacts' => ['/contactos', '/apoio'],
|
||||
'contacts trailing slash' => ['/contactos/', '/apoio'],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('aliases')]
|
||||
public function test_legacy_links_redirect_to_existing_pages_without_database_access(string $path, string $destination): void
|
||||
{
|
||||
$route = app('router')->getRoutes()->match(Request::create($path));
|
||||
$response = $route->run();
|
||||
|
||||
$this->assertSame(301, $response->getStatusCode());
|
||||
$this->assertSame($destination, parse_url($response->headers->get('Location'), PHP_URL_PATH));
|
||||
}
|
||||
|
||||
public function test_simulator_redirect_preserves_context_without_accepting_an_external_destination(): void
|
||||
{
|
||||
$query = ['dossier' => 'qa-context', 'opportunity' => 'qa-opportunity', 'redirect' => 'https://example.invalid'];
|
||||
$request = Request::create('/simulador?'.http_build_query($query));
|
||||
app()->instance('request', $request);
|
||||
$route = app('router')->getRoutes()->match($request);
|
||||
$response = $route->run();
|
||||
$location = $response->headers->get('Location');
|
||||
|
||||
$this->assertSame('/simulador-fiscal', parse_url($location, PHP_URL_PATH));
|
||||
$this->assertSame($request->getHost(), parse_url($location, PHP_URL_HOST));
|
||||
parse_str(parse_url($location, PHP_URL_QUERY), $actual);
|
||||
$this->assertSame($query, $actual);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user