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'));
}
}
}
}