Changeset 35197
- Timestamp:
- 10/15/2015 07:28:40 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/user/query.php
r35123 r35197 6 6 */ 7 7 class Tests_User_Query extends WP_UnitTestCase { 8 protected static $author_ids; 9 protected static $sub_ids; 10 protected static $editor_ids; 11 protected static $contrib_id; 12 protected static $admin_ids; 8 13 9 14 protected $user_id; 10 15 11 function setUp() { 12 parent::setUp(); 16 public static function wpSetUpBeforeClass( $factory ) { 17 self::$author_ids = $factory->user->create_many( 4, array( 18 'role' => 'author' 19 ) ); 20 21 self::$sub_ids = $factory->user->create_many( 2, array( 22 'role' => 'subscriber', 23 ) ); 24 25 self::$editor_ids = $factory->user->create_many( 3, array( 26 'role' => 'editor', 27 ) ); 28 29 self::$contrib_id = $factory->user->create( array( 30 'role' => 'contributor', 31 ) ); 32 33 self::$admin_ids = $factory->user->create_many( 2, array( 34 'role' => 'administrator', 35 ) ); 36 } 37 38 public static function wpTearDownAfterClass() { 39 $sets = array( 40 self::$author_ids, 41 self::$sub_ids, 42 self::$editor_ids, 43 array( self::$contrib_id ), 44 self::$admin_ids 45 ); 46 47 foreach ( $sets as $set ) { 48 foreach ( $set as $id ) { 49 if ( is_multisite() ) { 50 wpmu_delete_user( $id ); 51 } else { 52 wp_delete_user( $id ); 53 } 54 } 55 } 13 56 } 14 57 … … 32 75 33 76 public function test_include_single() { 34 $users = $this->factory->user->create_many( 2 );35 77 $q = new WP_User_Query( array( 36 78 'fields' => '', 37 'include' => $users[0],79 'include' => self::$author_ids[0], 38 80 ) ); 39 81 $ids = $q->get_results(); 40 82 41 $this->assertEquals( array( $users[0] ), $ids );83 $this->assertEquals( array( self::$author_ids[0] ), $ids ); 42 84 } 43 85 44 86 public function test_include_comma_separated() { 45 $users = $this->factory->user->create_many( 3 );46 87 $q = new WP_User_Query( array( 47 88 'fields' => '', 48 'include' => $users[0] . ', ' . $users[2],89 'include' => self::$author_ids[0] . ', ' . self::$author_ids[2], 49 90 ) ); 50 91 $ids = $q->get_results(); 51 92 52 $this->assertEqualSets( array( $users[0], $users[2] ), $ids );93 $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids ); 53 94 } 54 95 55 96 public function test_include_array() { 56 $users = $this->factory->user->create_many( 3 );57 97 $q = new WP_User_Query( array( 58 98 'fields' => '', 59 'include' => array( $users[0], $users[2] ),99 'include' => array( self::$author_ids[0], self::$author_ids[2] ), 60 100 ) ); 61 101 $ids = $q->get_results(); 62 102 63 $this->assertEqualSets( array( $users[0], $users[2] ), $ids );103 $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids ); 64 104 } 65 105 66 106 public function test_include_array_bad_values() { 67 $users = $this->factory->user->create_many( 3 );68 107 $q = new WP_User_Query( array( 69 108 'fields' => '', 70 'include' => array( $users[0], 'foo', $users[2] ),109 'include' => array( self::$author_ids[0], 'foo', self::$author_ids[2] ), 71 110 ) ); 72 111 $ids = $q->get_results(); 73 112 74 $this->assertEqualSets( array( $users[0], $users[2] ), $ids );113 $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids ); 75 114 } 76 115 77 116 public function test_exclude() { 78 $users = $this->factory->user->create_many( 3, array(79 'role' => 'author',80 ) );81 82 117 $q = new WP_User_Query( array( 83 118 'fields' => '', 84 'exclude' => $users[1],119 'exclude' => self::$author_ids[1], 85 120 ) ); 86 121 … … 89 124 // Indirect test in order to ignore default user created during installation. 90 125 $this->assertNotEmpty( $ids ); 91 $this->assertNotContains( $users[1], $ids );126 $this->assertNotContains( self::$author_ids[1], $ids ); 92 127 } 93 128 94 129 public function test_get_all() { 95 $this->factory->user->create_many( 3, array(96 'role' => 'author'97 ) );98 99 130 $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) ); 100 131 $users = $users->get_results(); 101 132 102 133 // +1 for the default user created during installation. 103 $this->assertEquals( 4, count( $users ) );134 $this->assertEquals( 13, count( $users ) ); 104 135 foreach ( $users as $user ) { 105 136 $this->assertInstanceOf( 'WP_User', $user ); … … 108 139 $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'fields' => 'all_with_meta' ) ); 109 140 $users = $users->get_results(); 110 $this->assertEquals( 4, count( $users ) );141 $this->assertEquals( 13, count( $users ) ); 111 142 foreach ( $users as $user ) { 112 143 $this->assertInstanceOf( 'WP_User', $user ); … … 136 167 137 168 public function test_orderby_meta_value() { 138 $users = $this->factory->user->create_many( 3, array( 139 'role' => 'author' 140 ) ); 141 142 update_user_meta( $users[0], 'last_name', 'Jones' ); 143 update_user_meta( $users[1], 'last_name', 'Albert' ); 144 update_user_meta( $users[2], 'last_name', 'Zorro' ); 145 146 $q = new WP_User_Query( array( 147 'include' => $users, 169 update_user_meta( self::$author_ids[0], 'last_name', 'Jones' ); 170 update_user_meta( self::$author_ids[1], 'last_name', 'Albert' ); 171 update_user_meta( self::$author_ids[2], 'last_name', 'Zorro' ); 172 173 $q = new WP_User_Query( array( 174 'include' => self::$author_ids, 148 175 'meta_key' => 'last_name', 149 176 'orderby' => 'meta_value', … … 151 178 ) ); 152 179 153 $expected = array( $users[1], $users[0], $users[2] );180 $expected = array( self::$author_ids[3], self::$author_ids[1], self::$author_ids[0], self::$author_ids[2] ); 154 181 155 182 $this->assertEquals( $expected, $q->get_results() ); … … 160 187 */ 161 188 public function test_orderby_meta_value_num() { 162 $users = $this->factory->user->create_many( 3, array( 163 'role' => 'author' 164 ) ); 165 166 update_user_meta( $users[0], 'user_age', '101' ); 167 update_user_meta( $users[1], 'user_age', '20' ); 168 update_user_meta( $users[2], 'user_age', '25' ); 169 170 $q = new WP_User_Query( array( 171 'include' => $users, 189 update_user_meta( self::$author_ids[0], 'user_age', '101' ); 190 update_user_meta( self::$author_ids[1], 'user_age', '20' ); 191 update_user_meta( self::$author_ids[2], 'user_age', '25' ); 192 193 $q = new WP_User_Query( array( 194 'include' => self::$author_ids, 172 195 'meta_key' => 'user_age', 173 196 'orderby' => 'meta_value_num', … … 175 198 ) ); 176 199 177 $expected = array( $users[1], $users[2], $users[0] );200 $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] ); 178 201 179 202 $this->assertEquals( $expected, $q->get_results() ); … … 184 207 */ 185 208 public function test_orderby_somekey_where_meta_key_is_somekey() { 186 $users = $this->factory->user->create_many( 3, array( 187 'role' => 'author' 188 ) ); 189 190 update_user_meta( $users[0], 'foo', 'zzz' ); 191 update_user_meta( $users[1], 'foo', 'aaa' ); 192 update_user_meta( $users[2], 'foo', 'jjj' ); 193 194 $q = new WP_User_Query( array( 195 'include' => $users, 209 update_user_meta( self::$author_ids[0], 'foo', 'zzz' ); 210 update_user_meta( self::$author_ids[1], 'foo', 'aaa' ); 211 update_user_meta( self::$author_ids[2], 'foo', 'jjj' ); 212 213 $q = new WP_User_Query( array( 214 'include' => self::$author_ids, 196 215 'meta_key' => 'foo', 197 216 'orderby' => 'foo', … … 199 218 ) ); 200 219 201 $expected = array( $users[1], $users[2], $users[0] );220 $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] ); 202 221 203 222 $this->assertEquals( $expected, $q->get_results() ); … … 208 227 */ 209 228 public function test_orderby_clause_key() { 210 $users = $this->factory->user->create_many( 3 ); 211 add_user_meta( $users[0], 'foo', 'aaa' ); 212 add_user_meta( $users[1], 'foo', 'zzz' ); 213 add_user_meta( $users[2], 'foo', 'jjj' ); 229 add_user_meta( self::$author_ids[0], 'foo', 'aaa' ); 230 add_user_meta( self::$author_ids[1], 'foo', 'zzz' ); 231 add_user_meta( self::$author_ids[2], 'foo', 'jjj' ); 214 232 215 233 $q = new WP_User_Query( array( … … 225 243 ) ); 226 244 227 $this->assertEquals( array( $users[1], $users[2], $users[0] ), $q->results );245 $this->assertEquals( array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] ), $q->results ); 228 246 } 229 247 … … 232 250 */ 233 251 public function test_orderby_clause_key_as_secondary_sort() { 234 $u1 = $this->factory->user->create( array(252 $u1 = self::$static_factory->user->create( array( 235 253 'user_registered' => '2015-01-28 03:00:00', 236 254 ) ); 237 $u2 = $this->factory->user->create( array(255 $u2 = self::$static_factory->user->create( array( 238 256 'user_registered' => '2015-01-28 05:00:00', 239 257 ) ); 240 $u3 = $this->factory->user->create( array(258 $u3 = self::$static_factory->user->create( array( 241 259 'user_registered' => '2015-01-28 03:00:00', 242 260 ) ); … … 267 285 */ 268 286 public function test_orderby_more_than_one_clause_key() { 269 $users = $this->factory->user->create_many( 3 ); 270 271 add_user_meta( $users[0], 'foo', 'jjj' ); 272 add_user_meta( $users[1], 'foo', 'zzz' ); 273 add_user_meta( $users[2], 'foo', 'jjj' ); 274 add_user_meta( $users[0], 'bar', 'aaa' ); 275 add_user_meta( $users[1], 'bar', 'ccc' ); 276 add_user_meta( $users[2], 'bar', 'bbb' ); 287 add_user_meta( self::$author_ids[0], 'foo', 'jjj' ); 288 add_user_meta( self::$author_ids[1], 'foo', 'zzz' ); 289 add_user_meta( self::$author_ids[2], 'foo', 'jjj' ); 290 add_user_meta( self::$author_ids[0], 'bar', 'aaa' ); 291 add_user_meta( self::$author_ids[1], 'bar', 'ccc' ); 292 add_user_meta( self::$author_ids[2], 'bar', 'bbb' ); 277 293 278 294 $q = new WP_User_Query( array( … … 294 310 ) ); 295 311 296 $this->assertEquals( array( $users[2], $users[0], $users[1] ), $q->results );312 $this->assertEquals( array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] ), $q->results ); 297 313 } 298 314 … … 314 330 global $wpdb; 315 331 316 $users = $this->factory->user->create_many( 4 );317 332 $q = new WP_User_Query( array( 318 333 'orderby' => 'include', 319 'include' => array( $users[1], $users[0], $users[3] ),334 'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), 320 335 'fields' => '', 321 336 ) ); 322 337 323 $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . $users[1] . ',' . $users[0] . ',' . $users[3] . ' )';338 $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )'; 324 339 $this->assertContains( $expected_orderby, $q->query_orderby ); 325 340 326 341 // assertEquals() respects order but ignores type (get_results() returns numeric strings). 327 $this->assertEquals( array( $users[1], $users[0], $users[3] ), $q->get_results() );342 $this->assertEquals( array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results() ); 328 343 } 329 344 … … 334 349 global $wpdb; 335 350 336 $users = $this->factory->user->create_many( 4 );337 351 $q = new WP_User_Query( array( 338 352 'orderby' => 'include', 339 'include' => array( $users[1], $users[0], $users[1], $users[3] ),353 'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[1], self::$author_ids[3] ), 340 354 'fields' => '', 341 355 ) ); 342 356 343 $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . $users[1] . ',' . $users[0] . ',' . $users[3] . ' )';357 $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )'; 344 358 $this->assertContains( $expected_orderby, $q->query_orderby ); 345 359 346 360 // assertEquals() respects order but ignores type (get_results() returns numeric strings). 347 $this->assertEquals( array( $users[1], $users[0], $users[3] ), $q->get_results() );361 $this->assertEquals( array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results() ); 348 362 } 349 363 … … 352 366 */ 353 367 public function test_orderby_space_separated() { 354 global $wpdb;355 356 368 $q = new WP_User_Query( array( 357 369 'orderby' => 'login nicename', … … 366 378 */ 367 379 public function test_orderby_flat_array() { 368 global $wpdb;369 370 380 $q = new WP_User_Query( array( 371 381 'orderby' => array( 'login', 'nicename' ), … … 379 389 */ 380 390 public function test_orderby_array_contains_invalid_item() { 381 global $wpdb;382 383 391 $q = new WP_User_Query( array( 384 392 'orderby' => array( 'login', 'foo', 'nicename' ), … … 392 400 */ 393 401 public function test_orderby_array_contains_all_invalid_items() { 394 global $wpdb;395 396 402 $q = new WP_User_Query( array( 397 403 'orderby' => array( 'foo', 'bar', 'baz' ), … … 405 411 */ 406 412 public function test_orderby_array() { 407 global $wpdb;408 409 413 $q = new WP_User_Query( array( 410 414 'orderby' => array( … … 422 426 */ 423 427 public function test_orderby_array_should_discard_invalid_columns() { 424 global $wpdb;425 426 428 $q = new WP_User_Query( array( 427 429 'orderby' => array( … … 440 442 function test_number() { 441 443 // +1 for the default user created by the test suite. 442 $user_ids = $this->factory->user->create_many( 3 );443 444 444 $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) ); 445 445 $users = $users->get_results(); 446 $this->assertEquals( 4, count( $users ) );446 $this->assertEquals( 13, count( $users ) ); 447 447 448 448 $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => 10 ) ); 449 449 $users = $users->get_results(); 450 $this->assertEquals( 4, count( $users ) );450 $this->assertEquals( 10, count( $users ) ); 451 451 452 452 $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => 2 ) ); … … 456 456 $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => -1 ) ); 457 457 $users = $users->get_results(); 458 $this->assertEquals( 4, count( $users ) );458 $this->assertEquals( 13, count( $users ) ); 459 459 } 460 460 … … 526 526 */ 527 527 function test_meta_query_with_role() { 528 $author_ids = $this->factory->user->create_many( 4, array( 'role' => 'author' ) ); 529 530 add_user_meta( $author_ids[0], 'foo', 'bar' ); 531 add_user_meta( $author_ids[1], 'foo', 'baz' ); 528 add_user_meta( self::$author_ids[0], 'foo', 'bar' ); 529 add_user_meta( self::$author_ids[1], 'foo', 'baz' ); 532 530 533 531 // Users with foo = bar or baz restricted to the author role. … … 548 546 ) ); 549 547 550 $this->assertEquals( array( $author_ids[0],$author_ids[1] ), $query->get_results() );548 $this->assertEquals( array( self::$author_ids[0], self::$author_ids[1] ), $query->get_results() ); 551 549 } 552 550 553 551 public function test_roles_and_caps_should_be_populated_for_default_value_of_blog_id() { 554 $u = $this->factory->user->create( array( 'role' => 'author' ) );555 556 552 $query = new WP_User_Query( array( 557 'include' => $u,553 'include' => self::$author_ids[0], 558 554 ) ); 559 555 … … 571 567 } 572 568 573 $u = $this->factory->user->create( array( 'role' => 'author' ) );574 575 569 $query = new WP_User_Query( array( 576 'include' => $u,570 'include' => self::$author_ids[0], 577 571 'blog_id' => get_current_blog_id(), 578 572 ) ); … … 591 585 } 592 586 593 $u = $this->factory->user->create( array( 'role' => 'author' ) );594 595 587 $query = new WP_User_Query( array( 596 'include' => $u,588 'include' => self::$author_ids[0], 597 589 'blog_id' => get_current_blog_id(), 598 590 ) ); … … 612 604 613 605 $b = $this->factory->blog->create(); 614 $u = $this->factory->user->create(); 615 add_user_to_blog( $b, $u, 'author' );606 607 add_user_to_blog( $b, self::$author_ids[0], 'author' ); 616 608 617 609 $query = new WP_User_Query( array( 618 'include' => $u,610 'include' => self::$author_ids[0], 619 611 'blog_id' => $b, 620 612 'fields' => 'all_with_meta', … … 638 630 639 631 $b = $this->factory->blog->create(); 640 $u = $this->factory->user->create(); 641 add_user_to_blog( $b, $u, 'author' ); 632 add_user_to_blog( $b, self::$author_ids[0], 'author' ); 642 633 643 634 $query = new WP_User_Query( array( 644 635 'fields' => 'all', 645 'include' => $u,636 'include' => self::$author_ids[0], 646 637 'blog_id' => $b, 647 638 ) ); … … 664 655 665 656 $b = $this->factory->blog->create(); 666 $users = $this->factory->user->create_many( 3 ); 667 668 add_user_to_blog( $b, $users[0], 'subscriber' ); 669 add_user_to_blog( $b, $users[1], 'author' ); 670 add_user_to_blog( $b, $users[2], 'editor' ); 657 658 add_user_to_blog( $b, self::$author_ids[0], 'subscriber' ); 659 add_user_to_blog( $b, self::$author_ids[1], 'author' ); 660 add_user_to_blog( $b, self::$author_ids[2], 'editor' ); 671 661 672 662 $q = new WP_User_Query( array( … … 677 667 $found = wp_list_pluck( $q->get_results(), 'ID' ); 678 668 679 $this->assertNotContains( $users[0], $found );680 $this->assertContains( $users[1], $found );681 $this->assertContains( $users[2], $found );669 $this->assertNotContains( self::$author_ids[0], $found ); 670 $this->assertContains( self::$author_ids[1], $found ); 671 $this->assertContains( self::$author_ids[2], $found ); 682 672 } 683 673 … … 691 681 692 682 $b = $this->factory->blog->create(); 693 $users = $this->factory->user->create_many( 3 ); 694 695 add_user_to_blog( $b, $users[0], 'subscriber' ); 696 add_user_to_blog( $b, $users[1], 'author' ); 697 add_user_to_blog( $b, $users[2], 'editor' ); 698 699 add_user_meta( $users[1], 'foo', 'bar' ); 700 add_user_meta( $users[2], 'foo', 'baz' ); 683 684 add_user_to_blog( $b, self::$author_ids[0], 'subscriber' ); 685 add_user_to_blog( $b, self::$author_ids[1], 'author' ); 686 add_user_to_blog( $b, self::$author_ids[2], 'editor' ); 687 688 add_user_meta( self::$author_ids[1], 'foo', 'bar' ); 689 add_user_meta( self::$author_ids[2], 'foo', 'baz' ); 701 690 702 691 $q = new WP_User_Query( array( … … 711 700 $found = wp_list_pluck( $q->get_results(), 'ID' ); 712 701 713 $this->assertNotContains( $users[0], $found );714 $this->assertContains( $users[1], $found );715 $this->assertNotContains( $users[2], $found );702 $this->assertNotContains( self::$author_ids[0], $found ); 703 $this->assertContains( self::$author_ids[1], $found ); 704 $this->assertNotContains( self::$author_ids[2], $found ); 716 705 } 717 706 … … 723 712 register_post_type( 'wptests_pt_private', array( 'public' => false ) ); 724 713 725 $users = $this->factory->user->create_many( 3 ); 726 727 $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) ); 728 $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) ); 714 $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) ); 715 $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) ); 729 716 730 717 $q = new WP_User_Query( array( … … 733 720 734 721 $found = wp_list_pluck( $q->get_results(), 'ID' ); 735 $expected = array( $users[0] );722 $expected = array( self::$author_ids[0] ); 736 723 737 724 $this->assertEqualSets( $expected, $found ); … … 745 732 register_post_type( 'wptests_pt_private', array( 'public' => false ) ); 746 733 747 $users = $this->factory->user->create_many( 3 ); 748 749 $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) ); 750 $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) ); 751 $this->factory->post->create( array( 'post_author' => $users[2], 'post_status' => 'publish', 'post_type' => 'post' ) ); 734 $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) ); 735 $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) ); 736 $this->factory->post->create( array( 'post_author' => self::$author_ids[2], 'post_status' => 'publish', 'post_type' => 'post' ) ); 752 737 753 738 $q = new WP_User_Query( array( … … 756 741 757 742 $found = wp_list_pluck( $q->get_results(), 'ID' ); 758 $expected = array( $users[1], $users[2] );743 $expected = array( self::$author_ids[1], self::$author_ids[2] ); 759 744 760 745 $this->assertEqualSets( $expected, $found ); … … 768 753 register_post_type( 'wptests_pt_private', array( 'public' => false ) ); 769 754 770 $users = $this->factory->user->create_many( 3 ); 771 772 $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'draft', 'post_type' => 'wptests_pt_public' ) ); 773 $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'inherit', 'post_type' => 'wptests_pt_private' ) ); 774 $this->factory->post->create( array( 'post_author' => $users[2], 'post_status' => 'publish', 'post_type' => 'post' ) ); 755 $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'draft', 'post_type' => 'wptests_pt_public' ) ); 756 $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'inherit', 'post_type' => 'wptests_pt_private' ) ); 757 $this->factory->post->create( array( 'post_author' => self::$author_ids[2], 'post_status' => 'publish', 'post_type' => 'post' ) ); 775 758 776 759 $q = new WP_User_Query( array( … … 779 762 780 763 $found = wp_list_pluck( $q->get_results(), 'ID' ); 781 $expected = array( $users[2] );764 $expected = array( self::$author_ids[2] ); 782 765 783 766 $this->assertEqualSets( $expected, $found ); … … 792 775 } 793 776 794 $users = $this->factory->user->create_many( 3 );795 777 $blogs = $this->factory->blog->create_many( 2 ); 796 778 797 add_user_to_blog( $blogs[0], $users[0], 'author' );798 add_user_to_blog( $blogs[0], $users[1], 'author' );799 add_user_to_blog( $blogs[1], $users[0], 'author' );800 add_user_to_blog( $blogs[1], $users[1], 'author' );779 add_user_to_blog( $blogs[0], self::$author_ids[0], 'author' ); 780 add_user_to_blog( $blogs[0], self::$author_ids[1], 'author' ); 781 add_user_to_blog( $blogs[1], self::$author_ids[0], 'author' ); 782 add_user_to_blog( $blogs[1], self::$author_ids[1], 'author' ); 801 783 802 784 switch_to_blog( $blogs[0] ); 803 $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'post' ) );785 $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'post' ) ); 804 786 restore_current_blog(); 805 787 806 788 switch_to_blog( $blogs[1] ); 807 $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'post' ) );789 $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'post' ) ); 808 790 restore_current_blog(); 809 791 … … 814 796 815 797 $found = wp_list_pluck( $q->get_results(), 'ID' ); 816 $expected = array( $users[1] );798 $expected = array( self::$author_ids[1] ); 817 799 818 800 $this->assertEqualSets( $expected, $found ); … … 823 805 */ 824 806 public function test_top_level_or_meta_query_should_eliminate_duplicate_matches() { 825 $users = $this->factory->user->create_many( 3 ); 826 827 add_user_meta( $users[0], 'foo', 'bar' ); 828 add_user_meta( $users[1], 'foo', 'bar' ); 829 add_user_meta( $users[0], 'foo2', 'bar2' ); 807 add_user_meta( self::$author_ids[0], 'foo', 'bar' ); 808 add_user_meta( self::$author_ids[1], 'foo', 'bar' ); 809 add_user_meta( self::$author_ids[0], 'foo2', 'bar2' ); 830 810 831 811 $q = new WP_User_Query( array( … … 844 824 845 825 $found = wp_list_pluck( $q->get_results(), 'ID' ); 846 $expected = array( $users[0], $users[1] );826 $expected = array( self::$author_ids[0], self::$author_ids[1] ); 847 827 848 828 $this->assertEqualSets( $expected, $found ); … … 853 833 */ 854 834 public function test_nested_or_meta_query_should_eliminate_duplicate_matches() { 855 $users = $this->factory->user->create_many( 3 ); 856 857 add_user_meta( $users[0], 'foo', 'bar' ); 858 add_user_meta( $users[1], 'foo', 'bar' ); 859 add_user_meta( $users[0], 'foo2', 'bar2' ); 860 add_user_meta( $users[1], 'foo3', 'bar3' ); 835 add_user_meta( self::$author_ids[0], 'foo', 'bar' ); 836 add_user_meta( self::$author_ids[1], 'foo', 'bar' ); 837 add_user_meta( self::$author_ids[0], 'foo2', 'bar2' ); 838 add_user_meta( self::$author_ids[1], 'foo3', 'bar3' ); 861 839 862 840 $q = new WP_User_Query( array( … … 882 860 883 861 $found = wp_list_pluck( $q->get_results(), 'ID' ); 884 $expected = array( $users[0], $users[1] );862 $expected = array( self::$author_ids[0], self::$author_ids[1] ); 885 863 886 864 $this->assertEqualSets( $expected, $found ); … … 891 869 */ 892 870 public function test_paged() { 893 $users = $this->factory->user->create_many( 5 );894 895 871 $q = new WP_User_Query( array( 896 872 'number' => 2, … … 901 877 ) ); 902 878 903 $this->assertEquals( array( $users[2], $users[1] ), $q->results );879 $this->assertEquals( array( self::$contrib_id, self::$editor_ids[2] ), $q->results ); 904 880 } 905 881 … … 930 906 */ 931 907 public function test_get_single_role_by_user_query() { 932 $this->factory->user->create_many( 2, array(933 'role' => 'subscriber',934 ) );935 936 $this->factory->user->create( array(937 'role' => 'contributor',938 ) );939 940 908 $wp_user_search = new WP_User_Query( array( 'role' => 'subscriber' ) ); 941 909 $users = $wp_user_search->get_results(); … … 948 916 */ 949 917 public function test_get_multiple_roles_by_user_query() { 950 $this->factory->user->create_many( 2, array(951 'role' => 'subscriber',952 ) );953 954 $this->factory->user->create_many( 3, array(955 'role' => 'editor',956 ) );957 958 $this->factory->user->create( array(959 'role' => 'contributor',960 ) );961 962 918 $wp_user_search = new WP_User_Query( array( 'role__in' => array( 'subscriber', 'editor' ) ) ); 963 919 $users = $wp_user_search->get_results(); … … 969 925 */ 970 926 public function test_get_single_role_by_string() { 971 $this->factory->user->create_many( 2, array(972 'role' => 'subscriber',973 ) );974 975 $this->factory->user->create( array(976 'role' => 'contributor',977 ) );978 979 927 $users = get_users( array( 980 928 'role' => 'subscriber', … … 988 936 */ 989 937 public function test_get_single_role_by_string_which_is_similar() { 990 $editors = $this->factory->user->create_many( 2, array(991 'role' => 'editor',992 ) );993 994 938 $another_editor = $this->factory->user->create( array( 995 939 'role' => 'another-editor', … … 1001 945 ) ); 1002 946 1003 $this->assertEqualSets( $editors, $users );947 $this->assertEqualSets( self::$editor_ids, $users ); 1004 948 } 1005 949 … … 1009 953 */ 1010 954 public function test_get_single_role_by_array() { 1011 $this->factory->user->create_many( 2, array(1012 'role' => 'subscriber',1013 ) );1014 1015 $this->factory->user->create( array(1016 'role' => 'contributor',1017 ) );1018 1019 955 $users = get_users( array( 1020 956 'role' => array( 'subscriber' ), … … 1028 964 */ 1029 965 public function test_get_multiple_roles_should_only_match_users_who_have_each_role() { 1030 $subscribers = $this->factory->user->create_many( 2, array(1031 'role' => 'subscriber',1032 ) );1033 1034 $this->factory->user->create_many( 3, array(1035 'role' => 'editor',1036 ) );1037 1038 $this->factory->user->create_many( 2, array(1039 'role' => 'administrator',1040 ) );1041 1042 966 $users = new WP_User_Query( array( 'role' => array( 'subscriber', 'editor' ) ) ); 1043 967 $users = $users->get_results(); … … 1045 969 $this->assertEmpty( $users ); 1046 970 1047 foreach ( $subscribers as $subscriber ) {971 foreach ( self::$sub_ids as $subscriber ) { 1048 972 $subscriber = get_user_by( 'ID', $subscriber ); 1049 973 $subscriber->add_role( 'editor' ); … … 1064 988 */ 1065 989 public function test_get_multiple_roles_or() { 1066 $this->factory->user->create_many( 2, array(1067 'role' => 'subscriber',1068 ) );1069 1070 $this->factory->user->create_many( 3, array(1071 'role' => 'editor',1072 ) );1073 1074 $this->factory->user->create_many( 2, array(1075 'role' => 'administrator',1076 ) );1077 1078 $this->factory->user->create_many( 1, array(1079 'role' => 'contributor',1080 ) );1081 1082 990 $users = new WP_User_Query( array( 'role__in' => array( 'subscriber', 'editor', 'administrator' ) ) ); 1083 991 $users = $users->get_results(); … … 1094 1002 */ 1095 1003 public function test_get_multiple_roles_by_comma_separated_list() { 1096 $subscribers = $this->factory->user->create_many( 2, array(1097 'role' => 'subscriber',1098 ) );1099 1100 $this->factory->user->create_many( 3, array(1101 'role' => 'editor',1102 ) );1103 1104 1004 $users = get_users( array( 1105 1005 'role' => 'subscriber, editor', … … 1108 1008 $this->assertEmpty( $users ); 1109 1009 1110 foreach ( $subscribers as $subscriber ) {1010 foreach ( self::$sub_ids as $subscriber ) { 1111 1011 $subscriber = get_user_by( 'ID', $subscriber ); 1112 1012 $subscriber->add_role( 'editor' ); … … 1125 1025 public function test_get_multiple_roles_with_meta() { 1126 1026 // Create administrator user + meta 1127 $administrator_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 1128 update_user_meta( $administrator_id, 'mk1', 1 ); 1129 update_user_meta( $administrator_id, 'mk2', 1 ); 1027 update_user_meta( self::$admin_ids[0], 'mk1', 1 ); 1028 update_user_meta( self::$admin_ids[0], 'mk2', 1 ); 1130 1029 1131 1030 // Create editor user + meta 1132 $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 1133 update_user_meta( $editor_id, 'mk1', 1 ); 1134 update_user_meta( $editor_id, 'mk2', 2 ); 1031 update_user_meta( self::$editor_ids[0], 'mk1', 1 ); 1032 update_user_meta( self::$editor_ids[0], 'mk2', 2 ); 1135 1033 1136 1034 // Create subscriber user + meta 1137 $subscriber_id = $this->factory->user->create( array( 'role' => 'subscriber' ) ); 1138 update_user_meta( $subscriber_id, 'mk1', 1 ); 1139 update_user_meta( $subscriber_id, 'mk2', 1 ); 1035 update_user_meta( self::$sub_ids[0], 'mk1', 1 ); 1036 update_user_meta( self::$sub_ids[0], 'mk2', 1 ); 1140 1037 1141 1038 // Create contributor user + meta 1142 $contributor_id = $this->factory->user->create( array( 'role' => 'contributor' ) ); 1143 update_user_meta( $contributor_id, 'mk1', 1 ); 1144 update_user_meta( $contributor_id, 'mk2', 2 ); 1039 update_user_meta( self::$contrib_id, 'mk1', 1 ); 1040 update_user_meta( self::$contrib_id, 'mk2', 2 ); 1145 1041 1146 1042 // Fetch users … … 1166 1062 // Check results 1167 1063 $this->assertEquals( 1, count( $users ) ); 1168 $this->assertSame( $editor_id, (int) $users[0]->ID );1064 $this->assertSame( self::$editor_ids[0], (int) $users[0]->ID ); 1169 1065 } 1170 1066 … … 1173 1069 */ 1174 1070 public function test_role_exclusion() { 1175 $this->factory->user->create_many( 2, array(1176 'role' => 'subscriber',1177 ) );1178 1179 $this->factory->user->create_many( 3, array(1180 'role' => 'editor',1181 ) );1182 1183 1071 $users = get_users( array( 1184 1072 'role__not_in' => 'subscriber', … … 1186 1074 1187 1075 // +1 for the default user created during installation. 1188 $this->assertEquals( 4, count( $users ) );1076 $this->assertEquals( 11, count( $users ) ); 1189 1077 1190 1078 $users = get_users( array( … … 1193 1081 1194 1082 // +1 for the default user created during installation. 1195 $this->assertEquals( 3, count( $users ) );1083 $this->assertEquals( 10, count( $users ) ); 1196 1084 } 1197 1085 … … 1200 1088 */ 1201 1089 public function test_role__in_role__not_in_combined() { 1202 $subscribers = $this->factory->user->create_many( 2, array( 1203 'role' => 'subscriber', 1204 ) ); 1205 1206 $this->factory->user->create_many( 3, array( 1207 'role' => 'editor', 1208 ) ); 1209 1210 foreach ( $subscribers as $subscriber ) { 1090 foreach ( self::$sub_ids as $subscriber ) { 1211 1091 $subscriber = get_user_by( 'ID', $subscriber ); 1212 1092 $subscriber->add_role( 'editor' ); … … 1231 1111 */ 1232 1112 public function test_role__not_in_role_combined() { 1233 $subscribers = $this->factory->user->create_many( 2, array( 1234 'role' => 'subscriber', 1235 ) ); 1236 1237 $this->factory->user->create_many( 3, array( 1238 'role' => 'editor', 1239 ) ); 1240 1241 $subscriber = get_user_by( 'ID', $subscribers[0] ); 1113 $subscriber = get_user_by( 'ID', self::$sub_ids[0] ); 1242 1114 $subscriber->add_role( 'editor' ); 1243 1115 … … 1254 1126 */ 1255 1127 public function test_role__not_in_user_without_role() { 1256 $user_without_rule = $this->factory->user->get_object_by_id( $this->factory->user->create( array( 1257 'role' => 'subscriber', 1258 ) ) ); 1128 $user_without_rule = get_user_by( 'ID', self::$sub_ids[0] ); 1259 1129 1260 1130 $user_without_rule->remove_role( 'subscriber' ); 1261 1262 $this->factory->user->create_many( 3, array(1263 'role' => 'editor',1264 ) );1265 1131 1266 1132 $users = get_users( array( … … 1269 1135 1270 1136 // +1 for the default user created during installation. 1271 $this->assertEquals( 5, count( $users ) );1137 $this->assertEquals( 12, count( $users ) ); 1272 1138 1273 1139 $users = get_users( array( … … 1276 1142 1277 1143 // +1 for the default user created during installation. 1278 $this->assertEquals( 2, count( $users ) );1144 $this->assertEquals( 10, count( $users ) ); 1279 1145 } 1280 1146 … … 1288 1154 1289 1155 $sites = $this->factory->blog->create_many( 2 ); 1290 $users = $this->factory->user->create_many( 2 ); 1291 1292 add_user_to_blog( $sites[0], $users[0], 'author' ); 1293 add_user_to_blog( $sites[1], $users[1], 'author' ); 1156 1157 add_user_to_blog( self::$author_ids[0], self::$author_ids[0], 'author' ); 1158 add_user_to_blog( self::$author_ids[1], self::$author_ids[1], 'author' ); 1294 1159 1295 1160 $found = get_users( array( … … 1298 1163 ) ); 1299 1164 1300 $this->assertEqualSets( array( $users[1] ), $found );1165 $this->assertEqualSets( array( self::$author_ids[1] ), $found ); 1301 1166 } 1302 1167 … … 1311 1176 1312 1177 $site_id = get_current_blog_id(); 1313 $u = $this->factory->user->create(); 1314 add_user_to_blog( $site_id, $u, 'author' ); 1315 1316 $q = new WP_User_Query( array( 1317 'include' => $u, 1178 add_user_to_blog( $site_id, self::$author_ids[0], 'author' ); 1179 1180 $q = new WP_User_Query( array( 1181 'include' => self::$author_ids[0], 1318 1182 ) ); 1319 1183 … … 1321 1185 1322 1186 $q->prepare_query( array( 1323 'include' => $u,1187 'include' => self::$author_ids[0], 1324 1188 ) ); 1325 1189 … … 1327 1191 1328 1192 $q->prepare_query( array( 1329 'include' => $u,1193 'include' => self::$author_ids[0], 1330 1194 ) ); 1331 1195
Note: See TracChangeset
for help on using the changeset viewer.