Csv::previewRows('players',0,$rows,false,$season); $apply=function($rows)use($actor){DB::query('INSERT INTO import_batches(actor_id,kind,filename,fingerprint,payload,base_revision) VALUES(?,?,?,?,?,?)',[$actor,'players','44171420d.xlsx',hash('sha256',json_encode($rows)),json_encode($rows),DB::revision()]);return Csv::apply((int)DB::conn()->lastInsertId(),$actor);}; $rows=$preview($raw);foreach($rows as $r)if($r['error'])throw new RuntimeException($r['error']);check(count($rows)===75,'75 licences reconnues'); $before=DB::all('SELECT * FROM sanctions ORDER BY id');$apply($rows);check($before===DB::all('SELECT * FROM sanctions ORDER BY id'),'Sanctions intactes'); $numbers=array_map(fn($r)=>$r['data']['numero_licence'],$raw);$marks=implode(',',array_fill(0,count($numbers),'?')); $licences=Cachets::decorate(DB::all("SELECT l.* FROM person_licences l JOIN licence_cachets c ON c.licence_id=l.id WHERE l.season='2026/2027' AND l.licence_number IN ($marks)",$numbers)); $counts=['none'=>0,'normal'=>0,'outside'=>0];foreach($licences as $l)$counts[Cachets::at($l['imported_cachets'],'2026-09-26')['mutation']]++; check($counts===['none'=>65,'normal'=>9,'outside'=>1],'65 sans cachet, 9 MN, 1 MH : '.json_encode($counts)); $counts=['none'=>0,'normal'=>0,'outside'=>0];foreach($licences as $l)$counts[Cachets::at($l['imported_cachets'],'2026-10-15')['mutation']]++; check($counts===['none'=>66,'normal'=>8,'outside'=>1],'Expiration du 14 octobre respectée'); $again=$preview($raw);check(count(array_filter($again,fn($r)=>$r['action']==='unchanged'))===75,'Réimport identique sans doublon');$rev=DB::revision();$apply($again);check(DB::revision()===$rev,'Réimport identique ne périme pas les contrôles'); $stamped=current(array_filter($raw,fn($r)=>$r['data']['cachet_code']==='MN'));$pn=$stamped['data']['numero_personne'];$ln=$stamped['data']['numero_licence']; $lid=(int)DB::value('SELECT id FROM person_licences WHERE season=? AND licence_number=?',['2026/2027',$ln]);$saved=DB::one('SELECT * FROM licence_cachets WHERE licence_id=?',[$lid]); $legacy=$stamped;foreach(array_keys($legacy['data']) as $k)if(str_starts_with($k,'cachet_'))unset($legacy['data'][$k]);$apply($preview([$legacy]));check(DB::one('SELECT * FROM licence_cachets WHERE licence_id=?',[$lid])===$saved,'Ancien export sans colonnes nefface rien'); $without=$stamped;foreach(array_keys($without['data']) as $k)if(str_starts_with($k,'cachet_'))$without['data'][$k]='';$without['data']['nature_changement_de_club']='Mutée'; $apply($preview([$without]));check(DB::value('SELECT payload FROM licence_cachets WHERE licence_id=?',[$lid])==='[]','Export complet sans cachet remplace ancien cachet malgré Mutée'); $apply($preview([$stamped])); $before=DB::all('SELECT * FROM licence_cachets ORDER BY licence_id');$bad=$stamped;$bad['data']['cachet_date_de_fin']='31/02/2026';$errors=$preview([$without,$bad]); try{$apply($errors);throw new LogicException('Expected refusal');}catch(RuntimeException $e){check(DB::all('SELECT * FROM licence_cachets ORDER BY licence_id')===$before,'Erreur de date : lot entier refusé');} $old=$without;$apply($preview([$old],'2025/2026'));check(DB::value('SELECT payload FROM licence_cachets WHERE licence_id=?',[$lid])===$saved['payload'],'Autre saison ne modifie pas le cachet courant'); $a=$stamped;$b=$stamped;$b['data']['cachet_code']='MH';$b['data']['cachet_libelle']='MUTATION HORS PERIODE';$both=$preview([$a,$a,$b]);check(count($both)===1 && count($both[0]['cachets'])===2,'Plusieurs lignes de cachets regroupées');$apply($both); $one=Cachets::decorate(DB::all('SELECT * FROM person_licences WHERE id=?',[$lid]))[0];check(Cachets::at($one['imported_cachets'],'2026-09-26')['mutation']==='outside','Pas de double comptage MN + MH'); $apply($preview([$stamped])); $revision=DB::revision();Cachets::migrate();check(DB::revision()===$revision,'Migration cachets relançable'); check((int)DB::value("SELECT COUNT(*) FROM audit_log WHERE action='cachets_import'")>=75,'Historique des modifications conservé'); echo "$n tests SQL cachets réussis.\n";