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

38 lines
4.8 KiB
PHP

<?php
require dirname(__DIR__).'/bootstrap.php';
use App\{DB,Regulations,Reports,Csv,Xlsx};
if(getenv('ESBSC_TEST_DATABASE')!=='1')exit('Base jetable requise');
$n=0;function ok($v,$m){global $n;if(!$v)throw new RuntimeException($m);echo 'V3 SQL '.(++$n).' '.$m."\n";}
$date=date('Y-m-d',strtotime('+7 days'));$season=Regulations::season($date);
$count=DB::value('SELECT COUNT(*) FROM players');Regulations::migrate();ok(DB::value('SELECT COUNT(*) FROM players')===$count,'Migration répétable sans perte');
$actor=(int)DB::value('SELECT id FROM users LIMIT 1');
DB::query("INSERT INTO matches(fff_match_id,team_id,competition,match_date,status,is_official,counts_for_purge) VALUES('V3-FUTURE',1,'V3 championnat',?,'scheduled',1,0)",[$date]);$mid=(int)DB::conn()->lastInsertId();
DB::query("INSERT INTO regulation_profiles(team_id,season,competition,mutation_base,mutation_bonus,outside_limit,max_players,confirmed,reference) VALUES(1,?,'V3 championnat',4,1,2,14,1,'Notification test')",[$season]);
$ids=[];
for($i=0;$i<6;$i++){
DB::query("INSERT INTO players(person_number,last_name,first_name,active) VALUES(?,?,?,1)",[$i?'V3-'.$i:'V3-TEST','TEST V3','Joueur '.$i]);$pid=(int)DB::conn()->lastInsertId();$ids[]=$pid;
DB::query("INSERT INTO person_licences(player_id,season,licence_number,licence_type) VALUES(?,?,?,'Libre')",[$pid,$season,'V3-LIC-'.$i]);$lid=(int)DB::conn()->lastInsertId();
DB::query("INSERT INTO licence_checks(licence_id,licence_status,qualified_on,mutation,mutation_until,category_ok,reference) VALUES(?,'active',?,'normal',?,'yes','Test')",[$lid,date('Y-m-d',strtotime('-10 days')),date('Y-m-d',strtotime('+100 days'))]);
}
foreach(['players','discipline','calendar:1'] as $scope)DB::query("INSERT INTO data_checks(scope,checked_at,note) VALUES(?,NOW(),'Test V3') ON DUPLICATE KEY UPDATE checked_at=NOW()",[$scope]);
$m=DB::one('SELECT * FROM matches WHERE id=?',[$mid]);$players=DB::all("SELECT * FROM players WHERE person_number LIKE 'V3-%'");
$r=Regulations::evaluate($m,$players,$ids);ok($r['summary']['status']==='blocked','Six mutés bloqués côté serveur');
$r=Regulations::evaluate($m,$players,array_slice($ids,0,5));ok($r['summary']['status']==='available','Cinq mutés E1');
$other=$m;$other['competition']='Coupe non configurée';ok(Regulations::evaluate($other,$players,[$ids[0]])['summary']['status']==='review','Pas de profil championnat appliqué à une coupe');
DB::query("INSERT INTO disciplinary_events(player_id,team_id,event_date,event_type,review_status) VALUES(?,1,?,'yellow','cancelled'),(?,1,?,'yellow','pending')",[$ids[0],date('Y-m-d'),$ids[0],date('Y-m-d')]);
$report=Reports::build($date,$date,$mid,true);ok(count(array_filter($report['groups'][0]['rows'],fn($r)=>(int)$r['player']['id']===$ids[0]))===0,'Jaunes annulés et provisoires exclus du risque');
DB::query('INSERT INTO squads(match_id,updated_by) VALUES(?,?)',[$mid,$actor]);$sid=(int)DB::conn()->lastInsertId();foreach($ids as $pid)DB::query('INSERT INTO squad_players(squad_id,player_id) VALUES(?,?)',[$sid,$pid]);
$report=Reports::build($date,$date,$mid,true);ok($report['groups'][0]['regulatory']['status']==='blocked','Rapport intègre les quotas');ok(str_starts_with(Reports::pdf($report),'%PDF'),'PDF V3 généré');
DB::query("INSERT INTO matches(fff_match_id,team_id,competition,match_date,status,is_official,counts_for_purge) VALUES('V3-PLAYED',1,'Coupe V3',?,'scheduled',1,0)",[date('Y-m-d',strtotime('-3 days'))]);$past=(int)DB::conn()->lastInsertId();
$payload=['entries'=>[['person_number'=>'V3-TEST','shirt'=>1,'starter'=>true]]];
DB::query("INSERT INTO fmi_uploads(actor_id,match_id,filename,file_key,file_hash,payload,base_revision) VALUES(?,?,'test.pdf',?,?,?,?)",[$actor,$past,str_repeat('a',32),str_repeat('b',64),json_encode($payload),DB::revision()]);$uid=(int)DB::conn()->lastInsertId();
App\Fmi::apply($uid,$actor,['V3-TEST'=>'1'],true);
ok(DB::value('SELECT status FROM matches WHERE id=?',[$past])==='played','FMI fait passer le calendrier à joué');
ok((int)DB::value('SELECT counts_for_purge FROM matches WHERE id=?',[$past])===1,'FMI actualise le compteur de purge');
ok((int)DB::value('SELECT participated FROM match_participations WHERE match_id=?',[$past])===1,'Participation conservée');
$cal="numero match;date;adversaire;statut\nV3-PLAYED;".date('Y-m-d',strtotime('-3 days')).";Test;prevu\n";
$preview=Csv::preview('calendar',1,$cal);ok($preview[0]['data']['status']==='played','Un export prévu ne dégrade pas une FMI jouée');
$otherDate=Csv::preview('calendar',1,str_replace(date('Y-m-d',strtotime('-3 days')),date('Y-m-d',strtotime('-2 days')),$cal));ok($otherDate[0]['action']==='error','Date contradictoire avec FMI bloquée');
try{App\Fmi::apply($uid,$actor,['V3-TEST'=>'1'],true);ok(false,'Réapplication');}catch(RuntimeException $e){ok(true,'Réapplication FMI refusée');}
echo "$n contrôles SQL V3 réussis\n";