Skip to content

Commit

Permalink
Merge pull request #1008 from Automattic/fix/test-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryJones authored Oct 1, 2023
2 parents 2445fa6 + 1f5c977 commit af2093f
Show file tree
Hide file tree
Showing 13 changed files with 913 additions and 954 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"wp-cli/extension-command": "^2.0",
"wp-cli/wp-cli-tests": "^3",
"wp-coding-standards/wpcs": "^2.3.0",
"yoast/wp-test-utils": "^1.1"
"yoast/wp-test-utils": "^1.2"
},
"autoload": {
"classmap": [
Expand Down
6 changes: 5 additions & 1 deletion template-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ function is_coauthor_for_post( $user, $post_id = 0 ) {
$coauthors = get_coauthors( $post_id );
if ( is_numeric( $user ) ) {
$user = get_userdata( $user );
$user = $user->user_login;
if ( isset( $user->user_login ) ) {
$user = $user->user_login;
} else {
return false;
}
} elseif ( isset( $user->user_login ) ) {
$user = $user->user_login;
} else {
Expand Down
218 changes: 67 additions & 151 deletions tests/Integration/AuthorQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,74 @@

class AuthorQueriesTest extends TestCase {

public function test__author_arg__user_is_post_author_query_as_post_author() {
$author_id = $this->factory()->user->create(
array(
'role' => 'author',
'user_login' => 'batman',
)
);
$author = get_userdata( $author_id );
$post_id = $this->factory()->post->create(
array(
'post_author' => $author_id,
'post_status' => 'publish',
'post_type' => 'post',
)
);
$this->_cap->add_coauthors( $post_id, array( $author->user_login ) );

wp_set_current_user( $author_id );
/**
* Test a simple query that a post is returned after setting a co-author on it.
* The focus is the query by user login instead of ID.
*/
public function test_get_post_by_user_login_when_single_author_is_set_as_post_author() {
$author = $this->create_author();
$post = $this->create_post( $author );
$this->_cap->add_coauthors( $post->ID, array( $author->user_login ) );

$query = new \WP_Query(
array(
'author' => $author_id,
'author_name' => $author->user_login, // This is the change.
)
);

$this->assertCount( 1, $query->posts );
$this->assertEquals( $post_id, $query->posts[0]->ID );
$this->assertEquals( $post->ID, $query->posts[0]->ID );
}

public function test__author_arg__user_is_post_author() {
$author_id = $this->factory()->user->create(
array(
'role' => 'author',
'user_login' => 'batman',
)
);
$author = get_userdata( $author_id );
$post_id = $this->factory()->post->create(
array(
'post_author' => $author_id,
'post_status' => 'publish',
'post_type' => 'post',
)
);
$this->_cap->add_coauthors( $post_id, array( $author->user_login ) );
/**
* Test a simple query that a post is returned after setting a co-author on it.
* This test is run as the default administrator user.
*/
public function test_get_post_by_user_ID_when_single_author_is_set_as_post_author_but_current_user_is_admin() {
$author = $this->create_author();
$post = $this->create_post( $author );
$this->_cap->add_coauthors( $post->ID, array( $author->user_login ) );

$query = new \WP_Query(
array(
'author' => $author_id,
'author' => $author->ID,
)
);

$this->assertCount( 1, $query->posts );
$this->assertEquals( $post_id, $query->posts[0]->ID );
$this->assertEquals( $post->ID, $query->posts[0]->ID );
}

public function test__author_name_arg__user_is_post_author() {
$author_id = $this->factory()->user->create(
array(
'role' => 'author',
'user_login' => 'batman',
)
);
$author = get_userdata( $author_id );
$post_id = $this->factory()->post->create(
array(
'post_author' => $author_id,
'post_status' => 'publish',
'post_type' => 'post',
)
);
$this->_cap->add_coauthors( $post_id, array( $author->user_login ) );
/**
* Test a simple query that a post is returned after setting a co-author on it.
* This test is run as the author user. This is to ensure that the logic works for non-administrator roles. See #508.
*/
public function test_get_post_by_user_ID_when_single_author_is_set_as_post_author_but_current_user_is_author() {
$author = $this->create_author();
$post = $this->create_post( $author );
$this->_cap->add_coauthors( $post->ID, array( $author->user_login ) );

wp_set_current_user( $author->ID ); // This is the only difference to the last test.

$query = new \WP_Query(
array(
'author_name' => $author->user_login,
'author' => $author->ID,
)
);

$this->assertCount( 1, $query->posts );
$this->assertEquals( $post_id, $query->posts[0]->ID );
$this->assertEquals( $post->ID, $query->posts[0]->ID );
}

public function test__author_name_arg__user_is_coauthor() {
$author1_id = $this->factory()->user->create(
array(
'role' => 'author',
'user_login' => 'batman',
)
);
$author1 = get_userdata( $author1_id );
$author2_id = $this->factory()->user->create(
array(
'role' => 'author',
'user_login' => 'superman',
)
);
$author2 = get_userdata( $author2_id );

$post_id = $this->factory()->post->create(
array(
'post_author' => $author1_id,
'post_status' => 'publish',
'post_type' => 'post',
)
);
$this->_cap->add_coauthors( $post_id, array( $author1->user_login, $author2->user_login ) );
/**
* Test a simple user_login query that a post is returned after setting multiple co-authors for it.
* The post_author is set as the first co-author.
*/
public function test_get_post_by_user_ID_when_queried_author_is_set_as_post_author() {
$author1 = $this->create_author( 'author1' );
$author2 = $this->create_author( 'author2' );
$post = $this->create_post( $author1 );
$this->_cap->add_coauthors( $post->ID, array( $author1->user_login, $author2->user_login ) );

$query = new \WP_Query(
array(
Expand All @@ -119,61 +80,35 @@ public function test__author_name_arg__user_is_coauthor() {
);

$this->assertCount( 1, $query->posts );
$this->assertEquals( $post_id, $query->posts[0]->ID );
$this->assertEquals( $post->ID, $query->posts[0]->ID );
}

public function test__author_arg__user_is_coauthor__author_arg() {
$author1_id = $this->factory()->user->create(
array(
'role' => 'author',
'user_login' => 'batman',
)
);
$author1 = get_userdata( $author1_id );
$author2_id = $this->factory()->user->create(
array(
'role' => 'author',
'user_login' => 'superman',
)
);
$author2 = get_userdata( $author2_id );

$post_id = $this->factory()->post->create(
array(
'post_author' => $author1_id,
'post_status' => 'publish',
'post_type' => 'post',
)
);
$this->_cap->add_coauthors( $post_id, array( $author1->user_login, $author2->user_login ) );
/**
* Test a simple user_login query that a post is returned after setting multiple co-authors for it.
* The post_author is set as the first co-author, but we're querying by the second co-author, so
* this is magic working now.
*/
public function test_get_post_by_user_ID_when_queried_author_is_not_set_as_post_author() {
$author1 = $this->create_author( 'author1' );
$author2 = $this->create_author( 'author2' );
$post = $this->create_post( $author1 );
$this->_cap->add_coauthors( $post->ID, array( $author1->user_login, $author2->user_login ) );

$query = new \WP_Query(
array(
'author' => $author2_id,
'author' => $author2->ID,
)
);

$this->assertCount( 1, $query->posts );
$this->assertEquals( $post_id, $query->posts[0]->ID );
$this->assertEquals( $post->ID, $query->posts[0]->ID );
}

public function test__author_name_arg_plus_tax_query__user_is_post_author() {
$author_id = $this->factory()->user->create(
array(
'role' => 'author',
'user_login' => 'batman',
)
);
$author = get_userdata( $author_id );
$post_id = $this->factory()->post->create(
array(
'post_author' => $author_id,
'post_status' => 'publish',
'post_type' => 'post',
)
);
$this->_cap->add_coauthors( $post_id, array( $author->user_login ) );
wp_set_post_terms( $post_id, 'test' );
public function test_get_post_by_user_ID_when_queried_author_is_set_as_post_author_but_there_is_a_tag() {
$author = $this->create_author();
$post = $this->create_post( $author );
$this->_cap->add_coauthors( $post->ID, array( $author->user_login ) );
wp_set_post_terms( $post->ID, 'test' );

$query = new \WP_Query(
array(
Expand All @@ -183,34 +118,15 @@ public function test__author_name_arg_plus_tax_query__user_is_post_author() {
);

$this->assertCount( 1, $query->posts );
$this->assertEquals( $post_id, $query->posts[0]->ID );
$this->assertEquals( $post->ID, $query->posts[0]->ID );
}

public function tests__author_name_arg_plus_tax_query__is_coauthor() {
$author1_id = $this->factory()->user->create(
array(
'role' => 'author',
'user_login' => 'batman',
)
);
$author1 = get_userdata( $author1_id );
$author2_id = $this->factory()->user->create(
array(
'role' => 'author',
'user_login' => 'superman',
)
);
$author2 = get_userdata( $author2_id );

$post_id = $this->factory()->post->create(
array(
'post_author' => $author1_id,
'post_status' => 'publish',
'post_type' => 'post',
)
);
$this->_cap->add_coauthors( $post_id, array( $author1->user_login, $author2->user_login ) );
wp_set_post_terms( $post_id, 'test' );
public function test_get_post_by_user_ID_when_queried_author_is_not_set_as_post_author_and_there_is_a_tag() {
$author1 = $this->create_author( 'author1' );
$author2 = $this->create_author( 'author2' );
$post = $this->create_post( $author1 );
$this->_cap->add_coauthors( $post->ID, array( $author1->user_login, $author2->user_login ) );
wp_set_post_terms( $post->ID, 'test' );

$query = new \WP_Query(
array(
Expand All @@ -220,6 +136,6 @@ public function tests__author_name_arg_plus_tax_query__is_coauthor() {
);

$this->assertCount( 1, $query->posts );
$this->assertEquals( $post_id, $query->posts[0]->ID );
$this->assertEquals( $post->ID, $query->posts[0]->ID );
}
}
14 changes: 7 additions & 7 deletions tests/Integration/CoAuthorsPlusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function test_get_coauthor_by_when_guest_author() {
$coauthor = $coauthors_plus->get_coauthor_by( 'id', $guest_author_id );

$this->assertInstanceOf( \stdClass::class, $coauthor );
$this->assertTrue( property_exists( $coauthor, 'ID' ) );
$this->assertObjectHasProperty( 'ID', $coauthor );
$this->assertEquals( $guest_author_id, $coauthor->ID );
$this->assertEquals( 'guest-author', $coauthor->type );
}
Expand All @@ -100,7 +100,7 @@ public function test_get_coauthor_by_when_guest_author_has_unicode_username() {
$coauthor = $coauthors_plus->get_coauthor_by( 'user_login', $user_login );

$this->assertInstanceOf( \stdClass::class, $coauthor );
$this->assertTrue( property_exists( $coauthor, 'ID' ) );
$this->assertObjectHasProperty( 'ID', $coauthor );
$this->assertEquals( $guest_author_id, $coauthor->ID );
$this->assertEquals( 'guest-author', $coauthor->type );
}
Expand All @@ -121,26 +121,26 @@ public function test_get_coauthor_by_when_guest_authors_not_enabled() {
$coauthor = $coauthors_plus->get_coauthor_by( 'id', $this->author1->ID );

$this->assertInstanceOf( \WP_User::class, $coauthor );
$this->assertTrue( property_exists( $coauthor, 'ID' ) );
$this->assertObjectHasProperty( 'ID', $coauthor );
$this->assertEquals( $this->author1->ID, $coauthor->ID );
$this->assertEquals( 'wpuser', $coauthor->type );

$coauthor = $coauthors_plus->get_coauthor_by( 'user_login', $this->author1->user_login );

$this->assertInstanceOf( \WP_User::class, $coauthor );
$this->assertTrue( property_exists( $coauthor->data, 'user_login' ) );
$this->assertObjectHasProperty( 'user_login', $coauthor->data );
$this->assertEquals( $this->author1->user_login, $coauthor->user_login );

$coauthor = $coauthors_plus->get_coauthor_by( 'user_nicename', $this->author1->user_nicename );

$this->assertInstanceOf( \WP_User::class, $coauthor );
$this->assertTrue( property_exists( $coauthor->data, 'user_nicename' ) );
$this->assertObjectHasProperty( 'user_nicename', $coauthor->data );
$this->assertEquals( $this->author1->user_nicename, $coauthor->user_nicename );

$coauthor = $coauthors_plus->get_coauthor_by( 'user_email', $this->author1->user_email );

$this->assertInstanceOf( \WP_User::class, $coauthor );
$this->assertTrue( property_exists( $coauthor->data, 'user_email' ) );
$this->assertObjectHasProperty( 'user_email', $coauthor->data );
$this->assertEquals( $this->author1->user_email, $coauthor->user_email );

remove_filter( 'coauthors_guest_authors_enabled', '__return_false' );
Expand All @@ -150,7 +150,7 @@ public function test_get_coauthor_by_when_guest_authors_not_enabled() {
$coauthor = $coauthors_plus->get_coauthor_by( 'id', $this->editor1->ID );

$this->assertInstanceOf( \stdClass::class, $coauthor );
$this->assertTrue( property_exists( $coauthor, 'linked_account' ) );
$this->assertObjectHasProperty( 'linked_account', $coauthor );
$this->assertEquals( $this->editor1->user_login, $coauthor->linked_account );
}

Expand Down
Loading

0 comments on commit af2093f

Please sign in to comment.