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