Carteira 4.3: ligações oportunidade↔carteira propostas e aprovadas na gestão

89 propostas, segundo os critérios aprovados pelo proprietário a partir da simulação do PII,
ficam «por decidir» com fonte e evidência. A página «Ligações da carteira» aprova ou rejeita
uma a uma ou por grupo; só a aprovação escreve a ligação, com registo de quem e quando, e
anular retira apenas a ligação que a aprovação criou.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
This commit is contained in:
Claude
2026-12-05 09:58:25 +02:00
committed by nala
parent 80b7bc18e1
commit eb21e12793
9 changed files with 1333 additions and 0 deletions
@@ -0,0 +1,93 @@
<?php
namespace Tests\Feature;
use App\Models\Opportunity;
use App\Models\Sector;
use App\Models\User;
use App\Services\PortfolioLinkReviews;
use Database\Seeders\InvestmentPortfolioSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Tests\Support\AuthenticatesQaActors;
use Tests\TestCase;
final class PortfolioLinkReviewsTest extends TestCase
{
use AuthenticatesQaActors;
use RefreshDatabase;
public function test_proposals_become_links_only_when_a_manager_approves_them(): void
{
$this->seed(InvestmentPortfolioSeeder::class);
$sector = Sector::create(['name' => 'Infraestruturas sintéticas', 'slug' => 'infra-sinteticas', 'is_active' => true]);
$make = fn (string $reference, string $title) => Opportunity::create(['sector_id' => $sector->id, 'reference' => $reference,
'slug' => 'synthetic-'.Str::uuid(), 'title' => $title, 'status' => 'published', 'published_at' => now()->subMinute()]);
$airport = $make('INV-MZ-GEN-1780690015914603', 'Melhoria do Aeroporto de Inhambane');
$make('INV-MZ-GEN-7335468891275115', 'Outro título sintético');
$tofo = $make('INV-MZ-GEN-8228761547465384', 'Centro e Mercado de Tofo');
$reviews = app(PortfolioLinkReviews::class);
$dry = $reviews->load(false);
$this->assertSame(3, $dry['created']);
$this->assertTrue(collect($dry['problems'])->contains(fn ($p) => str_contains($p, 'INV-MZ-GEN-7335468891275115') && str_contains($p, 'título diferente')));
$this->assertSame(0, DB::table('portfolio_link_reviews')->count());
$this->assertSame(3, $reviews->load(true)['created']);
$this->assertSame(['created' => 0, 'existing' => 3], array_slice($reviews->load(true), 0, 2));
$this->assertSame(0, DB::table('opportunity_projects')->count() + DB::table('opportunity_clusters')->count() + DB::table('opportunity_corridors')->count());
$this->actingAs(User::factory()->create())->getJson('/management/ligacoes-carteira')->assertForbidden();
$manager = User::factory()->create();
config(['portal.managers' => [$manager->email]]);
$this->actingAsWithMfa($manager);
$items = collect($this->getJson('/management/ligacoes-carteira')->assertOk()->assertJsonCount(3, 'items')->json('items'));
$a13 = $items->firstWhere('opportunity_id', $airport->id);
$this->assertStringStartsWith('A13 — ', $a13['target']);
$this->assertStringContainsString('A13-C02', $a13['evidence']);
$project = DB::table('projects')->where('code', 'A13')->value('id');
$this->postJson('/management/ligacoes-carteira', ['ids' => [$a13['id']], 'decision' => 'approve', 'note' => 'Confirmado'])->assertOk()->assertJsonPath('changed', 1);
$this->assertTrue(DB::table('opportunity_projects')->where(['opportunity_id' => $airport->id, 'project_id' => $project])->exists());
$review = DB::table('portfolio_link_reviews')->where('id', $a13['id'])->first();
$this->assertSame(['approved', $manager->id, true], [$review->status, $review->decided_by, $review->link_created]);
$this->assertSame(1, DB::table('audit_events')->where('action', 'portfolio_link_approve')->where('entity_id', $a13['id'])->count());
// Deciding again on an approved review changes nothing.
$this->postJson('/management/ligacoes-carteira', ['ids' => [$a13['id']], 'decision' => 'reject'])->assertOk()->assertJsonPath('changed', 0);
$this->postJson('/management/ligacoes-carteira', ['ids' => [$a13['id']], 'decision' => 'undo'])->assertOk();
$this->assertFalse(DB::table('opportunity_projects')->where('opportunity_id', $airport->id)->exists());
$this->assertSame('pending', DB::table('portfolio_link_reviews')->where('id', $a13['id'])->value('status'));
// A link that already existed is kept when its approval is undone.
DB::table('opportunity_projects')->insert(['opportunity_id' => $airport->id, 'project_id' => $project]);
$this->postJson('/management/ligacoes-carteira', ['ids' => [$a13['id']], 'decision' => 'approve'])->assertOk();
$this->assertFalse((bool) DB::table('portfolio_link_reviews')->where('id', $a13['id'])->value('link_created'));
$this->postJson('/management/ligacoes-carteira', ['ids' => [$a13['id']], 'decision' => 'undo'])->assertOk();
$this->assertTrue(DB::table('opportunity_projects')->where('opportunity_id', $airport->id)->exists());
// Tofo is outside Vilankulos: cluster and corridor, never the A13 project. Group approval decides both.
$tofoIds = $items->where('opportunity_id', $tofo->id)->pluck('id')->all();
$this->assertCount(2, $tofoIds);
$this->postJson('/management/ligacoes-carteira', ['ids' => $tofoIds, 'decision' => 'approve'])->assertOk()->assertJsonPath('changed', 2);
$this->assertTrue(DB::table('opportunity_clusters')->where('opportunity_id', $tofo->id)->exists());
$this->assertTrue(DB::table('opportunity_corridors')->where('opportunity_id', $tofo->id)->exists());
$this->assertFalse(DB::table('opportunity_projects')->where('opportunity_id', $tofo->id)->exists());
$this->getJson('/management/ligacoes-carteira?status=approved')->assertOk()->assertJsonCount(2, 'items');
}
public function test_every_proposal_targets_an_existing_portfolio_code_and_keeps_its_evidence(): void
{
$data = json_decode((string) file_get_contents(database_path(PortfolioLinkReviews::PROPOSALS)), true, 512, JSON_THROW_ON_ERROR);
$this->assertCount(89, $data['proposals']);
$portfolio = json_decode((string) file_get_contents(database_path('data/investment-portfolio.json')), true, 512, JSON_THROW_ON_ERROR);
$codes = array_merge(...array_map(fn ($kind) => array_column($portfolio[$kind], 'code'), ['projects', 'clusters', 'corridors']));
foreach ($data['proposals'] as $p) {
$this->assertContains($p['target_code'], $codes);
$this->assertArrayHasKey($p['relation'], PortfolioLinkReviews::RELATIONS);
$this->assertNotSame('', trim($p['evidence']));
}
$keys = array_map(fn ($p) => $p['reference'].'|'.$p['target_code'], $data['proposals']);
$this->assertSame(count($keys), count(array_unique($keys)));
}
}