true, 'active_writes' => 0, 'browser_demonstration' => false]; try { $portal = dirname(__DIR__, 2); $plan = json_decode(file_get_contents($portal.'/storage/app/private/investmoz-v2/portal-audit/internal-designation-plan.json'), true, 512, JSON_THROW_ON_ERROR); $qa = $plan['source']; if (!str_contains($qa, 'onboarding-qa-internal-')) throw new RuntimeException('Separate QA source required'); require $qa.'/qa-autoload.php'; $app = require $qa.'/bootstrap/app.php'; (new Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables)->bootstrap($app); (new Illuminate\Foundation\Bootstrap\LoadConfiguration)->bootstrap($app); $c = config('database.connections.'.config('database.default')); if ($c['driver'] !== 'pgsql' || (int)$c['port'] !== 55441 || $c['database'] !== 'onboarding_internal_20261011_191450_test' || !empty($c['url'])) throw new RuntimeException('QA target rejected'); config(['session.driver' => 'array', 'queue.default' => 'sync', 'mail.default' => 'array']); $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); if ((int)Illuminate\Support\Facades\DB::selectOne('SELECT inet_server_port() AS port')->port !== 55441) throw new RuntimeException('Server rejected'); $actors = json_decode(file_get_contents($qa.'/storage/app/private/onboarding-actors-local.json'), true, 512, JSON_THROW_ON_ERROR); $actor = App\Models\User::findOrFail($actors['promoter']['id']); $record = App\Models\Opportunity::where('owner_user_id', $actor->id)->where('status', 'draft')->firstOrFail(); $request = Illuminate\Http\Request::create('/minhas-oportunidades/'.$record->id.'/contacts', 'POST'); $request->setLaravelSession($app->make('session')->driver()); $app->instance('request', $request); session()->put(['portal.mfa_actor' => $actor->id, 'portal.mfa_at' => time()]); $db = Illuminate\Support\Facades\DB::class; $db::beginTransaction(); $counts = static fn () => ['contacts' => $db::table('contacts')->count(), 'investors' => $db::table('investors')->count(), 'links' => $db::table('investor_contacts')->count(), 'leads' => $db::table('leads')->count(), 'interests' => $db::table('interest_requests')->count(), 'jobs' => $db::table('jobs')->count()]; $before = $counts(); $newInvestor = $db::table('investors')->where('organization_id', $record->organization_id)->whereNull('deleted_at')->whereIn('status', ['prospect', 'active'])->count() === 0; $input = ['etag' => app(App\Services\OpportunityManagement::class)->etag($record), 'full_name' => 'Contacto sintético autorizado QA', 'email' => 'contact-'.bin2hex(random_bytes(6)).'@example.invalid', 'phone' => '+258840000000', 'investor_type' => 'domestic']; $service = app(App\Services\OpportunityContacts::class); $result = $service->save($actor, $record->id, $input); $repeated = $service->save($actor, $record->id, $input); if ($result['reused'] || !$repeated['reused'] || $result['contact']['id'] !== $repeated['contact']['id']) throw new RuntimeException('Idempotence failed'); $after = $counts(); if ($after['contacts'] !== $before['contacts'] + 1 || $after['investors'] !== $before['investors'] + ($newInvestor ? 1 : 0) || $after['links'] !== $before['links'] + 1 || $after['leads'] !== $before['leads'] || $after['interests'] !== $before['interests'] || $after['jobs'] !== $before['jobs']) throw new RuntimeException('Unexpected records'); $options = app(App\Services\OpportunityCanonicalContract::class)->options($actor, $record); if (!$options['contacts']->contains('id', $result['contact']['id'])) throw new RuntimeException('Recovery failed'); $assertFailure = function (callable $operation, int $expected, string $criterion) use (&$proof): void { try { $operation(); throw new RuntimeException('Expected rejection missing'); } catch (Illuminate\Validation\ValidationException $e) { if ($expected !== 422) throw $e; } catch (Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e) { if ($e->getStatusCode() !== $expected) throw $e; } $proof['criteria'][$criterion] = $expected; }; $assertFailure(fn () => $service->save($actor, $record->id, [...$input, 'etag' => str_repeat('0', 64)]), 409, 'stale_opportunity'); $assertFailure(fn () => $service->save($actor, $record->id, [...$input, 'phone' => '840000000']), 422, 'invalid_phone'); $assertFailure(fn () => $service->save($actor, $record->id, [...$input, 'full_name' => 'Changed synthetic name']), 422, 'master_not_overwritten'); session()->put(['portal.mfa_actor' => $actors['validator']['id'], 'portal.mfa_at' => time()]); $assertFailure(fn () => $service->save(App\Models\User::findOrFail($actors['validator']['id']), $record->id, $input), 403, 'validator_cannot_create_contact'); session()->put(['portal.mfa_actor' => $actor->id, 'portal.mfa_at' => time()]); $foreign = App\Models\Contact::create(['full_name' => 'Outra entidade QA', 'email' => 'other-'.bin2hex(random_bytes(6)).'@example.invalid', 'phone' => '+258840000001']); $assertFailure(fn () => $service->save($actor, $record->id, [...$input, 'email' => $foreign->email, 'full_name' => $foreign->full_name, 'phone' => $foreign->phone]), 422, 'unrelated_contact_not_associated'); $assertFailure(fn () => $service->save($actor, $record->id, [...$input, 'email' => 'new-'.bin2hex(random_bytes(6)).'@example.invalid', 'investor_id' => (string)Illuminate\Support\Str::uuid()]), 403, 'foreign_investor_rejected'); $proof += ['result' => 'passed', 'at' => gmdate('c'), 'target' => $c['database'].':55441', 'record_id' => $record->id, 'policy_fixture_not_real_login' => true, 'created_recovered_repeated' => true, 'no_fabricated_interest_or_lead' => true, 'before' => $before, 'after' => $after, 'rolled_back' => true]; $db::rollBack(); } catch (Throwable $e) { if (isset($db) && $db::transactionLevel()) $db::rollBack(); $proof += ['result' => 'failed', 'type' => $e::class, 'file' => basename($e->getFile()), 'line' => $e->getLine()]; } file_put_contents(__DIR__.'/contact-policy-qa.json', json_encode($proof, JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR)); echo json_encode($proof, JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR); exit($proof['result'] === 'passed' ? 0 : 1);