| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHPUnit bootstrap — WordPress function stubs for library-level tests. |
| 4 |
* |
| 5 |
* Provides minimal no-op implementations of WordPress functions so the |
| 6 |
* LinnoSDK\Telemetry classes can be exercised without a full WordPress runtime. |
| 7 |
*/ |
| 8 |
|
| 9 |
require_once __DIR__ . '/../vendor/autoload.php'; |
| 10 |
|
| 11 |
// --------------------------------------------------------------------------- |
| 12 |
// WordPress constants |
| 13 |
// --------------------------------------------------------------------------- |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
define( 'ABSPATH', sys_get_temp_dir() . '/' ); |
| 17 |
} |
| 18 |
|
| 19 |
// --------------------------------------------------------------------------- |
| 20 |
// Global $wpdb stub (used by Queue) |
| 21 |
// --------------------------------------------------------------------------- |
| 22 |
|
| 23 |
global $wpdb; |
| 24 |
$wpdb = new class { |
| 25 |
public string $prefix = 'wp_'; |
| 26 |
|
| 27 |
public function get_var( $query ) { |
| 28 |
return null; |
| 29 |
} |
| 30 |
|
| 31 |
public function prepare( $query, ...$args ) { |
| 32 |
return $query; |
| 33 |
} |
| 34 |
|
| 35 |
public function get_charset_collate(): string { |
| 36 |
return ''; |
| 37 |
} |
| 38 |
|
| 39 |
public function get_results( $query, $output = OBJECT ): array { |
| 40 |
return []; |
| 41 |
} |
| 42 |
|
| 43 |
public function insert( string $table, array $data ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
public function delete( string $table, array $where ) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
public function query( $query ) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
// --------------------------------------------------------------------------- |
| 57 |
// In-memory WordPress option store |
| 58 |
// --------------------------------------------------------------------------- |
| 59 |
|
| 60 |
$_wp_options = []; |
| 61 |
|
| 62 |
if ( ! function_exists( 'get_option' ) ) { |
| 63 |
function get_option( string $key, $default = false ) { |
| 64 |
global $_wp_options; |
| 65 |
return array_key_exists( $key, $_wp_options ) ? $_wp_options[ $key ] : $default; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if ( ! function_exists( 'update_option' ) ) { |
| 70 |
function update_option( string $key, $value, $autoload = true ): bool { |
| 71 |
global $_wp_options; |
| 72 |
$_wp_options[ $key ] = $value; |
| 73 |
return true; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if ( ! function_exists( 'delete_option' ) ) { |
| 78 |
function delete_option( string $key ): bool { |
| 79 |
global $_wp_options; |
| 80 |
unset( $_wp_options[ $key ] ); |
| 81 |
return true; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// --------------------------------------------------------------------------- |
| 86 |
// In-memory WordPress transient store |
| 87 |
// --------------------------------------------------------------------------- |
| 88 |
|
| 89 |
$_wp_transients = []; |
| 90 |
|
| 91 |
if ( ! function_exists( 'get_transient' ) ) { |
| 92 |
function get_transient( string $key ) { |
| 93 |
global $_wp_transients; |
| 94 |
return $_wp_transients[ $key ] ?? false; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! function_exists( 'set_transient' ) ) { |
| 99 |
function set_transient( string $key, $value, int $expiration = 0 ): bool { |
| 100 |
global $_wp_transients; |
| 101 |
$_wp_transients[ $key ] = $value; |
| 102 |
return true; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if ( ! function_exists( 'delete_transient' ) ) { |
| 107 |
function delete_transient( string $key ): bool { |
| 108 |
global $_wp_transients; |
| 109 |
unset( $_wp_transients[ $key ] ); |
| 110 |
return true; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// --------------------------------------------------------------------------- |
| 115 |
// WordPress hook system stubs |
| 116 |
// --------------------------------------------------------------------------- |
| 117 |
|
| 118 |
$_wp_actions = []; |
| 119 |
$_wp_filters = []; |
| 120 |
$_wp_hooks = []; // action name => array of [callback, priority, accepted_args] |
| 121 |
|
| 122 |
if ( ! function_exists( 'add_action' ) ) { |
| 123 |
function add_action( string $hook, $callback, int $priority = 10, int $accepted_args = 1 ): bool { |
| 124 |
global $_wp_hooks; |
| 125 |
$_wp_hooks[ $hook ][] = [ |
| 126 |
'callback' => $callback, |
| 127 |
'priority' => $priority, |
| 128 |
'accepted_args' => $accepted_args, |
| 129 |
]; |
| 130 |
return true; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if ( ! function_exists( 'add_filter' ) ) { |
| 135 |
function add_filter( string $hook, $callback, int $priority = 10, int $accepted_args = 1 ): bool { |
| 136 |
global $_wp_hooks; |
| 137 |
$_wp_hooks[ $hook ][] = [ |
| 138 |
'callback' => $callback, |
| 139 |
'priority' => $priority, |
| 140 |
'accepted_args' => $accepted_args, |
| 141 |
]; |
| 142 |
return true; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if ( ! function_exists( 'remove_action' ) ) { |
| 147 |
function remove_action( string $hook, $callback, int $priority = 10 ): bool { |
| 148 |
return true; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if ( ! function_exists( 'do_action' ) ) { |
| 153 |
function do_action( string $hook, ...$args ): void { |
| 154 |
global $_wp_hooks; |
| 155 |
if ( empty( $_wp_hooks[ $hook ] ) ) { |
| 156 |
return; |
| 157 |
} |
| 158 |
foreach ( $_wp_hooks[ $hook ] as $entry ) { |
| 159 |
$accepted = min( $entry['accepted_args'], count( $args ) ); |
| 160 |
call_user_func_array( $entry['callback'], array_slice( $args, 0, $accepted ) ); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if ( ! function_exists( 'apply_filters' ) ) { |
| 166 |
function apply_filters( string $hook, $value, ...$args ) { |
| 167 |
return $value; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if ( ! function_exists( 'has_action' ) ) { |
| 172 |
function has_action( string $hook, $callback = false ) { |
| 173 |
global $_wp_hooks; |
| 174 |
if ( false === $callback ) { |
| 175 |
return ! empty( $_wp_hooks[ $hook ] ); |
| 176 |
} |
| 177 |
if ( empty( $_wp_hooks[ $hook ] ) ) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
foreach ( $_wp_hooks[ $hook ] as $entry ) { |
| 181 |
if ( $entry['callback'] === $callback ) { |
| 182 |
return $entry['priority']; |
| 183 |
} |
| 184 |
} |
| 185 |
return false; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
// --------------------------------------------------------------------------- |
| 190 |
// WordPress plugin/activation helpers |
| 191 |
// --------------------------------------------------------------------------- |
| 192 |
|
| 193 |
if ( ! function_exists( 'register_activation_hook' ) ) { |
| 194 |
function register_activation_hook( string $file, $callback ): void { |
| 195 |
// no-op in test context |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
if ( ! function_exists( 'register_deactivation_hook' ) ) { |
| 200 |
function register_deactivation_hook( string $file, $callback ): void { |
| 201 |
// no-op in test context |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if ( ! function_exists( 'plugin_basename' ) ) { |
| 206 |
function plugin_basename( string $file ): string { |
| 207 |
return basename( dirname( $file ) ) . '/' . basename( $file ); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if ( ! function_exists( 'load_plugin_textdomain' ) ) { |
| 212 |
function load_plugin_textdomain( string $domain, $deprecated = false, string $path = '' ): bool { |
| 213 |
return true; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
// --------------------------------------------------------------------------- |
| 218 |
// WordPress URL helpers |
| 219 |
// --------------------------------------------------------------------------- |
| 220 |
|
| 221 |
if ( ! function_exists( 'get_site_url' ) ) { |
| 222 |
function get_site_url( $blog_id = null, string $path = '', string $scheme = '' ): string { |
| 223 |
return 'https://example.com'; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if ( ! function_exists( 'home_url' ) ) { |
| 228 |
function home_url( string $path = '', ?string $scheme = null ): string { |
| 229 |
return 'https://example.com'; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
if ( ! function_exists( 'esc_url_raw' ) ) { |
| 234 |
function esc_url_raw( string $url, array $protocols = [] ): string { |
| 235 |
return $url; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
if ( ! function_exists( 'esc_url' ) ) { |
| 240 |
function esc_url( string $url, array $protocols = [], string $context = 'display' ): string { |
| 241 |
return $url; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
if ( ! function_exists( 'sanitize_text_field' ) ) { |
| 246 |
function sanitize_text_field( string $str ): string { |
| 247 |
return trim( strip_tags( $str ) ); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
if ( ! function_exists( 'sanitize_key' ) ) { |
| 252 |
function sanitize_key( string $key ): string { |
| 253 |
return strtolower( preg_replace( '/[^a-z0-9_\-]/', '', strtolower( $key ) ) ); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
if ( ! function_exists( 'esc_html' ) ) { |
| 258 |
function esc_html( string $text ): string { |
| 259 |
return htmlspecialchars( $text, ENT_QUOTES, 'UTF-8' ); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
if ( ! function_exists( 'esc_attr' ) ) { |
| 264 |
function esc_attr( string $text ): string { |
| 265 |
return htmlspecialchars( $text, ENT_QUOTES, 'UTF-8' ); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
if ( ! function_exists( 'wp_verify_nonce' ) ) { |
| 270 |
function wp_verify_nonce( string $nonce, string $action = '-1' ) { |
| 271 |
return 1; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
if ( ! function_exists( 'wp_create_nonce' ) ) { |
| 276 |
function wp_create_nonce( string $action = '-1' ): string { |
| 277 |
return 'test_nonce'; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
// --------------------------------------------------------------------------- |
| 282 |
// WordPress cron stubs |
| 283 |
// --------------------------------------------------------------------------- |
| 284 |
|
| 285 |
if ( ! function_exists( 'wp_next_scheduled' ) ) { |
| 286 |
function wp_next_scheduled( string $hook, array $args = [] ) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if ( ! function_exists( 'wp_schedule_event' ) ) { |
| 292 |
function wp_schedule_event( int $timestamp, string $recurrence, string $hook, array $args = [] ): bool { |
| 293 |
return true; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if ( ! function_exists( 'wp_unschedule_event' ) ) { |
| 298 |
function wp_unschedule_event( int $timestamp, string $hook, array $args = [] ): bool { |
| 299 |
return true; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
// --------------------------------------------------------------------------- |
| 304 |
// WordPress user helpers |
| 305 |
// --------------------------------------------------------------------------- |
| 306 |
|
| 307 |
if ( ! function_exists( 'wp_get_current_user' ) ) { |
| 308 |
function wp_get_current_user(): object { |
| 309 |
return (object) [ |
| 310 |
'ID' => 0, |
| 311 |
'user_email' => '', |
| 312 |
'first_name' => '', |
| 313 |
'last_name' => '', |
| 314 |
]; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
if ( ! function_exists( 'get_avatar_url' ) ) { |
| 319 |
function get_avatar_url( $id_or_email, array $args = [] ): string { |
| 320 |
return ''; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if ( ! function_exists( 'current_user_can' ) ) { |
| 325 |
function current_user_can( string $capability, ...$args ): bool { |
| 326 |
return false; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
// --------------------------------------------------------------------------- |
| 331 |
// WordPress misc |
| 332 |
// --------------------------------------------------------------------------- |
| 333 |
|
| 334 |
if ( ! function_exists( 'wp_generate_uuid4' ) ) { |
| 335 |
function wp_generate_uuid4(): string { |
| 336 |
return sprintf( |
| 337 |
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
| 338 |
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), |
| 339 |
mt_rand( 0, 0xffff ), |
| 340 |
mt_rand( 0, 0x0fff ) | 0x4000, |
| 341 |
mt_rand( 0, 0x3fff ) | 0x8000, |
| 342 |
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) |
| 343 |
); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
// --------------------------------------------------------------------------- |
| 348 |
// WordPress DB / queue helpers |
| 349 |
// --------------------------------------------------------------------------- |
| 350 |
|
| 351 |
if ( ! function_exists( 'current_time' ) ) { |
| 352 |
function current_time( string $type ) { |
| 353 |
if ( 'mysql' === $type ) { |
| 354 |
return gmdate( 'Y-m-d H:i:s' ); |
| 355 |
} |
| 356 |
return time(); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
if ( ! function_exists( 'wp_json_encode' ) ) { |
| 361 |
function wp_json_encode( $data, int $options = 0, int $depth = 512 ) { |
| 362 |
return json_encode( $data, $options, $depth ); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
if ( ! function_exists( 'dbDelta' ) ) { |
| 367 |
function dbDelta( $queries = '', bool $execute = true ): array { |
| 368 |
return []; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
if ( ! function_exists( 'sanitize_title' ) ) { |
| 373 |
function sanitize_title( string $title ): string { |
| 374 |
return strtolower( preg_replace( '/[^a-z0-9\-_]/', '', preg_replace( '/\s+/', '-', $title ) ) ); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Reset all in-memory stub state between tests. |
| 380 |
* |
| 381 |
* Call this in setUp() or tearDown() to isolate test state. |
| 382 |
*/ |
| 383 |
function wp_reset_stubs(): void { |
| 384 |
global $_wp_options, $_wp_transients, $_wp_hooks; |
| 385 |
$_wp_options = []; |
| 386 |
$_wp_transients = []; |
| 387 |
$_wp_hooks = []; |
| 388 |
} |
| 389 |
|