Changeset 42343 for trunk/tests/phpunit/tests/meta.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/meta.php (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/meta.php
r38762 r42343 9 9 function setUp() { 10 10 parent::setUp(); 11 $this->author = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );12 $this->meta_id = add_metadata( 'user', $this->author->ID, 'meta_key', 'meta_value' );11 $this->author = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) ); 12 $this->meta_id = add_metadata( 'user', $this->author->ID, 'meta_key', 'meta_value' ); 13 13 $this->delete_meta_id = add_metadata( 'user', $this->author->ID, 'delete_meta_key', 'delete_meta_value' ); 14 14 } 15 15 16 function _meta_sanitize_cb ( $meta_value, $meta_key, $meta_type ) {16 function _meta_sanitize_cb( $meta_value, $meta_key, $meta_type ) { 17 17 return 'sanitized'; 18 18 } … … 69 69 // Let's try some invalid meta data 70 70 $this->assertFalse( update_metadata_by_mid( 'user', 0, 'meta_value' ) ); 71 $this->assertFalse( update_metadata_by_mid( 'user', $this->meta_id, 'meta_value', array( 'invalid', 'key' ) ) );71 $this->assertFalse( update_metadata_by_mid( 'user', $this->meta_id, 'meta_value', array( 'invalid', 'key' ) ) ); 72 72 73 73 // Let's see if caches get cleared after updates. 74 $meta = get_metadata_by_mid( 'user', $this->meta_id );74 $meta = get_metadata_by_mid( 'user', $this->meta_id ); 75 75 $first = get_user_meta( $meta->user_id, $meta->meta_key ); 76 76 $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'other_meta_value' ) ); … … 97 97 remove_action( 'updated_postmeta', array( $this, 'updated_meta' ) ); 98 98 99 $found = $this->updated_mids;99 $found = $this->updated_mids; 100 100 $this->updated_mids = array(); 101 101 … … 106 106 107 107 function test_metadata_exists() { 108 $this->assertFalse( metadata_exists( 'user', $this->author->ID, 'foobarbaz' ) );109 $this->assertTrue( metadata_exists( 'user', $this->author->ID, 'meta_key' ) );110 $this->assertFalse( metadata_exists( 'user', 1234567890, 'foobarbaz' ) );111 $this->assertFalse( metadata_exists( 'user', 1234567890, 'meta_key' ) );108 $this->assertFalse( metadata_exists( 'user', $this->author->ID, 'foobarbaz' ) ); 109 $this->assertTrue( metadata_exists( 'user', $this->author->ID, 'meta_key' ) ); 110 $this->assertFalse( metadata_exists( 'user', 1234567890, 'foobarbaz' ) ); 111 $this->assertFalse( metadata_exists( 'user', 1234567890, 'meta_key' ) ); 112 112 } 113 113 … … 127 127 */ 128 128 function test_user_metadata_not_exists() { 129 $u = get_users( array( 130 'meta_query' => array( 131 array( 'key' => 'meta_key', 'compare' => 'NOT EXISTS' ) 132 ) 133 ) ); 129 $u = get_users( 130 array( 131 'meta_query' => array( 132 array( 133 'key' => 'meta_key', 134 'compare' => 'NOT EXISTS', 135 ), 136 ), 137 ) 138 ); 134 139 135 140 $this->assertEquals( 1, count( $u ) ); … … 139 144 140 145 // Test EXISTS and NOT EXISTS together, no users should be found 141 $this->assertEquals( 0, count( get_users( array( 142 'meta_query' => array( 143 array( 'key' => 'meta_key', 'compare' => 'NOT EXISTS' ), 144 array( 'key' => 'delete_meta_key', 'compare' => 'EXISTS' ) 145 ) 146 ) ) ) ); 147 148 $this->assertEquals( 2, count( get_users( array( 149 'meta_query' => array( 150 array( 'key' => 'non_existing_meta', 'compare' => 'NOT EXISTS' ) 151 ) 152 ) ) ) ); 146 $this->assertEquals( 147 0, count( 148 get_users( 149 array( 150 'meta_query' => array( 151 array( 152 'key' => 'meta_key', 153 'compare' => 'NOT EXISTS', 154 ), 155 array( 156 'key' => 'delete_meta_key', 157 'compare' => 'EXISTS', 158 ), 159 ), 160 ) 161 ) 162 ) 163 ); 164 165 $this->assertEquals( 166 2, count( 167 get_users( 168 array( 169 'meta_query' => array( 170 array( 171 'key' => 'non_existing_meta', 172 'compare' => 'NOT EXISTS', 173 ), 174 ), 175 ) 176 ) 177 ) 178 ); 153 179 154 180 delete_metadata( 'user', $this->author->ID, 'meta_key' ); 155 181 156 $this->assertEquals( 2, count( get_users( array( 157 'meta_query' => array( 158 array( 'key' => 'meta_key', 'compare' => 'NOT EXISTS' ) 159 ) 160 ) ) ) ); 182 $this->assertEquals( 183 2, count( 184 get_users( 185 array( 186 'meta_query' => array( 187 array( 188 'key' => 'meta_key', 189 'compare' => 'NOT EXISTS', 190 ), 191 ), 192 ) 193 ) 194 ) 195 ); 161 196 } 162 197 163 198 function test_metadata_slashes() { 164 $key = __FUNCTION__;165 $value = 'Test\\singleslash';166 $expected = 'Testsingleslash';167 $value2 = 'Test\\\\doubleslash';199 $key = __FUNCTION__; 200 $value = 'Test\\singleslash'; 201 $expected = 'Testsingleslash'; 202 $value2 = 'Test\\\\doubleslash'; 168 203 $expected2 = 'Test\\doubleslash'; 169 204 $this->assertFalse( metadata_exists( 'user', $this->author->ID, $key ) ); … … 202 237 add_post_meta( $post_id2, 'num_as_longtext_desc', 100 ); 203 238 204 $posts = new WP_Query( array( 205 'fields' => 'ids', 206 'post_type' => 'any', 207 'meta_key' => 'num_as_longtext', 208 'meta_value' => '0', 209 'meta_compare' => '>', 210 'meta_type' => 'UNSIGNED', 211 'orderby' => 'meta_value', 212 'order' => 'ASC' 213 ) ); 239 $posts = new WP_Query( 240 array( 241 'fields' => 'ids', 242 'post_type' => 'any', 243 'meta_key' => 'num_as_longtext', 244 'meta_value' => '0', 245 'meta_compare' => '>', 246 'meta_type' => 'UNSIGNED', 247 'orderby' => 'meta_value', 248 'order' => 'ASC', 249 ) 250 ); 214 251 215 252 $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts ); … … 217 254 218 255 // Make sure the newer meta_query syntax behaves in a consistent way 219 $posts = new WP_Query( array( 220 'fields' => 'ids', 221 'post_type' => 'any', 222 'meta_query' => array( 223 array( 224 'key' => 'num_as_longtext', 225 'value' => '0', 226 'compare' => '>', 227 'type' => 'UNSIGNED', 256 $posts = new WP_Query( 257 array( 258 'fields' => 'ids', 259 'post_type' => 'any', 260 'meta_query' => array( 261 array( 262 'key' => 'num_as_longtext', 263 'value' => '0', 264 'compare' => '>', 265 'type' => 'UNSIGNED', 266 ), 228 267 ), 229 ),230 'orderby' => 'meta_value',231 'order' => 'ASC'232 ) );268 'orderby' => 'meta_value', 269 'order' => 'ASC', 270 ) 271 ); 233 272 234 273 $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts ); … … 236 275 237 276 // The legacy `meta_key` value should take precedence. 238 $posts = new WP_Query( array( 239 'fields' => 'ids', 240 'post_type' => 'any', 241 'meta_key' => 'num_as_longtext', 242 'meta_compare' => '>', 243 'meta_type' => 'UNSIGNED', 244 'meta_query' => array( 245 array( 246 'key' => 'num_as_longtext_desc', 247 'value' => '0', 248 'compare' => '>', 249 'type' => 'UNSIGNED', 277 $posts = new WP_Query( 278 array( 279 'fields' => 'ids', 280 'post_type' => 'any', 281 'meta_key' => 'num_as_longtext', 282 'meta_compare' => '>', 283 'meta_type' => 'UNSIGNED', 284 'meta_query' => array( 285 array( 286 'key' => 'num_as_longtext_desc', 287 'value' => '0', 288 'compare' => '>', 289 'type' => 'UNSIGNED', 290 ), 250 291 ), 251 ),252 'orderby' => 'meta_value',253 'order' => 'ASC'254 ) );292 'orderby' => 'meta_value', 293 'order' => 'ASC', 294 ) 295 ); 255 296 256 297 $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts ); … … 260 301 function test_meta_cache_order_asc() { 261 302 $post_id = self::factory()->post->create(); 262 $colors = array( 'red', 'blue', 'yellow', 'green' );263 foreach ( $colors as $color ) 303 $colors = array( 'red', 'blue', 'yellow', 'green' ); 304 foreach ( $colors as $color ) { 264 305 add_post_meta( $post_id, 'color', $color ); 306 } 265 307 266 308 foreach ( range( 1, 10 ) as $i ) { … … 268 310 $this->assertEquals( $meta, $colors ); 269 311 270 if ( 0 === $i % 2 ) 312 if ( 0 === $i % 2 ) { 271 313 wp_cache_delete( $post_id, 'post_meta' ); 314 } 272 315 } 273 316 } … … 335 378 */ 336 379 public function test_get_metadata_with_empty_key_array_value() { 337 $data = array( 1, 2 );380 $data = array( 1, 2 ); 338 381 $value = serialize( $data ); 339 382 add_metadata( 'user', $this->author->ID, 'foo', $data ); … … 347 390 */ 348 391 public function test_get_metadata_with_empty_key_object_value() { 349 $data = new stdClass;392 $data = new stdClass; 350 393 $data->foo = 'bar'; 394 $value = serialize( $data ); 395 add_metadata( 'user', $this->author->ID, 'foo', $data ); 396 $found = get_metadata( 'user', $this->author->ID ); 397 398 $this->assertEquals( array( $value ), $found['foo'] ); 399 } 400 401 /** 402 * @ticket 15030 403 */ 404 public function test_get_metadata_with_empty_key_nested_array_value() { 405 $data = array( 406 array( 1, 2 ), 407 array( 3, 4 ), 408 ); 351 409 $value = serialize( $data ); 352 410 add_metadata( 'user', $this->author->ID, 'foo', $data ); 353 411 $found = get_metadata( 'user', $this->author->ID ); 354 412 355 $this->assertEquals( array( $value ), $found['foo'] );356 }357 358 /**359 * @ticket 15030360 */361 public function test_get_metadata_with_empty_key_nested_array_value() {362 $data = array(363 array( 1, 2 ),364 array( 3, 4 ),365 );366 $value = serialize( $data );367 add_metadata( 'user', $this->author->ID, 'foo', $data );368 $found = get_metadata( 'user', $this->author->ID );369 370 413 $this->assertSame( array( $value ), $found['foo'] ); 371 414 } 372 415 373 /** Helpers * *********************************************************/416 /** Helpers */ 374 417 375 418 public function updated_meta( $meta_id ) {
Note: See TracChangeset
for help on using the changeset viewer.