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.
|
// NOTE: Do NOT stub wp_strip_all_tags() or sanitize_text_field() here.
|
||||||
if ( ! function_exists( 'wp_strip_all_tags' ) ) {
|
// Brain Monkey / Patchwork manages these stubs in individual tests via
|
||||||
function wp_strip_all_tags( $string, $remove_breaks = false ) {
|
// Functions\when(). Defining them in bootstrap causes Patchwork's
|
||||||
$string = strip_tags( $string );
|
// "DefinedTooEarly" error.
|
||||||
if ( $remove_breaks ) {
|
|
||||||
$string = preg_replace( '/[\r\n\t ]+/', ' ', $string );
|
// 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),
|
||||||
return trim( $string );
|
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( ! function_exists( 'sanitize_text_field' ) ) {
|
}
|
||||||
function sanitize_text_field( $str ) {
|
|
||||||
return trim( strip_tags( $str ) );
|
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] : '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -224,8 +224,7 @@ class OpenRouterModelRegistryTest extends BaseTestCase {
|
|||||||
return 'Connection timed out';
|
return 'Connection timed out';
|
||||||
};
|
};
|
||||||
Functions\when( 'wp_remote_get' )->alias( function () {
|
Functions\when( 'wp_remote_get' )->alias( function () {
|
||||||
$e = new \WP_Error_Stub();
|
return new \WP_Error( 'http_request_failed', 'Connection timed out' );
|
||||||
return $e;
|
|
||||||
} );
|
} );
|
||||||
|
|
||||||
// Accept that update_option will be called for the stale flag.
|
// 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