<?php
declare(strict_types=1);
require __DIR__.'/DbWriteAudit.php';$r=tempnam(sys_get_temp_dir(),'cdx-nt-audit-');unlink($r);mkdir($r,0700);file_put_contents($r.'/private-owned-map.json',json_encode(['staff_id'=>801,'user_id'=>901,'tenant'=>2147001021,'plan_ids'=>[701],'target_ids'=>[702],'current_ids'=>[601]]));$a=new DbWriteAudit($r);
$cases=[
['plan-insert-own',"INSERT INTO yoshop_nurtrition (staff_id,store_id) VALUES (801,2147001021)",true],
['target-insert-set-own',"INSERT INTO yoshop_nurtition_target SET staff_id=801,store_id=2147001021",true],
['history-insert-own',"INSERT INTO yoshop_meal_recommend_history (staff_id,store_id) VALUES (801,2147001021)",true],
['plan-soft-delete-own',"UPDATE yoshop_nurtrition SET is_delete=1 WHERE yoshop_nurtrition.store_id='2147001021' AND id=701",true],
['target-update-own',"UPDATE yoshop_nurtition_target SET recommend_energy=1 WHERE store_id=2147001021 AND id=702",true],
['recommend-update-own',"UPDATE yoshop_meal_recommend SET recommend_power=1 WHERE store_id=2147001021 AND id=601",true],
['plan-replacement-own',"UPDATE yoshop_nurtrition SET is_delete=1 WHERE staff_id=801 AND user_id=2147001021 AND set_date='2030-04-02' AND is_delete=0 AND store_id=2147001021",true],
['replace-other-staff',"UPDATE yoshop_nurtrition SET is_delete=1 WHERE staff_id=802 AND user_id=2147001021 AND set_date='2030-04-02' AND is_delete=0 AND store_id=2147001021",false],
['replace-other-operator',"UPDATE yoshop_nurtrition SET is_delete=1 WHERE staff_id=801 AND user_id=901 AND set_date='2030-04-02' AND is_delete=0 AND store_id=2147001021",false],
['replace-other-tenant',"UPDATE yoshop_nurtrition SET is_delete=1 WHERE staff_id=801 AND user_id=2147001021 AND set_date='2030-04-02' AND is_delete=0 AND store_id=10001",false],
['replace-other-date',"UPDATE yoshop_nurtrition SET is_delete=1 WHERE staff_id=801 AND user_id=2147001021 AND set_date='2020-04-02' AND is_delete=0 AND store_id=2147001021",false],
['replace-not-active',"UPDATE yoshop_nurtrition SET is_delete=1 WHERE staff_id=801 AND user_id=2147001021 AND set_date='2030-04-02' AND is_delete=1 AND store_id=2147001021",false],
['replace-missing-scope',"UPDATE yoshop_nurtrition SET is_delete=1 WHERE staff_id=801 AND set_date='2030-04-02' AND is_delete=0 AND store_id=2147001021",false],
['replace-or-widen',"UPDATE yoshop_nurtrition SET is_delete=1 WHERE staff_id=801 AND user_id=2147001021 AND set_date='2030-04-02' AND is_delete=0 AND store_id=2147001021 OR id=888",false],
['insert-other-staff',"INSERT INTO yoshop_nurtrition (staff_id,store_id) VALUES (802,2147001021)",false],
['insert-fraction-owner',"INSERT INTO yoshop_nurtrition (staff_id,store_id) VALUES (801.1,2147001021)",false],
['target-other-id',"UPDATE yoshop_nurtition_target SET recommend_energy=1 WHERE store_id=2147001021 AND id=703",false],
['plan-other-id',"UPDATE yoshop_nurtrition SET is_delete=1 WHERE store_id=2147001021 AND id=703",false],
['current-other-id',"UPDATE yoshop_meal_recommend SET recommend_power=1 WHERE store_id=2147001021 AND id=602",false],
['change-owner',"UPDATE yoshop_nurtition_target SET staff_id=802 WHERE store_id=2147001021 AND id=702",false],
['rekey',"UPDATE yoshop_nurtition_target SET id=703 WHERE store_id=2147001021 AND id=702",false],
['staff-write-forbidden',"UPDATE yoshop_staff SET weight=1 WHERE store_id=2147001021 AND id=801",false],
['foreign-schema',"INSERT INTO other.yoshop_nurtrition (staff_id,store_id) VALUES (801,2147001021)",false],
['physical-delete-forbidden',"DELETE FROM yoshop_nurtrition WHERE id=701",false],
];$out=[];foreach($cases as [$name,$sql,$want]){$ok=true;try{$a->observe($sql);}catch(RuntimeException $e){$ok=false;}if($ok!==$want)throw new RuntimeException('Audit regression '.$name);$out[]=['case'=>$name,'accepted'=>$ok,'expected'=>$want];}
echo json_encode(['assertions'=>count($out),'all_passed'=>true,'database_called'=>false,'post_execution_auditor_not_write_firewall'=>true,'cases'=>$out],JSON_PRETTY_PRINT),"\n";
foreach(['private-owned-map.json','private-dml-audit-sql.jsonl','write-audit.jsonl'] as $f)unlink($r.'/'.$f);rmdir($r);
