feat: integrate opportunity draft management with audited lifecycle
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Models\Sector;
|
||||
use App\Models\User;
|
||||
use App\Services\InvestmentCatalogue;
|
||||
use App\Services\InvestmentGeography;
|
||||
use App\Services\OpportunityManagement;
|
||||
use Database\Seeders\InvestmentAreaSeeder;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
@@ -74,9 +75,9 @@ class InvestmentTerritoryTest extends TestCase
|
||||
$this->assertCount(2, $service->context($opportunity->id)['primary_territory_path']);
|
||||
$manager = User::factory()->create();
|
||||
config(['portal.managers' => [$manager->email]]);
|
||||
$this->actingAs($manager)->post('/management/opportunities', ['sector_id' => $sector->id, 'reference' => 'SYNTHETIC-CREATE', 'slug' => 'synthetic-create', 'title' => 'Synthetic create',
|
||||
'status' => 'draft', 'is_featured' => false, 'estimated_investment_currency' => 'USD', 'province_id' => $province, 'district_id' => $district])->assertRedirect();
|
||||
$this->assertDatabaseHas('opportunities', ['reference' => 'SYNTHETIC-CREATE', 'primary_territory_id' => $district, 'territory_version' => 1]);
|
||||
$created = $this->actingAs($manager)->postJson('/management/opportunities/drafts', ['sector_id' => $sector->id, 'creation_key' => (string) Str::uuid(), 'reason' => 'Cadastro territorial de rascunho apenas em QA', 'title' => 'Synthetic create',
|
||||
'estimated_investment_currency' => 'USD', 'province_id' => $province, 'district_id' => $district])->assertCreated()->json();
|
||||
$this->assertDatabaseHas('opportunities', ['id' => $created['id'], 'primary_territory_id' => $district]);
|
||||
$this->post('/management/clusters', ['code' => 'GEO-CREATE', 'name' => 'Synthetic cluster', 'sector' => 'Synthetic', 'status' => 'planned', 'province_id' => $province, 'district_id' => $district])->assertRedirect();
|
||||
$this->assertDatabaseHas('economic_clusters', ['code' => 'GEO-CREATE', 'primary_territory_id' => $district]);
|
||||
$manager->forceFill(['two_factor_secret' => Crypt::encryptString('SYNTHETIC'), 'two_factor_confirmed_at' => now()])->save();
|
||||
@@ -283,11 +284,12 @@ class InvestmentTerritoryTest extends TestCase
|
||||
$province = $this->location('province', $country);
|
||||
$sector = Sector::create(['name' => 'Synthetic edit', 'slug' => 'synthetic-edit', 'is_active' => true]);
|
||||
$record = Opportunity::create(['sector_id' => $sector->id, 'reference' => 'EDIT-GEO', 'slug' => 'edit-geo', 'title' => 'Synthetic edit', 'status' => 'draft']);
|
||||
$this->actingAs($manager)->put('/management/opportunities/'.$record->id, [
|
||||
$this->actingAs($manager)->putJson('/management/opportunities/'.$record->id.'/draft', [
|
||||
'sector_id' => $sector->id, 'reference' => 'EDIT-GEO', 'slug' => 'edit-geo', 'title' => 'Synthetic edit',
|
||||
'status' => 'draft', 'is_featured' => false, 'estimated_investment_currency' => 'USD',
|
||||
'province_id' => $province, 'territory_version' => 0,
|
||||
])->assertRedirect();
|
||||
'etag' => app(OpportunityManagement::class)->etag($record->fresh()), 'reason' => 'Atualização territorial do rascunho em QA',
|
||||
])->assertOk();
|
||||
$this->assertDatabaseHas('opportunities', ['id' => $record->id, 'primary_territory_id' => $province]);
|
||||
$this->assertSame($province, $record->fresh()->primaryTerritory->id);
|
||||
$this->get('/management/opportunities/'.$record->id.'/edit')->assertOk();
|
||||
@@ -299,9 +301,10 @@ class InvestmentTerritoryTest extends TestCase
|
||||
$payload = ['sector_id' => $sector->id, 'reference' => 'EDIT-GEO', 'slug' => 'edit-geo', 'title' => 'Must not persist',
|
||||
'status' => 'draft', 'is_featured' => false, 'estimated_investment_currency' => 'USD',
|
||||
'province_id' => $province, 'district_id' => $district, 'territory_version' => 1];
|
||||
$this->putJson('/management/opportunities/'.$record->id, $payload)->assertUnprocessable()->assertJsonValidationErrors('district_id');
|
||||
$etag = app(OpportunityManagement::class)->etag($record->fresh());
|
||||
$this->putJson('/management/opportunities/'.$record->id.'/draft', [...$payload, 'etag' => $etag, 'reason' => 'Validação de hierarquia no rascunho QA'])->assertUnprocessable()->assertJsonValidationErrors('district_id');
|
||||
$this->assertDatabaseHas('opportunities', ['id' => $record->id, 'title' => 'Synthetic edit', 'primary_territory_id' => $province]);
|
||||
$this->putJson('/management/opportunities/'.$record->id, [...$payload, 'district_id' => null, 'territory_version' => 0])->assertConflict();
|
||||
$this->putJson('/management/opportunities/'.$record->id.'/draft', [...$payload, 'district_id' => null, 'etag' => str_repeat('0', 64), 'reason' => 'Teste de conflito de edição territorial QA'])->assertConflict();
|
||||
}
|
||||
|
||||
public function test_database_rejects_a_cycle_wrong_parent_and_duplicate_source_code(): void
|
||||
@@ -348,10 +351,10 @@ class InvestmentTerritoryTest extends TestCase
|
||||
$manager = User::factory()->create();
|
||||
config(['portal.managers' => [$manager->email]]);
|
||||
$this->actingAs($manager)->from('/management/opportunities/create')
|
||||
->post('/management/opportunities', [])->assertSessionHasErrors('reference');
|
||||
->post('/management/opportunities', [])->assertSessionHasErrors('status');
|
||||
$this->withCookie(config('session.cookie'), session()->getId())->withCredentials();
|
||||
$this->getJson('/management/territory-options?type=province')->assertOk()->assertSessionHasErrors('reference');
|
||||
$this->get('/management/opportunities/create')->assertInertia(fn (AssertableInertia $page) => $page->has('errors.reference'));
|
||||
$this->getJson('/management/territory-options?type=province')->assertOk()->assertSessionHasErrors('status');
|
||||
$this->get('/management/opportunities/create')->assertInertia(fn (AssertableInertia $page) => $page->has('errors.status'));
|
||||
}
|
||||
|
||||
public function test_investment_areas_are_idempotent_and_preserve_legacy_sectors(): void
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Opportunity;
|
||||
use App\Models\Project;
|
||||
use App\Models\Sector;
|
||||
use App\Models\User;
|
||||
use App\Services\OpportunityManagement;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class OpportunityManagementTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_installation_requires_management_permission_and_current_proofs(): void
|
||||
{
|
||||
$this->actingAs(User::factory()->create())->getJson('/management/opportunity-management-install')->assertForbidden();
|
||||
$manager = User::factory()->create();
|
||||
config(['portal.managers' => [$manager->email]]);
|
||||
$migration = require database_path('migrations/2026_10_09_020000_extend_opportunity_management.php');
|
||||
$migration->down();
|
||||
$preview = $this->actingAs($manager)->getJson('/management/opportunity-management-install')->assertOk()->assertJsonPath('installed', false)->assertJsonPath('ready', false)->json();
|
||||
$this->postJson('/management/opportunity-management-install', ['hash' => $preview['hash'], 'confirm' => true])->assertUnprocessable();
|
||||
$this->assertFalse(app(OpportunityManagement::class)->installed());
|
||||
}
|
||||
|
||||
public function test_incomplete_draft_recovers_and_soft_delete_restore_preserves_identity(): void
|
||||
{
|
||||
$manager = User::factory()->create();
|
||||
config(['portal.managers' => [$manager->email]]);
|
||||
$payload = ['creation_key' => (string) Str::uuid(), 'reason' => 'Criação de rascunho incompleto em QA'];
|
||||
$result = $this->actingAs($manager)->postJson('/management/opportunities/drafts', $payload)->assertCreated()->json();
|
||||
$id = $result['id'];
|
||||
$this->assertDatabaseHas('opportunities', ['id' => $id, 'title' => null, 'sector_id' => null, 'estimated_investment_currency' => null, 'status' => 'draft']);
|
||||
$this->postJson('/management/opportunities/drafts', $payload)->assertCreated()->assertJsonPath('id', $id);
|
||||
$this->assertSame(1, Opportunity::count());
|
||||
$this->postJson('/management/opportunities/drafts', [...$payload, 'title' => 'Different data'])->assertUnprocessable();
|
||||
$this->get('/management/opportunities/'.$id.'/edit')->assertOk();
|
||||
$update = $this->putJson('/management/opportunities/'.$id.'/draft', ['etag' => $result['etag'], 'title' => 'QA incomplete saved title', 'reason' => 'Edição de identificação incompleta QA'])->assertOk()->json();
|
||||
$this->putJson('/management/opportunities/'.$id.'/draft', ['etag' => $result['etag'], 'title' => 'Lost update', 'reason' => 'Tentativa concorrente para teste QA'])->assertConflict()->assertJsonValidationErrors('etag');
|
||||
$this->assertDatabaseHas('opportunities', ['id' => $id, 'title' => 'QA incomplete saved title']);
|
||||
$request = ['etag' => $update['etag'], 'action' => 'delete', 'reason' => 'Exclusão lógica autorizada apenas em QA', 'confirm' => true];
|
||||
$this->postJson('/management/opportunities/'.$id.'/lifecycle', [...$request, 'confirm' => false])->assertUnprocessable();
|
||||
$this->postJson('/management/opportunities/'.$id.'/lifecycle', $request)->assertOk();
|
||||
$this->assertSoftDeleted('opportunities', ['id' => $id]);
|
||||
$this->get('/management/opportunities/'.$id.'/edit')->assertOk();
|
||||
$record = Opportunity::withTrashed()->findOrFail($id);
|
||||
$this->postJson('/management/opportunities/'.$id.'/lifecycle', [...$request, 'action' => 'restore', 'etag' => app(OpportunityManagement::class)->etag($record)])->assertOk();
|
||||
$this->assertDatabaseHas('opportunities', ['id' => $id, 'status' => 'draft', 'deleted_at' => null]);
|
||||
$this->assertSame(4, DB::table('audit_events')->where('source_system', 'opportunity-management')->count());
|
||||
$this->actingAs(User::factory()->create())->get('/management/opportunities')->assertForbidden();
|
||||
$this->postJson('/management/opportunities/drafts', $payload)->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_closed_version_edits_do_not_change_public_values_classification_or_relations(): void
|
||||
{
|
||||
$manager = User::factory()->create();
|
||||
config(['portal.managers' => [$manager->email]]);
|
||||
$sector = Sector::create(['name' => 'QA sector', 'slug' => 'qa-management', 'is_active' => true]);
|
||||
$opportunity = Opportunity::create(['sector_id' => $sector->id, 'reference' => 'INV-MZ-QA-001', 'title' => 'Published QA original', 'promoter_type' => 'privado', 'slug' => 'qa-management', 'status' => 'published', 'published_at' => now()->subMinute()]);
|
||||
$project = Project::factory()->create();
|
||||
DB::table('opportunity_projects')->insert(['opportunity_id' => $opportunity->id, 'project_id' => $project->id]);
|
||||
DB::table('audit_events')->insert(['source_system' => 'opportunity-origin-review', 'entity_type' => 'opportunity_origin', 'action' => 'origin_classification_decided', 'actor_user_id' => $manager->id, 'after_data' => json_encode(['opportunity_id' => $opportunity->id, 'classification' => 'real', 'reason' => 'QA classification retained'])]);
|
||||
$service = app(OpportunityManagement::class);
|
||||
$update = $this->actingAs($manager)->putJson('/management/opportunities/'.$opportunity->id.'/draft', ['etag' => $service->etag($opportunity->fresh()), 'promoter_type' => 'privado', 'title' => 'QA new unpublished version', 'reason' => 'Nova versão em rascunho para teste QA'])->assertOk()->json();
|
||||
$this->assertSame('Published QA original', $opportunity->fresh()->title);
|
||||
$this->assertSame('QA new unpublished version', json_decode($opportunity->fresh()->management_draft, true)['title']);
|
||||
$this->getJson('/api/v1/opportunities/qa-management')->assertOk()->assertJsonMissingPath('data.management_draft')->assertJsonPath('data.title', 'Published QA original');
|
||||
$this->postJson('/management/opportunities/'.$opportunity->id.'/lifecycle', ['etag' => $update['etag'], 'action' => 'delete', 'reason' => 'Arquivo explícito de versão submetida QA', 'confirm' => true])->assertOk();
|
||||
$this->assertDatabaseHas('opportunities', ['id' => $opportunity->id, 'status' => 'archived', 'deleted_at' => null]);
|
||||
$this->assertTrue((bool) $opportunity->fresh()->management_closed_once);
|
||||
$this->assertNull($opportunity->fresh()->first_submitted_at);
|
||||
$this->assertDatabaseHas('opportunity_projects', ['opportunity_id' => $opportunity->id, 'project_id' => $project->id]);
|
||||
$this->assertSame(1, DB::table('audit_events')->where('source_system', 'opportunity-origin-review')->count());
|
||||
$this->putJson('/management/opportunities/'.$opportunity->id, ['status' => 'published'])->assertUnprocessable();
|
||||
$this->assertSame(0, Opportunity::published()->count());
|
||||
$this->postJson('/management/opportunities/'.$opportunity->id.'/lifecycle', ['etag' => $service->etag($opportunity->fresh()), 'action' => 'restore', 'reason' => 'Reabertura autorizada com revisão somente QA', 'confirm' => true])->assertOk();
|
||||
$this->assertSame('draft', $opportunity->fresh()->status);
|
||||
$this->assertSame(0, Opportunity::published()->count());
|
||||
try {
|
||||
DB::transaction(fn () => DB::table('opportunities')->where('id', $opportunity->id)->delete());
|
||||
$this->fail('Physical deletion after a closed version was accepted');
|
||||
} catch (QueryException $error) {
|
||||
$this->assertStringContainsString('Physical deletion forbidden', $error->getMessage());
|
||||
}
|
||||
$this->assertDatabaseHas('opportunity_projects', ['opportunity_id' => $opportunity->id, 'project_id' => $project->id]);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ use App\Models\InterestRequest;
|
||||
use App\Models\Opportunity;
|
||||
use App\Models\Sector;
|
||||
use App\Models\User;
|
||||
use App\Services\OpportunityManagement;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
@@ -113,24 +114,19 @@ class PortalManagementTest extends TestCase
|
||||
$this->get('/management/sectors/'.$sector->id.'/edit')->assertOk();
|
||||
}
|
||||
|
||||
public function test_opportunities_can_be_published_and_withdrawn_without_losing_money_precision(): void
|
||||
public function test_draft_money_precision_is_preserved_without_editorial_publication_bypass(): void
|
||||
{
|
||||
$this->actingAs($this->manager());
|
||||
$data = ['sector_id' => $this->sector()->id, 'reference' => 'INV-TEST', 'title' => 'Solar',
|
||||
'slug' => 'solar', 'status' => 'draft', 'is_featured' => true, 'maturity_level' => 4,
|
||||
'estimated_investment_amount' => '1234567890123456.78', 'estimated_investment_currency' => 'MZN'];
|
||||
$this->post('/management/opportunities', $data)->assertRedirect('/management/opportunities');
|
||||
$this->postJson('/management/opportunities/drafts', [...$data, 'creation_key' => (string) Str::uuid(), 'reason' => 'Rascunho financeiro incompleto em QA'])->assertCreated();
|
||||
$opportunity = Opportunity::firstOrFail();
|
||||
$this->getJson('/api/v1/opportunities/solar')->assertNotFound();
|
||||
$this->put('/management/opportunities/'.$opportunity->id, [...$data, 'status' => 'published'])->assertRedirect();
|
||||
$this->getJson('/api/v1/opportunities/solar')->assertOk()
|
||||
->assertJsonPath('data.estimated_investment_amount', '1234567890123456.78')
|
||||
->assertJsonPath('data.maturity_label', 'Projecto estruturado');
|
||||
$this->getJson('/api/v1/home')->assertJsonPath('data.stats.opportunities', 1);
|
||||
$this->put('/management/opportunities/'.$opportunity->id, [...$data, 'status' => 'archived'])->assertRedirect();
|
||||
$this->getJson('/api/v1/opportunities/solar')->assertNotFound();
|
||||
$this->assertSame('1234567890123456.78', $opportunity->estimated_investment_amount);
|
||||
$this->put('/management/opportunities/'.$opportunity->id, [...$data, 'status' => 'published'])->assertSessionHasErrors('status');
|
||||
$this->getJson('/api/v1/home')->assertJsonPath('data.stats.opportunities', 0);
|
||||
$this->put('/management/opportunities/'.$opportunity->id, [...$data, 'estimated_investment_amount' => '-1'])->assertSessionHasErrors('estimated_investment_amount');
|
||||
$this->putJson('/management/opportunities/'.$opportunity->id.'/draft', [...$data, 'etag' => app(OpportunityManagement::class)->etag($opportunity), 'reason' => 'Correção financeira testada apenas em QA', 'estimated_investment_amount' => '-1'])->assertUnprocessable()->assertJsonValidationErrors('estimated_investment_amount');
|
||||
}
|
||||
|
||||
public function test_review_tracks_the_authenticated_reviewer_and_rejects_invalid_states(): void
|
||||
|
||||
Reference in New Issue
Block a user