Improve dbDelta unit tests

Add new tests for more parts of dbDelta.  This was cleaned up and prepared for commit at #wcphilly contributor day.

Props jdgrimes, tryon, jtsternberg, ebinnion, JPry, avnarun, kevkoeh, jorbin, salcode.
Fixes #29020.



git-svn-id: https://develop.svn.wordpress.org/trunk@32770 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Aaron Jorbin 2015-06-14 19:22:20 +00:00
parent cda77232fe
commit 7988f78e44
1 changed files with 251 additions and 8 deletions

View File

@ -8,16 +8,260 @@
*/ */
class Tests_dbDelta extends WP_UnitTestCase { class Tests_dbDelta extends WP_UnitTestCase {
function test_create_new_table() { /**
include_once( ABSPATH . 'wp-admin/includes/upgrade.php'); * Make sure the upgrade code is loaded before the tests are run.
$table_name = 'test_new_table'; */
public static function setUpBeforeClass() {
$create = "CREATE TABLE $table_name (\n a varchar(255)\n)"; parent::setUpBeforeClass();
$expected = array( $table_name => "Created table $table_name" );
$actual = dbDelta( $create, false ); require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
}
$this->assertSame( $expected, $actual ); /**
* Create a custom table to be used in each test.
*/
public function setUp() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE {$wpdb->prefix}dbdelta_test (
id bigint(20) NOT NULL AUTO_INCREMENT,
column_1 varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1)
)
"
);
parent::setUp();
}
/**
* Delete the custom table on teardown.
*/
public function tearDown() {
global $wpdb;
parent::tearDown();
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test" );
}
/**
* Test table creation.
*/
public function test_creating_a_table() {
remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
global $wpdb;
$updates = dbDelta(
"CREATE TABLE {$wpdb->prefix}dbdelta_create_test (
id bigint(20) NOT NULL AUTO_INCREMENT,
column_1 varchar(255) NOT NULL,
PRIMARY KEY (id)
);"
);
$expected = array(
"{$wpdb->prefix}dbdelta_create_test" => "Created table {$wpdb->prefix}dbdelta_create_test"
);
$this->assertEquals( $expected, $updates );
$this->assertEquals(
"{$wpdb->prefix}dbdelta_create_test"
, $wpdb->get_var(
$wpdb->prepare(
'SHOW TABLES LIKE %s'
, $wpdb->esc_like( "{$wpdb->prefix}dbdelta_create_test" )
)
)
);
$wpdb->query( "DROP TABLE {$wpdb->prefix}dbdelta_create_test" );
}
/**
* Test that it does nothing for an existing table.
*/
public function test_existing_table() {
global $wpdb;
$updates = dbDelta(
"
CREATE TABLE {$wpdb->prefix}dbdelta_test (
id bigint(20) NOT NULL AUTO_INCREMENT,
column_1 varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1)
)
"
);
$this->assertEquals( array(), $updates );
}
/**
* Test the column type is updated.
*/
public function test_column_type_change() {
global $wpdb;
// id: bigint(20) => int(11)
$updates = dbDelta(
"
CREATE TABLE {$wpdb->prefix}dbdelta_test (
id int(11) NOT NULL AUTO_INCREMENT,
column_1 varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1)
)
"
);
$this->assertEquals(
array(
"{$wpdb->prefix}dbdelta_test.id"
=> "Changed type of {$wpdb->prefix}dbdelta_test.id from bigint(20) to int(11)"
)
, $updates
);
}
/**
* Test new column added.
*/
public function test_column_added() {
global $wpdb;
$updates = dbDelta(
"
CREATE TABLE {$wpdb->prefix}dbdelta_test (
id bigint(20) NOT NULL AUTO_INCREMENT,
column_1 varchar(255) NOT NULL,
extra_col longtext,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1)
)
"
);
$this->assertEquals(
array(
"{$wpdb->prefix}dbdelta_test.extra_col"
=> "Added column {$wpdb->prefix}dbdelta_test.extra_col"
)
, $updates
);
$this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
}
/**
* Test that it does nothing when a column is removed.
*
* @ticket 26801
*/
public function test_columns_arent_removed() {
global $wpdb;
// No column column_1
$updates = dbDelta(
"
CREATE TABLE {$wpdb->prefix}dbdelta_test (
id bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1)
)
"
);
$this->assertEquals( array(), $updates );
$this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
}
/**
* Test that nothing happens with $execute is false.
*/
public function test_no_execution() {
global $wpdb;
// Added column extra_col
$updates = dbDelta(
"
CREATE TABLE {$wpdb->prefix}dbdelta_test (
id bigint(20) NOT NULL AUTO_INCREMENT,
column_1 varchar(255) NOT NULL,
extra_col longtext,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1)
)
"
, false // Don't execute.
);
$this->assertEquals(
array(
"{$wpdb->prefix}dbdelta_test.extra_col"
=> "Added column {$wpdb->prefix}dbdelta_test.extra_col"
)
, $updates
);
$this->assertTableHasNotColumn( 'extra_col', $wpdb->prefix . 'dbdelta_test' );
}
//
// Assertions.
//
/**
* Assert that a table has a column.
*
* @param string $column The field name.
* @param string $table The database table name.
*/
protected function assertTableHasColumn( $column, $table ) {
global $wpdb;
$table_fields = $wpdb->get_results( "DESCRIBE {$table}" );
$this->assertCount( 1, wp_list_filter( $table_fields, array( 'Field' => $column ) ) );
}
/**
* Assert that a table doesn't have a column.
*
* @param string $column The field name.
* @param string $table The database table name.
*/
protected function assertTableHasNotColumn( $column, $table ) {
global $wpdb;
$table_fields = $wpdb->get_results( "DESCRIBE {$table}" );
$this->assertCount( 0, wp_list_filter( $table_fields, array( 'Field' => $column ) ) );
} }
/** /**
@ -30,7 +274,6 @@ class Tests_dbDelta extends WP_UnitTestCase {
$this->markTestSkipped( 'This test requires utf8mb4 support in MySQL.' ); $this->markTestSkipped( 'This test requires utf8mb4 support in MySQL.' );
} }
include_once( ABSPATH . 'wp-admin/includes/upgrade.php');
$table_name = 'test_truncated_index'; $table_name = 'test_truncated_index';
$create = "CREATE TABLE $table_name (\n a varchar(255) COLLATE utf8mb4_unicode_ci,\n KEY a (a)\n)"; $create = "CREATE TABLE $table_name (\n a varchar(255) COLLATE utf8mb4_unicode_ci,\n KEY a (a)\n)";