oportunidades 7
tests / ci (push) Waiting to run

This commit is contained in:
2026-10-13 11:40:29 +02:00
parent 20971d523f
commit 287e991486
2902 changed files with 1085456 additions and 25957 deletions
@@ -5,15 +5,21 @@ namespace Tests\Feature;
use App\Models\Opportunity;
use App\Models\User;
use App\Services\OpportunityCanonicalContract;
use App\Services\OpportunityCanonicalMetadata;
use App\Services\OpportunityCanonicalResponse;
use App\Services\OpportunityCanonicalValue;
use App\Services\OpportunityCategoryFields;
use App\Services\OpportunityCompleteness;
use App\Services\OpportunityManagement;
use App\Services\OpportunityModularProfiles;
use App\Services\OpportunityReviewMateriality;
use App\Services\PortalProfileAccess;
use App\Services\PortalProfileAdministration;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Inertia\Testing\AssertableInertia as Assert;
@@ -84,6 +90,132 @@ class OpportunityCanonicalWorkflowTest extends TestCase
$this->assertNotEmpty($schema['$defs']['businessTransactionCreate']['allOf']);
}
public function test_full_canonical_summary_survives_the_editor_echo_without_breaking_the_legacy_limit(): void
{
[$id, $promoter, , , $payload] = $this->fixture();
$payload['executive_summary'] = trim(str_repeat('Resumo canónico completo sintético QA. ', 30));
$this->assertGreaterThan(500, mb_strlen($payload['executive_summary']));
$this->loginFixture($promoter)->putJson('/minhas-oportunidades/'.$id.'/draft', [
'etag' => app(OpportunityManagement::class)->etag(Opportunity::findOrFail($id)),
'canonical' => $payload, 'summary' => $payload['executive_summary'], 'reason' => self::REASON,
])->assertOk();
$record = Opportunity::findOrFail($id);
$this->assertSame($payload['executive_summary'], app(OpportunityManagement::class)->values($record)['canonical']['executive_summary']);
$this->assertLessThanOrEqual(500, mb_strlen($record->summary));
$this->assertStringEndsWith('…', $record->summary);
$this->loginFixture($promoter)->putJson('/minhas-oportunidades/'.$id.'/draft', [
'etag' => app(OpportunityManagement::class)->etag($record),
'summary' => $payload['executive_summary'], 'reason' => self::REASON,
])->assertUnprocessable()->assertJsonValidationErrors('summary');
}
public function test_environmental_editor_reuses_canonical_fields_and_preserves_incomplete_drafts(): void
{
$service = app(OpportunityCanonicalContract::class);
$schema = $service->schema(true);
$this->assertSame('#/$defs/environmentalSocialCreate', $schema['$defs']['opportunityCreate']['properties']['environmental_social']['$ref']);
$this->assertArrayHasKey('assessment_status', $schema['$defs']['environmentalSocialCreate']['properties']);
$this->assertArrayHasKey('environmental_category', $schema['$defs']['environmentalSocialCreate']['properties']);
$incomplete = ['environmental_social' => ['assessment_status' => 'Pending QA']];
$this->assertSame($incomplete, $service->draft($incomplete));
$this->assertSame(['assessment_status', 'environmental_category'], $service->schema()['$defs']['environmentalSocialCreate']['required']);
}
public function test_editorial_change_retains_signed_reviews_and_price_reopens_only_property_review(): void
{
[$id, $promoter, $validator, , $payload, $document] = $this->fixture();
$this->action($promoter, $id, 'submit');
foreach (['property_review', 'registry_urban_review', 'building_compliance_review'] as $gate) {
$this->action($validator, $id, 'review_gate', ['gate' => $gate, 'gate_decision' => 'approved', 'evidence_document_ids' => [$document]]);
}
$this->action($validator, $id, 'approve');
$original = Opportunity::findOrFail($id)->workflow_context['reviews'];
$this->action($promoter, $id, 'request_revision');
$payload['official_title'] .= ' — correção editorial QA';
$this->loginFixture($promoter)->putJson('/minhas-oportunidades/'.$id.'/draft', ['etag' => app(OpportunityManagement::class)->etag(Opportunity::findOrFail($id)), 'canonical' => $payload, 'reason' => self::REASON])->assertOk();
$this->action($promoter, $id, 'submit');
$context = Opportunity::findOrFail($id)->workflow_context;
$this->assertSame([], $context['materiality']['reopened']);
foreach ($original as $gate => $review) {
$this->assertSame($review['hash'], $context['reviews'][$gate]['hash']);
$this->assertSame($review['at'], $context['reviews'][$gate]['at']);
$this->assertSame($review['actor_id'], $context['reviews'][$gate]['actor_id']);
}
$this->action($validator, $id, 'approve');
$this->action($promoter, $id, 'request_revision');
$payload['real_estate']['price'] += 25;
$this->loginFixture($promoter)->putJson('/minhas-oportunidades/'.$id.'/draft', ['etag' => app(OpportunityManagement::class)->etag(Opportunity::findOrFail($id)), 'canonical' => $payload, 'reason' => self::REASON])->assertOk();
$this->action($promoter, $id, 'submit');
$context = Opportunity::findOrFail($id)->workflow_context;
$this->assertSame(['property_review'], $context['materiality']['reopened']);
$this->assertSame(['registry_urban_review', 'building_compliance_review'], $context['materiality']['retained']);
$this->action($validator, $id, 'approve', [], 422);
$this->action($validator, $id, 'review_gate', ['gate' => 'property_review', 'gate_decision' => 'approved', 'evidence_document_ids' => [$document]]);
$this->action($validator, $id, 'approve');
// A retained decision still requires a currently authorised independent reviewer.
DB::table('portal_role_assignments')->where('user_id', $validator->id)->update(['revoked_at' => now()]);
$response = $this->loginFixture($promoter)->getJson('/minhas-oportunidades/'.$id.'/response')->assertOk()->json();
$this->assertNotNull($response['materiality']);
}
public function test_geometry_order_is_material_and_object_key_order_is_not(): void
{
$materiality = app(OpportunityReviewMateriality::class);
$a = ['payload' => ['opportunity_category' => 'real_estate'], 'resources' => ['locations' => [['payload' => ['geometry' => ['type' => 'LineString', 'coordinates' => [[1, 2], [3, 4], [5, 6]]]]]]]];
$b = $a;
$b['resources']['locations'][0]['payload']['geometry']['coordinates'] = [[3, 4], [1, 2], [5, 6]];
$this->assertSame($materiality->hash($a, 'property_review', [], true), $materiality->hash($b, 'property_review', [], true));
$this->assertNotSame($materiality->hash($a, 'property_review'), $materiality->hash($b, 'property_review'));
$this->assertSame(OpportunityCanonicalValue::hash(['price' => 100, 'currency' => 'MZN']), OpportunityCanonicalValue::hash(['currency' => 'MZN', 'price' => 100]));
$this->assertSame($materiality->hash($a, 'land_legal_review', [], true), $materiality->hash($b, 'land_legal_review', [], true));
$this->assertNotSame($materiality->hash($a, 'land_legal_review'), $materiality->hash($b, 'land_legal_review'));
}
public function test_inline_identity_reorder_delete_and_completion_do_not_forge_creation_or_approval(): void
{
$actor = User::factory()->create();
$metadata = app(OpportunityCanonicalMetadata::class);
$first = ['land_rights' => ['parcels' => [['cadastral_number' => 'A'], ['cadastral_number' => 'B']]]];
$m = $metadata->mutate([], $first, [], $actor, self::REASON);
$a = $m['active']['land_rights.parcels.0']['id'];
$b = $m['active']['land_rights.parcels.1']['id'];
$second = ['land_rights' => ['parcels' => array_reverse($first['land_rights']['parcels'])]];
$m = $metadata->mutate($first, $second, $m, $actor, self::REASON);
$this->assertSame($a, $m['active']['land_rights.parcels.1']['id']);
$this->assertSame($b, $m['active']['land_rights.parcels.0']['id']);
$third = ['land_rights' => ['parcels' => [$second['land_rights']['parcels'][0]]]];
$m = $metadata->mutate($second, $third, $m, $actor, self::REASON);
$this->assertSame($a, $m['deleted'][$a]['id']);
$this->assertSame($actor->id, $m['deleted'][$a]['deleted_by']);
$this->assertSame(self::REASON, $m['deleted'][$a]['delete_reason']);
$legacy = $metadata->mutate($first, $first, [], $actor, self::REASON);
$this->assertNull($legacy['active']['land_rights']['created_by']);
$this->assertSame('legacy_first_observation_not_creation', $legacy['active']['land_rights']['identity_source']);
$completion = app(OpportunityCompleteness::class)->calculate(['opportunity_category' => 'real_estate']);
$this->assertLessThan(100, $completion['percentage']);
$this->assertNotContains('total_investment', $completion['missing_paths']);
$this->assertNotContains('land_rights', $completion['missing_paths']);
}
public function test_private_resource_download_and_valid_signed_preview_require_organization_access(): void
{
[$id, $promoter, , , , $document] = $this->fixture();
$file = DB::table('opportunity_documents')->where('id', $document)->value('document_id');
$image = (string) Str::uuid();
DB::table('opportunity_images')->insert(['id' => $image, 'opportunity_id' => $id, 'document_id' => $file, 'created_by' => $promoter->id, 'version' => 1,
'payload' => json_encode(['title' => 'Synthetic permission fixture only', 'derivatives' => [['document_id' => $file, 'width' => 400, 'height' => 400]]])]);
$original = '/minhas-oportunidades/'.$id.'/resources/documents/'.$document.'/download';
$preview = URL::temporarySignedRoute('promoter.opportunity-image-preview', now()->addMinutes(10), ['id' => $id, 'resourceId' => $image, 'documentId' => $file]);
$this->loginFixture($promoter)->get($original)->assertOk();
$this->get($preview)->assertOk();
$organization = (string) Str::uuid();
DB::table('organizations')->insert(['id' => $organization, 'name' => 'Other synthetic QA organization', 'organization_type' => 'private']);
$outsider = $this->actor('promoter', $organization);
$this->loginFixture($outsider)->get($original)->assertForbidden();
$this->get($preview)->assertForbidden();
$this->assertDatabaseHas('documents', ['id' => $file, 'deleted_at' => null]);
}
public function test_read_response_preserves_ids_resources_permissions_and_does_not_invent_validation(): void
{
[$id, $promoter, $validator, , $payload, $document] = $this->fixture();
@@ -95,13 +227,19 @@ class OpportunityCanonicalWorkflowTest extends TestCase
$this->assertSame(OpportunityCanonicalResponse::VERSION, $response['adapter_version']);
$this->assertSame($id, $response['data']['id']);
$this->assertSame($record->reference, $response['data']['opportunity_code']);
$this->assertSame($promoter->id, $response['data']['created_by']);
$this->assertSame($promoter->id, $response['data']['updated_by']);
$this->assertSame($record->created_at->toIso8601String(), $response['data']['created_at']);
$this->assertNull($response['data']['deleted_by']);
$this->assertSame([], $response['data']['mutation_metadata_gaps']);
$this->assertSame($document, $response['data']['documents'][0]['id']);
$this->assertSame($payload['locations'], $response['resource_references']['locations']);
$this->assertNull($response['data']['approved_by']);
$this->assertFalse($response['compatibility']['normative_response_complete']);
$this->assertFalse($response['validation_result']['revalidated_on_read']);
$this->assertTrue($response['validation_result']['revalidated_on_read']);
$this->assertArrayNotHasKey('storage_key', $response['data']['documents'][0]);
$this->assertArrayNotHasKey('completion_percentage', $response['data']);
$this->assertSame(100, $response['data']['completion_percentage']);
$this->assertSame([], $response['data']['validation_result']['errors']);
$this->assertSame($before, $record->fresh()->getRawOriginal());
$this->loginFixture($validator)->getJson($url)->assertOk();
$otherOrg = (string) Str::uuid();
@@ -114,8 +252,10 @@ class OpportunityCanonicalWorkflowTest extends TestCase
$this->assertNotNull($submitted['data']['submitted_at']);
$this->assertNull($submitted['data']['approved_by']);
$this->assertNotNull($submitted['validation_result']['submission_hash']);
$this->assertEquals($payload['real_estate'], $submitted['data']['real_estate']);
$this->assertArrayNotHasKey('id', $submitted['data']['real_estate']);
$this->assertSame($payload['real_estate']['price'], $submitted['data']['real_estate']['price']);
$this->assertSame($payload['real_estate']['title_document_id'], $submitted['data']['real_estate']['title_document_id']);
$this->assertNotEmpty($submitted['data']['real_estate']['id']);
$this->assertSame($promoter->id, $submitted['data']['real_estate']['created_by']);
}
public function test_cadastral_uniqueness_and_original_succession_modality_are_enforced_without_filling_drafts(): void
@@ -220,7 +360,14 @@ class OpportunityCanonicalWorkflowTest extends TestCase
$this->assertDatabaseHas('tasks', ['id' => Opportunity::findOrFail($id)->workflow_context['review_task_id'], 'status' => 'done']);
$this->assertSame(self::REASON, Opportunity::findOrFail($id)->workflow_context['rejection']['reason']);
$this->action($promoter, $id, 'appeal');
app(PortalProfileAdministration::class)->insertGrant((string) Str::uuid(), User::factory()->create(), [
'user_id' => $validator->id, 'role' => 'validator', 'opportunity_types' => ['real_estate'],
'responsibilities' => ['appeal_reviewer'], 'source' => 'synthetic-policy-test-fixture',
]);
$validator->refresh();
$this->assertTrue(app(PortalProfileAccess::class)->has($validator, 'validator'));
$this->action($validator, $id, 'appeal_reopen', [], 403);
$this->assertTrue(Opportunity::findOrFail($id)->workflow_context['appeal']['pending']);
$appeal = $this->actor('validator', $promoter->id.'unused', ['appeal_reviewer']);
$this->action($appeal, $id, 'appeal_reopen');
$this->assertFalse(Opportunity::findOrFail($id)->workflow_context['appeal']['pending']);
@@ -230,6 +377,53 @@ class OpportunityCanonicalWorkflowTest extends TestCase
$this->loginFixture($promoter)->get('/minhas-oportunidades/'.$id.'/resources/documents/'.$document.'/download')->assertNotFound();
}
public function test_long_unicode_reason_completes_existing_task_and_preserves_full_decision(): void
{
[$id, $promoter, $validator] = $this->fixture();
$this->action($promoter, $id, 'submit');
$this->action($validator, $id, 'start_review');
$task = Opportunity::findOrFail($id)->workflow_context['review_task_id'];
$reason = trim(str_repeat('Fundamentação íntegra com evidências e condições necessárias. ', 8));
$this->action($validator, $id, 'request_correction', ['reason' => $reason, 'correction_items' => ['canonical.executive_summary']]);
$this->assertDatabaseHas('tasks', ['id' => $task, 'status' => 'done', 'outcome' => mb_substr($reason, 0, 79).'…']);
$context = Opportunity::findOrFail($id)->workflow_context;
$this->assertSame($reason, $context['last_decision']['reason']);
$this->assertSame($reason, $context['events'][array_key_last($context['events'])]['reason']);
$this->assertSame('needs_information', $context['working_state']);
}
public function test_resubmission_without_content_edit_creates_an_open_review_cycle_and_preserves_old_task(): void
{
[$id, $promoter, $validator, $publisher, $payload, $document] = $this->fixture();
$this->action($promoter, $id, 'submit');
$this->action($validator, $id, 'start_review');
foreach (['property_review', 'registry_urban_review', 'building_compliance_review'] as $gate) {
$this->action($validator, $id, 'review_gate', ['gate' => $gate, 'gate_decision' => 'approved', 'evidence_document_ids' => [$document]]);
}
$this->action($validator, $id, 'approve');
$this->action($publisher, $id, 'publish');
$before = Opportunity::findOrFail($id)->workflow_context;
$task = $before['review_task_id'];
$this->action($publisher, $id, 'suspend');
$this->action($promoter, $id, 'request_revision');
$this->action($promoter, $id, 'submit');
$this->action($validator, $id, 'start_review');
$after = Opportunity::findOrFail($id)->workflow_context;
$this->assertSame($before['content_revision'], $after['content_revision']);
$this->assertNotSame($task, $after['review_task_id']);
$this->assertDatabaseHas('tasks', ['id' => $task, 'status' => 'done']);
$this->assertDatabaseHas('tasks', ['id' => $after['review_task_id'], 'status' => 'in_progress', 'assignee_user_id' => $validator->id, 'due_at' => null]);
$this->assertCount(2, DB::table('tasks')->where('source_system', 'opportunity-workflow')->where('task_type', 'opportunity_review')->get());
foreach (['property_review', 'registry_urban_review', 'building_compliance_review'] as $gate) {
$this->action($validator, $id, 'review_gate', ['gate' => $gate, 'gate_decision' => 'approved', 'evidence_document_ids' => [$document]]);
}
$this->action($validator, $id, 'approve');
$this->action($publisher, $id, 'publish');
$published = Opportunity::findOrFail($id)->workflow_context;
$this->assertSame($before['publication']['hash'], $published['publication']['hash']);
$this->assertNotSame($before['approval']['review_cycle'], $published['approval']['review_cycle']);
}
public function test_semantic_errors_are_keyed_to_fields_and_incomplete_drafts_remain_recoverable(): void
{
[$id,$promoter,$validator,$publisher,$payload,$document] = $this->fixture();
@@ -265,7 +459,7 @@ class OpportunityCanonicalWorkflowTest extends TestCase
$this->action($opinionReviewer, $id, 'respond_opinion', ['opinion_id' => $opinion, 'gate_decision' => 'approved', 'evidence_document_ids' => [$document]]);
$context = Opportunity::findOrFail($id)->workflow_context;
$this->assertSame('answered', $context['opinions'][$opinion]['status']);
$this->assertArrayNotHasKey('reviews', $context);
$this->assertSame([], $context['reviews']);
$this->assertDatabaseHas('tasks', ['id' => $context['opinions'][$opinion]['task_id'], 'status' => 'done']);
$this->action($validator, $id, 'approve', [], 422);
}