| 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 | /** |