diff --git a/tests/phpunit/tests/user/query.php b/tests/phpunit/tests/user/query.php index 44da476f59..196b3d4fd4 100644 --- a/tests/phpunit/tests/user/query.php +++ b/tests/phpunit/tests/user/query.php @@ -521,4 +521,82 @@ class Tests_User_Query extends WP_UnitTestCase { $this->assertEquals( array( $author_ids[0], $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, + ) ); + + $found = $query->get_results(); + + $this->assertNotEmpty( $found ); + $user = reset( $found ); + $this->assertSame( array( 'author' ), $user->roles ); + $this->assertSame( array( 'author' => true ), $user->caps ); + } + + public function test_roles_and_caps_should_be_populated_for_explicit_value_of_blog_id_on_nonms() { + if ( is_multisite() ) { + $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, + 'blog_id' => get_current_blog_id(), + ) ); + + $found = $query->get_results(); + + $this->assertNotEmpty( $found ); + $user = reset( $found ); + $this->assertSame( array( 'author' ), $user->roles ); + $this->assertSame( array( 'author' => true ), $user->caps ); + } + + public function test_roles_and_caps_should_be_populated_for_explicit_value_of_current_blog_id_on_ms() { + if ( ! is_multisite() ) { + $this->markTestSkipped( __METHOD__ . ' is a multisite-only test.' ); + } + + $u = $this->factory->user->create( array( 'role' => 'author' ) ); + + $query = new WP_User_Query( array( + 'include' => $u, + 'blog_id' => get_current_blog_id(), + ) ); + + $found = $query->get_results(); + + $this->assertNotEmpty( $found ); + $user = reset( $found ); + $this->assertSame( array( 'author' ), $user->roles ); + $this->assertSame( array( 'author' => true ), $user->caps ); + } + + public function test_roles_and_caps_should_be_populated_for_explicit_value_of_different_blog_id_on_ms_when_fields_all_with_meta() { + if ( ! is_multisite() ) { + $this->markTestSkipped( __METHOD__ . ' is a multisite-only test.' ); + } + + $b = $this->factory->blog->create(); + $u = $this->factory->user->create(); + add_user_to_blog( $b, $u, 'author' ); + + $query = new WP_User_Query( array( + 'include' => $u, + 'blog_id' => $b, + 'fields' => 'all_with_meta', + ) ); + + $found = $query->get_results(); + + $this->assertNotEmpty( $found ); + $user = reset( $found ); + $this->assertSame( array( 'author' ), $user->roles ); + $this->assertSame( array( 'author' => true ), $user->caps ); + } }