diff --git a/tests/phpunit/tests/user/query.php b/tests/phpunit/tests/user/query.php index f3a0dcb4c5..a7d8a69727 100644 --- a/tests/phpunit/tests/user/query.php +++ b/tests/phpunit/tests/user/query.php @@ -5,11 +5,54 @@ * @group user */ class Tests_User_Query extends WP_UnitTestCase { + protected static $author_ids; + protected static $sub_ids; + protected static $editor_ids; + protected static $contrib_id; + protected static $admin_ids; protected $user_id; - function setUp() { - parent::setUp(); + public static function wpSetUpBeforeClass( $factory ) { + self::$author_ids = $factory->user->create_many( 4, array( + 'role' => 'author' + ) ); + + self::$sub_ids = $factory->user->create_many( 2, array( + 'role' => 'subscriber', + ) ); + + self::$editor_ids = $factory->user->create_many( 3, array( + 'role' => 'editor', + ) ); + + self::$contrib_id = $factory->user->create( array( + 'role' => 'contributor', + ) ); + + self::$admin_ids = $factory->user->create_many( 2, array( + 'role' => 'administrator', + ) ); + } + + public static function wpTearDownAfterClass() { + $sets = array( + self::$author_ids, + self::$sub_ids, + self::$editor_ids, + array( self::$contrib_id ), + self::$admin_ids + ); + + foreach ( $sets as $set ) { + foreach ( $set as $id ) { + if ( is_multisite() ) { + wpmu_delete_user( $id ); + } else { + wp_delete_user( $id ); + } + } + } } function test_get_and_set() { @@ -31,83 +74,71 @@ class Tests_User_Query extends WP_UnitTestCase { } public function test_include_single() { - $users = $this->factory->user->create_many( 2 ); $q = new WP_User_Query( array( 'fields' => '', - 'include' => $users[0], + 'include' => self::$author_ids[0], ) ); $ids = $q->get_results(); - $this->assertEquals( array( $users[0] ), $ids ); + $this->assertEquals( array( self::$author_ids[0] ), $ids ); } public function test_include_comma_separated() { - $users = $this->factory->user->create_many( 3 ); $q = new WP_User_Query( array( 'fields' => '', - 'include' => $users[0] . ', ' . $users[2], + 'include' => self::$author_ids[0] . ', ' . self::$author_ids[2], ) ); $ids = $q->get_results(); - $this->assertEqualSets( array( $users[0], $users[2] ), $ids ); + $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids ); } public function test_include_array() { - $users = $this->factory->user->create_many( 3 ); $q = new WP_User_Query( array( 'fields' => '', - 'include' => array( $users[0], $users[2] ), + 'include' => array( self::$author_ids[0], self::$author_ids[2] ), ) ); $ids = $q->get_results(); - $this->assertEqualSets( array( $users[0], $users[2] ), $ids ); + $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids ); } public function test_include_array_bad_values() { - $users = $this->factory->user->create_many( 3 ); $q = new WP_User_Query( array( 'fields' => '', - 'include' => array( $users[0], 'foo', $users[2] ), + 'include' => array( self::$author_ids[0], 'foo', self::$author_ids[2] ), ) ); $ids = $q->get_results(); - $this->assertEqualSets( array( $users[0], $users[2] ), $ids ); + $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids ); } public function test_exclude() { - $users = $this->factory->user->create_many( 3, array( - 'role' => 'author', - ) ); - $q = new WP_User_Query( array( 'fields' => '', - 'exclude' => $users[1], + 'exclude' => self::$author_ids[1], ) ); $ids = $q->get_results(); // Indirect test in order to ignore default user created during installation. $this->assertNotEmpty( $ids ); - $this->assertNotContains( $users[1], $ids ); + $this->assertNotContains( self::$author_ids[1], $ids ); } public function test_get_all() { - $this->factory->user->create_many( 3, array( - 'role' => 'author' - ) ); - $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) ); $users = $users->get_results(); // +1 for the default user created during installation. - $this->assertEquals( 4, count( $users ) ); + $this->assertEquals( 13, count( $users ) ); foreach ( $users as $user ) { $this->assertInstanceOf( 'WP_User', $user ); } $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'fields' => 'all_with_meta' ) ); $users = $users->get_results(); - $this->assertEquals( 4, count( $users ) ); + $this->assertEquals( 13, count( $users ) ); foreach ( $users as $user ) { $this->assertInstanceOf( 'WP_User', $user ); } @@ -135,22 +166,18 @@ class Tests_User_Query extends WP_UnitTestCase { } public function test_orderby_meta_value() { - $users = $this->factory->user->create_many( 3, array( - 'role' => 'author' - ) ); - - update_user_meta( $users[0], 'last_name', 'Jones' ); - update_user_meta( $users[1], 'last_name', 'Albert' ); - update_user_meta( $users[2], 'last_name', 'Zorro' ); + update_user_meta( self::$author_ids[0], 'last_name', 'Jones' ); + update_user_meta( self::$author_ids[1], 'last_name', 'Albert' ); + update_user_meta( self::$author_ids[2], 'last_name', 'Zorro' ); $q = new WP_User_Query( array( - 'include' => $users, + 'include' => self::$author_ids, 'meta_key' => 'last_name', 'orderby' => 'meta_value', 'fields' => 'ids' ) ); - $expected = array( $users[1], $users[0], $users[2] ); + $expected = array( self::$author_ids[3], self::$author_ids[1], self::$author_ids[0], self::$author_ids[2] ); $this->assertEquals( $expected, $q->get_results() ); } @@ -159,22 +186,18 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 27887 */ public function test_orderby_meta_value_num() { - $users = $this->factory->user->create_many( 3, array( - 'role' => 'author' - ) ); - - update_user_meta( $users[0], 'user_age', '101' ); - update_user_meta( $users[1], 'user_age', '20' ); - update_user_meta( $users[2], 'user_age', '25' ); + update_user_meta( self::$author_ids[0], 'user_age', '101' ); + update_user_meta( self::$author_ids[1], 'user_age', '20' ); + update_user_meta( self::$author_ids[2], 'user_age', '25' ); $q = new WP_User_Query( array( - 'include' => $users, + 'include' => self::$author_ids, 'meta_key' => 'user_age', 'orderby' => 'meta_value_num', 'fields' => 'ids' ) ); - $expected = array( $users[1], $users[2], $users[0] ); + $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] ); $this->assertEquals( $expected, $q->get_results() ); } @@ -183,22 +206,18 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 31265 */ public function test_orderby_somekey_where_meta_key_is_somekey() { - $users = $this->factory->user->create_many( 3, array( - 'role' => 'author' - ) ); - - update_user_meta( $users[0], 'foo', 'zzz' ); - update_user_meta( $users[1], 'foo', 'aaa' ); - update_user_meta( $users[2], 'foo', 'jjj' ); + update_user_meta( self::$author_ids[0], 'foo', 'zzz' ); + update_user_meta( self::$author_ids[1], 'foo', 'aaa' ); + update_user_meta( self::$author_ids[2], 'foo', 'jjj' ); $q = new WP_User_Query( array( - 'include' => $users, + 'include' => self::$author_ids, 'meta_key' => 'foo', 'orderby' => 'foo', 'fields' => 'ids' ) ); - $expected = array( $users[1], $users[2], $users[0] ); + $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] ); $this->assertEquals( $expected, $q->get_results() ); } @@ -207,10 +226,9 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 31265 */ public function test_orderby_clause_key() { - $users = $this->factory->user->create_many( 3 ); - add_user_meta( $users[0], 'foo', 'aaa' ); - add_user_meta( $users[1], 'foo', 'zzz' ); - add_user_meta( $users[2], 'foo', 'jjj' ); + add_user_meta( self::$author_ids[0], 'foo', 'aaa' ); + add_user_meta( self::$author_ids[1], 'foo', 'zzz' ); + add_user_meta( self::$author_ids[2], 'foo', 'jjj' ); $q = new WP_User_Query( array( 'fields' => 'ids', @@ -224,20 +242,20 @@ class Tests_User_Query extends WP_UnitTestCase { 'order' => 'DESC', ) ); - $this->assertEquals( array( $users[1], $users[2], $users[0] ), $q->results ); + $this->assertEquals( array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] ), $q->results ); } /** * @ticket 31265 */ public function test_orderby_clause_key_as_secondary_sort() { - $u1 = $this->factory->user->create( array( + $u1 = self::$static_factory->user->create( array( 'user_registered' => '2015-01-28 03:00:00', ) ); - $u2 = $this->factory->user->create( array( + $u2 = self::$static_factory->user->create( array( 'user_registered' => '2015-01-28 05:00:00', ) ); - $u3 = $this->factory->user->create( array( + $u3 = self::$static_factory->user->create( array( 'user_registered' => '2015-01-28 03:00:00', ) ); @@ -266,14 +284,12 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 31265 */ public function test_orderby_more_than_one_clause_key() { - $users = $this->factory->user->create_many( 3 ); - - add_user_meta( $users[0], 'foo', 'jjj' ); - add_user_meta( $users[1], 'foo', 'zzz' ); - add_user_meta( $users[2], 'foo', 'jjj' ); - add_user_meta( $users[0], 'bar', 'aaa' ); - add_user_meta( $users[1], 'bar', 'ccc' ); - add_user_meta( $users[2], 'bar', 'bbb' ); + add_user_meta( self::$author_ids[0], 'foo', 'jjj' ); + add_user_meta( self::$author_ids[1], 'foo', 'zzz' ); + add_user_meta( self::$author_ids[2], 'foo', 'jjj' ); + add_user_meta( self::$author_ids[0], 'bar', 'aaa' ); + add_user_meta( self::$author_ids[1], 'bar', 'ccc' ); + add_user_meta( self::$author_ids[2], 'bar', 'bbb' ); $q = new WP_User_Query( array( 'fields' => 'ids', @@ -293,7 +309,7 @@ class Tests_User_Query extends WP_UnitTestCase { ), ) ); - $this->assertEquals( array( $users[2], $users[0], $users[1] ), $q->results ); + $this->assertEquals( array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] ), $q->results ); } /** @@ -313,18 +329,17 @@ class Tests_User_Query extends WP_UnitTestCase { public function test_orderby_include() { global $wpdb; - $users = $this->factory->user->create_many( 4 ); $q = new WP_User_Query( array( 'orderby' => 'include', - 'include' => array( $users[1], $users[0], $users[3] ), + 'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), 'fields' => '', ) ); - $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . $users[1] . ',' . $users[0] . ',' . $users[3] . ' )'; + $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )'; $this->assertContains( $expected_orderby, $q->query_orderby ); // assertEquals() respects order but ignores type (get_results() returns numeric strings). - $this->assertEquals( array( $users[1], $users[0], $users[3] ), $q->get_results() ); + $this->assertEquals( array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results() ); } /** @@ -333,26 +348,23 @@ class Tests_User_Query extends WP_UnitTestCase { public function test_orderby_include_duplicate_values() { global $wpdb; - $users = $this->factory->user->create_many( 4 ); $q = new WP_User_Query( array( 'orderby' => 'include', - 'include' => array( $users[1], $users[0], $users[1], $users[3] ), + 'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[1], self::$author_ids[3] ), 'fields' => '', ) ); - $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . $users[1] . ',' . $users[0] . ',' . $users[3] . ' )'; + $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )'; $this->assertContains( $expected_orderby, $q->query_orderby ); // assertEquals() respects order but ignores type (get_results() returns numeric strings). - $this->assertEquals( array( $users[1], $users[0], $users[3] ), $q->get_results() ); + $this->assertEquals( array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results() ); } /** * @ticket 31265 */ public function test_orderby_space_separated() { - global $wpdb; - $q = new WP_User_Query( array( 'orderby' => 'login nicename', 'order' => 'ASC', @@ -365,8 +377,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 31265 */ public function test_orderby_flat_array() { - global $wpdb; - $q = new WP_User_Query( array( 'orderby' => array( 'login', 'nicename' ), ) ); @@ -378,8 +388,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 31265 */ public function test_orderby_array_contains_invalid_item() { - global $wpdb; - $q = new WP_User_Query( array( 'orderby' => array( 'login', 'foo', 'nicename' ), ) ); @@ -391,8 +399,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 31265 */ public function test_orderby_array_contains_all_invalid_items() { - global $wpdb; - $q = new WP_User_Query( array( 'orderby' => array( 'foo', 'bar', 'baz' ), ) ); @@ -404,8 +410,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 31265 */ public function test_orderby_array() { - global $wpdb; - $q = new WP_User_Query( array( 'orderby' => array( 'login' => 'DESC', @@ -421,8 +425,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 31265 */ public function test_orderby_array_should_discard_invalid_columns() { - global $wpdb; - $q = new WP_User_Query( array( 'orderby' => array( 'login' => 'DESC', @@ -439,15 +441,13 @@ class Tests_User_Query extends WP_UnitTestCase { */ function test_number() { // +1 for the default user created by the test suite. - $user_ids = $this->factory->user->create_many( 3 ); - $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) ); $users = $users->get_results(); - $this->assertEquals( 4, count( $users ) ); + $this->assertEquals( 13, count( $users ) ); $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => 10 ) ); $users = $users->get_results(); - $this->assertEquals( 4, count( $users ) ); + $this->assertEquals( 10, count( $users ) ); $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => 2 ) ); $users = $users->get_results(); @@ -455,7 +455,7 @@ class Tests_User_Query extends WP_UnitTestCase { $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => -1 ) ); $users = $users->get_results(); - $this->assertEquals( 4, count( $users ) ); + $this->assertEquals( 13, count( $users ) ); } /** @@ -525,10 +525,8 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 23849 */ function test_meta_query_with_role() { - $author_ids = $this->factory->user->create_many( 4, array( 'role' => 'author' ) ); - - add_user_meta( $author_ids[0], 'foo', 'bar' ); - add_user_meta( $author_ids[1], 'foo', 'baz' ); + add_user_meta( self::$author_ids[0], 'foo', 'bar' ); + add_user_meta( self::$author_ids[1], 'foo', 'baz' ); // Users with foo = bar or baz restricted to the author role. $query = new WP_User_Query( array( @@ -547,14 +545,12 @@ class Tests_User_Query extends WP_UnitTestCase { ), ) ); - $this->assertEquals( array( $author_ids[0], $author_ids[1] ), $query->get_results() ); + $this->assertEquals( array( self::$author_ids[0], self::$author_ids[1] ), $query->get_results() ); } public function test_roles_and_caps_should_be_populated_for_default_value_of_blog_id() { - $u = $this->factory->user->create( array( 'role' => 'author' ) ); - $query = new WP_User_Query( array( - 'include' => $u, + 'include' => self::$author_ids[0], ) ); $found = $query->get_results(); @@ -570,10 +566,8 @@ class Tests_User_Query extends WP_UnitTestCase { $this->markTestSkipped( __METHOD__ . ' is a non-multisite-only test.' ); } - $u = $this->factory->user->create( array( 'role' => 'author' ) ); - $query = new WP_User_Query( array( - 'include' => $u, + 'include' => self::$author_ids[0], 'blog_id' => get_current_blog_id(), ) ); @@ -590,10 +584,8 @@ class Tests_User_Query extends WP_UnitTestCase { $this->markTestSkipped( __METHOD__ . ' is a multisite-only test.' ); } - $u = $this->factory->user->create( array( 'role' => 'author' ) ); - $query = new WP_User_Query( array( - 'include' => $u, + 'include' => self::$author_ids[0], 'blog_id' => get_current_blog_id(), ) ); @@ -611,11 +603,11 @@ class Tests_User_Query extends WP_UnitTestCase { } $b = $this->factory->blog->create(); - $u = $this->factory->user->create(); - add_user_to_blog( $b, $u, 'author' ); + + add_user_to_blog( $b, self::$author_ids[0], 'author' ); $query = new WP_User_Query( array( - 'include' => $u, + 'include' => self::$author_ids[0], 'blog_id' => $b, 'fields' => 'all_with_meta', ) ); @@ -637,12 +629,11 @@ class Tests_User_Query extends WP_UnitTestCase { } $b = $this->factory->blog->create(); - $u = $this->factory->user->create(); - add_user_to_blog( $b, $u, 'author' ); + add_user_to_blog( $b, self::$author_ids[0], 'author' ); $query = new WP_User_Query( array( 'fields' => 'all', - 'include' => $u, + 'include' => self::$author_ids[0], 'blog_id' => $b, ) ); @@ -663,11 +654,10 @@ class Tests_User_Query extends WP_UnitTestCase { } $b = $this->factory->blog->create(); - $users = $this->factory->user->create_many( 3 ); - add_user_to_blog( $b, $users[0], 'subscriber' ); - add_user_to_blog( $b, $users[1], 'author' ); - add_user_to_blog( $b, $users[2], 'editor' ); + add_user_to_blog( $b, self::$author_ids[0], 'subscriber' ); + add_user_to_blog( $b, self::$author_ids[1], 'author' ); + add_user_to_blog( $b, self::$author_ids[2], 'editor' ); $q = new WP_User_Query( array( 'who' => 'authors', @@ -676,9 +666,9 @@ class Tests_User_Query extends WP_UnitTestCase { $found = wp_list_pluck( $q->get_results(), 'ID' ); - $this->assertNotContains( $users[0], $found ); - $this->assertContains( $users[1], $found ); - $this->assertContains( $users[2], $found ); + $this->assertNotContains( self::$author_ids[0], $found ); + $this->assertContains( self::$author_ids[1], $found ); + $this->assertContains( self::$author_ids[2], $found ); } /** @@ -690,14 +680,13 @@ class Tests_User_Query extends WP_UnitTestCase { } $b = $this->factory->blog->create(); - $users = $this->factory->user->create_many( 3 ); - add_user_to_blog( $b, $users[0], 'subscriber' ); - add_user_to_blog( $b, $users[1], 'author' ); - add_user_to_blog( $b, $users[2], 'editor' ); + add_user_to_blog( $b, self::$author_ids[0], 'subscriber' ); + add_user_to_blog( $b, self::$author_ids[1], 'author' ); + add_user_to_blog( $b, self::$author_ids[2], 'editor' ); - add_user_meta( $users[1], 'foo', 'bar' ); - add_user_meta( $users[2], 'foo', 'baz' ); + add_user_meta( self::$author_ids[1], 'foo', 'bar' ); + add_user_meta( self::$author_ids[2], 'foo', 'baz' ); $q = new WP_User_Query( array( 'who' => 'authors', @@ -710,9 +699,9 @@ class Tests_User_Query extends WP_UnitTestCase { $found = wp_list_pluck( $q->get_results(), 'ID' ); - $this->assertNotContains( $users[0], $found ); - $this->assertContains( $users[1], $found ); - $this->assertNotContains( $users[2], $found ); + $this->assertNotContains( self::$author_ids[0], $found ); + $this->assertContains( self::$author_ids[1], $found ); + $this->assertNotContains( self::$author_ids[2], $found ); } /** @@ -722,17 +711,15 @@ class Tests_User_Query extends WP_UnitTestCase { register_post_type( 'wptests_pt_public', array( 'public' => true ) ); register_post_type( 'wptests_pt_private', array( 'public' => false ) ); - $users = $this->factory->user->create_many( 3 ); - - $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) ); - $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) ); + $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) ); + $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) ); $q = new WP_User_Query( array( 'has_published_posts' => true, ) ); $found = wp_list_pluck( $q->get_results(), 'ID' ); - $expected = array( $users[0] ); + $expected = array( self::$author_ids[0] ); $this->assertEqualSets( $expected, $found ); } @@ -744,18 +731,16 @@ class Tests_User_Query extends WP_UnitTestCase { register_post_type( 'wptests_pt_public', array( 'public' => true ) ); register_post_type( 'wptests_pt_private', array( 'public' => false ) ); - $users = $this->factory->user->create_many( 3 ); - - $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) ); - $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) ); - $this->factory->post->create( array( 'post_author' => $users[2], 'post_status' => 'publish', 'post_type' => 'post' ) ); + $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) ); + $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) ); + $this->factory->post->create( array( 'post_author' => self::$author_ids[2], 'post_status' => 'publish', 'post_type' => 'post' ) ); $q = new WP_User_Query( array( 'has_published_posts' => array( 'wptests_pt_private', 'post' ), ) ); $found = wp_list_pluck( $q->get_results(), 'ID' ); - $expected = array( $users[1], $users[2] ); + $expected = array( self::$author_ids[1], self::$author_ids[2] ); $this->assertEqualSets( $expected, $found ); } @@ -767,18 +752,16 @@ class Tests_User_Query extends WP_UnitTestCase { register_post_type( 'wptests_pt_public', array( 'public' => true ) ); register_post_type( 'wptests_pt_private', array( 'public' => false ) ); - $users = $this->factory->user->create_many( 3 ); - - $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'draft', 'post_type' => 'wptests_pt_public' ) ); - $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'inherit', 'post_type' => 'wptests_pt_private' ) ); - $this->factory->post->create( array( 'post_author' => $users[2], 'post_status' => 'publish', 'post_type' => 'post' ) ); + $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'draft', 'post_type' => 'wptests_pt_public' ) ); + $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'inherit', 'post_type' => 'wptests_pt_private' ) ); + $this->factory->post->create( array( 'post_author' => self::$author_ids[2], 'post_status' => 'publish', 'post_type' => 'post' ) ); $q = new WP_User_Query( array( 'has_published_posts' => array( 'wptests_pt_public', 'wptests_pt_private', 'post' ), ) ); $found = wp_list_pluck( $q->get_results(), 'ID' ); - $expected = array( $users[2] ); + $expected = array( self::$author_ids[2] ); $this->assertEqualSets( $expected, $found ); } @@ -791,20 +774,19 @@ class Tests_User_Query extends WP_UnitTestCase { $this->markTestSkipped( __METHOD__ . ' requires multisite.' ); } - $users = $this->factory->user->create_many( 3 ); $blogs = $this->factory->blog->create_many( 2 ); - add_user_to_blog( $blogs[0], $users[0], 'author' ); - add_user_to_blog( $blogs[0], $users[1], 'author' ); - add_user_to_blog( $blogs[1], $users[0], 'author' ); - add_user_to_blog( $blogs[1], $users[1], 'author' ); + add_user_to_blog( $blogs[0], self::$author_ids[0], 'author' ); + add_user_to_blog( $blogs[0], self::$author_ids[1], 'author' ); + add_user_to_blog( $blogs[1], self::$author_ids[0], 'author' ); + add_user_to_blog( $blogs[1], self::$author_ids[1], 'author' ); switch_to_blog( $blogs[0] ); - $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'post' ) ); + $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'post' ) ); restore_current_blog(); switch_to_blog( $blogs[1] ); - $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'post' ) ); + $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'post' ) ); restore_current_blog(); $q = new WP_User_Query( array( @@ -813,7 +795,7 @@ class Tests_User_Query extends WP_UnitTestCase { ) ); $found = wp_list_pluck( $q->get_results(), 'ID' ); - $expected = array( $users[1] ); + $expected = array( self::$author_ids[1] ); $this->assertEqualSets( $expected, $found ); } @@ -822,11 +804,9 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 32592 */ public function test_top_level_or_meta_query_should_eliminate_duplicate_matches() { - $users = $this->factory->user->create_many( 3 ); - - add_user_meta( $users[0], 'foo', 'bar' ); - add_user_meta( $users[1], 'foo', 'bar' ); - add_user_meta( $users[0], 'foo2', 'bar2' ); + add_user_meta( self::$author_ids[0], 'foo', 'bar' ); + add_user_meta( self::$author_ids[1], 'foo', 'bar' ); + add_user_meta( self::$author_ids[0], 'foo2', 'bar2' ); $q = new WP_User_Query( array( 'meta_query' => array( @@ -843,7 +823,7 @@ class Tests_User_Query extends WP_UnitTestCase { ) ); $found = wp_list_pluck( $q->get_results(), 'ID' ); - $expected = array( $users[0], $users[1] ); + $expected = array( self::$author_ids[0], self::$author_ids[1] ); $this->assertEqualSets( $expected, $found ); } @@ -852,12 +832,10 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 32592 */ public function test_nested_or_meta_query_should_eliminate_duplicate_matches() { - $users = $this->factory->user->create_many( 3 ); - - add_user_meta( $users[0], 'foo', 'bar' ); - add_user_meta( $users[1], 'foo', 'bar' ); - add_user_meta( $users[0], 'foo2', 'bar2' ); - add_user_meta( $users[1], 'foo3', 'bar3' ); + add_user_meta( self::$author_ids[0], 'foo', 'bar' ); + add_user_meta( self::$author_ids[1], 'foo', 'bar' ); + add_user_meta( self::$author_ids[0], 'foo2', 'bar2' ); + add_user_meta( self::$author_ids[1], 'foo3', 'bar3' ); $q = new WP_User_Query( array( 'meta_query' => array( @@ -881,7 +859,7 @@ class Tests_User_Query extends WP_UnitTestCase { ) ); $found = wp_list_pluck( $q->get_results(), 'ID' ); - $expected = array( $users[0], $users[1] ); + $expected = array( self::$author_ids[0], self::$author_ids[1] ); $this->assertEqualSets( $expected, $found ); } @@ -890,8 +868,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 25145 */ public function test_paged() { - $users = $this->factory->user->create_many( 5 ); - $q = new WP_User_Query( array( 'number' => 2, 'paged' => 2, @@ -900,7 +876,7 @@ class Tests_User_Query extends WP_UnitTestCase { 'fields' => 'ids', ) ); - $this->assertEquals( array( $users[2], $users[1] ), $q->results ); + $this->assertEquals( array( self::$contrib_id, self::$editor_ids[2] ), $q->results ); } /** @@ -929,14 +905,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 22212 */ public function test_get_single_role_by_user_query() { - $this->factory->user->create_many( 2, array( - 'role' => 'subscriber', - ) ); - - $this->factory->user->create( array( - 'role' => 'contributor', - ) ); - $wp_user_search = new WP_User_Query( array( 'role' => 'subscriber' ) ); $users = $wp_user_search->get_results(); @@ -947,18 +915,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 22212 */ public function test_get_multiple_roles_by_user_query() { - $this->factory->user->create_many( 2, array( - 'role' => 'subscriber', - ) ); - - $this->factory->user->create_many( 3, array( - 'role' => 'editor', - ) ); - - $this->factory->user->create( array( - 'role' => 'contributor', - ) ); - $wp_user_search = new WP_User_Query( array( 'role__in' => array( 'subscriber', 'editor' ) ) ); $users = $wp_user_search->get_results(); $this->assertEquals( 5, count( $users ) ); @@ -968,14 +924,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 22212 */ public function test_get_single_role_by_string() { - $this->factory->user->create_many( 2, array( - 'role' => 'subscriber', - ) ); - - $this->factory->user->create( array( - 'role' => 'contributor', - ) ); - $users = get_users( array( 'role' => 'subscriber', ) ); @@ -987,10 +935,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 22212 */ public function test_get_single_role_by_string_which_is_similar() { - $editors = $this->factory->user->create_many( 2, array( - 'role' => 'editor', - ) ); - $another_editor = $this->factory->user->create( array( 'role' => 'another-editor', ) ); @@ -1000,7 +944,7 @@ class Tests_User_Query extends WP_UnitTestCase { 'fields' => 'ids', ) ); - $this->assertEqualSets( $editors, $users ); + $this->assertEqualSets( self::$editor_ids, $users ); } @@ -1008,14 +952,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 22212 */ public function test_get_single_role_by_array() { - $this->factory->user->create_many( 2, array( - 'role' => 'subscriber', - ) ); - - $this->factory->user->create( array( - 'role' => 'contributor', - ) ); - $users = get_users( array( 'role' => array( 'subscriber' ), ) ); @@ -1027,24 +963,12 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 22212 */ public function test_get_multiple_roles_should_only_match_users_who_have_each_role() { - $subscribers = $this->factory->user->create_many( 2, array( - 'role' => 'subscriber', - ) ); - - $this->factory->user->create_many( 3, array( - 'role' => 'editor', - ) ); - - $this->factory->user->create_many( 2, array( - 'role' => 'administrator', - ) ); - $users = new WP_User_Query( array( 'role' => array( 'subscriber', 'editor' ) ) ); $users = $users->get_results(); $this->assertEmpty( $users ); - foreach ( $subscribers as $subscriber ) { + foreach ( self::$sub_ids as $subscriber ) { $subscriber = get_user_by( 'ID', $subscriber ); $subscriber->add_role( 'editor' ); } @@ -1063,22 +987,6 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 22212 */ public function test_get_multiple_roles_or() { - $this->factory->user->create_many( 2, array( - 'role' => 'subscriber', - ) ); - - $this->factory->user->create_many( 3, array( - 'role' => 'editor', - ) ); - - $this->factory->user->create_many( 2, array( - 'role' => 'administrator', - ) ); - - $this->factory->user->create_many( 1, array( - 'role' => 'contributor', - ) ); - $users = new WP_User_Query( array( 'role__in' => array( 'subscriber', 'editor', 'administrator' ) ) ); $users = $users->get_results(); @@ -1093,21 +1001,13 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 22212 */ public function test_get_multiple_roles_by_comma_separated_list() { - $subscribers = $this->factory->user->create_many( 2, array( - 'role' => 'subscriber', - ) ); - - $this->factory->user->create_many( 3, array( - 'role' => 'editor', - ) ); - $users = get_users( array( 'role' => 'subscriber, editor', ) ); $this->assertEmpty( $users ); - foreach ( $subscribers as $subscriber ) { + foreach ( self::$sub_ids as $subscriber ) { $subscriber = get_user_by( 'ID', $subscriber ); $subscriber->add_role( 'editor' ); } @@ -1124,24 +1024,20 @@ class Tests_User_Query extends WP_UnitTestCase { */ public function test_get_multiple_roles_with_meta() { // Create administrator user + meta - $administrator_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); - update_user_meta( $administrator_id, 'mk1', 1 ); - update_user_meta( $administrator_id, 'mk2', 1 ); + update_user_meta( self::$admin_ids[0], 'mk1', 1 ); + update_user_meta( self::$admin_ids[0], 'mk2', 1 ); // Create editor user + meta - $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) ); - update_user_meta( $editor_id, 'mk1', 1 ); - update_user_meta( $editor_id, 'mk2', 2 ); + update_user_meta( self::$editor_ids[0], 'mk1', 1 ); + update_user_meta( self::$editor_ids[0], 'mk2', 2 ); // Create subscriber user + meta - $subscriber_id = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - update_user_meta( $subscriber_id, 'mk1', 1 ); - update_user_meta( $subscriber_id, 'mk2', 1 ); + update_user_meta( self::$sub_ids[0], 'mk1', 1 ); + update_user_meta( self::$sub_ids[0], 'mk2', 1 ); // Create contributor user + meta - $contributor_id = $this->factory->user->create( array( 'role' => 'contributor' ) ); - update_user_meta( $contributor_id, 'mk1', 1 ); - update_user_meta( $contributor_id, 'mk2', 2 ); + update_user_meta( self::$contrib_id, 'mk1', 1 ); + update_user_meta( self::$contrib_id, 'mk2', 2 ); // Fetch users $users = get_users( array( @@ -1165,49 +1061,33 @@ class Tests_User_Query extends WP_UnitTestCase { // Check results $this->assertEquals( 1, count( $users ) ); - $this->assertSame( $editor_id, (int) $users[0]->ID ); + $this->assertSame( self::$editor_ids[0], (int) $users[0]->ID ); } /** * @ticket 22212 */ public function test_role_exclusion() { - $this->factory->user->create_many( 2, array( - 'role' => 'subscriber', - ) ); - - $this->factory->user->create_many( 3, array( - 'role' => 'editor', - ) ); - $users = get_users( array( 'role__not_in' => 'subscriber', ) ); // +1 for the default user created during installation. - $this->assertEquals( 4, count( $users ) ); + $this->assertEquals( 11, count( $users ) ); $users = get_users( array( 'role__not_in' => 'editor', ) ); // +1 for the default user created during installation. - $this->assertEquals( 3, count( $users ) ); + $this->assertEquals( 10, count( $users ) ); } /** * @ticket 22212 */ public function test_role__in_role__not_in_combined() { - $subscribers = $this->factory->user->create_many( 2, array( - 'role' => 'subscriber', - ) ); - - $this->factory->user->create_many( 3, array( - 'role' => 'editor', - ) ); - - foreach ( $subscribers as $subscriber ) { + foreach ( self::$sub_ids as $subscriber ) { $subscriber = get_user_by( 'ID', $subscriber ); $subscriber->add_role( 'editor' ); } @@ -1230,15 +1110,7 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 22212 */ public function test_role__not_in_role_combined() { - $subscribers = $this->factory->user->create_many( 2, array( - 'role' => 'subscriber', - ) ); - - $this->factory->user->create_many( 3, array( - 'role' => 'editor', - ) ); - - $subscriber = get_user_by( 'ID', $subscribers[0] ); + $subscriber = get_user_by( 'ID', self::$sub_ids[0] ); $subscriber->add_role( 'editor' ); $users = get_users( array( @@ -1253,29 +1125,23 @@ class Tests_User_Query extends WP_UnitTestCase { * @ticket 22212 */ public function test_role__not_in_user_without_role() { - $user_without_rule = $this->factory->user->get_object_by_id( $this->factory->user->create( array( - 'role' => 'subscriber', - ) ) ); + $user_without_rule = get_user_by( 'ID', self::$sub_ids[0] ); $user_without_rule->remove_role( 'subscriber' ); - $this->factory->user->create_many( 3, array( - 'role' => 'editor', - ) ); - $users = get_users( array( 'role__not_in' => 'subscriber', ) ); // +1 for the default user created during installation. - $this->assertEquals( 5, count( $users ) ); + $this->assertEquals( 12, count( $users ) ); $users = get_users( array( 'role__not_in' => 'editor', ) ); // +1 for the default user created during installation. - $this->assertEquals( 2, count( $users ) ); + $this->assertEquals( 10, count( $users ) ); } /** @@ -1287,17 +1153,16 @@ class Tests_User_Query extends WP_UnitTestCase { } $sites = $this->factory->blog->create_many( 2 ); - $users = $this->factory->user->create_many( 2 ); - add_user_to_blog( $sites[0], $users[0], 'author' ); - add_user_to_blog( $sites[1], $users[1], 'author' ); + add_user_to_blog( self::$author_ids[0], self::$author_ids[0], 'author' ); + add_user_to_blog( self::$author_ids[1], self::$author_ids[1], 'author' ); $found = get_users( array( 'blog_id' => $sites[1], 'fields' => 'ID', ) ); - $this->assertEqualSets( array( $users[1] ), $found ); + $this->assertEqualSets( array( self::$author_ids[1] ), $found ); } /** @@ -1310,23 +1175,22 @@ class Tests_User_Query extends WP_UnitTestCase { } $site_id = get_current_blog_id(); - $u = $this->factory->user->create(); - add_user_to_blog( $site_id, $u, 'author' ); + add_user_to_blog( $site_id, self::$author_ids[0], 'author' ); $q = new WP_User_Query( array( - 'include' => $u, + 'include' => self::$author_ids[0], ) ); $r1 = $q->request; $q->prepare_query( array( - 'include' => $u, + 'include' => self::$author_ids[0], ) ); $r2 = $q->request; $q->prepare_query( array( - 'include' => $u, + 'include' => self::$author_ids[0], ) ); $r3 = $q->request;