Files
ESBSC-CONVOC/tests/merge_person.php
2026-11-11 09:24:28 +01:00

25 lines
2.5 KiB
PHP

<?php
require dirname(__DIR__).'/bootstrap.php';
use App\{DB,People,PersonMerge,Csv,Sports};
if(getenv('ESBSC_TEST_DATABASE')!=='1')exit('Base de test requise');
function ok($v,$s){if(!$v)throw new RuntimeException($s);echo "OK $s\n";}
$actor=(int)DB::value('SELECT id FROM users LIMIT 1');
DB::query("INSERT INTO players(id,licence_number,last_name,first_name) VALUES(40,'89572837','HEBRAS','Emmanuel'),(107,'89572842','HEBRAS','Emmanuel')");
$r=['line'=>2,'data'=>['numero_personne'=>'1100781229','numero_licence'=>'89572837','nom'=>'HEBRAS','prenom'=>'Emmanuel','type_licence'=>'Libre']];
$s=$r;$s['data']['numero_licence']='89572842';$s['data']['type_licence']='Dirigeant';
$a=People::preview($r,'2026/2027');$b=People::preview($s,'2026/2027');
try{DB::transaction(function()use($a,$b,$actor){People::apply($a,$actor);People::apply($b,$actor);});throw new LogicException('Import should reject');}catch(RuntimeException $e){ok(str_contains($e->getMessage(),'Rapprocher les fiches'),'Conflit intercepté avant erreur SQL');}
ok(DB::value('SELECT person_number FROM players WHERE id=40')===null,'Lot en conflit annulé entièrement');
$san=Sports::save('sanctions',Sports::sanction(['player_id'=>107,'effective_date'=>'2026-09-01','matches_required'=>1,'label'=>'Test fusion']),0,$actor);
DB::query("INSERT INTO disciplinary_events(player_id,event_date,event_type) VALUES(107,'2026-08-31','yellow')");$event=(int)DB::conn()->lastInsertId();
$mid=DB::value('SELECT id FROM matches LIMIT 1');DB::query('INSERT INTO squads(match_id,updated_by) VALUES(?,?)',[$mid,$actor]);$sq=(int)DB::conn()->lastInsertId();DB::query('INSERT INTO squad_players VALUES(?,40),(?,107)',[$sq,$sq]);
PersonMerge::run(40,107,'1100781229','2026/2027');
ok(!DB::one('SELECT * FROM players WHERE id=107'),'Ancienne fiche regroupée');
ok((int)DB::value('SELECT player_id FROM sanctions WHERE id=?',[$san])===40,'Sanction conservée');
ok((int)DB::value('SELECT player_id FROM disciplinary_events WHERE id=?',[$event])===40,'Carton conservé');
ok((int)DB::value('SELECT COUNT(*) FROM squad_players WHERE squad_id=?',[$sq])===1,'Convocation dédoublonnée');
ok((int)DB::value('SELECT COUNT(*) FROM person_licences WHERE player_id=40')===2,'Deux licences conservées');
ok(DB::value('SELECT person_number FROM players WHERE id=40')==='1100781229','Numéro personne affecté');
ok((int)DB::value("SELECT COUNT(*) FROM audit_log WHERE action='merge_person'")===1,'Fusion historisée');
foreach([$r,$s] as $raw){$v=People::preview($raw,'2026/2027');ok($v['id']===40,'Réimport retrouve la fiche commune');}