Skip to content

Commit

Permalink
add: new assertion to help determine if a Post has the correct Authors
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiesshop committed Jul 3, 2024
1 parent 8dd6d67 commit db9a6d7
Showing 1 changed file with 118 additions and 165 deletions.
283 changes: 118 additions & 165 deletions tests/Integration/CoAuthorsPlusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Automattic\CoAuthorsPlus\Tests\Integration;

use PHPUnit\Framework\InvalidArgumentException;
use WP_Query;
use WP_Term;

Expand Down Expand Up @@ -723,6 +724,42 @@ public function assertIsGuestAuthorNotWpUser( object $author ): void {
);
}

/**
* This function handles asserting that a post has the authors specified, and the correct number of authors.
*
* @param int $post_id The Post ID.
* @param array $authors The authors to check that are assigned to a post.
*
* @return void
* @throws InvalidArgumentException Throws exception if $authors is not a valid Guest Author or WP_User object or a string.
*/
public function assertPostHasCoAuthors( int $post_id, array $authors ) {
$authors = array_map(
function ( $author ) {
if ( is_object( $author ) ) {
return 'cap-' . $author->user_login;
} elseif ( is_string( $author ) ) {
return $author; // Assuming that caller is giving author slug.
} else {
throw InvalidArgumentException::create( 2, 'Authors should be string, Guest Author Object, or WP_User' );
}
},
$authors
);

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertSameSize(
$authors,
$post_author_terms
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $authors );
}
}

/**
* This is a basic test to ensure that any authors being assigned to a post
Expand Down Expand Up @@ -814,20 +851,13 @@ public function test_append_post_author_who_has_not_been_linked() {
// Although we added a co-author, the wp_posts.post_author column should still be attributed to $author2.
$this->assertEquals( $this->author2->ID, $query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 2, $post_author_terms );

$author_slugs = array(
'cap-' . $this->author2->user_login,
'cap-' . $this->author3->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->author2->user_login,
$this->author3->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}
}

/**
Expand Down Expand Up @@ -865,21 +895,14 @@ public function test_assign_post_authors_from_authors_who_have_not_been_linked()
$this->assertEquals( 1, $query->found_posts );
$this->assertEquals( $this->author3->ID, $query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 3, $post_author_terms );

$author_slugs = array(
'cap-' . $this->author3->user_login,
'cap-' . $this->editor1->user_login,
'cap-' . $this->author2->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->author3->user_login,
$this->editor1->user_login,
$this->author2->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}
}

/**
Expand Down Expand Up @@ -1034,21 +1057,14 @@ public function test_append_post_authors_from_coauthors_who_have_not_been_linked
$this->assertEquals( 1, $second_query->found_posts );
$this->assertEquals( $this->author1->ID, $second_query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 3, $post_author_terms );

$author_slugs = array(
'cap-' . $this->author1->user_login,
'cap-' . $guest_author_1->user_login,
'cap-' . $guest_author_2->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->author1->user_login,
$guest_author_1->user_login,
$guest_author_2->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}
}

/**
Expand Down Expand Up @@ -1108,20 +1124,13 @@ public function test_assign_coauthors_from_coauthors_and_user_who_have_not_been_
$this->assertEquals( 1, $second_query->found_posts );
$this->assertEquals( $this->author3->ID, $second_query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 2, $post_author_terms );

$author_slugs = array(
'cap-' . $this->author3->user_login,
'cap-' . $guest_author_1->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->author3->user_login,
$guest_author_1->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}
}

/**
Expand Down Expand Up @@ -1181,21 +1190,14 @@ public function test_append_coauthors_from_coauthors_and_user_who_have_not_been_
$this->assertEquals( 1, $second_query->found_posts );
$this->assertEquals( $this->author1->ID, $second_query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 3, $post_author_terms );

$author_slugs = array(
'cap-' . $this->author1->user_login,
'cap-' . $this->author3->user_login,
'cap-' . $guest_author_1->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->author1->user_login,
$this->author3->user_login,
$guest_author_1->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}
}

/**
Expand Down Expand Up @@ -1266,20 +1268,13 @@ public function test_assign_post_authors_from_coauthors_who_are_linked() {
$this->assertEquals( 1, $second_query->found_posts );
$this->assertEquals( $this->author2->ID, $second_query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 2, $post_author_terms );

$author_slugs = array(
'cap-' . $this->author2->user_login,
'cap-' . $this->author3->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->author2->user_login,
$this->author3->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}
}

/**
Expand Down Expand Up @@ -1352,21 +1347,14 @@ public function test_append_post_authors_from_coauthors_one_of_whom_is_linked()
$this->assertEquals( 1, $second_query->found_posts );
$this->assertEquals( $this->editor1->ID, $second_query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 3, $post_author_terms );

$author_slugs = array(
'cap-' . $this->editor1->user_login,
'cap-' . $guest_author_1->user_login,
'cap-' . $this->author3->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->editor1->user_login,
$guest_author_1->user_login,
$this->author3->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}
}

/**
Expand Down Expand Up @@ -1433,21 +1421,14 @@ public function test_assign_multiple_post_authors_wp_user_guest_author_linked_us
$this->assertEquals( 1, $second_query->found_posts );
$this->assertEquals( $this->author1->ID, $second_query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 3, $post_author_terms );

$author_slugs = array(
'cap-' . $this->author1->user_login,
'cap-' . $guest_author_1->user_login,
'cap-' . $this->author3->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->author1->user_login,
$guest_author_1->user_login,
$this->author3->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}
}

/**
Expand Down Expand Up @@ -1534,21 +1515,14 @@ public function test_assign_multiple_post_authors_only_one_linked_passed_last()
$this->assertEquals( 1, $second_query->found_posts );
$this->assertEquals( $this->author3->ID, $second_query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 3, $post_author_terms );

$author_slugs = array(
'cap-' . $this->author3->user_login,
'cap-' . $guest_author_1->user_login,
'cap-' . $guest_author_2->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->author3->user_login,
$guest_author_1->user_login,
$guest_author_2->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}
}

/**
Expand Down Expand Up @@ -1619,21 +1593,14 @@ public function test_assign_multiple_post_authors_one_user_before_one_linked_pas
$this->assertEquals( 1, $second_query->found_posts );
$this->assertEquals( $this->author2->ID, $second_query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 3, $post_author_terms );

$author_slugs = array(
'cap-' . $this->author2->user_login,
'cap-' . $this->author3->user_login,
'cap-' . $guest_author_1->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->author2->user_login,
$this->author3->user_login,
$guest_author_1->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}
}

/**
Expand Down Expand Up @@ -1705,21 +1672,14 @@ public function test_assign_multiple_post_authors_one_linked_passed_first() {
$this->assertEquals( 1, $second_query->found_posts );
$this->assertEquals( $this->author3->ID, $second_query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 3, $post_author_terms );

$author_slugs = array(
'cap-' . $this->author2->user_login,
'cap-' . $this->author3->user_login,
'cap-' . $guest_author_1->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->author2->user_login,
$this->author3->user_login,
$guest_author_1->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}
}

/**
Expand Down Expand Up @@ -1792,22 +1752,15 @@ public function test_assign_multiple_post_authors_one_linked_passed_using_user_l
$this->assertEquals( 1, $second_query->found_posts );
$this->assertEquals( $this->author3->ID, $second_query->posts[0]->post_author );

$post_author_terms = wp_get_post_terms( $post_id, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $post_author_terms );
$this->assertCount( 3, $post_author_terms );

$author_slugs = array(
'cap-' . $this->author2->user_login,
'cap-' . $this->author3->user_login,
'cap-' . $guest_author_1->user_login,
$this->assertPostHasCoAuthors(
$post_id,
[
$this->author2->user_login,
$this->author3->user_login,
$guest_author_1->user_login,
]
);

foreach ( $post_author_terms as $term ) {
$this->assertInstanceOf( WP_Term::class, $term );
$this->assertContains( $term->slug, $author_slugs );
}

$guest_author_term = wp_get_post_terms( $linked_author_1->ID, $this->_cap->coauthor_taxonomy );

$this->assertIsArray( $guest_author_term );
Expand Down

0 comments on commit db9a6d7

Please sign in to comment.