test: add wordpress content stubs

This commit is contained in:
Keith Solomon
2026-04-28 18:09:42 -05:00
parent 5c0b22f4c4
commit 7a30bbf1de
2 changed files with 483 additions and 0 deletions
+352
View File
@@ -536,6 +536,358 @@ if ( ! function_exists( 'rest_ensure_response' ) ) {
}
}
if ( ! function_exists( 'wp_insert_post' ) ) {
/**
* Minimal post inserter for unit tests.
*
* @param array<string, mixed> $postarr Post data.
* @param bool $wp_error Whether to return WP_Error on failure.
* @return int|\WP_Error
*/
function wp_insert_post( array $postarr, $wp_error = false ) {
if ( isset( $postarr['ID'] ) && (int) $postarr['ID'] > 0 ) {
$post_id = (int) $postarr['ID'];
} else {
$post_id = (int) ( $GLOBALS['wpcs_test_next_post_id'] ?? 1 );
$GLOBALS['wpcs_test_next_post_id'] = $post_id + 1;
}
if ( $post_id <= 0 && $wp_error ) {
return new WP_Error( 'invalid_post_id', 'Post ID is invalid.' );
}
$GLOBALS['wpcs_test_posts'][ $post_id ] = array_merge(
array(
'ID' => $post_id,
'post_title' => '',
'post_content' => '',
'post_excerpt' => '',
'post_status' => 'draft',
'post_type' => 'post',
'post_name' => '',
'post_parent' => 0,
'menu_order' => 0,
'post_mime_type' => '',
),
$postarr,
array( 'ID' => $post_id )
);
return $post_id;
}
}
if ( ! function_exists( 'wp_update_post' ) ) {
/**
* Minimal post updater for unit tests.
*
* @param array<string, mixed> $postarr Post data.
* @param bool $wp_error Whether to return WP_Error on failure.
* @return int|\WP_Error
*/
function wp_update_post( array $postarr, $wp_error = false ) {
$post_id = (int) ( $postarr['ID'] ?? 0 );
if ( $post_id <= 0 || ! isset( $GLOBALS['wpcs_test_posts'][ $post_id ] ) ) {
return $wp_error ? new WP_Error( 'invalid_post_id', 'Post does not exist.' ) : 0;
}
$GLOBALS['wpcs_test_posts'][ $post_id ] = array_merge(
$GLOBALS['wpcs_test_posts'][ $post_id ],
$postarr,
array( 'ID' => $post_id )
);
return $post_id;
}
}
if ( ! function_exists( 'get_post' ) ) {
/**
* Minimal post reader for unit tests.
*
* @param mixed $post Post ID.
* @param string $output Output format.
* @param string $filter Filter context.
* @return array<string, mixed>|object|null
*/
function get_post( $post = null, $output = 'ARRAY_A', $filter = 'raw' ) {
$GLOBALS['wpcs_test_post_filter'] = $filter;
$post_id = (int) $post;
$data = $GLOBALS['wpcs_test_posts'][ $post_id ] ?? null;
if ( null === $data ) {
return null;
}
return 'OBJECT' === $output ? (object) $data : $data;
}
}
if ( ! function_exists( 'get_posts' ) ) {
/**
* Minimal posts query for unit tests.
*
* @param array<string, mixed> $args Query args.
* @return array<int, array<string, mixed>>
*/
function get_posts( array $args = array() ) {
$posts = array_values( $GLOBALS['wpcs_test_posts'] ?? array() );
if ( isset( $args['post_type'] ) && 'any' !== $args['post_type'] ) {
$post_types = is_array( $args['post_type'] ) ? $args['post_type'] : array( $args['post_type'] );
$posts = array_filter(
$posts,
static function ( array $post ) use ( $post_types ): bool {
return in_array( $post['post_type'] ?? '', $post_types, true );
}
);
}
if ( isset( $args['meta_key'], $args['meta_value'] ) ) {
$posts = array_filter(
$posts,
static function ( array $post ) use ( $args ): bool {
$values = $GLOBALS['wpcs_test_post_meta'][ (int) $post['ID'] ][ (string) $args['meta_key'] ] ?? array();
return in_array( $args['meta_value'], $values, true );
}
);
}
return array_values( $posts );
}
}
if ( ! function_exists( 'wp_delete_post' ) ) {
/**
* Minimal post deleter for unit tests.
*
* @param int $post_id Post ID.
* @param bool $force_delete Force delete flag.
* @return bool
*/
function wp_delete_post( $post_id, $force_delete = false ) {
$GLOBALS['wpcs_test_force_delete'][ (int) $post_id ] = (bool) $force_delete;
unset( $GLOBALS['wpcs_test_posts'][ (int) $post_id ] );
unset( $GLOBALS['wpcs_test_post_meta'][ (int) $post_id ] );
return true;
}
}
if ( ! function_exists( 'update_post_meta' ) ) {
/**
* Minimal post meta updater for unit tests.
*
* @param int $post_id Post ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
* @return bool
*/
function update_post_meta( $post_id, $meta_key, $meta_value ) {
$GLOBALS['wpcs_test_post_meta'][ (int) $post_id ][ (string) $meta_key ] = array( $meta_value );
return true;
}
}
if ( ! function_exists( 'get_post_meta' ) ) {
/**
* Minimal post meta reader for unit tests.
*
* @param int $post_id Post ID.
* @param string $key Meta key.
* @param bool $single Whether to return single value.
* @return mixed
*/
function get_post_meta( $post_id, $key = '', $single = false ) {
$meta = $GLOBALS['wpcs_test_post_meta'][ (int) $post_id ] ?? array();
if ( '' === $key ) {
return $meta;
}
$values = $meta[ $key ] ?? array();
if ( $single ) {
return $values[0] ?? '';
}
return $values;
}
}
if ( ! function_exists( 'delete_post_meta' ) ) {
/**
* Minimal post meta deleter for unit tests.
*
* @param int $post_id Post ID.
* @param string $meta_key Meta key.
* @return bool
*/
function delete_post_meta( $post_id, $meta_key ) {
unset( $GLOBALS['wpcs_test_post_meta'][ (int) $post_id ][ (string) $meta_key ] );
return true;
}
}
if ( ! function_exists( 'wp_insert_term' ) ) {
/**
* Minimal term inserter for unit tests.
*
* @param string $term Term name.
* @param string $taxonomy Taxonomy.
* @param array<string, mixed> $args Term args.
* @return array<string, int>|\WP_Error
*/
function wp_insert_term( $term, $taxonomy, array $args = array() ) {
if ( '' === (string) $term || '' === (string) $taxonomy ) {
return new WP_Error( 'invalid_term', 'Term name and taxonomy are required.' );
}
$term_id = (int) ( $GLOBALS['wpcs_test_next_term_id'] ?? 1 );
$GLOBALS['wpcs_test_next_term_id'] = $term_id + 1;
$slug = (string) ( $args['slug'] ?? sanitize_key( $term ) );
$GLOBALS['wpcs_test_terms'][ $term_id ] = array(
'term_id' => $term_id,
'term_taxonomy_id' => $term_id,
'name' => (string) $term,
'taxonomy' => (string) $taxonomy,
'slug' => $slug,
'description' => (string) ( $args['description'] ?? '' ),
'parent' => (int) ( $args['parent'] ?? 0 ),
);
return array(
'term_id' => $term_id,
'term_taxonomy_id' => $term_id,
);
}
}
if ( ! function_exists( 'wp_update_term' ) ) {
/**
* Minimal term updater for unit tests.
*
* @param int $term_id Term ID.
* @param string $taxonomy Taxonomy.
* @param array<string, mixed> $args Term args.
* @return array<string, int>|\WP_Error
*/
function wp_update_term( $term_id, $taxonomy, array $args = array() ) {
$term_id = (int) $term_id;
if ( ! isset( $GLOBALS['wpcs_test_terms'][ $term_id ] ) ) {
return new WP_Error( 'invalid_term_id', 'Term does not exist.' );
}
$GLOBALS['wpcs_test_terms'][ $term_id ] = array_merge(
$GLOBALS['wpcs_test_terms'][ $term_id ],
$args,
array(
'term_id' => $term_id,
'term_taxonomy_id' => $term_id,
'taxonomy' => (string) $taxonomy,
)
);
return array(
'term_id' => $term_id,
'term_taxonomy_id' => $term_id,
);
}
}
if ( ! function_exists( 'get_term_by' ) ) {
/**
* Minimal term reader for unit tests.
*
* @param string $field Field name.
* @param mixed $value Field value.
* @param string $taxonomy Taxonomy.
* @return array<string, mixed>|false
*/
function get_term_by( $field, $value, $taxonomy ) {
foreach ( $GLOBALS['wpcs_test_terms'] ?? array() as $term ) {
if ( (string) ( $term['taxonomy'] ?? '' ) !== (string) $taxonomy ) {
continue;
}
if ( isset( $term[ $field ] ) && (string) $value === (string) $term[ $field ] ) {
return $term;
}
}
return false;
}
}
if ( ! function_exists( 'wp_set_object_terms' ) ) {
/**
* Minimal object term relationship setter for unit tests.
*
* @param int $object_id Object ID.
* @param string|array<mixed> $terms Terms.
* @param string $taxonomy Taxonomy.
* @return array<int, mixed>
*/
function wp_set_object_terms( $object_id, $terms, $taxonomy ) {
$term_values = is_array( $terms ) ? array_values( $terms ) : array( $terms );
$GLOBALS['wpcs_test_object_terms'][ (int) $object_id ][ (string) $taxonomy ] = $term_values;
return $term_values;
}
}
if ( ! function_exists( 'wp_insert_attachment' ) ) {
/**
* Minimal attachment inserter for unit tests.
*
* @param array<string, mixed> $args Attachment args.
* @param mixed $file File path.
* @param int $parent_post_id Parent post ID.
* @param bool $wp_error Whether to return WP_Error on failure.
* @return int|\WP_Error
*/
function wp_insert_attachment( array $args, $file = false, $parent_post_id = 0, $wp_error = false ) {
$GLOBALS['wpcs_test_attachment_files'][] = $file;
$args['post_type'] = 'attachment';
$args['post_parent'] = (int) $parent_post_id;
return wp_insert_post( $args, $wp_error );
}
}
if ( ! function_exists( 'wp_update_attachment_metadata' ) ) {
/**
* Minimal attachment metadata updater for unit tests.
*
* @param int $attachment_id Attachment ID.
* @param mixed $data Metadata.
* @return bool
*/
function wp_update_attachment_metadata( $attachment_id, $data ) {
$GLOBALS['wpcs_test_attachment_metadata'][ (int) $attachment_id ] = $data;
return true;
}
}
if ( ! function_exists( 'wp_get_attachment_metadata' ) ) {
/**
* Minimal attachment metadata reader for unit tests.
*
* @param int $attachment_id Attachment ID.
* @return mixed
*/
function wp_get_attachment_metadata( $attachment_id ) {
return $GLOBALS['wpcs_test_attachment_metadata'][ (int) $attachment_id ] ?? false;
}
}
if ( ! function_exists( 'admin_url' ) ) {
/**
* Minimal admin URL helper for unit tests.