diff --git a/tests/phpunit/test-wp-user-query.php b/tests/phpunit/test-wp-user-query.php new file mode 100644 index 0000000..0f4530e --- /dev/null +++ b/tests/phpunit/test-wp-user-query.php @@ -0,0 +1,142 @@ +post; + + // Attributed to Editor, owned by Admin. + $factory->create_and_get( [ + 'post_author' => self::$users['admin']->ID, + 'post_status' => 'publish', + POSTS_PARAM => [ + self::$users['editor']->ID, + ], + ] ); + + // Attributed to Editor, owned by nobody. + $factory->create_and_get( [ + 'post_status' => 'publish', + POSTS_PARAM => [ + self::$users['editor']->ID, + ], + ] ); + + // Attributed to Guest, owned by Author. + $factory->create_and_get( [ + 'post_author' => self::$users['author']->ID, + 'post_status' => 'publish', + POSTS_PARAM => [ + self::$users[ GUEST_ROLE ]->ID, + ], + ] ); + + // Attributed to Guest, owned by Admin. + $factory->create_and_get( [ + 'post_author' => self::$users['admin']->ID, + 'post_status' => 'publish', + POSTS_PARAM => [ + self::$users[ GUEST_ROLE ]->ID, + ], + ] ); + + // Owned by Author. + $factory->create_and_get( [ + 'post_author' => self::$users['author']->ID, + 'post_status' => 'publish', + 'post_type' => 'non_existent_type', + ] ); + + // Owned by Admin. + $factory->create_and_get( [ + 'post_author' => self::$users['admin']->ID, + 'post_status' => 'publish', + 'post_type' => 'non_existent_type', + ] ); + + $common_args = [ + 'fields' => 'ids', + 'orderby' => 'ID', + 'order' => 'ASC', + ]; + + // Queries for attributed published post types. + $test_args = [ + 'has_published_posts' => [ 'post' ], + ]; + + foreach ( $test_args as $test_key => $test_value ) { + $args = array_merge( $common_args, [ + $test_key => $test_value, + ] ); + + $query = new WP_User_Query(); + $users = $query->query( $args ); + + $this->assertSame( + [ self::$users['editor']->ID, self::$users[ GUEST_ROLE ]->ID ], + $users, + "User IDs for attributed {$test_key} query are incorrect." + ); + } + + // Queries for non attributed published post types. + $test_args = [ + 'has_published_posts' => [ 'non_existent_type' ], + ]; + + foreach ( $test_args as $test_key => $test_value ) { + $args = array_merge( $common_args, [ + $test_key => $test_value, + ] ); + + $query = new WP_User_Query(); + $users = $query->query( $args ); + + $this->assertSame( + [ self::$users['admin']->ID, self::$users['author']->ID ], + $users, + "User IDs for non attributed {$test_key} query are incorrect." + ); + } + + // Queries for non attributed published post types. + $test_args = [ + 'has_published_posts' => [ 'post', 'non_existent_type' ], + ]; + + foreach ( $test_args as $test_key => $test_value ) { + $args = array_merge( $common_args, [ + $test_key => $test_value, + ] ); + + $query = new WP_User_Query(); + $users = $query->query( $args ); + + $this->assertSame( + [ + self::$users['admin']->ID, + self::$users['editor']->ID, + self::$users['author']->ID, + self::$users[ GUEST_ROLE ]->ID + ], + $users, + "User IDs for combined attributed and non-attributed {$test_key} query are incorrect." + ); + } + } + +}