Files
SGF/scripts/sgf-fiscal-obligations-boundaries.php
2026-11-11 09:00:17 +02:00

15 lines
4.7 KiB
PHP

<?php
require __DIR__.'/sgf-fiscal-obligations-proof-bootstrap.php';$host->beginTransaction();try{
$key=(string)Illuminate\Support\Str::uuid();[$a]=$foCommand(13,$foInput($key,0,$foData));[$approved]=$foCommand(14,$foInput($key,1,['decision'=>'approved'],'review'));$template=(array)$host->table('fo_versions')->find($approved['id']);unset($template['id']);for($i=0;$i<29;$i++){$row=$template;$row['record_key']=(string)Illuminate\Support\Str::uuid();$row['previous_id']=null;$row['reason']='Fixture SQL de relatório, não obrigação de negócio criada.';$data=json_decode($row['data'],true);if($i===0)$data['due_on']=null;if($i===1)$data['due_on']='2026-12-16';$row['data']=json_encode($data);$host->table('fo_versions')->insert($row);}
$p1=$foList(17,['per_page'=>25]);$p2=$foList(17,['page'=>2,'per_page'=>25]);$p100=$foList(17,['per_page'=>100]);$ok(count($p1['records']['data'])===25&&count($p2['records']['data'])===5,'latest records paginated25+5');$ok(array_column(array_merge($p1['records']['data'],$p2['records']['data']),'id')===array_column($p100['records']['data'],'id'),'ordered25 pages equal100 without duplicate');$ok($foList(17,['due_state'=>'unknown'])['records']['total']===1,'unknown due filter SQL');$ok($foList(17,['due_state'=>'overdue','reference_on'=>'2026-12-16'])['records']['total']===28,'overdue filter SQL');$ok($foList(17,['due_state'=>'not_overdue'])['records']['total']===29,'same day future filter SQL');$ok($foList(17,['due_from'=>'2026-12-16','due_to'=>'2026-12-16'])['records']['total']===1,'deadline range before pagination');$ok($foList(17,['status'=>'active','period'=>$foData['period']])['records']['total']===30,'status and period combined');$ok($foList(17,['period'=>'2026-10'])['records']['total']===0,'different source period no fallback');$ok($foList(17,['page'=>100000])['records']['data']===[],'far page no fallback');$reject(fn()=>$foList(17,['due_from'=>'2026-12-16']),422,'incomplete deadline range refused');$reject(fn()=>$foList(17,['due_from'=>'2026-12-16','due_to'=>'2026-12-15']),422,'inverted deadline range refused');
// Scoped foreign fixture must never be read, even when the source map ID matches.
$row=$template;$row['record_key']=(string)Illuminate\Support\Str::uuid();$row['previous_id']=null;$unit=$host->table('units')->where('company_id',2)->where('id','<>',2)->first();if($unit){$row['unit_id']=$unit->id;$host->table('fo_versions')->insert($row);$ok($foList(17,['per_page'=>100])['records']['total']===30,'foreign unit fixture excluded');$reject(fn()=>$foList(17,['record_key'=>$row['record_key']]),403,'foreign unit history refused');}
$selectFiscal(17);request()->attributes->set('sgf_module','finance');$reject(fn()=>$fo->listing(17,$foQuery),403,'wrong active module refused');config(['fiscal_obligations.enabled'=>false]);$reject(fn()=>$foList(),404,'disabled feature refused');config(['fiscal_obligations.enabled'=>true]);
// Existing reviewed key, amendment cut, then authority/identity/map changes.
$amend=array_replace($foData,['operation'=>'amend','due_on'=>'2026-12-16']);$cut=$foPreview(13,$foInput($key,2,$amend));$host->beginTransaction();try{$host->table('permission_grants')->where(['user_id'=>13,'permission'=>'fiscal_forms.view','unit_id'=>2])->delete();$reject(fn()=>$foConfirm(13,$cut['preview']),403,'source access revoked revalidated');}finally{$host->rollBack();}
$host->beginTransaction();try{$host->table('users')->where('id',13)->update(['name'=>'QA identidade alterada apenas no savepoint']);$reject(fn()=>$foConfirm(13,$cut['preview']),409,'actor identity change invalidates preview');}finally{$host->rollBack();}
$host->beginTransaction();try{$map=(array)$host->table('fx_versions')->find($foSource['form_id']);unset($map['id']);$map['version']++;$map['previous_id']=$foSource['form_id'];$map['state']='prepared';$map['action']='prepare';$map['actor_id']=13;$host->table('fx_versions')->insert($map);$reject(fn()=>$foConfirm(13,$cut['preview']),409,'map change after preview requires new conference');}finally{$host->rollBack();}
$immutable=false;$host->beginTransaction();try{$host->table('fo_versions')->where('id',$approved['id'])->update(['reason'=>'Tentativa de reescrever evidência aprovada']);}catch(Illuminate\Database\QueryException$e){$immutable=str_contains($e->getMessage(),'SGF_FISCAL_OBLIGATIONS_APPEND_ONLY');}finally{$host->rollBack();}$ok($immutable,'versions database append-only');
}finally{while($host->transactionLevel()>0)$host->rollBack();}
file_put_contents($work.'/fiscal-obligations-boundaries.json',json_encode(['passed'=>true,'checks'=>$checks,'sql_report_fixtures'=>29,'fixtures_rolled_back'=>true,'business_commands_8085'=>0],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'checks'=>count($checks),'proof'=>$work.'/fiscal-obligations-boundaries.json']).PHP_EOL;