| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Test dbDelta(). |
| | 5 | * |
| | 6 | * @group upgrade |
| | 7 | * @group db |
| | 8 | */ |
| | 9 | class Test_DB_dbDelta extends WP_UnitTestCase { |
| | 10 | |
| | 11 | /** |
| | 12 | * Make sure the upgrade code is loaded before the tests are run. |
| | 13 | */ |
| | 14 | public static function setUpBeforeClass() { |
| | 15 | |
| | 16 | parent::setUpBeforeClass(); |
| | 17 | |
| | 18 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| | 19 | } |
| | 20 | |
| | 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(); |
| | 41 | } |
| | 42 | |
| | 43 | /** |
| | 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 | $this->assertEquals( |
| | 74 | array( "{$wpdb->prefix}dbdelta_create_test" => "Created table {$wpdb->prefix}dbdelta_create_test" ) |
| | 75 | , $updates |
| | 76 | ); |
| | 77 | |
| | 78 | $this->assertEquals( |
| | 79 | "{$wpdb->prefix}dbdelta_create_test" |
| | 80 | , $wpdb->get_var( |
| | 81 | $wpdb->prepare( |
| | 82 | 'SHOW TABLES LIKE %s' |
| | 83 | , $wpdb->esc_like( "{$wpdb->prefix}dbdelta_create_test" ) |
| | 84 | ) |
| | 85 | ) |
| | 86 | ); |
| | 87 | |
| | 88 | $wpdb->query( "DROP TABLE {$wpdb->prefix}dbdelta_create_test" ); |
| | 89 | } |
| | 90 | |
| | 91 | /** |
| | 92 | * Test that it does nothing for an existing table. |
| | 93 | */ |
| | 94 | public function test_existing_table() { |
| | 95 | |
| | 96 | global $wpdb; |
| | 97 | |
| | 98 | $updates = dbDelta( |
| | 99 | " |
| | 100 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| | 101 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| | 102 | column_1 varchar(255) NOT NULL, |
| | 103 | PRIMARY KEY (id), |
| | 104 | KEY key_1 (column_1), |
| | 105 | KEY compoud_key (id,column_1) |
| | 106 | ) |
| | 107 | " |
| | 108 | ); |
| | 109 | |
| | 110 | $this->assertEquals( array(), $updates ); |
| | 111 | } |
| | 112 | |
| | 113 | /** |
| | 114 | * Test the column type is updated. |
| | 115 | */ |
| | 116 | public function test_column_type_change() { |
| | 117 | |
| | 118 | global $wpdb; |
| | 119 | |
| | 120 | // id: bigint(20) => int(11) |
| | 121 | $updates = dbDelta( |
| | 122 | " |
| | 123 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| | 124 | id int(11) NOT NULL AUTO_INCREMENT, |
| | 125 | column_1 varchar(255) NOT NULL, |
| | 126 | PRIMARY KEY (id), |
| | 127 | KEY key_1 (column_1), |
| | 128 | KEY compoud_key (id,column_1) |
| | 129 | ) |
| | 130 | " |
| | 131 | ); |
| | 132 | |
| | 133 | $this->assertEquals( |
| | 134 | array( |
| | 135 | "{$wpdb->prefix}dbdelta_test.id" |
| | 136 | => "Changed type of {$wpdb->prefix}dbdelta_test.id from bigint(20) to int(11)" |
| | 137 | ) |
| | 138 | , $updates |
| | 139 | ); |
| | 140 | } |
| | 141 | |
| | 142 | /** |
| | 143 | * Test new column added. |
| | 144 | */ |
| | 145 | public function test_column_added() { |
| | 146 | |
| | 147 | global $wpdb; |
| | 148 | |
| | 149 | $updates = dbDelta( |
| | 150 | " |
| | 151 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| | 152 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| | 153 | column_1 varchar(255) NOT NULL, |
| | 154 | extra_col longtext, |
| | 155 | PRIMARY KEY (id), |
| | 156 | KEY key_1 (column_1), |
| | 157 | KEY compoud_key (id,column_1) |
| | 158 | ) |
| | 159 | " |
| | 160 | ); |
| | 161 | |
| | 162 | $this->assertEquals( |
| | 163 | array( |
| | 164 | "{$wpdb->prefix}dbdelta_test.extra_col" |
| | 165 | => "Added column {$wpdb->prefix}dbdelta_test.extra_col" |
| | 166 | ) |
| | 167 | , $updates |
| | 168 | ); |
| | 169 | |
| | 170 | $this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' ); |
| | 171 | } |
| | 172 | |
| | 173 | /** |
| | 174 | * Test that it does nothing when a column is removed. |
| | 175 | * |
| | 176 | * @ticket 26801 |
| | 177 | */ |
| | 178 | public function test_columns_arent_removed() { |
| | 179 | |
| | 180 | global $wpdb; |
| | 181 | |
| | 182 | // No column column_1 |
| | 183 | $updates = dbDelta( |
| | 184 | " |
| | 185 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| | 186 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| | 187 | PRIMARY KEY (id), |
| | 188 | KEY key_1 (column_1), |
| | 189 | KEY compoud_key (id,column_1) |
| | 190 | ) |
| | 191 | " |
| | 192 | ); |
| | 193 | |
| | 194 | $this->assertEquals( array(), $updates ); |
| | 195 | |
| | 196 | $this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' ); |
| | 197 | } |
| | 198 | |
| | 199 | /** |
| | 200 | * Test that nothing happens with $execute is false. |
| | 201 | */ |
| | 202 | public function test_no_execution() { |
| | 203 | |
| | 204 | global $wpdb; |
| | 205 | |
| | 206 | // Added column extra_col |
| | 207 | $updates = dbDelta( |
| | 208 | " |
| | 209 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| | 210 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| | 211 | column_1 varchar(255) NOT NULL, |
| | 212 | extra_col longtext, |
| | 213 | PRIMARY KEY (id), |
| | 214 | KEY key_1 (column_1), |
| | 215 | KEY compoud_key (id,column_1) |
| | 216 | ) |
| | 217 | " |
| | 218 | , false // Don't execute. |
| | 219 | ); |
| | 220 | |
| | 221 | $this->assertEquals( |
| | 222 | array( |
| | 223 | "{$wpdb->prefix}dbdelta_test.extra_col" |
| | 224 | => "Added column {$wpdb->prefix}dbdelta_test.extra_col" |
| | 225 | ) |
| | 226 | , $updates |
| | 227 | ); |
| | 228 | |
| | 229 | $this->assertTableHasNotColumn( 'extra_col', $wpdb->prefix . 'dbdelta_test' ); |
| | 230 | } |
| | 231 | |
| | 232 | // |
| | 233 | // Assertions. |
| | 234 | // |
| | 235 | |
| | 236 | /** |
| | 237 | * Assert that a table has a column. |
| | 238 | * |
| | 239 | * @param string $column The field name. |
| | 240 | * @param string $table The database table name. |
| | 241 | */ |
| | 242 | protected function assertTableHasColumn( $column, $table ) { |
| | 243 | |
| | 244 | global $wpdb; |
| | 245 | |
| | 246 | $table_fields = $wpdb->get_results( "DESCRIBE {$table}" ); |
| | 247 | |
| | 248 | $this->assertCount( 1, wp_list_filter( $table_fields, array( 'Field' => $column ) ) ); |
| | 249 | } |
| | 250 | |
| | 251 | /** |
| | 252 | * Assert that a table doesn't have a column. |
| | 253 | * |
| | 254 | * @param string $column The field name. |
| | 255 | * @param string $table The database table name. |
| | 256 | */ |
| | 257 | protected function assertTableHasNotColumn( $column, $table ) { |
| | 258 | |
| | 259 | global $wpdb; |
| | 260 | |
| | 261 | $table_fields = $wpdb->get_results( "DESCRIBE {$table}" ); |
| | 262 | |
| | 263 | $this->assertCount( 0, wp_list_filter( $table_fields, array( 'Field' => $column ) ) ); |
| | 264 | } |
| | 265 | } |