Skip to content

Commit f593d85

Browse files
committed
Tests: Restore the database connection earlier when switching test groups.
When plugins don't disable the `backupGlobals` PHPUnit option in their own tests, `$wpdb` is backed up and restored between classes of tests. The serialisation process used for this broke the database connection. This previously wasn't a problem, as it was reconnecting before each test. [38398] introduced some changes that required the connection to be available in `setUpBeforeClass()`, earlier than in was previously reconnecting. This didn't cause warnings in Core, but it did cause warnings for plugins that don't disable the `backupGlobals` option. The database connection now reconnects in `setUpBeforeClass()`. This change also fixes a few Core tests that weren't calling `parent::setUpBeforeClass()` or `parent::tearDown()` correctly. Fixes #39327. git-svn-id: https://develop.svn.wordpress.org/trunk@39626 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4b984b7 commit f593d85

5 files changed

Lines changed: 23 additions & 5 deletions

File tree

tests/phpunit/includes/testcase.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public static function get_called_class() {
5757
}
5858

5959
public static function setUpBeforeClass() {
60+
global $wpdb;
61+
62+
$wpdb->suppress_errors = false;
63+
$wpdb->show_errors = true;
64+
$wpdb->db_connect();
65+
ini_set('display_errors', 1 );
66+
6067
parent::setUpBeforeClass();
6168

6269
$c = self::get_called_class();
@@ -98,11 +105,8 @@ function setUp() {
98105
$this->_backup_hooks();
99106
}
100107

101-
global $wpdb, $wp_rewrite;
102-
$wpdb->suppress_errors = false;
103-
$wpdb->show_errors = true;
104-
$wpdb->db_connect();
105-
ini_set('display_errors', 1 );
108+
global $wp_rewrite;
109+
106110
$this->clean_up_global_scope();
107111

108112
/*

tests/phpunit/includes/utils.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@ public function __construct() {
389389
$this->ready = true;
390390
$this->field_types = $wpdb->field_types;
391391
$this->charset = $wpdb->charset;
392+
393+
$this->dbuser = $wpdb->dbuser;
394+
$this->dbpassword = $wpdb->dbpassword;
395+
$this->dbname = $wpdb->dbname;
396+
$this->dbhost = $wpdb->dbhost;
392397
}
393398

394399
public function __call( $name, $arguments ) {

tests/phpunit/tests/db.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Tests_DB extends WP_UnitTestCase {
2020
protected static $_wpdb;
2121

2222
public static function setUpBeforeClass() {
23+
parent::setUpBeforeClass();
2324
self::$_wpdb = new wpdb_exposed_methods_for_testing();
2425
}
2526

@@ -63,6 +64,11 @@ public function test_db_reconnect() {
6364
$wpdb->close();
6465

6566
$var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" );
67+
68+
// Ensure all database handles have been properly reconnected after this test.
69+
$wpdb->db_connect();
70+
self::$_wpdb->db_connect();
71+
6672
$this->assertGreaterThan( 0, $var );
6773
}
6874

tests/phpunit/tests/db/charset.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Tests_DB_Charset extends WP_UnitTestCase {
2222
private static $server_info;
2323

2424
public static function setUpBeforeClass() {
25+
parent::setUpBeforeClass();
26+
2527
require_once( dirname( dirname( __FILE__ ) ) . '/db.php' );
2628

2729
self::$_wpdb = new wpdb_exposed_methods_for_testing();

tests/phpunit/tests/rewrite/numericSlugs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function setUp() {
1616
}
1717

1818
public function tearDown() {
19+
parent::tearDown();
1920
remove_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ), 10, 6 );
2021
}
2122

0 commit comments

Comments
 (0)