26 lines
9.2 KiB
PHP
26 lines
9.2 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);});
|
|
$root=dirname(__DIR__,2);$out=__DIR__;$work='C:/Users/macbook/.config/investmoz/legacy-import-delta-QA-20261015';if(is_dir($work))throw new RuntimeException('Preserve existing QA run');mkdir($work,0700,true);require $root.'/tools/portal-qa-environment.php';$target=portalQaEnvironment();$target['database']='legacy_import_delta_20261015_test';foreach(['DB_DATABASE'=>$target['database'],'QUEUE_CONNECTION'=>'sync']as$k=>$v){putenv($k.'='.$v);$_ENV[$k]=$_SERVER[$k]=$v;}
|
|
require $root.'/vendor/autoload.php';require $out.'/fonte-QA/app/Console/Commands/ImportDocumentBatch.php';$app=require $root.'/bootstrap/app.php';$app->useStoragePath($work.'/storage');foreach(['app/private/document-import-staging/legacy-agronegocio','logs','framework/views','framework/cache/data']as$d)mkdir($work.'/storage/'.$d,0700,true);$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();portalQaAssertTarget($target);config(['filesystems.disks.local.root'=>$work.'/storage/app/private','cache.default'=>'array','queue.default'=>'sync','portal.legacy_content_qa_enabled'=>true]);
|
|
use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Artisan;
|
|
if(!App\Models\User::findOrFail(2)->can('manage-portal'))throw new RuntimeException('Responsible account is not effectively authorized');$denied=App\Models\User::all()->first(fn($u)=>!$u->can('manage-portal'));if(!$denied)throw new RuntimeException('Need existing unauthorized actor');
|
|
$stage=$work.'/storage/app/private/document-import-staging';foreach(['manifest.json','Agronegocio.png','agronegocio-source.txt']as$n)copy($root.'/storage/app/private/document-import-staging/legacy-agronegocio/'.$n,$stage.'/legacy-agronegocio/'.$n);
|
|
$manifest='legacy-agronegocio/manifest.json';$mf=$stage.'/'.$manifest;$raw=file_get_contents($mf);$image=$stage.'/legacy-agronegocio/Agronegocio.png';$imageBytes=file_get_contents($image);$checks=[];$calls=[];
|
|
$check=function($name,$v)use(&$checks){if(!$v)throw new RuntimeException('Failed: '.$name);$checks[$name]=true;};
|
|
$snapshot=function()use($work){$tables=[];foreach(DB::select("select tablename from pg_tables where schemaname='public' order by tablename")as$t){$n='"'.str_replace('"','""',$t->tablename).'"';$rows=DB::select('select row_to_json(t)::text as value from '.$n.' t order by row_to_json(t)::text');$tables[$t->tablename]=hash('sha256',implode("\n",array_map(fn($r)=>$r->value,$rows)));}$files=[];$dir=$work.'/storage/app/private/portal-documents';if(is_dir($dir)){foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir,FilesystemIterator::SKIP_DOTS))as$f)if($f->isFile())$files[$f->getFilename()]=hash_file('sha256',$f->getPathname());}ksort($files);return ['tables'=>$tables,'files'=>$files];};
|
|
$call=function(array$args)use(&$calls){$exit=Artisan::call('documents:import-batch',$args);$text=Artisan::output();$calls[]=['args'=>$args,'exit'=>$exit,'output'=>$text];return $exit;};$good=['manifest'=>$manifest,'--pilot-agronegocio'=>true,'--actor'=>'2'];$apply=$good+['--apply'=>true];
|
|
$base=$snapshot();$check('preview_success',$call($good)===0);$check('preview_no_database_or_original_file_write',$snapshot()===$base);
|
|
file_put_contents($mf,$raw."\n");$check('modified_manifest_refused',$call($apply)!==0);file_put_contents($mf,$raw);$check('modified_manifest_no_write',$snapshot()===$base);
|
|
mkdir($stage.'/other-pilot');file_put_contents($stage.'/other-pilot/manifest.json',$raw);$check('different_manifest_path_refused',$call(array_replace($apply,['manifest'=>'other-pilot/manifest.json']))!==0);$check('different_path_no_write',$snapshot()===$base);
|
|
file_put_contents($image,$imageBytes.'changed');$check('wrong_image_hash_refused',$call($apply)!==0);file_put_contents($image,$imageBytes);$check('wrong_second_entry_prevents_first_entry_write',$snapshot()===$base);
|
|
$article=$stage.'/legacy-agronegocio/agronegocio-source.txt';$articleBytes=file_get_contents($article);file_put_contents($article,$articleBytes.'changed');$check('wrong_article_hash_refused',$call($apply)!==0);file_put_contents($article,$articleBytes);$check('wrong_article_no_write',$snapshot()===$base);
|
|
foreach(['missing'=>null,'unknown'=>'999999999','decimal'=>'2.0','unauthorized'=>(string)$denied->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);}
|
|
$managers=config('portal.managers');config(['portal.managers'=>[]]);$check('effective_permission_removed_refused',$call($apply)!==0);config(['portal.managers'=>$managers]);$check('effective_permission_removed_no_write',$snapshot()===$base);
|
|
$revisionBefore=DB::table('portal_resource_revisions')->count();$resourcesBefore=DB::table('portal_resources')->count();$documentsBefore=DB::table('documents')->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',['legacy-tempo-post-171985-20260420','legacy-tempo-agronegocio-image-5e3840705cf98226'])->get(['d.id','d.external_id','d.sha256','d.storage_key','d.uploaded_by','d.data_classification','d.status','d.malware_scan_status','d.malware_scanned_sha256','r.id as resource_id','r.published','r.metadata_version','r.created_by']);$check('exactly_two_new_resources',count($entries)===2&&DB::table('documents')->count()===$documentsBefore+2&&DB::table('portal_resources')->count()===$resourcesBefore+2&&DB::table('portal_resource_revisions')->count()===$revisionBefore+2);foreach($entries as$e){$check('private_'.$e->resource_id,!$e->published&&$e->data_classification==='internal'&&(int)$e->uploaded_by===2&&(int)$e->created_by===2);$check('private_bytes_'.$e->resource_id,hash_file('sha256',$work.'/storage/app/private/'.$e->storage_key)===trim($e->sha256));}
|
|
$after=$snapshot();$check('replay_success',$call($apply)===0);$check('replay_no_database_or_file_change',$snapshot()===$after);
|
|
$reader=app(App\Services\LegacyContent::class);try{$reader->article('agronegocio');$check('unpublished_reader_denied',false);}catch(Symfony\Component\HttpKernel\Exception\HttpException $e){$check('unpublished_reader_denied',$e->getStatusCode()===404);}
|
|
$clean=$entries->every(fn($e)=>$e->malware_scan_status==='clean'&&trim($e->sha256)===$e->malware_scanned_sha256);$check('real_scan_same_bytes_clean',$clean);
|
|
DB::beginTransaction();try{foreach($entries as$e){DB::table('documents')->where('id',$e->id)->update(['rights_status'=>'authorized','data_classification'=>'public']);DB::table('portal_resources')->where('id',$e->resource_id)->update(['rights_status'=>'authorized','published'=>true]);}$articleData=$reader->article('agronegocio');$check('reader_recovers_imported_identifier',$articleData['legacy_id']===171985&&$entries->contains(fn($e)=>(int)$e->resource_id===$articleData['id']));$check('reader_recovers_imported_image',$articleData['image']!==null&&$articleData['image']['sha256']==='5e3840705cf98226d1f189cf665aaa98cb05da96c645159a1332a1d429d642eb');}finally{DB::rollBack();}$check('eligibility_projection_rolled_back',$snapshot()===$after);
|
|
$cmd=new App\Console\Commands\ImportDocumentBatch;$method=new ReflectionMethod($cmd,'validatePilotBackup');$bp=['verified_at'=>gmdate('c'),'target'=>['database'=>'investemoz','port'=>5432],'restore_target'=>['database'=>'legacy_import_delta_20261015_test','port'=>55441],'restore_verified'=>true,'files_verified'=>true,'dump_path'=>$mf,'sha256'=>hash_file('sha256',$mf),'files_path'=>$image,'files_sha256'=>hash_file('sha256',$image)];$proof=$work.'/backup-validation-fixture.json';file_put_contents($proof,json_encode($bp));$method->invoke($cmd,$proof);$check('backup_validator_accepts_valid_fixture',true);foreach(['expired','wrong_target','wrong_hash','restore_not_verified','missing']as$kind){$bad=$bp;if($kind==='expired')$bad['verified_at']=gmdate('c',time()-90000);if($kind==='wrong_target')$bad['target']['database']='another';if($kind==='wrong_hash')$bad['sha256']=str_repeat('0',64);if($kind==='restore_not_verified')$bad['restore_verified']=false;file_put_contents($proof,json_encode($bad));try{$method->invoke($cmd,$kind==='missing'?$proof.'.missing':$proof);$check('backup_'.$kind.'_refused',false);}catch(RuntimeException $e){$check('backup_'.$kind.'_refused',true);}}
|
|
$v=['at'=>gmdate('c'),'target'=>$target,'result'=>'passed','checks'=>$checks,'entries'=>$entries,'calls'=>$calls,'QA_storage_private'=>$work,'publication'=>'No persistent publication. Reader eligibility tested only in rolled-back QA transaction, after real clean scan.','backup_validation_fixture'=>'Synthetic paths used only to exercise validator; not a real backup/restoration receipt.','CLI_actor_attribution'=>'Existing responsible account, verified email/effective manage-portal; not described as an authenticated browser session.','active_changes'=>0,'accepted_functional_path_repeated'=>false];file_put_contents($out.'/PROVA-IMPORTADOR-QA.json',json_encode($v,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR));echo json_encode(['result'=>'passed','checks'=>count($checks),'imports'=>count($entries),'replay_no_changes'=>true,'persistent_publications'=>0,'active_changes'=>0]);
|