Files
Investidor-V2/tests/Feature/PiiAnchorRelationsTest.php
T

57 lines
3.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use App\Services\PiiIntegratedProjects;
use Database\Seeders\InvestmentPortfolioSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\Support\AuthenticatesQaActors;
use Tests\TestCase;
final class PiiAnchorRelationsTest extends TestCase
{
use AuthenticatesQaActors;
use RefreshDatabase;
public function test_manager_validates_changes_rejects_and_reopens_anchor_relations(): void
{
$this->seed(InvestmentPortfolioSeeder::class);
app(PiiIntegratedProjects::class)->run(true);
$project = fn (string $code) => DB::table('projects')->where('code', $code)->first();
$links = fn (string $code) => DB::table('complementary_project_links')->where('complementary_project_id', $project($code)->id)->whereNull('deleted_at')->pluck('relation_type')->all();
$relation = fn (string $code) => json_decode($project($code)->metadata, true)['anchor_relation'];
$this->actingAs(User::factory()->create())->getJson('/management/relacoes-pii')->assertForbidden();
$manager = User::factory()->create();
config(['portal.managers' => [$manager->email]]);
$this->actingAsWithMfa($manager);
$this->getJson('/management/relacoes-pii')->assertOk()->assertJsonCount(161, 'items')->assertJsonPath('items.0.anchor_code', 'A03');
// A03-C01 «Parque Industrial de Nacala»: suggestion from «alimentada pelo PDEC» is market.
$this->assertSame(['market'], $links('A03-C01'));
$this->postJson('/management/relacoes-pii', ['ids' => [$project('A03-C01')->id], 'decision' => 'validate'])->assertOk()->assertJsonPath('changed', 1);
$this->assertSame(['validada', 'market', $manager->id], [$relation('A03-C01')['status'], $relation('A03-C01')['type'], $relation('A03-C01')['decided_by']]);
// Another type replaces the internal link; rejecting removes it; reopening restores the suggestion.
$this->postJson('/management/relacoes-pii', ['ids' => [$project('A03-C02')->id], 'decision' => 'validate', 'type' => 'enabler', 'note' => 'Infra-estrutura'])->assertOk();
$this->assertSame(['enabler'], $links('A03-C02'));
$this->postJson('/management/relacoes-pii', ['ids' => [$project('A03-C03')->id], 'decision' => 'reject'])->assertOk();
$this->assertSame([], $links('A03-C03'));
$this->postJson('/management/relacoes-pii', ['ids' => [$project('A03-C03')->id], 'decision' => 'reopen'])->assertOk();
$this->assertSame('por_validar', $relation('A03-C03')['status']);
$this->assertSame([$relation('A03-C03')['suggested_type']], $links('A03-C03'));
// A project without suggestion needs an explicit type.
$bare = DB::table('projects')->where('source_system', PiiIntegratedProjects::SOURCE_SYSTEM)->whereNull('metadata->anchor_relation->suggested_type')->orderBy('code')->first();
$this->postJson('/management/relacoes-pii', ['ids' => [$bare->id], 'decision' => 'validate'])->assertOk()->assertJsonPath('changed', 0);
$this->postJson('/management/relacoes-pii', ['ids' => [$bare->id], 'decision' => 'validate', 'type' => 'supplier'])->assertOk()->assertJsonPath('changed', 1);
$this->assertSame(['supplier'], $links($bare->code));
$this->getJson('/management/relacoes-pii?status=validada')->assertOk()->assertJsonCount(3, 'items');
$this->assertSame(5, DB::table('audit_events')->where('source_system', 'pii-anchor-relations')->count());
$this->assertSame(0, DB::table('complementary_project_links')->where('source_system', PiiIntegratedProjects::SOURCE_SYSTEM)->where('data_classification', '!=', 'internal')->count());
}
}