Skip to content

Instantly share code, notes, and snippets.

@salcode
Last active August 29, 2015 14:23
Show Gist options
  • Save salcode/d3c03a3e95e9eaab2cb9 to your computer and use it in GitHub Desktop.
Save salcode/d3c03a3e95e9eaab2cb9 to your computer and use it in GitHub Desktop.
#wcphilly contributors #unittests
<?php
/**
* Test dbDelta()
*
* @group upgrade
* @group dbdelta
*/
class Tests_dbDelta extends WP_UnitTestCase {
/**
* Make sure the upgrade code is loaded before the tests are run.
*/
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
}
/**
* 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 skip global table change
*
* @group ms-site
* @group multisite
*/
public function test_skip_global_table_change() {
global $wpdb;
if ( ! is_multisite() ) {
$this->markTestSkipped( 'Can not test global table skip if not multisite' );
}
$blog_id = $this->factory->blog->create();
switch_to_blog( $blog_id );
$updates = dbDelta(
"ALTER TABLE {$wpdb->base_prefix}users
ADD testcol bigint(20);
", false
);
restore_current_blog();
$this->assertEquals( array(), $updates );
}
/**
* 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 ) ) );
}
/**
* @ticket 31869
*/
function test_truncated_index() {
global $wpdb;
if ( ! $wpdb->has_cap( 'utf8mb4' ) ) {
$this->markTestSkipped( 'This test requires utf8mb4 support in MySQL.' );
}
$table_name = 'test_truncated_index';
$create = "CREATE TABLE $table_name (\n a varchar(255) COLLATE utf8mb4_unicode_ci,\n KEY a (a)\n)";
$wpdb->query( $create );
$actual = dbDelta( $create, false );
$this->assertSame( array(), $actual );
}
}
@tryonegg
Copy link

@jtsternberg
Copy link

jtsternberg

@ebinnion
Copy link

ebinnion

@JPry
Copy link

JPry commented Jun 14, 2015

WordPress.org Username: JPry

@avnarun
Copy link

avnarun commented Jun 14, 2015

avnarun

@kevko
Copy link

kevko commented Jun 14, 2015

My .org username: kevkoeh
Jorby jorbs: jorbin

@tryonegg
Copy link

/**
 * Test inserting into the database
 */
public function test_insert_into_table(){
    global $wpdb;

    $insert = dbDelta(
        "INSERT INTO {$wpdb->prefix}dbdelta_test (column_1) VALUES ('wcphilly2015')"
    );

    $this->assertEquals(
        array( )
        , $insert
    );

    $this->assertTableRowHasValue( 'column_1', 'wcphilly2015',  $wpdb->prefix . 'dbdelta_test' );

}
//
// Assertions.
//

/**
 * Assert that a table has a row with a value in a field.
 *
 * @param string $column The field name.
 * @param string $value  The field value.
 * @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'" );

    $expected = (object) array(
        $column => $value
    );

    $this->assertEquals( $expected, $table_row );
}

@salcode
Copy link
Author

salcode commented Jun 14, 2015

phpunit --group=multisite -c tests/phpunit/multisite.xml

@CamdenSegal
Copy link

camdensegal

@salcode
Copy link
Author

salcode commented Jun 14, 2015

Current state of the gist is incomplete failure to get coverage on line 2019 when run with

phpunit --group=ms-site -c tests/phpunit/multisite.xml --coverage-html=src/wp-content/coverage tests/phpunit/tests/dbdelta.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment