fix(portal): preserve territorial totals and legacy redirect context

This commit is contained in:
2026-11-10 10:41:46 +02:00
parent 2209b83820
commit 377c40a21a
15 changed files with 686 additions and 20 deletions
+45
View File
@@ -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'));
}
}
}
}
+59
View File
@@ -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());
}
}
+52
View File
@@ -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);
}
}