For unit tests that call wpmu_create_blog(), Blog factory, or installation code that attempts to clear transients: suppress database errors on setUp and restore on tearDown.

There are a few places in core that were preventing this from working by explicity setting `$wpdb->suppress_errors` to `false`. Instead, they should inherit the value that existed before errors were suppressed.

This allows Multisite unit tests to run without explosive database errors, and allows `$wpdb->suppress_errors` to be overridden all the way down the chain.

Fixes #26102.


git-svn-id: https://develop.svn.wordpress.org/trunk@26252 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2013-11-18 20:44:34 +00:00
parent d42e8eee3d
commit ff4f1bb170
5 changed files with 35 additions and 11 deletions

View File

@ -1553,9 +1553,9 @@ function dbDelta( $queries = '', $execute = true ) {
continue;
// Fetch the table column structure from the database
$wpdb->suppress_errors();
$suppress = $wpdb->suppress_errors();
$tablefields = $wpdb->get_results("DESCRIBE {$table};");
$wpdb->suppress_errors( false );
$wpdb->suppress_errors( $suppress );
if ( ! $tablefields )
continue;

View File

@ -1129,10 +1129,10 @@ function install_blog( $blog_id, $blog_title = '' ) {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$wpdb->suppress_errors();
$suppress = $wpdb->suppress_errors();
if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) )
die( '<h1>' . __( 'Already Installed' ) . '</h1><p>' . __( 'You appear to have already installed WordPress. To reinstall please clear your old database tables first.' ) . '</p></body></html>' );
$wpdb->suppress_errors( false );
$wpdb->suppress_errors( $suppress );
$url = get_blogaddress_by_id( $blog_id );
@ -1179,11 +1179,11 @@ function install_blog_defaults($blog_id, $user_id) {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$wpdb->suppress_errors();
$suppress = $wpdb->suppress_errors();
wp_install_defaults($user_id);
$wpdb->suppress_errors( false );
$wpdb->suppress_errors( $suppress );
}
/**

View File

@ -159,9 +159,14 @@ class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
}
function create_object( $args ) {
global $wpdb;
$meta = isset( $args['meta'] ) ? $args['meta'] : array();
$user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
return wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
// temp tables will trigger db errors when we attempt to reference them as new temp tables
$suppress = $wpdb->suppress_errors();
$blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
$wpdb->suppress_errors( $suppress );
return $blog;
}
function update_object( $blog_id, $fields ) {}

View File

@ -9,13 +9,22 @@ if ( is_multisite() ) :
*/
class Tests_MS extends WP_UnitTestCase {
protected $plugin_hook_count = 0;
protected $suppress = false;
function setUp() {
global $wpdb;
parent::setUp();
$this->suppress = $wpdb->suppress_errors();
$_SERVER['REMOTE_ADDR'] = '';
}
function tearDown() {
global $wpdb;
parent::tearDown();
$wpdb->suppress_errors( $this->suppress );
}
/**
* @ticket 22917
*/
@ -105,9 +114,9 @@ class Tests_MS extends WP_UnitTestCase {
$this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
foreach ( $wpdb->tables( 'blog', false ) as $table ) {
$wpdb->suppress_errors();
$suppress = $wpdb->suppress_errors();
$table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
$wpdb->suppress_errors( false );
$wpdb->suppress_errors( $suppress );
$this->assertNotEmpty( $table_fields );
$result = $wpdb->get_results( "SELECT * FROM $prefix$table LIMIT 1" );
if ( 'commentmeta' == $table || 'links' == $table )
@ -139,9 +148,9 @@ class Tests_MS extends WP_UnitTestCase {
$prefix = $wpdb->get_blog_prefix( $blog_id );
foreach ( $wpdb->tables( 'blog', false ) as $table ) {
$wpdb->suppress_errors();
$suppress = $wpdb->suppress_errors();
$table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
$wpdb->suppress_errors( false );
$wpdb->suppress_errors( $suppress );
if ( $drop_tables )
$this->assertEmpty( $table_fields );
else

View File

@ -5,12 +5,22 @@ if ( is_multisite() ) :
* @group option
*/
class Tests_Option_BlogOption extends WP_UnitTestCase {
protected $suppress = false;
function setUp() {
global $wpdb;
parent::setUp();
$this->suppress = $wpdb->suppress_errors();
$_SERVER['REMOTE_ADDR'] = null;
}
function tearDown() {
global $wpdb;
parent::tearDown();
$wpdb->suppress_errors( $this->suppress );
}
function test_from_same_site() {
$key = rand_str();
$key2 = rand_str();