commit de armazens
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-04 09:40:26 +02:00
parent e753f7a0a4
commit 63d20b10fc
245 changed files with 10847 additions and 230 deletions
+267
View File
@@ -4,11 +4,13 @@ use App\Enums\PurchasingRole;
use App\Models\CandidateAccount;
use App\Models\Tenant;
use App\Models\User;
use App\Services\AdministrativeGeography;
use App\Services\CandidateProfiles;
use App\Services\CandidateSubmissions;
use App\Services\CandidateTriage;
use App\Services\PeopleAccess;
use App\Services\PeopleHiring;
use App\Services\ReceivedCandidateAvailability;
use App\Services\RequisitionWorkflow;
use App\Services\SubmissionForms;
use Illuminate\Database\QueryException;
@@ -46,6 +48,271 @@ beforeEach(function () {
'answers' => ['full_name' => 'Pessoa fictícia', 'availability_location' => $this->profile['availability']]];
});
function territoryReceived(object $test, array $changes = []): int
{
$account = app(CandidateProfiles::class)->register($test->tenant->id, [...$test->credentials, 'email' => Str::uuid().'@territory.example.test']);
$account->setAttribute('email_verified_at', now())->save();
app(CandidateProfiles::class)->save($account, [...$test->profile, ...$changes]);
$location = app(CandidateProfiles::class)->profile($account)['data']['availability'];
return app(CandidateSubmissions::class)->submit($account, $test->process->id, [...$test->body, 'operation_key' => (string) Str::uuid(), 'answers' => [...$test->body['answers'], 'availability_location' => $location]]);
}
function territoryReference(bool $otherProvince = false): array
{
$record = collect(app(AdministrativeGeography::class)->catalogue()['records'])->first(fn ($record) => $record['selectable'] && $record['level'] === 4 && ($otherProvince ? $record['codes'][0] !== '01' : $record['codes'][0] === '01'));
return ['country' => 'MZ', 'catalogue' => config('geography.source_sha256'), 'record_id' => $record['id']];
}
function receivedAvailabilityApplication(object $test, array $profileChanges = []): int
{
config(['geography.enabled' => true]);
$submission = territoryReceived($test, ['availability_reference' => territoryReference(), ...$profileChanges]);
$test->postJson('/pessoas/'.$test->process->id.'/recebidas/'.$submission.'/triagem', electronicTriagePayload($test))->assertRedirect();
return $submission;
}
test('received availability projection reuses main additional and manual submitted data on page and dossier', function () {
$this->withoutVite();
$reference = territoryReference(true);
$submission = receivedAvailabilityApplication($this, ['additional_availability' => [$this->profile['availability'], []], 'additional_availability_references' => [null, $reference]]);
$received = json_decode(DB::table('candidate_submissions')->where('id', $submission)->value('snapshot'), true);
$current = CandidateAccount::query()->findOrFail(DB::table('candidate_submissions')->where('id', $submission)->value('candidate_account_id'));
app(CandidateProfiles::class)->save($current, [...$this->profile, 'version' => 1, 'additional_availability' => [], 'additional_availability_references' => [], 'availability_reference' => territoryReference(true)]);
$before = [];
foreach (['people_processes', 'people_applications', 'people_reviews', 'candidate_submissions', 'candidate_triage', 'audit_logs'] as $table) {
$before[$table] = DB::table($table)->orderBy('id')->get()->toArray();
}
$page = $this->get('/pessoas/'.$this->process->id)->assertOk()->assertInertia(fn (Assert $page) => $page->where('candidates.0.received_availability.submission_id', $submission)->where('candidates.0.received_availability.profile_version', 1));
$availability = $page->inertiaProps('candidates.0.received_availability');
expect($availability['availability'])->toBe($received['profile']['data']['availability'])->and($availability['additional_availability'])->toBe($received['profile']['data']['additional_availability'])->and($availability['additional_availability_references'])->toBe([null, $reference]);
expect($availability)->not->toHaveKeys(['email', 'phone', 'documents', 'answers', 'candidate_account_id']);
$this->getJson('/pessoas/'.$this->process->id.'/dossier.json')->assertOk()->assertJsonPath('candidates.0.received_availability', $availability);
foreach ($before as $table => $rows) {
expect(DB::table($table)->orderBy('id')->get()->toArray())->toEqual($rows);
}
});
test('received availability projection hides the data from view-only and review-only scopes', function () {
$this->withoutVite();
receivedAvailabilityApplication($this);
$url = '/pessoas/'.$this->process->id;
$this->actingAs($this->reviewer)->get($url)->assertOk()->assertInertia(fn (Assert $page) => $page->missing('candidates.0.received_availability'));
$this->getJson($url.'/dossier.json')->assertOk()->assertJsonMissingPath('candidates.0.received_availability');
$this->actingAs($this->admin);
app(PeopleAccess::class)->assign($this->admin, $this->process->id, ['version' => DB::table('people_access_states')->where('people_process_id', $this->process->id)->value('version'), 'user_id' => $this->reviewer->id, 'scopes' => ['review', 'interviews'], 'reason' => 'Consultar fontes do perfil recebido para conferência.']);
$this->actingAs($this->reviewer)->get($url)->assertInertia(fn (Assert $page) => $page->has('candidates.0.received_availability'));
DB::table('people_process_access')->where('people_process_id', $this->process->id)->where('user_id', $this->reviewer->id)->update(['scopes' => json_encode(['view', 'review'])]);
$this->getJson($url.'/dossier.json')->assertOk()->assertJsonMissingPath('candidates.0.received_availability');
});
test('received availability projection is not added to selection matrices or their frozen decision basis', function () {
$this->withoutVite();
receivedAvailabilityApplication($this);
$this->travel(8)->days();
$record = $this->process->fresh();
app(PeopleHiring::class)->execute($this->admin, $record->id, 'close', ['version' => $record->version, 'reason' => 'Fechar recepção sintética para avaliar.']);
$application = DB::table('people_applications')->sole();
$this->actingAs($this->reviewer);
app(PeopleHiring::class)->execute($this->reviewer, $record->id, 'evaluate', ['version' => $record->fresh()->version, 'reason' => 'Avaliação sintética pelas competências.', 'no_conflict' => true, 'application_id' => $application->id, 'answers' => [['score' => '8', 'evidence' => 'Competências verificadas nesta avaliação sintética.']]]);
$this->actingAs($this->admin);
app(PeopleHiring::class)->execute($this->admin, $record->id, 'select', ['version' => $record->fresh()->version, 'reason' => 'Proposta sintética com base nas avaliações.', 'no_conflict' => true, 'application_id' => $application->id]);
$before = $record->fresh()->selection;
expect($before['rows'][0])->not->toHaveKey('received_availability');
$this->get('/pessoas/'.$record->id)->assertOk()->assertInertia(fn (Assert $page) => $page->has('candidates.0.received_availability')->missing('process.selection.rows.0.received_availability'));
$this->getJson('/pessoas/'.$record->id.'/dossier.json')->assertOk()->assertJsonMissingPath('process.selection.rows.0.received_availability');
expect($record->fresh()->selection)->toBe($before)->and(app(PeopleHiring::class)->matrix($record->fresh())[0])->not->toHaveKey('received_availability');
});
test('received availability projection does not invent geography for manual internal applications or refused submissions', function () {
$this->withoutVite();
$id = territoryReceived($this);
$this->postJson('/pessoas/'.$this->process->id.'/recebidas/'.$id.'/triagem', electronicTriagePayload($this, 'not_accepted'))->assertRedirect();
DB::table('people_applications')->insert(['tenant_id' => $this->tenant->id, 'people_process_id' => $this->process->id, 'created_by' => $this->admin->id, 'operation_key' => (string) Str::uuid(), 'reference' => 'MANUAL-SEM-GEO', 'name' => 'Pessoa manual sintética', 'summary' => 'Resumo sem origem electrónica associada.', 'received_on' => today(), 'created_at' => now(), 'updated_at' => now()]);
$this->get('/pessoas/'.$this->process->id)->assertInertia(fn (Assert $page) => $page->has('candidates', 1)->missing('candidates.0.received_availability'));
});
test('received availability projection keeps historical locality names when the catalogue is disabled', function () {
$this->withoutVite();
receivedAvailabilityApplication($this);
config(['geography.enabled' => false]);
$this->getJson('/pessoas/'.$this->process->id.'/dossier.json')->assertOk()->assertJsonPath('candidates.0.received_availability.availability.province', 'Niassa');
});
test('received availability projection preserves manual submitted text without assigning catalogue references', function () {
$this->withoutVite();
receivedAvailabilityApplication($this, ['availability_reference' => null]);
$this->getJson('/pessoas/'.$this->process->id.'/dossier.json')->assertOk()
->assertJsonPath('candidates.0.received_availability.availability', $this->profile['availability'])
->assertJsonPath('candidates.0.received_availability.availability_reference', null)
->assertJsonPath('candidates.0.received_availability.additional_availability_references', []);
});
test('received availability projection revalidates current actor authority and cannot cross processes', function () {
receivedAvailabilityApplication($this);
$record = $this->process->fresh();
$rows = app(PeopleHiring::class)->matrix($record);
$stale = $this->admin->fresh();
DB::table('users')->where('id', $this->admin->id)->update(['purchasing_role' => PurchasingRole::Requester->value]);
expect(app(ReceivedCandidateAvailability::class)->attach($stale, $record, $rows)[0])->not->toHaveKey('received_availability');
DB::table('users')->where('id', $this->admin->id)->update(['purchasing_role' => PurchasingRole::Admin->value]);
$other = app(PeopleHiring::class)->execute($this->admin, null, 'create', ['operation_key' => (string) Str::uuid(), 'kind' => 'service', 'title' => 'Outro processo da projecção', 'purpose' => 'Processo sintético distinto para verificar o âmbito.', 'closes_on' => today()->addWeek()->toDateString(), 'minimum_reviews' => 1, 'criteria' => [['label' => 'Competências', 'weight' => 100]]]);
expect(app(ReceivedCandidateAvailability::class)->attach($this->admin, $other, $rows)[0])->not->toHaveKey('received_availability');
});
test('candidate territory filters main and additional frozen references without classifying matching manual names', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
$reference = territoryReference();
$main = territoryReceived($this, ['availability_reference' => $reference]);
$extra = territoryReceived($this, ['availability_reference' => territoryReference(true), 'additional_availability' => [$this->profile['availability'], []], 'additional_availability_references' => [null, $reference]]);
$snapshot = json_decode(DB::table('candidate_submissions')->where('id', $main)->value('snapshot'), true);
territoryReceived($this, ['availability' => $snapshot['profile']['data']['availability']]);
territoryReceived($this, ['availability_reference' => territoryReference(true)]);
$url = '/pessoas/'.$this->process->id.'/recebidas';
$this->get($url.'?location=2')->assertInertia(fn (Assert $page) => $page->where('submissions.total', 2)->where('submissions.data.0.id', $main)->where('submissions.data.1.id', $extra)->where('geographyFilter.path.0.name', 'Niassa'));
$this->get($url)->assertInertia(fn (Assert $page) => $page->where('submissions.total', 4));
expect(DB::table('candidate_triage')->count())->toBe(0)->and(DB::table('people_reviews')->count())->toBe(0);
expect(app(CandidateTriage::class)->pending($this->process->fresh()))->toBe(4);
});
test('candidate territory never replaces the received location with the current profile location', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
$id = territoryReceived($this, ['availability_reference' => territoryReference()]);
$row = DB::table('candidate_submissions')->where('id', $id)->first();
$account = CandidateAccount::query()->findOrFail($row->candidate_account_id);
app(CandidateProfiles::class)->save($account, [...$this->profile, 'version' => 1, 'availability_reference' => territoryReference(true)]);
$this->get('/pessoas/'.$this->process->id.'/recebidas?location=2')->assertInertia(fn (Assert $page) => $page->where('submissions.total', 1)->where('submissions.data.0.id', $id));
expect(DB::table('candidate_submissions')->where('id', $id)->value('snapshot'))->toBe($row->snapshot);
});
test('candidate territory paginates oldest matching submissions and keeps focus and filter together', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
$ids = [];
for ($index = 0; $index < 21; $index++) {
$ids[] = territoryReceived($this, ['availability_reference' => territoryReference()]);
}
$url = '/pessoas/'.$this->process->id.'/recebidas';
$this->get($url.'?location=2')->assertInertia(fn (Assert $page) => $page->where('submissions.total', 21)->has('submissions.data', 20)->where('submissions.data.0.id', $ids[0]));
$response = $this->get($url.'?location=2&page=2')->assertInertia(fn (Assert $page) => $page->has('submissions.data', 1)->where('submissions.data.0.id', $ids[20]));
expect($response->inertiaProps('submissions.prev_page_url'))->toContain('location=2');
$this->get($url.'?location=2&submission='.$ids[0])->assertInertia(fn (Assert $page) => $page->where('submissions.total', 1)->where('submissions.data.0.id', $ids[0]));
});
test('candidate territory keeps the OR branches inside the process and tenant boundaries', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
$id = territoryReceived($this, ['additional_availability' => [[]], 'additional_availability_references' => [territoryReference()]]);
$copy = (array) DB::table('candidate_submissions')->where('id', $id)->first();
unset($copy['id']);
$other = $this->process->replicate();
$other->operation_key = (string) Str::uuid();
$other->title = 'Outro processo sintético';
$other->save();
DB::table('candidate_submissions')->insert([...$copy, 'people_process_id' => $other->id, 'operation_key' => (string) Str::uuid(), 'reference' => (string) Str::uuid()]);
$foreign = Tenant::query()->create(['name' => 'Outra empresa territorial']);
$foreignAdmin = User::factory()->create(['tenant_id' => $foreign->id, 'purchasing_role' => PurchasingRole::Admin]);
$this->actingAs($foreignAdmin);
app(CandidateProfiles::class)->configure($foreignAdmin, ['version' => 0, 'registration_open' => true, 'notice' => 'Aviso sintético da outra empresa para teste de isolamento.', 'reason' => 'Preparar origem sintética de outra empresa.']);
$foreignAccount = app(CandidateProfiles::class)->register($foreign->id, [...$this->credentials, 'email' => Str::uuid().'@foreign.example.test']);
$this->actingAs($this->admin);
$foreignProcess = $this->process->replicate();
$foreignProcess->operation_key = (string) Str::uuid();
$foreignProcess->tenant_id = $foreign->id;
$foreignProcess->created_by = $foreignAdmin->id;
$foreignProcess->save();
DB::table('candidate_submissions')->insert([...$copy, 'candidate_account_id' => $foreignAccount->id, 'tenant_id' => $foreign->id, 'people_process_id' => $foreignProcess->id, 'operation_key' => (string) Str::uuid(), 'reference' => (string) Str::uuid()]);
$this->get('/pessoas/'.$this->process->id.'/recebidas?location=2')->assertInertia(fn (Assert $page) => $page->where('submissions.total', 1)->where('submissions.data.0.id', $id));
$this->getJson('/pessoas/'.$foreignProcess->id.'/divisao-administrativa')->assertNotFound();
});
test('candidate territory respects revoked scoped access and candidate identities on both routes', function () {
config(['geography.enabled' => true]);
$base = '/pessoas/'.$this->process->id;
$this->actingAs($this->reviewer)->getJson($base.'/divisao-administrativa')->assertForbidden();
$this->actingAs($this->admin);
DB::table('people_process_access')->where('people_process_id', $this->process->id)->where('user_id', $this->admin->id)->update(['expires_at' => now()->subMinute()]);
$this->getJson($base.'/divisao-administrativa')->assertForbidden();
$this->getJson($base.'/recebidas?location=2')->assertForbidden();
Auth::guard('web')->logout();
$this->getJson($base.'/divisao-administrativa')->assertUnauthorized();
});
test('candidate territory has a people-scoped catalogue without opening the procurement context', function () {
config(['geography.enabled' => true, 'work_context.entitlements.'.$this->tenant->id => ['people.internal']]);
$this->tenant->update(['work_context_policy' => ['revision' => 1, 'enabled' => ['people.internal']]]);
$this->post('/contexto', ['area' => 'people', 'option' => 'people.internal'])->assertRedirect();
$this->getJson('/pessoas/'.$this->process->id.'/divisao-administrativa')->assertOk()->assertJsonCount(11, 'data')->assertHeader('Cache-Control', 'max-age=0, no-store, private');
$this->getJson('/api/divisao-administrativa')->assertForbidden();
});
test('candidate territory rejects invalid locations and can be disabled without losing the unfiltered inbox', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
$url = '/pessoas/'.$this->process->id;
$this->getJson($url.'/recebidas?location=999999')->assertUnprocessable();
$this->getJson($url.'/recebidas?location[]=2')->assertUnprocessable();
$this->getJson($url.'/recebidas?page=0')->assertUnprocessable();
config(['geography.enabled' => false]);
$this->getJson($url.'/divisao-administrativa')->assertServiceUnavailable();
$this->getJson($url.'/recebidas?location=2')->assertServiceUnavailable();
$this->get($url.'/recebidas')->assertInertia(fn (Assert $page) => $page->where('geographyEnabled', false));
});
test('candidate territory excludes references from other editions and handles missing additional lists', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
$id = territoryReceived($this, ['availability_reference' => territoryReference()]);
$snapshot = json_decode(DB::table('candidate_submissions')->where('id', $id)->value('snapshot'), true);
$snapshot['profile']['data']['availability_reference']['catalogue'] = str_repeat('0', 64);
$snapshot['profile']['data']['additional_availability_references'] = ['not_a_list' => territoryReference()];
$copy = (array) DB::table('candidate_submissions')->where('id', $id)->first();
unset($copy['id']);
$account = CandidateAccount::query()->findOrFail($copy['candidate_account_id'])->replicate();
$account->email = Str::uuid().'@historical.example.test';
$account->save();
$stale = DB::table('candidate_submissions')->insertGetId([...$copy, 'candidate_account_id' => $account->id, 'operation_key' => (string) Str::uuid(), 'reference' => (string) Str::uuid(), 'snapshot' => json_encode($snapshot, JSON_THROW_ON_ERROR)]);
territoryReceived($this);
$url = '/pessoas/'.$this->process->id.'/recebidas?location=2';
$this->get($url)->assertInertia(fn (Assert $page) => $page->where('submissions.total', 1)->where('submissions.data.0.id', $id));
$this->get($url.'&submission='.$stale)->assertInertia(fn (Assert $page) => $page->where('submissions.total', 0));
});
test('candidate geography submission freezes canonical location and does not change when profile is edited later', function () {
config(['geography.enabled' => true]);
$reference = ['country' => 'MZ', 'catalogue' => config('geography.source_sha256'), 'record_id' => '5'];
$this->post($this->base.'/perfil', [...$this->profile, 'version' => 1, 'availability_reference' => $reference])->assertSessionHasNoErrors();
$location = app(CandidateProfiles::class)->profile($this->account)['data']['availability'];
$body = [...$this->body, 'profile_version' => 2, 'answers' => [...$this->body['answers'], 'availability_location' => $location]];
$this->post($this->base.'/oportunidades/'.$this->process->id, $body)->assertRedirect();
$submission = DB::table('candidate_submissions')->sole();
$snapshot = json_decode($submission->snapshot, true);
expect($snapshot['profile']['data']['availability_reference'])->toBe($reference)->and($snapshot['profile']['data']['availability'])->toBe($location);
$this->post($this->base.'/perfil', [...$this->profile, 'version' => 2, 'availability_reference' => null])->assertSessionHasNoErrors();
$this->post($this->base.'/oportunidades/'.$this->process->id, $body)->assertRedirect();
expect(DB::table('candidate_submissions')->count())->toBe(1)->and(DB::table('candidate_submissions')->sole()->snapshot)->toBe($submission->snapshot);
});
test('candidate additional geography submission reuses a selected additional location and freezes references', function () {
config(['geography.enabled' => true]);
$record = collect(app(AdministrativeGeography::class)->catalogue()['records'])->first(fn ($record) => $record['selectable'] && $record['level'] === 4);
$reference = ['country' => 'MZ', 'catalogue' => config('geography.source_sha256'), 'record_id' => $record['id']];
$this->post($this->base.'/perfil', [...$this->profile, 'version' => 1, 'additional_availability' => [[]], 'additional_availability_references' => [$reference]])->assertSessionHasNoErrors();
$location = app(CandidateProfiles::class)->profile($this->account)['data']['additional_availability'][0];
$body = [...$this->body, 'profile_version' => 2, 'answers' => [...$this->body['answers'], 'availability_location' => $location]];
$this->post($this->base.'/oportunidades/'.$this->process->id, $body)->assertRedirect();
$submission = DB::table('candidate_submissions')->sole();
$snapshot = json_decode($submission->snapshot, true);
expect($snapshot['profile']['data']['additional_availability_references'])->toBe([$reference])->and($snapshot['answers']['availability_location'])->toBe($location);
$this->post($this->base.'/perfil', [...$this->profile, 'version' => 2, 'additional_availability' => [], 'additional_availability_references' => []])->assertSessionHasNoErrors();
$this->post($this->base.'/oportunidades/'.$this->process->id, $body)->assertRedirect();
expect(DB::table('candidate_submissions')->count())->toBe(1)->and(DB::table('candidate_submissions')->sole()->snapshot)->toBe($submission->snapshot)->and(DB::table('candidate_triage')->count())->toBe(0);
});
test('guests see only explicitly published public opportunity fields while submission remains authenticated', function () {
Auth::guard('candidate')->logout();
Auth::guard('web')->logout();