Files
Investidor-V2/outputs/investmoz-legado-trio-QA-20261015/prove-trio-QA.php
T
nala 6a76aac80f
tests / ci (push) Canceled after 0s
versao compilada 1
2026-10-16 09:17:28 +02:00

34 lines
11 KiB
PHP

<?php
ini_set('zend.exception_ignore_args','1');set_exception_handler(function(Throwable $e){fwrite(STDERR,$e::class.': '.$e->getMessage().' line '.$e->getLine().PHP_EOL);exit(1);});
if(is_file(__DIR__.'/PROVA-TRIO-QA.json'))throw new RuntimeException('Preserve completed proof');require __DIR__.'/boot-QA.php';
use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Artisan;use Illuminate\Support\Facades\Storage;use Illuminate\Support\Str;
$checks=[];$calls=[];$check=function($name,$v)use(&$checks){if(!$v)throw new RuntimeException('Failed: '.$name);$checks[$name]=true;};
$snapshot=function(){ $tables=[];foreach(DB::select("select tablename from pg_tables where schemaname='public' order by tablename")as$t){$name='"'.str_replace('"','""',$t->tablename).'"';$rows=DB::select('select row_to_json(t)::text as v from '.$name.' t order by row_to_json(t)::text');$tables[$t->tablename]=hash('sha256',implode("\n",array_column($rows,'v')));} $files=[];foreach(Storage::disk('local')->files('portal-documents')as$f)$files[$f]=hash_file('sha256',Storage::disk('local')->path($f));ksort($files);return ['tables'=>$tables,'files'=>$files];};
$call=function($args)use(&$calls){$code=Artisan::call('documents:import-batch',$args);$calls[]=['args'=>$args,'exit'=>$code,'output'=>Artisan::output()];return $code;};
$folder='legacy-trio-20261015';$stage=$work.'/storage/app/private/document-import-staging/'.$folder;if(is_dir($stage))throw new RuntimeException('Preserve prepared staging');mkdir($stage,0700,true);foreach(glob(__DIR__.'/lote-privado/'.$folder.'/*')as$f)copy($f,$stage.'/'.basename($f));$mf=$stage.'/manifest.json';$raw=file_get_contents($mf);$records=json_decode($raw,true)['documents'];
$manager=App\Models\User::findOrFail(2);$check('responsible_effectively_authorized',$manager->can('manage-portal'));$bad=App\Models\User::all()->first(fn($u)=>!$u->can('manage-portal'));$check('existing_denied_actor_available',$bad!==null);
$base=$snapshot();$old=DB::table('portal_resources as r')->join('documents as d','d.id','=','r.document_id')->orderBy('r.id')->get(['r.*','d.id as document_uuid','d.sha256','d.rights_status as file_rights','d.data_classification'])->toJson();
$good=['manifest'=>$folder.'/manifest.json','--sectors-trio'=>true,'--actor'=>'2'];$apply=$good+['--apply'=>true];
$check('preview_success',$call($good)===0);$check('preview_no_write',$snapshot()===$base);
file_put_contents($mf,$raw."\n");$check('changed_manifest_refused',$call($apply)!==0);file_put_contents($mf,$raw);$check('changed_manifest_no_write',$snapshot()===$base);
$last=$work.'/storage/app/private/document-import-staging/'.$records[2]['relative_path'];$bytes=file_get_contents($last);file_put_contents($last,$bytes.'changed');$check('last_article_tamper_refused_before_first_write',$call($apply)!==0);file_put_contents($last,$bytes);$check('tamper_no_write',$snapshot()===$base);
foreach(['missing'=>null,'nonexistent'=>'99999999','decimal'=>'2.0','unauthorized'=>(string)$bad->id]as$label=>$id){$args=$apply;if($id===null)unset($args['--actor']);else$args['--actor']=$id;$check('actor_'.$label.'_refused',$call($args)!==0);$check('actor_'.$label.'_no_write',$snapshot()===$base);}
foreach(['--pilot-agronegocio','--sectors-lot20']as$flag){$check('mixed_'.$flag.'_refused',$call($apply+[$flag=>true])!==0);$check('mixed_'.$flag.'_no_write',$snapshot()===$base);}
$image=DB::table('documents')->where('external_id','legacy-tempo-image-28f41f12e10ab345')->first();$check('shared_image_exists',$image!==null);$imageBefore=DB::table('portal_resources')->where('document_id',$image->id)->first();$check('shared_image_published_baseline',(bool)$imageBefore->published);
DB::beginTransaction();try{DB::table('documents')->where('id',$image->id)->update(['rights_status'=>'restricted']);$restricted=$snapshot();$check('reused_image_restricted_refused',$call($apply)!==0);$check('restricted_image_no_import_write',$snapshot()===$restricted);}finally{DB::rollBack();}
$imagePath=Storage::disk('local')->path($image->storage_key);$imageBytes=file_get_contents($imagePath);try{file_put_contents($imagePath,$imageBytes.'changed');$check('reused_image_wrong_bytes_refused',$call($apply)!==0);}finally{file_put_contents($imagePath,$imageBytes);}$check('reuse_failures_no_write',$snapshot()===$base);
$docs=DB::table('documents')->count();$resources=DB::table('portal_resources')->count();$scans=DB::table('document_malware_scans')->count();$check('private_import_success',$call($apply)===0);
$entries=DB::table('documents as d')->join('portal_resources as r','r.document_id','=','d.id')->whereIn('d.external_id',array_column($records,'external_id'))->orderBy('r.id')->get(['r.id','r.document_id','r.published','r.metadata_version','d.external_id','d.sha256','d.data_classification','d.malware_scan_status','d.malware_scanned_sha256']);
$check('exactly_three_new_articles_zero_images',count($entries)===3&&DB::table('documents')->count()===$docs+3&&DB::table('portal_resources')->count()===$resources+3&&DB::table('document_malware_scans')->count()===$scans+3);
$check('private_and_unpublished',$entries->every(fn($e)=>!$e->published&&$e->data_classification==='internal'));$check('three_real_scans_same_bytes_clean',$entries->every(fn($e)=>$e->malware_scan_status==='clean'&&trim($e->sha256)===$e->malware_scanned_sha256));$after=$snapshot();$check('replay_success',$call($apply)===0);$check('replay_no_duplicate_or_write',$snapshot()===$after);
$reader=app(App\Services\LegacyContent::class);foreach(['tech-fintech','imobiliario','bens-de-consumo-franquias']as$slug){try{$reader->article($slug);$check('private_'.$slug.'_not_public',false);}catch(Symfony\Component\HttpKernel\Exception\HttpException $e){$check('private_'.$slug.'_not_public',$e->getStatusCode()===404);}}
// QA controller integration with an explicitly selected authorized test request actor.
// This is not a browser/MFA acceptance or authorization to publish in the active destination.
$controller=app(App\Http\Controllers\PortalServicesController::class);foreach($entries as$e){$m=collect($records)->firstWhere('external_id',$e->external_id);$data=array_intersect_key($m,array_flip(['title','description','category','topic','issuer','document_date','version_label','source_url','source_reference','document_type','language']));$data+=['document_id'=>$e->document_id,'metadata_version'=>1,'source_confirmation'=>'identified','legal_status'=>'not_applicable','rights_status'=>'authorized','territorial_scope'=>'unknown','data_classification'=>'public','independent_review'=>false,'legislation'=>false,'essential'=>false,'associations'=>[]];$data['source_reference'].=' Preparação/publicação exclusivamente QA autorizada pelo utilizador; ativo permanece por decidir.';$request=Illuminate\Http\Request::create('/management/portal-services/resources/'.$e->id,'PUT',$data);$request->setUserResolver(fn()=>$manager);$controller->updateResource($request,$e->id);$check('metadata_does_not_publish_'.$e->id,!DB::table('portal_resources')->where('id',$e->id)->value('published'));$request=Illuminate\Http\Request::create('/management/portal-services/resources/'.$e->id,'POST',['published'=>true]);$request->setUserResolver(fn()=>$manager);$controller->moderate($request,'resources',$e->id);}
$resolved=[];foreach(config('legacy_content.articles')as$slug=>$identity){if(!in_array($identity['external_id'],array_column($records,'external_id')))continue;$article=$reader->article($slug);$source=$reader->articleForResource($article['id']);$check('sector_'.$slug,$article['sector']['slug']===$identity['sector_slug']);$check('existing_image_reused_'.$slug,$article['image']['sha256']===trim($image->sha256)&&$article['image']['href']==='/documentos/'.$imageBefore->id.'/view');$check('context_'.$slug,$source['start_href']==='/investimentos/a-partir-de/'.$slug&&$source['version']===3);$resolved[]=['slug'=>$slug,'resource_id'=>$article['id'],'sha256'=>$article['sha256'],'version'=>$article['version'],'source_url'=>$article['source_url'],'image_href'=>$article['image']['href'],'landing_href'=>$article['landing_href'],'sector'=>$article['sector'],'start_href'=>$article['start_href']];}
$check('all_three_resolved',count($resolved)===3);$existing=DB::table('portal_resources as r')->join('documents as d','d.id','=','r.document_id')->whereNotIn('r.id',$entries->pluck('id'))->orderBy('r.id')->get(['r.*','d.id as document_uuid','d.sha256','d.rights_status as file_rights','d.data_classification'])->toJson();$check('all_preexisting_library_rows_and_image_unchanged',$old===$existing);
$owner=App\Models\User::forceCreate(['name'=>'QA trio synthetic owner','email'=>'qa-trio-owner@example.invalid','password'=>Illuminate\Support\Facades\Hash::make(Str::random(40)),'email_verified_at'=>now()]);$path=app(App\Services\InvestorPath::class);$desk=app(App\Services\VirtualDesk::class);$cases=[];
foreach($resolved as$a){$caseId=$path->create($owner,['title'=>'QA trio '.$a['slug'],'client_key'=>(string)Str::uuid(),'reuse_consent'=>true]);$case=$desk->case($owner,$caseId);$reader->attach($owner,$caseId,['version'=>$case->version,'resource_id'=>$a['resource_id']]);$view=$path->view($owner,$caseId);$ref=$view['content_sources'];$check('dossier_source_'.$a['slug'],count($ref)===1&&$ref[0]['resource_id']===$a['resource_id']&&$ref[0]['sha256']===$a['sha256']&&$ref[0]['source_url']===$a['source_url']&&$ref[0]['metadata_version']===3);$afterAttach=$desk->case($owner,$caseId);$eventCount=DB::table('desk_events')->where('case_id',$caseId)->count();$reader->attach($owner,$caseId,['version'=>$afterAttach->version,'resource_id'=>$a['resource_id']]);$check('dossier_reference_replay_'.$a['slug'],$desk->case($owner,$caseId)->version===$afterAttach->version&&DB::table('desk_events')->where('case_id',$caseId)->count()===$eventCount);$cases[]=['id'=>$caseId,'owner_id'=>$owner->id,'slug'=>$a['slug'],'resource_id'=>$a['resource_id'],'sha256'=>$a['sha256'],'return_href'=>'/'.$a['slug']];}
$service=DB::table('desk_service_versions')->where('code','project-preparation')->where('status','published')->orderByDesc('version')->first(['id','code','version']);$check('support_resolved_by_destination_code',$service!==null);
$v=['at'=>gmdate('c'),'target'=>$target,'result'=>'passed','checks'=>$checks,'resources'=>$entries,'resolved_origins'=>$resolved,'QA_dossiers'=>$cases,'support'=>$service,'shared_image_resource_id'=>$imageBefore->id,'new_images'=>0,'calls'=>$calls,'scope'=>'Three new articles only; QA controller/service integration, not browser MFA acceptance; existing accepted sectors not retested','active_changes'=>0];file_put_contents(__DIR__.'/PROVA-TRIO-QA.json',json_encode($v,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR));echo json_encode(['result'=>'passed','checks'=>count($checks),'new_articles'=>3,'new_images'=>0,'dossiers'=>3,'active_changes'=>0]);