<?php
declare(strict_types=1);
require __DIR__.'/ConfigWriteAudit.php';$r=tempnam(sys_get_temp_dir(),'cdx-dl-audit-');unlink($r);mkdir($r,0700);file_put_contents($r.'/private-owned-map.json',json_encode(['rows'=>['yoshop_delivery'=>['701'=>2147001221]]]));$a=new ConfigWriteAudit($r,2147001221);
$cases=[
['parent-insert-own',"INSERT INTO yoshop_delivery (store_id,name) VALUES (2147001221,'CDXDL10_甲')",true],
['child-insert-deferred-fk',"INSERT INTO yoshop_delivery_rule (store_id,delivery_id) VALUES (2147001221,701)",true],
['child-set-deferred-fk',"INSERT INTO yoshop_delivery_rule SET store_id=2147001221,delivery_id=702",true],
['parent-edit-own',"UPDATE yoshop_delivery SET sort=2 WHERE yoshop_delivery.store_id=2147001221 AND delivery_id=701",true],
['rule-replace-own',"DELETE FROM yoshop_delivery_rule WHERE yoshop_delivery_rule.store_id=2147001221 AND delivery_id=701",true],
['insert-other-store',"INSERT INTO yoshop_delivery (store_id) VALUES (10001)",false],
['child-no-parent',"INSERT INTO yoshop_delivery_rule (store_id) VALUES (2147001221)",false],
['child-zero-parent',"INSERT INTO yoshop_delivery_rule (store_id,delivery_id) VALUES (2147001221,0)",false],
['child-fraction-parent',"INSERT INTO yoshop_delivery_rule (store_id,delivery_id) VALUES (2147001221,701.1)",false],
['other-parent-edit',"UPDATE yoshop_delivery SET sort=2 WHERE store_id=2147001221 AND delivery_id=702",false],
['other-parent-delete',"DELETE FROM yoshop_delivery_rule WHERE store_id=2147001221 AND delivery_id=702",false],
['no-store-delete',"DELETE FROM yoshop_delivery_rule WHERE delivery_id=701",false],
['or-delete',"DELETE FROM yoshop_delivery_rule WHERE store_id=2147001221 AND delivery_id=701 OR delivery_id=702",false],
['parent-physical-delete',"DELETE FROM yoshop_delivery WHERE store_id=2147001221 AND delivery_id=701",false],
['change-owner',"UPDATE yoshop_delivery SET store_id=10001 WHERE store_id=2147001221 AND delivery_id=701",false],
['rekey',"UPDATE yoshop_delivery SET delivery_id=702 WHERE store_id=2147001221 AND delivery_id=701",false],
['child-update-forbidden',"UPDATE yoshop_delivery_rule SET first=2 WHERE store_id=2147001221 AND delivery_id=701",false],
['goods-write-forbidden',"INSERT INTO yoshop_goods (store_id) VALUES (2147001221)",false],
['foreign-schema',"DELETE FROM other.yoshop_delivery_rule WHERE store_id=2147001221 AND delivery_id=701",false],
];foreach($cases as [$name,$sql,$want]){$ok=true;try{$a->observe($sql);}catch(RuntimeException $e){$ok=false;}if($ok!==$want)throw new RuntimeException('Delivery audit '.$name);}echo json_encode(['cases'=>count($cases),'all_passed'=>true,'database_called'=>false,'post_execution_only'=>true,'new_child_fk_independent_verification_deferred_until_commit'=>true]),"\n";foreach(['private-owned-map.json','private-write-sql.jsonl','write-audit.jsonl'] as $f)unlink($r.'/'.$f);rmdir($r);
