Skip to content

Commit

Permalink
Add test case for WP_User_Query
Browse files Browse the repository at this point in the history
  • Loading branch information
roborourke committed May 28, 2024
1 parent 6ecd56c commit 0c1fd32
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions tests/phpunit/test-wp-user-query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/**
* WP_Query tests.
*
* @package authorship
*/

declare( strict_types=1 );

namespace Authorship\Tests;

use const Authorship\GUEST_ROLE;
use const Authorship\POSTS_PARAM;

use WP_User_Query;

class TestWPUserQuery extends TestCase {
public function testQueryForAuthorWithPublishedPostsReturnsAttributedAuthors() : void {
$factory = self::factory()->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."
);
}
}

}

0 comments on commit 0c1fd32

Please sign in to comment.