fix(tests): resolve Patchwork conflict and WP_Error_Stub issue
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -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 ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user