Wordpress/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php
Sergey Biryukov ebe75fb903 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
2019-12-25 18:10:19 +00:00

101 lines
2.9 KiB
PHP

<?php
/**
* Unit test factory for sites on a multisite network.
*
* Note: The below @method notations are defined solely for the benefit of IDEs,
* as a way to indicate expected return values from the given factory methods.
*
* @method int create( $args = array(), $generation_definitions = null )
* @method WP_Site create_and_get( $args = array(), $generation_definitions = null )
* @method int[] create_many( $count, $args = array(), $generation_definitions = null )
*/
class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
public function __construct( $factory = null ) {
global $current_site, $base;
parent::__construct( $factory );
$this->default_generation_definitions = array(
'domain' => $current_site->domain,
'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
'site_id' => $current_site->id,
);
}
/**
* Creates a site object.
*
* @param array $args Arguments for the site object.
*
* @return int|WP_Error The site ID on success, WP_Error object on failure.
*/
public function create_object( $args ) {
global $wpdb;
// 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 = wp_insert_site( $args );
$wpdb->suppress_errors( $suppress );
// Tell WP we're done installing.
wp_installing( false );
return $blog;
}
/**
* Updates a site object. Not implemented.
*
* @param int $blog_id ID of the site to update.
* @param array $fields The fields to update.
*
* @return void
*/
public function update_object( $blog_id, $fields ) {}
/**
* Retrieves a site by a given ID.
*
* @param int $blog_id ID of the site to retrieve.
*
* @return WP_Site|null The site object on success, null on failure.
*/
public function get_object_by_id( $blog_id ) {
return get_site( $blog_id );
}
}