From 4f13afbb77e2171efd1829b95e0c61cdcc361c7a Mon Sep 17 00:00:00 2001 From: peptiderepo Date: Thu, 16 Apr 2026 17:38:35 +0700 Subject: [PATCH] fix(tests): resolve Patchwork conflict and WP_Error_Stub issue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove wp_strip_all_tags/sanitize_text_field stubs from bootstrap (Brain Monkey manages these via Functions\when() in individual tests; defining them early triggers Patchwork's DefinedTooEarly error) - Add WP_Error class stub to bootstrap (class, not function — safe for Patchwork) so model registry and image pipeline tests can use it - Replace WP_Error_Stub with standard WP_Error in model registry test Co-Authored-By: Claude Opus 4.6 --- tests/bootstrap.php | 44 ++++++++++++++----- .../Services/OpenRouterModelRegistryTest.php | 13 +----- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3a2f501..83714a7 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -93,18 +93,38 @@ if ( ! class_exists( 'WP_Query' ) ) { } } -// Stub WP text-processing functions used by image prompt builder. -if ( ! function_exists( 'wp_strip_all_tags' ) ) { - function wp_strip_all_tags( $string, $remove_breaks = false ) { - $string = strip_tags( $string ); - if ( $remove_breaks ) { - $string = preg_replace( '/[\r\n\t ]+/', ' ', $string ); +// NOTE: Do NOT stub wp_strip_all_tags() or sanitize_text_field() here. +// Brain Monkey / Patchwork manages these stubs in individual tests via +// Functions\when(). Defining them in bootstrap causes Patchwork's +// "DefinedTooEarly" error. + +// WP_Error stub — used by model registry and image pipeline tests. +// This is safe to define here because it's a class (not a function), +// so Patchwork doesn't need to intercept its definition. +if ( ! class_exists( 'WP_Error' ) ) { + class WP_Error { + public $errors = []; + public $error_data = []; + + public function __construct( $code = '', $message = '', $data = '' ) { + if ( $code ) { + $this->errors[ $code ][] = $message; + if ( $data ) { + $this->error_data[ $code ] = $data; + } + } + } + + public function get_error_code() { + $codes = array_keys( $this->errors ); + return $codes ? $codes[0] : ''; + } + + public function get_error_message( $code = '' ) { + if ( ! $code ) { + $code = $this->get_error_code(); + } + return isset( $this->errors[ $code ] ) ? $this->errors[ $code ][0] : ''; } - return trim( $string ); - } -} -if ( ! function_exists( 'sanitize_text_field' ) ) { - function sanitize_text_field( $str ) { - return trim( strip_tags( $str ) ); } } diff --git a/tests/unit/Services/OpenRouterModelRegistryTest.php b/tests/unit/Services/OpenRouterModelRegistryTest.php index fcafcef..e246643 100644 --- a/tests/unit/Services/OpenRouterModelRegistryTest.php +++ b/tests/unit/Services/OpenRouterModelRegistryTest.php @@ -224,8 +224,7 @@ class OpenRouterModelRegistryTest extends BaseTestCase { return 'Connection timed out'; }; Functions\when( 'wp_remote_get' )->alias( function () { - $e = new \WP_Error_Stub(); - return $e; + return new \WP_Error( 'http_request_failed', 'Connection timed out' ); } ); // Accept that update_option will be called for the stale flag. @@ -240,13 +239,3 @@ class OpenRouterModelRegistryTest extends BaseTestCase { } } -/** - * Minimal WP_Error stub for testing is_wp_error() branches. - */ -if ( ! class_exists( 'WP_Error_Stub' ) ) { - class WP_Error_Stub { - public function get_error_message(): string { - return 'Connection timed out'; - } - } -}