Make WordPress Core

Ticket #29020: 29020.3.diff

File 29020.3.diff, 6.4 KB (added by salcode, 10 years ago)
  • tests/phpunit/tests/dbdelta.php

     
    88 */
    99class Tests_dbDelta extends WP_UnitTestCase {
    1010
    11         function test_create_new_table() {
    12                 include_once( ABSPATH . 'wp-admin/includes/upgrade.php');
    13                 $table_name = 'test_new_table';
     11        /**
     12         * Make sure the upgrade code is loaded before the tests are run.
     13         */
     14        public static function setUpBeforeClass() {
    1415
    15                 $create = "CREATE TABLE $table_name (\n a varchar(255)\n)";
    16                 $expected = array( $table_name => "Created table $table_name" );
     16                parent::setUpBeforeClass();
    1717
    18                 $actual = dbDelta( $create, false );
     18                require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     19        }
    1920
    20                 $this->assertSame( $expected, $actual );
     21        /**
     22         * Create a custom table to be used in each test.
     23         */
     24        public function setUp() {
     25
     26                global $wpdb;
     27
     28                $wpdb->query(
     29                        "
     30                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     31                                id bigint(20) NOT NULL AUTO_INCREMENT,
     32                                column_1 varchar(255) NOT NULL,
     33                                PRIMARY KEY  (id),
     34                                KEY key_1 (column_1),
     35                                KEY compoud_key (id,column_1)
     36                        )
     37                        "
     38                );
     39
     40                parent::setUp();
    2141        }
    2242
    2343        /**
     44         * Delete the custom table on teardown.
     45         */
     46        public function tearDown() {
     47
     48                global $wpdb;
     49
     50                parent::tearDown();
     51
     52                $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test" );
     53        }
     54
     55        /**
     56         * Test table creation.
     57         */
     58        public function test_creating_a_table() {
     59
     60                remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
     61                remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
     62
     63                global $wpdb;
     64
     65                $updates = dbDelta(
     66                        "CREATE TABLE {$wpdb->prefix}dbdelta_create_test (
     67                                id bigint(20) NOT NULL AUTO_INCREMENT,
     68                                column_1 varchar(255) NOT NULL,
     69                                PRIMARY KEY  (id)
     70                        );"
     71                );
     72
     73                $expected = array(
     74                        "{$wpdb->prefix}dbdelta_create_test" => "Created table {$wpdb->prefix}dbdelta_create_test"
     75                );
     76
     77                $this->assertEquals( $expected, $updates );
     78
     79                $this->assertEquals(
     80                        "{$wpdb->prefix}dbdelta_create_test"
     81                        , $wpdb->get_var(
     82                                $wpdb->prepare(
     83                                        'SHOW TABLES LIKE %s'
     84                                        , $wpdb->esc_like( "{$wpdb->prefix}dbdelta_create_test" )
     85                                )
     86                        )
     87                );
     88
     89                $wpdb->query( "DROP TABLE {$wpdb->prefix}dbdelta_create_test" );
     90        }
     91
     92        /**
     93         * Test that it does nothing for an existing table.
     94         */
     95        public function test_existing_table() {
     96
     97                global $wpdb;
     98
     99                $updates = dbDelta(
     100                        "
     101                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     102                                id bigint(20) NOT NULL AUTO_INCREMENT,
     103                                column_1 varchar(255) NOT NULL,
     104                                PRIMARY KEY  (id),
     105                                KEY key_1 (column_1),
     106                                KEY compoud_key (id,column_1)
     107                        )
     108                        "
     109                );
     110
     111                $this->assertEquals( array(), $updates );
     112        }
     113
     114        /**
     115         * Test the column type is updated.
     116         */
     117        public function test_column_type_change() {
     118
     119                global $wpdb;
     120
     121                // id: bigint(20) => int(11)
     122                $updates = dbDelta(
     123                        "
     124                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     125                                id int(11) NOT NULL AUTO_INCREMENT,
     126                                column_1 varchar(255) NOT NULL,
     127                                PRIMARY KEY  (id),
     128                                KEY key_1 (column_1),
     129                                KEY compoud_key (id,column_1)
     130                        )
     131                        "
     132                );
     133
     134                $this->assertEquals(
     135                        array(
     136                                "{$wpdb->prefix}dbdelta_test.id"
     137                                        => "Changed type of {$wpdb->prefix}dbdelta_test.id from bigint(20) to int(11)"
     138                        )
     139                        , $updates
     140                );
     141        }
     142
     143        /**
     144         * Test new column added.
     145         */
     146        public function test_column_added() {
     147
     148                global $wpdb;
     149
     150                $updates = dbDelta(
     151                        "
     152                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     153                                id bigint(20) NOT NULL AUTO_INCREMENT,
     154                                column_1 varchar(255) NOT NULL,
     155                                extra_col longtext,
     156                                PRIMARY KEY  (id),
     157                                KEY key_1 (column_1),
     158                                KEY compoud_key (id,column_1)
     159                        )
     160                        "
     161                );
     162
     163                $this->assertEquals(
     164                        array(
     165                                "{$wpdb->prefix}dbdelta_test.extra_col"
     166                                        => "Added column {$wpdb->prefix}dbdelta_test.extra_col"
     167                        )
     168                        , $updates
     169                );
     170
     171                $this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
     172        }
     173
     174        /**
     175         * Test that it does nothing when a column is removed.
     176         *
     177         * @ticket 26801
     178         */
     179        public function test_columns_arent_removed() {
     180
     181                global $wpdb;
     182
     183                // No column column_1
     184                $updates = dbDelta(
     185                        "
     186                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     187                                id bigint(20) NOT NULL AUTO_INCREMENT,
     188                                PRIMARY KEY  (id),
     189                                KEY key_1 (column_1),
     190                                KEY compoud_key (id,column_1)
     191                        )
     192                        "
     193                );
     194
     195                $this->assertEquals( array(), $updates );
     196
     197                $this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
     198        }
     199
     200        /**
     201         * Test that nothing happens with $execute is false.
     202         */
     203        public function test_no_execution() {
     204
     205                global $wpdb;
     206
     207                // Added column extra_col
     208                $updates = dbDelta(
     209                        "
     210                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     211                                id bigint(20) NOT NULL AUTO_INCREMENT,
     212                                column_1 varchar(255) NOT NULL,
     213                                extra_col longtext,
     214                                PRIMARY KEY  (id),
     215                                KEY key_1 (column_1),
     216                                KEY compoud_key (id,column_1)
     217                        )
     218                        "
     219                        , false // Don't execute.
     220                );
     221
     222                $this->assertEquals(
     223                        array(
     224                                "{$wpdb->prefix}dbdelta_test.extra_col"
     225                                        => "Added column {$wpdb->prefix}dbdelta_test.extra_col"
     226                        )
     227                        , $updates
     228                );
     229
     230                $this->assertTableHasNotColumn( 'extra_col', $wpdb->prefix . 'dbdelta_test' );
     231        }
     232
     233        //
     234        // Assertions.
     235        //
     236
     237        /**
     238         * Assert that a table has a column.
     239         *
     240         * @param string $column The field name.
     241         * @param string $table  The database table name.
     242         */
     243        protected function assertTableHasColumn( $column, $table ) {
     244
     245                global $wpdb;
     246
     247                $table_fields = $wpdb->get_results( "DESCRIBE {$table}" );
     248
     249                $this->assertCount( 1, wp_list_filter( $table_fields, array( 'Field' => $column ) ) );
     250        }
     251
     252        /**
     253         * Assert that a table doesn't have a column.
     254         *
     255         * @param string $column The field name.
     256         * @param string $table  The database table name.
     257         */
     258        protected function assertTableHasNotColumn( $column, $table ) {
     259
     260                global $wpdb;
     261
     262                $table_fields = $wpdb->get_results( "DESCRIBE {$table}" );
     263
     264                $this->assertCount( 0, wp_list_filter( $table_fields, array( 'Field' => $column ) ) );
     265        }
     266
     267        /**
    24268         * @ticket 31869
    25269         */
    26270        function test_truncated_index() {
     
    30274                        $this->markTestSkipped( 'This test requires utf8mb4 support in MySQL.' );
    31275                }
    32276
    33                 include_once( ABSPATH . 'wp-admin/includes/upgrade.php');
    34277                $table_name = 'test_truncated_index';
    35278
    36279                $create = "CREATE TABLE $table_name (\n a varchar(255) COLLATE utf8mb4_unicode_ci,\n KEY a (a)\n)";