From ebe75fb903d34914f8f17af9d56c25b47edf990a Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 25 Dec 2019 18:10:19 +0000 Subject: [PATCH] Tests: Switch `WP_UnitTest_Factory_For_Blog::create_object()` to use `wp_insert_site()`. Map some arguments for backward compatibility with `wpmu_create_blog()` previously used there. Props davidbaumwald, danielbachhuber, jeremyfelt, SergeyBiryukov. See #47195. git-svn-id: https://develop.svn.wordpress.org/trunk@47011 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-unittest-factory-for-blog.php | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php index ba6b9bba4d..424197b835 100644 --- a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php @@ -32,11 +32,43 @@ class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing { */ public function create_object( $args ) { global $wpdb; - $meta = isset( $args['meta'] ) ? $args['meta'] : array( 'public' => 1 ); - $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id(); - // temp tables will trigger db errors when we attempt to reference them as new temp tables + + // Map some arguments for backward compatibility with wpmu_create_blog() previously used here. + if ( ! isset( $args['public'] ) ) { + // Default to public, unless an options array was provided. + $args['public'] = ! isset( $args['meta'] ) ? 1 : 0; + } + + if ( ! isset( $args['user_id'] ) ) { + $args['user_id'] = get_current_user_id(); + } + + if ( isset( $args['site_id'] ) ) { + $args['network_id'] = $args['site_id']; + unset( $args['site_id'] ); + } + + if ( isset( $args['meta'] ) ) { + // The `$site_data_whitelist` matches the one used in `wpmu_create_blog()`. + $site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); + + foreach ( $args['meta'] as $key => $value ) { + // Promote whitelisted keys to top-level arguments, add others to the options array. + if ( in_array( $key, $site_data_whitelist, true ) ) { + $args[ $key ] = $value; + } else { + $args['options'][ $key ] = $value; + } + } + + unset( $args['meta'] ); + } + + // Temporary tables will trigger DB errors when we attempt to reference them as new temporary tables. $suppress = $wpdb->suppress_errors(); - $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] ); + + $blog = wp_insert_site( $args ); + $wpdb->suppress_errors( $suppress ); // Tell WP we're done installing.