文件
dayu-color/Backend/tests/contract.php
T

54 行
5.4 KiB
PHP

<?php
// Product endpoint contracts; WordPress and DB are stubbed, never load production settings.
error_reporting(E_ALL);
set_error_handler(function($severity,$message,$file,$line){ throw new ErrorException($message,0,$severity,$file,$line); });
define('ABSPATH',__DIR__.'/');define('MINUTE_IN_SECONDS',60);define('DAY_IN_SECONDS',86400);define('ARRAY_A','ARRAY_A');
$testRoot=sys_get_temp_dir().'/dayu-color-contract-'.bin2hex(random_bytes(6));mkdir($testRoot,0700,true);
function add_action(...$args){}
function get_option($key){return '1';}
function register_rest_route($namespace,$route,$args){$GLOBALS['routes'][$route]=$args;}
function current_user_can($permission){return false;}
function wp_upload_dir(...$args){return array('basedir'=>$GLOBALS['testRoot']);}
function sanitize_textarea_field($value){return strip_tags($value);}
function sanitize_text_field($value){return strip_tags($value);}
function wp_salt($scheme){return 'test-only-not-production';}
function get_transient($key){return 0;}
function set_transient(...$args){}
function wp_json_encode($value,$options=0){return json_encode($value,$options);}
class WP_Error {public $code,$data;function __construct($code,$message,$data){$this->code=$code;$this->data=$data;}}
class WP_REST_Response {public $data,$status;function __construct($data,$status,$headers){$this->data=$data;$this->status=$status;}}
class WP_REST_Request {public $params,$body;function __construct($params=array(),$body=null){$this->params=$params;$this->body=$body;}function get_param($name){return $this->params[$name]??null;}function get_body(){return json_encode($this->body);}function get_json_params(){return $this->body;}}
class FakeDB {public $prefix='wp_',$rows=array();function prepare($sql,...$args){return $args[0]??$sql;}function get_var($sql){return isset($this->rows[$sql])?$sql:null;}function insert($table,$row){$this->rows[$row['event_id']]=$row;return 1;}}
$wpdb=new FakeDB;
require dirname(__DIR__).'/wp-content/mu-plugins/dayu-color-beta.php';
$checks=0;
function check($value,$label){global $checks;if(!$value)throw new RuntimeException($label);$checks++;}
function request($body){return new WP_REST_Request(array(),$body);}
try {
Dayu_Color_Beta::routes();
check(($routes['/diagnostics'][1]['permission_callback'])()===false,'anonymous log reads denied');
check(Dayu_Color_Beta::update(new WP_REST_Request(array('bundle_id'=>'other')))->data['status']===400,'product separation');
$response=Dayu_Color_Beta::update(new WP_REST_Request(array('bundle_id'=>'com.dayu.color','current_build'=>44)));
check($response->status===200 && !$response->data['update_available'],'empty feed is usable');
$fields=Dayu_Color_Beta::clean_fields(array('frames'=>'/Users/test/secret.mov','error_domain'=>'token=not-a-token','arbitrary'=>'private','duration_ms'=>INF,'gpu'=>'M4'));
check($fields['frames']==='[REDACTED]' && $fields['error_domain']==='[REDACTED]' && !isset($fields['arbitrary']) && !isset($fields['duration_ms']) && $fields['gpu']==='M4','allowlist and redaction');
$id='12345678-1234-4234-8234-123456789012';
$event=array('event_id'=>$id,'session_id'=>$id,'source'=>'plugin','version'=>'1.7.0','build'=>45,'event'=>'first_render','occurred_at'=>time(),'fields'=>array('duration_ms'=>1200));
$body=array('bundle_id'=>'com.dayu.color','installation_id'=>$id,'events'=>array($event));
$reply=Dayu_Color_Beta::ingest(request($body));
check($reply->status===200 && $reply->data['accepted'] && count($wpdb->rows)===1,'valid event accepted');
Dayu_Color_Beta::ingest(request($body));check(count($wpdb->rows)===1,'retry idempotency');
foreach(array('version'=>array(),'build'=>array(),'event'=>'material_path','session_id'=>'bad','occurred_at'=>null) as $key=>$bad){$b=$body;$b['events'][0][$key]=$bad;check(Dayu_Color_Beta::ingest(request($b))->data['status']===400,'malformed '.$key);}
$b=$body;$b['events']=array_fill(0,33,$event);check(Dayu_Color_Beta::ingest(request($b))->data['status']===400,'batch limit');
$b=$body;$b['padding']=str_repeat('a',100000);check(Dayu_Color_Beta::ingest(request($b))->data['status']===413,'byte limit');
$manifest=array('bundle_id'=>'com.dayu.color','channel'=>'beta','version'=>'1.7.0','build'=>45,'package_type'=>'pkg','install_mode'=>'pkg_installer','size'=>123,'sha256'=>str_repeat('a',64),'download_url'=>'https://dayu.media/wp-content/uploads/dayu-color-beta/updates/dayu-color-1-7-0-45.pkg');
mkdir(dirname(Dayu_Color_Beta::manifest_path()),0700,true);file_put_contents(Dayu_Color_Beta::manifest_path(),json_encode($manifest));
check(Dayu_Color_Beta::update(new WP_REST_Request(array('bundle_id'=>'com.dayu.color','current_build'=>44)))->data['update_available']===true,'older build sees update');
check(Dayu_Color_Beta::update(new WP_REST_Request(array('bundle_id'=>'com.dayu.color','current_build'=>45)))->data['update_available']===false,'current build no downgrade');
foreach(array('download_url'=>'https://other.example/evil.pkg','sha256'=>'bad','build'=>array(),'size'=>1073741825,'install_mode'=>'zip_replace') as $key=>$bad){$m=$manifest;$m[$key]=$bad;file_put_contents(Dayu_Color_Beta::manifest_path(),json_encode($m));check(Dayu_Color_Beta::manifest()===array(),'bad manifest '.$key);}
echo json_encode(array('passed'=>true,'checks'=>$checks),JSON_PRETTY_PRINT)."\n";
} finally {
if(is_file(Dayu_Color_Beta::manifest_path()))unlink(Dayu_Color_Beta::manifest_path());
@rmdir($testRoot.'/dayu-color-beta/updates');@rmdir($testRoot.'/dayu-color-beta');@rmdir($testRoot);
}