30 lines
9.7 KiB
PHP
30 lines
9.7 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-sector-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_sector_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-setores-lote20-20261015','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';$folder='legacy-setores-lote20-20261015';foreach(glob($out.'/lote-privado/'.$folder.'/*')as$f)copy($f,$stage.'/'.$folder.'/'.basename($f));$manifest=$folder.'/manifest.json';$mf=$stage.'/'.$manifest;$raw=file_get_contents($mf);$records=json_decode($raw,true)['documents'];$image=$stage.'/'.$records[count($records)-1]['relative_path'];$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,'--sectors-lot20'=>true,'--actor'=>'2'];$apply=$good+['--apply'=>true];
|
|
$pilotBefore=DB::table('documents as d')->join('portal_resources as r','r.document_id','=','d.id')->where('d.external_id','like','legacy-tempo-%')->orderBy('d.external_id')->get(['d.*','r.id as resource_id','r.published','r.metadata_version'])->toJson();$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.'/'.$records[0]['relative_path'];$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);
|
|
$check('both_named_flags_refused',$call($apply+['--pilot-agronegocio'=>true])!==0);$check('both_flags_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',array_column($records,'external_id'))->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_20_new_resources',count($entries)===20&&DB::table('documents')->count()===$documentsBefore+20&&DB::table('portal_resources')->count()===$resourcesBefore+20&&DB::table('portal_resource_revisions')->count()===$revisionBefore+20);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('energia');$check('private_sector_article_not_public',false);}catch(Symfony\Component\HttpKernel\Exception\HttpException $e){$check('private_sector_article_not_public',$e->getStatusCode()===404);}
|
|
$check('real_scan_exact_bytes_clean',$entries->every(fn($e)=>$e->malware_scan_status==='clean'&&trim($e->sha256)===$e->malware_scanned_sha256));
|
|
$resolved=[];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]);}
|
|
foreach(config('legacy_content.articles')as$slug=>$identity){$record=null;foreach($records as$rec)if($rec['external_id']===$identity['external_id'])$record=$rec;if(!$record||!$identity['publishable'])continue;$articleData=$reader->article($slug);$source=json_decode(file_get_contents($stage.'/'.$record['relative_path']),true);$check('origin_hash_'.$slug,$articleData['sha256']===$record['sha256']);$check('image_hash_'.$slug,$articleData['image']!==null&&$articleData['image']['sha256']===$source['image_sha256']);$fromId=$reader->articleForResource($articleData['id']);$check('new_library_ID_continuity_'.$slug,$fromId['href']==='/'.$slug&&$fromId['start_href']==='/investimentos/a-partir-de/'.$slug&&$fromId['version']===$articleData['version']);
|
|
$links=[];foreach($source['references']??[]as$link){$path=rtrim(parse_url($link,PHP_URL_PATH),'/');$targetSlug=ltrim($path,'/');$targetArticle=$reader->article($targetSlug);$check('link_'.$slug.'_'.$targetSlug,$targetArticle['href']==='/'.$targetSlug);$links[]=$targetArticle['href'];}$resolved[]=['slug'=>$slug,'resource_id'=>$articleData['id'],'sha256'=>$articleData['sha256'],'image_sha256'=>$articleData['image']['sha256'],'start_href'=>$articleData['start_href'],'resolved_links'=>$links];}
|
|
$check('all_16_sector_origins_resolved',count($resolved)===16);
|
|
}finally{DB::rollBack();}$check('eligibility_projection_rolled_back',$snapshot()===$after);
|
|
$ids=array_column($records,'external_id');$pilotAfter=DB::table('documents as d')->join('portal_resources as r','r.document_id','=','d.id')->where('d.external_id','like','legacy-tempo-%')->whereNotIn('d.external_id',$ids)->orderBy('d.external_id')->get(['d.*','r.id as resource_id','r.published','r.metadata_version'])->toJson();$check('preexisting_agribusiness_rows_unchanged',$pilotBefore===$pilotAfter);$check('no_persistent_publication',DB::table('portal_resources')->whereIn('document_id',$entries->pluck('id'))->where('published',true)->count()===0);
|
|
$v=['at'=>gmdate('c'),'target'=>$target,'result'=>'passed','checks'=>$checks,'resources'=>$entries,'calls'=>$calls,'resolved_origins'=>$resolved,'QA_storage_private'=>$work,'scope'=>'New lot only; named import, links/image resolution, new library IDs/start context resolver; no pilot login/dossier cycle repeated','publication'=>'Eligibility projection rolled back; no persistent publication','preexisting_pilot_preserved'=>true,'active_changes'=>0];file_put_contents($out.'/PROVA-LOTE20-QA.json',json_encode($v,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR));echo json_encode(['result'=>'passed','checks'=>count($checks),'private_imports'=>count($entries),'resolved_origins'=>count($resolved),'persistent_publications'=>0,'active_changes'=>0]);
|