Tests: Add `Tests_dbDelta::assertTableHasPrimaryKey()`.

Also fix a typo in the name for the compound key.

Props charlestonsw.
Fixes #34877.

git-svn-id: https://develop.svn.wordpress.org/trunk@36552 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling (ocean90) 2016-02-17 18:54:27 +00:00
parent e1cab8a017
commit 653e0b0827
1 changed files with 24 additions and 9 deletions

View File

@ -33,7 +33,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
column_1 varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1),
KEY compound_key (id,column_1),
FULLTEXT KEY fulltext_key (column_1)
) ENGINE=MyISAM
"
@ -105,7 +105,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
column_1 varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1)
KEY compound_key (id,column_1)
)
"
);
@ -128,7 +128,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
column_1 varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1)
KEY compound_key (id,column_1)
)
"
);
@ -157,7 +157,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
extra_col longtext,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1)
KEY compound_key (id,column_1)
)
"
);
@ -171,6 +171,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
);
$this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
$this->assertTableHasPrimaryKey( 'id' , $wpdb->prefix . 'dbdelta_test' );
}
/**
@ -189,7 +190,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
id bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1)
KEY compound_key (id,column_1)
)
"
);
@ -215,7 +216,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
extra_col longtext,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1)
KEY compound_key (id,column_1)
)
"
, false // Don't execute.
@ -265,7 +266,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
column_1 varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY key_1 (column_1),
KEY compoud_key (id,column_1),
KEY compound_key (id,column_1),
FULLTEXT KEY fulltext_key (column_1)
)
", false
@ -286,7 +287,6 @@ class Tests_dbDelta extends WP_UnitTestCase {
* @param string $table The database table name.
*/
protected function assertTableRowHasValue( $column, $value, $table ) {
global $wpdb;
$table_row = $wpdb->get_row( "select $column from {$table} where $column = '$value'" );
@ -305,7 +305,6 @@ class Tests_dbDelta extends WP_UnitTestCase {
* @param string $table The database table name.
*/
protected function assertTableHasColumn( $column, $table ) {
global $wpdb;
$table_fields = $wpdb->get_results( "DESCRIBE {$table}" );
@ -313,6 +312,22 @@ class Tests_dbDelta extends WP_UnitTestCase {
$this->assertCount( 1, wp_list_filter( $table_fields, array( 'Field' => $column ) ) );
}
/**
* Assert that a table has a primary key.
*
* Checks for single-column primary keys. May not work for multi-column primary keys.
*
* @param string $column The column for the primary key.
* @param string $table The database table name.
*/
protected function assertTableHasPrimaryKey( $column , $table ) {
global $wpdb;
$table_indices = $wpdb->get_results( "SHOW INDEX FROM {$table}" );
$this->assertCount( 1, wp_list_filter( $table_indices, array( 'Key_name' => 'PRIMARY' , 'Column_name' => $column ) , 'AND' ) );
}
/**
* Assert that a table doesn't have a column.
*