<?php
declare(strict_types=1);
require __DIR__.'/ConfigWriteAudit.php';$r=tempnam(sys_get_temp_dir(),'cdx-cs-audit-');unlink($r);mkdir($r,0700);$a=new ConfigWriteAudit($r,2147001421);
$cases=[
['insert-h5-own',"INSERT INTO yoshop_h5_setting (store_id,`key`,`values`) VALUES (2147001421,'basic','{}')",true],
['insert-wx-own',"INSERT INTO yoshop_wxapp_setting SET store_id=2147001421,`key`='examine',`values`='{}'",true],
['update-own',"UPDATE yoshop_h5_setting SET `values`='{}',store_id=2147001421,`key`='basic' WHERE yoshop_h5_setting.store_id=2147001421 AND `key`='basic'",true],
['duplicate-same-owner',"UPDATE yoshop_h5_setting SET `values`='{}' WHERE store_id=2147001421 AND `key`='basic' AND yoshop_h5_setting.store_id='2147001421'",true],
['duplicate-same-key',"UPDATE yoshop_wxapp_setting SET `values`='{}' WHERE store_id=2147001421 AND `key`='examine' AND `key`='examine'",true],
['other-store-insert',"INSERT INTO yoshop_h5_setting (store_id,`key`) VALUES (10001,'basic')",false],
['illegal-key',"INSERT INTO yoshop_h5_setting (store_id,`key`) VALUES (2147001421,'examine')",false],
['unknown-key',"INSERT INTO yoshop_wxapp_setting (store_id,`key`) VALUES (2147001421,'unknown')",false],
['no-store',"UPDATE yoshop_h5_setting SET `values`='{}' WHERE `key`='basic'",false],
['no-key',"UPDATE yoshop_h5_setting SET `values`='{}' WHERE store_id=2147001421",false],
['conflicting-store',"UPDATE yoshop_h5_setting SET `values`='{}' WHERE store_id=2147001421 AND `key`='basic' AND store_id=10001",false],
['conflicting-key',"UPDATE yoshop_wxapp_setting SET `values`='{}' WHERE store_id=2147001421 AND `key`='basic' AND `key`='examine'",false],
['change-owner',"UPDATE yoshop_h5_setting SET store_id=10001 WHERE store_id=2147001421 AND `key`='basic'",false],
['rekey',"UPDATE yoshop_wxapp_setting SET `key`='examine' WHERE store_id=2147001421 AND `key`='basic'",false],
['or-widen',"UPDATE yoshop_h5_setting SET `values`='{}' WHERE store_id=2147001421 AND `key`='basic' OR store_id=10001",false],
['fraction-owner',"INSERT INTO yoshop_h5_setting (store_id,`key`) VALUES (2147001421.1,'basic')",false],
['delete-forbidden',"DELETE FROM yoshop_h5_setting WHERE store_id=2147001421 AND `key`='basic'",false],
['foreign-schema',"UPDATE other.yoshop_h5_setting SET `values`='{}' WHERE store_id=2147001421 AND `key`='basic'",false],
['system-settings-forbidden',"INSERT INTO yoshop_store_setting (store_id,`key`) VALUES (2147001421,'basic')",false],
];foreach($cases as [$name,$sql,$want]){$ok=true;try{$a->observe($sql);}catch(RuntimeException $e){$ok=false;}if($ok!==$want)throw new RuntimeException('Client settings audit '.$name);}echo json_encode(['cases'=>count($cases),'all_passed'=>true,'database_called'=>false,'all_duplicate_scope_atoms_checked'=>true,'post_execution_only'=>true]),"\n";foreach(['private-write-sql.jsonl','write-audit.jsonl'] as $f)unlink($r.'/'.$f);rmdir($r);
