-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ecd56c
commit 0c1fd32
Showing
1 changed file
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
); | ||
} | ||
} | ||
|
||
} |