From d567f0f286c178090cceef57285ac37885ad1551 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Thu, 21 Sep 2023 14:54:32 +0100 Subject: [PATCH 01/13] Tests: Refactor CoauthorsPostLinksSingle --- .../CoauthorsPostsLinksSingleTest.php | 66 +++++++++++++++++++ tests/Integration/TemplateTagsTest.php | 27 -------- tests/Integration/TestCase.php | 9 +++ 3 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 tests/Integration/TemplateTags/CoauthorsPostsLinksSingleTest.php diff --git a/tests/Integration/TemplateTags/CoauthorsPostsLinksSingleTest.php b/tests/Integration/TemplateTags/CoauthorsPostsLinksSingleTest.php new file mode 100644 index 00000000..83ba4c18 --- /dev/null +++ b/tests/Integration/TemplateTags/CoauthorsPostsLinksSingleTest.php @@ -0,0 +1,66 @@ +create_author(); + $this->setExpectedIncorrectUsage( 'coauthors_posts_links_single' ); + unset( $author->ID, $author->user_nicename, $author->display_name ); + + $return = coauthors_posts_links_single( $author ); + } + + /** + * Checks single co-author linked to their post archive. + */ + public function test_single_link_is_returned_with_default_args() { + $author = $this->create_author(); + + $author_link = coauthors_posts_links_single( $author ); + + $this->assertStringContainsString( 'href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '"', $author_link, 'Author link not found.' ); + $this->assertStringContainsString( $author->display_name, $author_link, 'Author name not found.' ); + // Here we are checking author name should not be more than one time. + // Asserting ">{$this->author1->display_name}<" because "$this->author1->display_name" can be multiple times like in href, title, etc. + $this->assertEquals( 1, substr_count( $author_link, ">{$author->display_name}<" ) ); + } + + /** + * Checks single co-author linked to their post archive. + */ + public function test_single_link_is_returned_with_filtered_args() { + $author = $this->create_author( 'filtered-author' ); + add_filter( 'coauthors_posts_link', function( $args ) use ( $author ) { + return array( + 'before_html' => 'Before', + 'href' => 'https://example.com/', + 'rel' => '', + 'title' => null, // Would be nice for the attribute not to appear at all for this. + 'class' => 'my-author-link', + 'text' => apply_filters( 'the_author', $author->display_name ), + 'after_html' => 'After', + ); + }); + + $author_link = coauthors_posts_links_single( $author ); + + $this->assertEquals( + 'Before' . $author->display_name . 'After', + $author_link, + 'Author link parameters were not filtered correctly.' + ); + } +} diff --git a/tests/Integration/TemplateTagsTest.php b/tests/Integration/TemplateTagsTest.php index 933abb9a..52b75d5d 100644 --- a/tests/Integration/TemplateTagsTest.php +++ b/tests/Integration/TemplateTagsTest.php @@ -382,33 +382,6 @@ public function test_coauthors() { $post = $post_backup; } - /** - * Checks single co-author linked to their post archive. - * - * @covers ::coauthors_posts_links_single() - */ - public function test_coauthors_posts_links_single() { - - global $post; - - // Backing up global post. - $post_backup = $post; - - $post = $this->post; - - $author_link = coauthors_posts_links_single( $this->author1 ); - - $this->assertStringContainsString( 'href="' . get_author_posts_url( $this->author1->ID, $this->author1->user_nicename ) . '"', $author_link, 'Author link not found.' ); - $this->assertStringContainsString( $this->author1->display_name, $author_link, 'Author name not found.' ); - - // Here we are checking author name should not be more than one time. - // Asserting ">{$this->author1->display_name}<" because "$this->author1->display_name" can be multiple times like in href, title, etc. - $this->assertEquals( 1, substr_count( $author_link, ">{$this->author1->display_name}<" ) ); - - // Restore global post from backup. - $post = $post_backup; - } - /** * Checks co-authors first names, without links to their posts. * diff --git a/tests/Integration/TestCase.php b/tests/Integration/TestCase.php index 2834de18..6bd93cc0 100644 --- a/tests/Integration/TestCase.php +++ b/tests/Integration/TestCase.php @@ -25,4 +25,13 @@ public function set_up() { $this->_cap = $coauthors_plus; $this->_api = new Endpoints( $coauthors_plus ); } + + protected function create_author( $user_login = 'author' ) { + return $this->factory()->user->create_and_get( + array( + 'role' => 'author', + 'user_login' => $user_login, + ) + ); + } } From 3f37b272e5a116cd132d8403057de9cac00bf2e8 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sat, 23 Sep 2023 16:36:15 +0100 Subject: [PATCH 02/13] Tests: Refactor CoauthorsPostLinks --- .../TemplateTags/CoauthorsPostsLinksTest.php | 107 ++++++++++++++++++ tests/Integration/TemplateTagsTest.php | 60 ---------- tests/Integration/TestCase.php | 9 ++ 3 files changed, 116 insertions(+), 60 deletions(-) create mode 100644 tests/Integration/TemplateTags/CoauthorsPostsLinksTest.php diff --git a/tests/Integration/TemplateTags/CoauthorsPostsLinksTest.php b/tests/Integration/TemplateTags/CoauthorsPostsLinksTest.php new file mode 100644 index 00000000..eeeb30a9 --- /dev/null +++ b/tests/Integration/TemplateTags/CoauthorsPostsLinksTest.php @@ -0,0 +1,107 @@ +assertEquals( 10, has_filter( 'the_author', array( $coauthors_plus_template_filters, 'filter_the_author' ) ) ); + } + + /** + * Test that single author posts link is retrieved via coauthors_posts_links_single(), + * and suitably prefixed / suffixed. + * + * @see https://github.com/Automattic/Co-Authors-Plus/issues/279 + */ + public function test_coauthors_posts_links_for_single_author() { + $author = $this->create_author(); + $post = $this->factory()->post->create_and_get( + array( + 'post_author' => $author->ID, + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => 'post', + ) + ); + $GLOBALS['post'] = $post; + + $coauthors_posts_links = coauthors_posts_links( null, null, null, null, false ); + + $this->assertEquals( + '', + $coauthors_posts_links, + 'Single author post link incorrect.' + ); + } + + /** + * Test co-author posts links are retrieved for multiple authors and default args. + */ + public function test_coauthors_posts_links_for_multiple_authors_with_default_args() { + global $coauthors_plus; + + $author = $this->create_author(); + $editor = $this->create_editor(); + $post = $this->factory()->post->create_and_get( + array( + 'post_author' => $author->ID, + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => 'post', + ) + ); + $GLOBALS['post'] = $post; + $coauthors_plus->add_coauthors( $post->ID, array( $editor->user_login ), true ); + + $coauthors_posts_links = coauthors_posts_links( null, null, null, null, false ); + + $this->assertEquals( + ' and ', + $coauthors_posts_links, + 'Multiple author post links incorrect.' + ); + } + + /** + * Test that co-author posts link is retrieved via coauthors_posts_links_single() but for multiple authors. + */ + public function test_coauthors_posts_links_for_multiple_authors_with_amended_args() { + global $coauthors_plus; + + $author1 = $this->create_author( 'author1' ); + $author2 = $this->create_author( 'author2' ); + $editor = $this->create_editor(); + $post = $this->factory()->post->create_and_get( + array( + 'post_author' => $author1->ID, + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => 'post', + ) + ); + $GLOBALS['post'] = $post; + $coauthors_plus->add_coauthors( $post->ID, array( $author2->user_login, $editor->user_login ), true ); + + $coauthors_posts_links = coauthors_posts_links( ' and ', ' & ', 'By ', '.', false ); + + $this->assertEquals( + 'By and & .', + $coauthors_posts_links, + 'Multiple author post links incorrect.' + ); + } +} diff --git a/tests/Integration/TemplateTagsTest.php b/tests/Integration/TemplateTagsTest.php index 52b75d5d..abf42fe9 100644 --- a/tests/Integration/TemplateTagsTest.php +++ b/tests/Integration/TemplateTagsTest.php @@ -45,66 +45,6 @@ public function set_up() { ); } - /** - * Tests for co-authors display names, with links to their posts. - * - * @see https://github.com/Automattic/Co-Authors-Plus/issues/279 - * - * @covers ::coauthors_posts_links() - */ - public function test_coauthors_posts_links() { - - global $coauthors_plus, $coauthors_plus_template_filters; - - // Backing up global post. - $post_backup = $GLOBALS['post']; - - $GLOBALS['post'] = $this->post; - - // Checks for single post author. - $single_cpl = coauthors_posts_links( null, null, null, null, false ); - - $this->assertStringContainsString( 'href="' . get_author_posts_url( $this->author1->ID, $this->author1->user_nicename ) . '"', $single_cpl, 'Author link not found.' ); - $this->assertStringContainsString( $this->author1->display_name, $single_cpl, 'Author name not found.' ); - - // Checks for multiple post author. - $coauthors_plus->add_coauthors( $this->post->ID, array( $this->editor1->user_login ), true ); - - $multiple_cpl = coauthors_posts_links( null, null, null, null, false ); - - $this->assertStringContainsString( 'href="' . get_author_posts_url( $this->author1->ID, $this->author1->user_nicename ) . '"', $multiple_cpl, 'Main author link not found.' ); - $this->assertStringContainsString( $this->author1->display_name, $multiple_cpl, 'Main author name not found.' ); - - // Here we are checking author name should not be more than one time. - // Asserting ">{$this->author1->display_name}<" because "$this->author1->display_name" can be multiple times like in href, title, etc. - $this->assertEquals( 1, substr_count( $multiple_cpl, ">{$this->author1->display_name}<" ) ); - $this->assertStringContainsString( ' and ', $multiple_cpl, 'Coauthors name separator is not matched.' ); - $this->assertStringContainsString( 'href="' . get_author_posts_url( $this->editor1->ID, $this->editor1->user_nicename ) . '"', $multiple_cpl, 'Coauthor link not found.' ); - $this->assertStringContainsString( $this->editor1->display_name, $multiple_cpl, 'Coauthor name not found.' ); - - // Here we are checking editor name should not be more than one time. - // Asserting ">{$this->editor1->display_name}<" because "$this->editor1->display_name" can be multiple times like in href, title, etc. - $this->assertEquals( 1, substr_count( $multiple_cpl, ">{$this->editor1->display_name}<" ) ); - - $multiple_cpl = coauthors_links( null, ' or ', null, null, false ); - - $this->assertStringContainsString( ' or ', $multiple_cpl, 'Coauthors name separator is not matched.' ); - - $this->assertEquals( - 10, - has_filter( - 'the_author', - array( - $coauthors_plus_template_filters, - 'filter_the_author', - ) - ) - ); - - // Restore backed up post to global. - $GLOBALS['post'] = $post_backup; - } - /** * Tests for co-authors display names. * diff --git a/tests/Integration/TestCase.php b/tests/Integration/TestCase.php index 6bd93cc0..4895dbcf 100644 --- a/tests/Integration/TestCase.php +++ b/tests/Integration/TestCase.php @@ -34,4 +34,13 @@ protected function create_author( $user_login = 'author' ) { ) ); } + + protected function create_editor( $user_login = 'editor' ) { + return $this->factory()->user->create_and_get( + array( + 'role' => 'editor', + 'user_login' => $user_login, + ) + ); + } } From 0efff3146317f64f53202e430d7ab3a18ff234f7 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sat, 23 Sep 2023 22:31:35 +0100 Subject: [PATCH 03/13] Tests: Refactor AuthorQueries Add improved test names and documentation, while simplifying the arrangements. --- tests/Integration/AuthorQueriesTest.php | 194 ++++++++++-------------- 1 file changed, 76 insertions(+), 118 deletions(-) diff --git a/tests/Integration/AuthorQueriesTest.php b/tests/Integration/AuthorQueriesTest.php index d8d7296a..41b3a59f 100644 --- a/tests/Integration/AuthorQueriesTest.php +++ b/tests/Integration/AuthorQueriesTest.php @@ -4,113 +4,98 @@ 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, + /** + * 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->factory()->post->create_and_get( + 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 ); + $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( + /** + * 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->factory()->post->create_and_get( array( - 'role' => 'author', - 'user_login' => 'batman', - ) - ); - $author = get_userdata( $author_id ); - $post_id = $this->factory()->post->create( - array( - 'post_author' => $author_id, + 'post_author' => $author->ID, 'post_status' => 'publish', 'post_type' => 'post', ) ); - $this->_cap->add_coauthors( $post_id, array( $author->user_login ) ); + $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( + /** + * 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->factory()->post->create_and_get( array( - 'role' => 'author', - 'user_login' => 'batman', - ) - ); - $author = get_userdata( $author_id ); - $post_id = $this->factory()->post->create( - array( - 'post_author' => $author_id, + 'post_author' => $author->ID, 'post_status' => 'publish', 'post_type' => 'post', ) ); - $this->_cap->add_coauthors( $post_id, array( $author->user_login ) ); + $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( + /** + * 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->factory()->post->create_and_get( array( - 'post_author' => $author1_id, + 'post_author' => $author1->ID, 'post_status' => 'publish', 'post_type' => 'post', ) ); - $this->_cap->add_coauthors( $post_id, array( $author1->user_login, $author2->user_login ) ); + $this->_cap->add_coauthors( $post->ID, array( $author1->user_login, $author2->user_login ) ); $query = new \WP_Query( array( @@ -119,61 +104,47 @@ 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, + /** + * 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->factory()->post->create_and_get( + array( + 'post_author' => $author1->ID, 'post_status' => 'publish', 'post_type' => 'post', ) ); - $this->_cap->add_coauthors( $post_id, array( $author1->user_login, $author2->user_login ) ); + $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( + 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->factory()->post->create_and_get( array( - 'role' => 'author', - 'user_login' => 'batman', - ) - ); - $author = get_userdata( $author_id ); - $post_id = $this->factory()->post->create( - array( - 'post_author' => $author_id, + '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' ); + $this->_cap->add_coauthors( $post->ID, array( $author->user_login ) ); + wp_set_post_terms( $post->ID, 'test' ); $query = new \WP_Query( array( @@ -183,34 +154,21 @@ 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( + 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->factory()->post->create_and_get( array( - 'post_author' => $author1_id, + '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' ); + $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( @@ -220,6 +178,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 ); } } From 2b384c878b669e5a3da582ef81ac5ee818feb807 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sat, 23 Sep 2023 22:35:51 +0100 Subject: [PATCH 04/13] Tests: Move SUT setup from TestCase to test set_up Wasn't being used elsewhere in the test suite. --- tests/Integration/EndpointsTest.php | 7 +++++++ tests/Integration/TestCase.php | 5 ----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/Integration/EndpointsTest.php b/tests/Integration/EndpointsTest.php index 88421761..c272a261 100644 --- a/tests/Integration/EndpointsTest.php +++ b/tests/Integration/EndpointsTest.php @@ -26,12 +26,19 @@ class EndpointsTest extends TestCase { private $coauthor2; private $post; + /** + * @var Endpoints + */ + private $_api; + public function set_up() { parent::set_up(); global $coauthors_plus; + $this->_api = new Endpoints( $coauthors_plus ); + $this->author1 = $this->factory()->user->create_and_get( array( 'role' => 'author', diff --git a/tests/Integration/TestCase.php b/tests/Integration/TestCase.php index 4895dbcf..d57f93ef 100644 --- a/tests/Integration/TestCase.php +++ b/tests/Integration/TestCase.php @@ -13,17 +13,12 @@ class TestCase extends \Yoast\WPTestUtils\WPIntegration\TestCase { * @var CoAuthors_Plus */ protected $_cap; - /** - * @var Endpoints - */ - protected $_api; public function set_up() { parent::set_up(); global $coauthors_plus; $this->_cap = $coauthors_plus; - $this->_api = new Endpoints( $coauthors_plus ); } protected function create_author( $user_login = 'author' ) { From 68c0e931c3f18775d7ef1ba0f798c38e98650fd5 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sat, 23 Sep 2023 23:26:51 +0100 Subject: [PATCH 05/13] Tests: Refactor EndpointTests --- tests/Integration/EndpointsTest.php | 217 ++++++++-------------------- tests/Integration/TestCase.php | 30 +++- 2 files changed, 85 insertions(+), 162 deletions(-) diff --git a/tests/Integration/EndpointsTest.php b/tests/Integration/EndpointsTest.php index c272a261..f13f7cb8 100644 --- a/tests/Integration/EndpointsTest.php +++ b/tests/Integration/EndpointsTest.php @@ -3,28 +3,14 @@ namespace Automattic\CoAuthorsPlus\Tests\Integration; use CoAuthors\API\Endpoints; +use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; /* * @coversDefaultClass \CoAuthors\API\Endpoints */ class EndpointsTest extends TestCase { - use \DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; - - private $author1; - private $author2; - private $editor1; - private $contributor1; - private $subscriber1; - /** - * @var int|WP_Error - */ - private $coauthor1; - /** - * @var int|WP_Error - */ - private $coauthor2; - private $post; + use ArraySubsetAsserts; /** * @var Endpoints @@ -38,65 +24,6 @@ public function set_up() { global $coauthors_plus; $this->_api = new Endpoints( $coauthors_plus ); - - $this->author1 = $this->factory()->user->create_and_get( - array( - 'role' => 'author', - 'user_login' => 'author1', - ) - ); - - $this->author2 = $this->factory()->user->create_and_get( - array( - 'role' => 'author', - 'user_login' => 'author2', - ) - ); - - $this->editor1 = $this->factory()->user->create_and_get( - array( - 'role' => 'editor', - 'user_login' => 'editor1', - ) - ); - - $this->contributor1 = $this->factory()->user->create_and_get( - array( - 'role' => 'contributor', - 'user_login' => 'contributor1', - ) - ); - - $this->subscriber1 = $this->factory()->user->create_and_get( - array( - 'role' => 'subscriber', - 'user_login' => 'subscriber1', - ) - ); - - $this->coauthor1 = $coauthors_plus->guest_authors->create( - array( - 'user_login' => 'coauthor1', - 'display_name' => 'coauthor1', - ) - ); - - $this->coauthor2 = $coauthors_plus->guest_authors->create( - array( - 'user_login' => 'coauthor2', - 'display_name' => 'coauthor2', - ) - ); - - $this->post = $this->factory()->post->create_and_get( - array( - 'post_author' => $this->author1->ID, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => 'post', - ) - ); } /** @@ -191,12 +118,16 @@ public function test_add_endpoints() { * @covers \CoAuthors\API\Endpoints::get_coauthors_search_results */ public function test_get_coauthors_search_results() { + $author1 = $this->create_author( 'author1' ); + $author2 = $this->create_author( 'author2' ); + $guest_author1 = $this->create_guest_author( 'guest_author1' ); + $guest_author2 = $this->create_guest_author( 'guest_author2' ); $get_request = new \WP_REST_Request( 'GET' ); $get_request->set_url_params( array( 'q' => 'auth', - 'existing_authors' => 'author1,coauthor2', + 'existing_authors' => 'author1,guest_author2', ) ); @@ -208,7 +139,7 @@ public function test_get_coauthors_search_results() { 'displayName' => 'author2', ), array( - 'displayName' => 'coauthor1', + 'displayName' => 'guest_author1', ), ), $get_response->data, @@ -235,9 +166,10 @@ public function test_get_coauthors_search_results() { * @covers \CoAuthors\API\Endpoints::get_coauthors */ public function test_authors_get_coauthors() { - $test_post = $this->factory()->post->create_and_get( + $author = $this->create_author(); + $post = $this->factory()->post->create_and_get( array( - 'post_author' => $this->author1->ID, + 'post_author' => $author->ID, 'post_status' => 'publish', 'post_content' => rand_str(), 'post_title' => rand_str(), @@ -245,44 +177,40 @@ public function test_authors_get_coauthors() { ) ); - $test_post_id = $test_post->ID; - $get_request = new \WP_REST_Request( 'GET' ); $get_request->set_url_params( array( - 'post_id' => $test_post_id, + 'post_id' => $post->ID, ) ); $get_response = $this->_api->get_coauthors( $get_request ); - $this->assertEquals( 'author1', $get_response->data[0]['userNicename'] ); - + $this->assertEquals( 'author', $get_response->data[0]['userNicename'] ); } /** * @covers \CoAuthors\API\Endpoints::update_coauthors */ public function test_update_coauthors() { - - wp_set_current_user( $this->editor1->ID ); - - $test_post = $this->factory()->post->create_and_get( + $author = $this->create_author(); + $editor = $this->create_editor(); + $guest_author = $this->create_guest_author(); + $post = $this->factory()->post->create_and_get( array( - 'post_author' => $this->author1->ID, + 'post_author' => $author->ID, 'post_status' => 'publish', 'post_content' => rand_str(), 'post_title' => rand_str(), 'post_type' => 'post', ) ); - - $test_post_id = $test_post->ID; + wp_set_current_user( $editor->ID ); $post_request = new \WP_REST_Request( 'POST' ); $post_request->set_url_params( array( - 'post_id' => $test_post_id, - 'new_authors' => $this->author1->user_nicename . ',coauthor2', + 'post_id' => $post->ID, + 'new_authors' => $author->user_nicename . ',guest_author', ) ); @@ -291,91 +219,62 @@ public function test_update_coauthors() { $this->assertCount( 2, $update_response->data ); } - public function test_can_edit_coauthors() { - $post_id = $this->factory()->post->create( - array( - 'post_author' => $this->editor1->ID, - ) - ); - - $request = new \WP_REST_Request( - 'GET', - '' + public function data_only_editor_role_can_edit_coauthors() { + return array( + 'Subscriber' => array( + 'subscriber', + false + ), + 'Contributor' => array( + 'contributor', + false + ), + 'Author' => array( + 'author', + false + ), + 'Editor' => array( + 'editor', + true + ), ); - - wp_set_current_user( $this->editor1->ID ); - - $this->assertTrue( $this->_api->can_edit_coauthors() ); - - wp_set_current_user( $this->author1->ID ); - - $this->assertFalse( $this->_api->can_edit_coauthors() ); - - wp_set_current_user( $this->contributor1->ID ); - - $this->assertFalse( $this->_api->can_edit_coauthors() ); - - wp_set_current_user( $this->subscriber1->ID ); - - $this->assertFalse( $this->_api->can_edit_coauthors() ); } - public function test_can_edit_coauthors__with_post_param() { - $post_id = $this->factory()->post->create( - array( - 'post_author' => $this->editor1->ID, - ) - ); - - $request = new \WP_REST_Request( - 'GET', - '' - ); - $request->set_default_params( - array( - 'post_id' => $post_id, - ) - ); - - wp_set_current_user( $this->editor1->ID ); - - $this->assertTrue( $this->_api->can_edit_coauthors() ); - - wp_set_current_user( $this->author1->ID ); - - $this->assertFalse( $this->_api->can_edit_coauthors() ); - - wp_set_current_user( $this->contributor1->ID ); - - $this->assertFalse( $this->_api->can_edit_coauthors() ); - - wp_set_current_user( $this->subscriber1->ID ); - - $this->assertFalse( $this->_api->can_edit_coauthors() ); + /** + * @dataProvider data_only_editor_role_can_edit_coauthors + * @param $role_name + * @param $outcome + * + * @return void + */ + public function test_which_role_can_edit_coauthors( $role_name, $outcome ) { + $role = $this->{"create_$role_name"}(); + wp_set_current_user( $role->ID ); + $this->assertEquals( $outcome, $this->_api->can_edit_coauthors() ); } /** * @covers \CoAuthors\API\Endpoints::remove_author_link */ public function test_remove_author_link() { - - $test_post = $this->factory()->post->create_and_get( + $editor = $this->create_editor(); + $post = $this->factory()->post->create_and_get( array( - 'post_author' => $this->editor1->ID, + 'post_author' => $editor->ID, 'post_status' => 'publish', 'post_type' => 'post', ) ); - $request = new \WP_REST_Request( 'GET', '/wp/v2/posts/' . $test_post->ID ); + $request = new \WP_REST_Request( 'GET', '/wp/v2/posts/' . $post->ID ); - wp_set_current_user( $this->editor1->ID ); + wp_set_current_user( $editor->ID ); $request->set_param( 'context', 'edit' ); $response = rest_do_request( $request ); - $this->_api->remove_author_link( $response, $test_post, $request ); + $this->_api->remove_author_link( $response, $post, $request ); $this->assertArrayNotHasKey( Endpoints::SUPPORT_LINK, @@ -387,14 +286,13 @@ public function test_remove_author_link() { $response = rest_do_request( $request ); - $this->_api->remove_author_link( $response, $test_post, $request ); + $this->_api->remove_author_link( $response, $post, $request ); $this->assertArrayHasKey( Endpoints::SUPPORT_LINK, $response->get_links(), 'Failed to assert that links are unchanged when block editor is disabled.' ); - } /** @@ -415,6 +313,5 @@ public function test_modify_response() { ) ); } - } } diff --git a/tests/Integration/TestCase.php b/tests/Integration/TestCase.php index d57f93ef..6042f674 100644 --- a/tests/Integration/TestCase.php +++ b/tests/Integration/TestCase.php @@ -2,8 +2,6 @@ namespace Automattic\CoAuthorsPlus\Tests\Integration; -use CoAuthors\API\Endpoints; - /** * Base unit test class for Co-Authors Plus */ @@ -21,6 +19,24 @@ public function set_up() { $this->_cap = $coauthors_plus; } + protected function create_subscriber( $user_login = 'subscriber' ) { + return $this->factory()->user->create_and_get( + array( + 'role' => 'subscriber', + 'user_login' => $user_login, + ) + ); + } + + protected function create_contributor( $user_login = 'contributor' ) { + return $this->factory()->user->create_and_get( + array( + 'role' => 'contributor', + 'user_login' => $user_login, + ) + ); + } + protected function create_author( $user_login = 'author' ) { return $this->factory()->user->create_and_get( array( @@ -38,4 +54,14 @@ protected function create_editor( $user_login = 'editor' ) { ) ); } + + protected function create_guest_author( $user_login = 'guest_author' ) { + global $coauthors_plus; + return $coauthors_plus->guest_authors->create( + array( + 'display_name' => $user_login, + 'user_login' => $user_login, + ) + ); + } } From 2b28c47b974a45c6aed5f2be58158baad81c1a5b Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Wed, 27 Sep 2023 17:03:05 +0100 Subject: [PATCH 06/13] Composer: update yoast/wp-test-utils Allows the use of `assertObjectHasProperty`. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2b776753..935b0b97 100644 --- a/composer.json +++ b/composer.json @@ -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": [ From 64d5fb092f281545eb30c9ed321844a33a536d05 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Wed, 27 Sep 2023 17:03:48 +0100 Subject: [PATCH 07/13] Tests: Use assetObjectHasProperty assertion --- tests/Integration/CoAuthorsPlusTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Integration/CoAuthorsPlusTest.php b/tests/Integration/CoAuthorsPlusTest.php index e6a52473..7468a041 100644 --- a/tests/Integration/CoAuthorsPlusTest.php +++ b/tests/Integration/CoAuthorsPlusTest.php @@ -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 ); } @@ -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 ); } @@ -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' ); @@ -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 ); } From 239c0398ab45fe02e686b1a0bcba87b4515f66d1 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sun, 1 Oct 2023 16:12:36 +0100 Subject: [PATCH 08/13] Tests: Refactor is_coauthor_for_post tests --- .../TemplateTags/IsCoauthorForPostTest.php | 118 ++++++++++++++++++ tests/Integration/TemplateTagsTest.php | 81 ------------ 2 files changed, 118 insertions(+), 81 deletions(-) create mode 100644 tests/Integration/TemplateTags/IsCoauthorForPostTest.php diff --git a/tests/Integration/TemplateTags/IsCoauthorForPostTest.php b/tests/Integration/TemplateTags/IsCoauthorForPostTest.php new file mode 100644 index 00000000..ee45f028 --- /dev/null +++ b/tests/Integration/TemplateTags/IsCoauthorForPostTest.php @@ -0,0 +1,118 @@ +create_author(); + $post = $this->factory()->post->create_and_get( + array( + 'post_author' => $author->ID, + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => 'post', + ) + ); + $user = new \WP_User(); + + $this->assertFalse( is_coauthor_for_post( 1234, $post->ID ), 'User ID should not exist.' ); + $this->assertFalse( is_coauthor_for_post( 0, $post->ID ), 'User ID 0 should not be valid.' ); + $this->assertFalse( is_coauthor_for_post( $user, $post->ID ), 'User_login should not be set.' ); + } + + /** + * Checks whether a user is a coauthor of a non-existent post. + */ + public function test_return_false_when_post_does_not_exist() { + $author = $this->create_author(); + $post = $this->factory()->post->create_and_get( + array( + 'post_author' => $author->ID, + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => 'post', + ) + ); + + $this->assertFalse( is_coauthor_for_post( $author->ID, '1234' ) ); + } + + /** + * Checks whether a user is a coauthor of a non-existent post. + */ + public function test_return_false_when_post_is_not_provided_and_global_post_is_not_set() { + $author = $this->create_author(); + unset( $GLOBALS['post'] ); + + $this->assertFalse( is_coauthor_for_post( $author->ID ) ); + } + + /** + * Checks whether user is a coauthor of the post when user is set in either way, + * as user_id or user object but he/she is not coauthor of the post. + */ + public function test_is_coauthor_for_post_when_user_numeric_or_user_login_set_but_not_coauthor() { + $author = $this->create_author(); + $editor = $this->create_editor(); + $post = $this->factory()->post->create_and_get( + array( + 'post_author' => $author->ID, + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => 'post', + ) + ); + + $this->assertFalse( is_coauthor_for_post( $editor->ID, $post->ID ) ); + $this->assertFalse( is_coauthor_for_post( $editor, $post->ID ) ); + } + + /** + * Checks whether user is a coauthor of the post. + */ + public function test_is_coauthor_for_post_when_user_is_coauthor() { + $author = $this->create_author(); + $editor = $this->create_editor(); + $post = $this->factory()->post->create_and_get( + array( + 'post_author' => $author->ID, + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => 'post', + ) + ); + + global $coauthors_plus; + + // Checking with specific post and user_id as well as user object. + $this->assertTrue( is_coauthor_for_post( $author->ID, $post->ID ) ); + $this->assertTrue( is_coauthor_for_post( $author, $post->ID ) ); + + $coauthors_plus->add_coauthors( $post->ID, array( $editor->user_login ), true ); + + $this->assertTrue( is_coauthor_for_post( $editor->ID, $post->ID ) ); + $this->assertTrue( is_coauthor_for_post( $editor, $post->ID ) ); + + // Now checking with global post and user_id as well as user object. + $GLOBALS['post'] = $post; + + $this->assertTrue( is_coauthor_for_post( $author->ID ) ); + $this->assertTrue( is_coauthor_for_post( $author ) ); + + $this->assertTrue( is_coauthor_for_post( $editor->ID ) ); + $this->assertTrue( is_coauthor_for_post( $editor ) ); + } +} diff --git a/tests/Integration/TemplateTagsTest.php b/tests/Integration/TemplateTagsTest.php index abf42fe9..d5fb48bf 100644 --- a/tests/Integration/TemplateTagsTest.php +++ b/tests/Integration/TemplateTagsTest.php @@ -202,87 +202,6 @@ public function test_coauthors_order() { $this->assertEquals( $expected, wp_list_pluck( get_coauthors( $post_id ), 'user_login' ) ); } - /** - * Checks whether user is a coauthor of the post when user or post not exists. - * - * @covers ::is_coauthor_for_post() - */ - public function test_is_coauthor_for_post_when_user_or_post_not_exists() { - - global $post; - - // Backing up global post. - $post_backup = $post; - - $this->assertFalse( is_coauthor_for_post( '' ) ); - $this->assertFalse( is_coauthor_for_post( '', $this->post->ID ) ); - $this->assertFalse( is_coauthor_for_post( $this->author1->ID ) ); - - $post = $this->post; - - $this->assertFalse( is_coauthor_for_post( '' ) ); - - // Restore global post from backup. - $post = $post_backup; - } - - /** - * Checks whether user is a coauthor of the post when user is not expected as ID, - * or user_login is not set in user object. - * - * @covers ::is_coauthor_for_post() - */ - public function test_is_coauthor_for_post_when_user_not_numeric_or_user_login_not_set() { - - $this->assertFalse( is_coauthor_for_post( 'test' ) ); - } - - /** - * Checks whether user is a coauthor of the post when user is set in either way, - * as user_id or user object but he/she is not coauthor of the post. - * - * @covers ::is_coauthor_for_post() - */ - public function test_is_coauthor_for_post_when_user_numeric_or_user_login_set_but_not_coauthor() { - - $this->assertFalse( is_coauthor_for_post( $this->editor1->ID, $this->post->ID ) ); - $this->assertFalse( is_coauthor_for_post( $this->editor1, $this->post->ID ) ); - } - - /** - * Checks whether user is a coauthor of the post. - * - * @covers ::is_coauthor_for_post() - */ - public function test_is_coauthor_for_post_when_user_is_coauthor() { - - global $post, $coauthors_plus; - - // Backing up global post. - $post_backup = $post; - - // Checking with specific post and user_id as well ass user object. - $this->assertTrue( is_coauthor_for_post( $this->author1->ID, $this->post->ID ) ); - $this->assertTrue( is_coauthor_for_post( $this->author1, $this->post->ID ) ); - - $coauthors_plus->add_coauthors( $this->post->ID, array( $this->editor1->user_login ), true ); - - $this->assertTrue( is_coauthor_for_post( $this->editor1->ID, $this->post->ID ) ); - $this->assertTrue( is_coauthor_for_post( $this->editor1, $this->post->ID ) ); - - // Now checking with global post and user_id as well ass user object. - $post = $this->post; - - $this->assertTrue( is_coauthor_for_post( $this->author1->ID ) ); - $this->assertTrue( is_coauthor_for_post( $this->author1 ) ); - - $this->assertTrue( is_coauthor_for_post( $this->editor1->ID ) ); - $this->assertTrue( is_coauthor_for_post( $this->editor1 ) ); - - // Restore global post from backup. - $post = $post_backup; - } - /** * Tests for co-authors display names, without links to their posts. * From d1cb44818fd70f367ce5f119fe4e802d44dfb076 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sun, 1 Oct 2023 16:15:30 +0100 Subject: [PATCH 09/13] Fix is_coauthor_for_post() for invalid user ID If a numeric user ID is passed then there was a presumption that a User object was going to be returned from `get_userdata()`. However, when that user does not exist, then the code was trying to get a property from a `false` boolean value. --- template-tags.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/template-tags.php b/template-tags.php index 26fe9180..95a41f1a 100644 --- a/template-tags.php +++ b/template-tags.php @@ -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 { From 4d2195638ba51c6f8454c59b10d961a53484645d Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sun, 1 Oct 2023 17:05:28 +0100 Subject: [PATCH 10/13] Tests: Refactor get_coauthors() tests --- .../TemplateTags/GetCoauthorsTest.php | 151 ++++++++++++++++++ tests/Integration/TemplateTagsTest.php | 106 ------------ 2 files changed, 151 insertions(+), 106 deletions(-) create mode 100644 tests/Integration/TemplateTags/GetCoauthorsTest.php diff --git a/tests/Integration/TemplateTags/GetCoauthorsTest.php b/tests/Integration/TemplateTags/GetCoauthorsTest.php new file mode 100644 index 00000000..9c7401e8 --- /dev/null +++ b/tests/Integration/TemplateTags/GetCoauthorsTest.php @@ -0,0 +1,151 @@ +assertEmpty( get_coauthors() ); + } + + /** + * Checks coauthors when post exist (not global). + */ + public function test_get_coauthors_when_post_exists() { + $author = $this->create_author(); + $editor = $this->create_editor(); + $post = $this->factory()->post->create_and_get( + array( + 'post_author' => $author->ID, + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => 'post', + ) + ); + + global $coauthors_plus; + + // Compare single author. + $this->assertEquals( array( $author->ID ), wp_list_pluck( get_coauthors( $post->ID ), 'ID' ) ); + + // Compare multiple authors. + $coauthors_plus->add_coauthors( $post->ID, array( $editor->user_login ), true ); + $this->assertEquals( + array( + $author->ID, + $editor->ID, + ), + wp_list_pluck( get_coauthors( $post->ID ), 'ID' ) + ); + } + + /** + * Checks coauthors when terms for post not exist. + */ + public function test_get_coauthors_when_terms_for_post_not_exists() { + + $post_id = $this->factory()->post->create(); + $this->assertEmpty( get_coauthors( $post_id ) ); + } + + /** + * Checks coauthors when post not exist but global post does. + */ + public function test_get_coauthors_when_global_post_exists() { + + global $post; + + // Backing up global post. + $post_backup = $post; + + $post = $this->factory()->post->create_and_get(); + + $this->assertEmpty( get_coauthors() ); + + $user_id = $this->factory()->user->create(); + $post = $this->factory()->post->create_and_get( + array( + 'post_author' => $user_id, + ) + ); + + $this->assertEquals( array( $user_id ), wp_list_pluck( get_coauthors(), 'ID' ) ); + + // Restore global post from backup. + $post = $post_backup; + + } + + /** + * Checks coauthors when post not exist but global post_ID does. + */ + public function test_get_coauthors_when_global_post_id_exists() { + + global $post_ID; + + // Backing up global post_ID. + $post_ID_backup = $post_ID; + + $post = $this->factory()->post->create_and_get(); + + $GLOBALS['post_ID'] = $post->ID; + + $this->assertEmpty( get_coauthors() ); + + $user_id = $this->factory()->user->create(); + $post = $this->factory()->post->create_and_get( + array( + 'post_author' => $user_id, + ) + ); + + $GLOBALS['post_ID'] = $post->ID; + + $this->assertEquals( array( $user_id ), wp_list_pluck( get_coauthors(), 'ID' ) ); + + // Restore global post from backup. + $post_ID = $post_ID_backup; + } + + /** + * Checks coauthors order. + */ + public function test_coauthors_order() { + + global $coauthors_plus; + + $author = $this->create_author(); + $editor = $this->create_editor(); + $post_id = $this->factory()->post->create(); + + // Checks when no author exist. + $this->assertEmpty( get_coauthors( $post_id ) ); + + // Checks coauthors order. + $coauthors_plus->add_coauthors( $post_id, array( $author->user_login ), true ); + $coauthors_plus->add_coauthors( $post_id, array( $editor->user_login ), true ); + + $expected = array( $author->user_login, $editor->user_login ); + + $this->assertEquals( $expected, wp_list_pluck( get_coauthors( $post_id ), 'user_login' ) ); + + // Checks coauthors order after modifying. + $post_id = $this->factory()->post->create(); + + $coauthors_plus->add_coauthors( $post_id, array( $editor->user_login ), true ); + $coauthors_plus->add_coauthors( $post_id, array( $author->user_login ), true ); + + $expected = array( $editor->user_login, $author->user_login ); + + $this->assertEquals( $expected, wp_list_pluck( get_coauthors( $post_id ), 'user_login' ) ); + } +} diff --git a/tests/Integration/TemplateTagsTest.php b/tests/Integration/TemplateTagsTest.php index d5fb48bf..3d873fa9 100644 --- a/tests/Integration/TemplateTagsTest.php +++ b/tests/Integration/TemplateTagsTest.php @@ -96,112 +96,6 @@ public function test_coauthors_links() { $GLOBALS['post'] = $post_backup; } - /** - * Checks coauthors when post not exist. - * - * @covers ::get_coauthors() - */ - public function test_get_coauthors_when_post_not_exists() { - - $this->assertEmpty( get_coauthors() ); - } - - /** - * Checks coauthors when post exist (not global). - * - * @covers ::get_coauthors() - */ - public function test_get_coauthors_when_post_exists() { - - global $coauthors_plus; - - // Compare single author. - $this->assertEquals( array( $this->author1->ID ), wp_list_pluck( get_coauthors( $this->post->ID ), 'ID' ) ); - - // Compare multiple authors. - $coauthors_plus->add_coauthors( $this->post->ID, array( $this->editor1->user_login ), true ); - $this->assertEquals( - array( - $this->author1->ID, - $this->editor1->ID, - ), - wp_list_pluck( get_coauthors( $this->post->ID ), 'ID' ) - ); - } - - /** - * Checks coauthors when terms for post not exist. - * - * @covers ::get_coauthors() - */ - public function test_get_coauthors_when_terms_for_post_not_exists() { - - $post_id = $this->factory()->post->create(); - $this->assertEmpty( get_coauthors( $post_id ) ); - } - - /** - * Checks coauthors when post not exist. - * - * @covers ::get_coauthors() - */ - public function test_get_coauthors_when_global_post_exists() { - - global $post; - - // Backing up global post. - $post_backup = $post; - - $post = $this->factory()->post->create_and_get(); - - $this->assertEmpty( get_coauthors() ); - - $user_id = $this->factory()->user->create(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $user_id, - ) - ); - - $this->assertEquals( array( $user_id ), wp_list_pluck( get_coauthors(), 'ID' ) ); - - // Restore global post from backup. - $post = $post_backup; - } - - /** - * Checks coauthors order. - * - * @covers ::get_coauthors() - */ - public function test_coauthors_order() { - - global $coauthors_plus; - - $post_id = $this->factory()->post->create(); - - // Checks when no author exist. - $this->assertEmpty( get_coauthors( $post_id ) ); - - // Checks coauthors order. - $coauthors_plus->add_coauthors( $post_id, array( $this->author1->user_login ), true ); - $coauthors_plus->add_coauthors( $post_id, array( $this->editor1->user_login ), true ); - - $expected = array( $this->author1->user_login, $this->editor1->user_login ); - - $this->assertEquals( $expected, wp_list_pluck( get_coauthors( $post_id ), 'user_login' ) ); - - // Checks coauthors order after modifying. - $post_id = $this->factory()->post->create(); - - $coauthors_plus->add_coauthors( $post_id, array( $this->editor1->user_login ), true ); - $coauthors_plus->add_coauthors( $post_id, array( $this->author1->user_login ), true ); - - $expected = array( $this->editor1->user_login, $this->author1->user_login ); - - $this->assertEquals( $expected, wp_list_pluck( get_coauthors( $post_id ), 'user_login' ) ); - } - /** * Tests for co-authors display names, without links to their posts. * From 076d259d36a88538cb61da8e7f593150b38c8c61 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sun, 1 Oct 2023 17:54:45 +0100 Subject: [PATCH 11/13] Tests: Add create_post() test helper method --- tests/Integration/AuthorQueriesTest.php | 56 +++---------------- tests/Integration/EndpointsTest.php | 28 +--------- .../TemplateTags/CoauthorsPostsLinksTest.php | 30 +--------- .../TemplateTags/GetCoauthorsTest.php | 10 +--- .../TemplateTags/IsCoauthorForPostTest.php | 40 ++----------- tests/Integration/TestCase.php | 15 +++++ 6 files changed, 33 insertions(+), 146 deletions(-) diff --git a/tests/Integration/AuthorQueriesTest.php b/tests/Integration/AuthorQueriesTest.php index 41b3a59f..74aa5a00 100644 --- a/tests/Integration/AuthorQueriesTest.php +++ b/tests/Integration/AuthorQueriesTest.php @@ -10,13 +10,7 @@ class AuthorQueriesTest extends TestCase { */ public function test_get_post_by_user_login_when_single_author_is_set_as_post_author() { $author = $this->create_author(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); $this->_cap->add_coauthors( $post->ID, array( $author->user_login ) ); $query = new \WP_Query( @@ -35,13 +29,7 @@ public function test_get_post_by_user_login_when_single_author_is_set_as_post_au */ 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->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); $this->_cap->add_coauthors( $post->ID, array( $author->user_login ) ); $query = new \WP_Query( @@ -60,13 +48,7 @@ public function test_get_post_by_user_ID_when_single_author_is_set_as_post_autho */ 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->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_type' => 'post', - ) - ); + $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. @@ -88,13 +70,7 @@ public function test_get_post_by_user_ID_when_single_author_is_set_as_post_autho 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->factory()->post->create_and_get( - array( - 'post_author' => $author1->ID, - 'post_status' => 'publish', - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author1 ); $this->_cap->add_coauthors( $post->ID, array( $author1->user_login, $author2->user_login ) ); $query = new \WP_Query( @@ -115,13 +91,7 @@ public function test_get_post_by_user_ID_when_queried_author_is_set_as_post_auth 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->factory()->post->create_and_get( - array( - 'post_author' => $author1->ID, - 'post_status' => 'publish', - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author1 ); $this->_cap->add_coauthors( $post->ID, array( $author1->user_login, $author2->user_login ) ); $query = new \WP_Query( @@ -136,13 +106,7 @@ public function test_get_post_by_user_ID_when_queried_author_is_not_set_as_post_ 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->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); $this->_cap->add_coauthors( $post->ID, array( $author->user_login ) ); wp_set_post_terms( $post->ID, 'test' ); @@ -160,13 +124,7 @@ public function test_get_post_by_user_ID_when_queried_author_is_set_as_post_auth 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->factory()->post->create_and_get( - array( - 'post_author' => $author1->ID, - 'post_status' => 'publish', - 'post_type' => 'post', - ) - ); + $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' ); diff --git a/tests/Integration/EndpointsTest.php b/tests/Integration/EndpointsTest.php index f13f7cb8..55004353 100644 --- a/tests/Integration/EndpointsTest.php +++ b/tests/Integration/EndpointsTest.php @@ -167,15 +167,7 @@ public function test_get_coauthors_search_results() { */ public function test_authors_get_coauthors() { $author = $this->create_author(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); $get_request = new \WP_REST_Request( 'GET' ); $get_request->set_url_params( @@ -195,15 +187,7 @@ public function test_update_coauthors() { $author = $this->create_author(); $editor = $this->create_editor(); $guest_author = $this->create_guest_author(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); wp_set_current_user( $editor->ID ); $post_request = new \WP_REST_Request( 'POST' ); @@ -258,13 +242,7 @@ public function test_which_role_can_edit_coauthors( $role_name, $outcome ) { */ public function test_remove_author_link() { $editor = $this->create_editor(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $editor->ID, - 'post_status' => 'publish', - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $editor ); $request = new \WP_REST_Request( 'GET', '/wp/v2/posts/' . $post->ID ); diff --git a/tests/Integration/TemplateTags/CoauthorsPostsLinksTest.php b/tests/Integration/TemplateTags/CoauthorsPostsLinksTest.php index eeeb30a9..4c15623a 100644 --- a/tests/Integration/TemplateTags/CoauthorsPostsLinksTest.php +++ b/tests/Integration/TemplateTags/CoauthorsPostsLinksTest.php @@ -26,15 +26,7 @@ public function test_the_author_filter_is_retained() { */ public function test_coauthors_posts_links_for_single_author() { $author = $this->create_author(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); $GLOBALS['post'] = $post; $coauthors_posts_links = coauthors_posts_links( null, null, null, null, false ); @@ -54,15 +46,7 @@ public function test_coauthors_posts_links_for_multiple_authors_with_default_arg $author = $this->create_author(); $editor = $this->create_editor(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); $GLOBALS['post'] = $post; $coauthors_plus->add_coauthors( $post->ID, array( $editor->user_login ), true ); @@ -84,15 +68,7 @@ public function test_coauthors_posts_links_for_multiple_authors_with_amended_arg $author1 = $this->create_author( 'author1' ); $author2 = $this->create_author( 'author2' ); $editor = $this->create_editor(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $author1->ID, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author1 ); $GLOBALS['post'] = $post; $coauthors_plus->add_coauthors( $post->ID, array( $author2->user_login, $editor->user_login ), true ); diff --git a/tests/Integration/TemplateTags/GetCoauthorsTest.php b/tests/Integration/TemplateTags/GetCoauthorsTest.php index 9c7401e8..f59b2f57 100644 --- a/tests/Integration/TemplateTags/GetCoauthorsTest.php +++ b/tests/Integration/TemplateTags/GetCoauthorsTest.php @@ -22,15 +22,7 @@ public function test_get_coauthors_when_post_not_exists() { public function test_get_coauthors_when_post_exists() { $author = $this->create_author(); $editor = $this->create_editor(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); global $coauthors_plus; diff --git a/tests/Integration/TemplateTags/IsCoauthorForPostTest.php b/tests/Integration/TemplateTags/IsCoauthorForPostTest.php index ee45f028..4a66bc87 100644 --- a/tests/Integration/TemplateTags/IsCoauthorForPostTest.php +++ b/tests/Integration/TemplateTags/IsCoauthorForPostTest.php @@ -14,15 +14,7 @@ class IsCoauthorForPostTest extends TestCase { */ public function test_return_false_when_user_is_not_valid() { $author = $this->create_author(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); $user = new \WP_User(); $this->assertFalse( is_coauthor_for_post( 1234, $post->ID ), 'User ID should not exist.' ); @@ -35,15 +27,7 @@ public function test_return_false_when_user_is_not_valid() { */ public function test_return_false_when_post_does_not_exist() { $author = $this->create_author(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); $this->assertFalse( is_coauthor_for_post( $author->ID, '1234' ) ); } @@ -65,15 +49,7 @@ public function test_return_false_when_post_is_not_provided_and_global_post_is_n public function test_is_coauthor_for_post_when_user_numeric_or_user_login_set_but_not_coauthor() { $author = $this->create_author(); $editor = $this->create_editor(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); $this->assertFalse( is_coauthor_for_post( $editor->ID, $post->ID ) ); $this->assertFalse( is_coauthor_for_post( $editor, $post->ID ) ); @@ -85,15 +61,7 @@ public function test_is_coauthor_for_post_when_user_numeric_or_user_login_set_bu public function test_is_coauthor_for_post_when_user_is_coauthor() { $author = $this->create_author(); $editor = $this->create_editor(); - $post = $this->factory()->post->create_and_get( - array( - 'post_author' => $author->ID, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => 'post', - ) - ); + $post = $this->create_post( $author ); global $coauthors_plus; diff --git a/tests/Integration/TestCase.php b/tests/Integration/TestCase.php index 6042f674..cbbd9e40 100644 --- a/tests/Integration/TestCase.php +++ b/tests/Integration/TestCase.php @@ -64,4 +64,19 @@ protected function create_guest_author( $user_login = 'guest_author' ) { ) ); } + + protected function create_post( \WP_User $author = null ) { + if ( null === $author ) { + $author = $this->create_author(); + } + return $this->factory()->post->create_and_get( + array( + 'post_author' => $author->ID, + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => 'post', + ) + ); + } } From d891960e266ceee23cd9235de712d293507ad2f1 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sun, 1 Oct 2023 18:05:41 +0100 Subject: [PATCH 12/13] Tests: Refactor coauthors_wp_list_authors() tests --- .../CoauthorsWpListAuthorsTest.php | 260 ++++++++++++++++++ tests/Integration/TemplateTagsTest.php | 249 ----------------- 2 files changed, 260 insertions(+), 249 deletions(-) create mode 100644 tests/Integration/TemplateTags/CoauthorsWpListAuthorsTest.php diff --git a/tests/Integration/TemplateTags/CoauthorsWpListAuthorsTest.php b/tests/Integration/TemplateTags/CoauthorsWpListAuthorsTest.php new file mode 100644 index 00000000..88feaf34 --- /dev/null +++ b/tests/Integration/TemplateTags/CoauthorsWpListAuthorsTest.php @@ -0,0 +1,260 @@ +create_author(); + $editor = $this->create_editor(); + $post = $this->create_post( $author ); + + $args = array( + 'echo' => false, + ); + + // Because the author has a published post, but the editor doesn't, the author would be included but not the editor. + $coauthors = coauthors_wp_list_authors( $args ); + + $this->assertStringContainsString( 'href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '"', $coauthors, 'Author link not found.' ); + $this->assertStringContainsString( $author->display_name, $coauthors, 'Author name not found.' ); + + $this->assertStringNotContainsString( 'href="' . get_author_posts_url( $editor->ID, $editor->user_nicename ) . '"', $coauthors ); + $this->assertStringNotContainsString( $editor->display_name, $coauthors ); + + // Now add the editor as a co-author to a post. + $coauthors_plus->add_coauthors( $post->ID, array( $editor->user_login ), true ); + + $coauthors = coauthors_wp_list_authors( $args ); + + $this->assertStringContainsString( 'href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '"', $coauthors, 'Main author link not found.' ); + $this->assertStringContainsString( $author->display_name, $coauthors, 'Main author name not found.' ); + + // Here we are checking author name should not be more than one time. + // Asserting ">{$author->display_name}<" because "$author->display_name" can be multiple times like in href, title, etc. + $this->assertEquals( 1, substr_count( $coauthors, ">{$author->display_name}<" ) ); + + $this->assertStringContainsString( '
  • ', $coauthors, 'Co-authors name separator is not matched.' ); + $this->assertStringContainsString( 'href="' . get_author_posts_url( $editor->ID, $editor->user_nicename ) . '"', $coauthors, 'Co-author link not found.' ); + $this->assertStringContainsString( $editor->display_name, $coauthors, 'Co-author name not found.' ); + + // Here we are checking editor name should not be more than one time. + // Asserting ">{$editor->display_name}<" because "$editor->display_name" can be multiple times like in href, title, etc. + $this->assertEquals( 1, substr_count( $coauthors, ">{$editor->display_name}<" ) ); + } + + /** + * Checks all the co-authors of the blog with optioncount option. + */ + public function test_list_authors_with_optioncount_arg_enabled() { + $author = $this->create_author(); + $post = $this->create_post( $author ); + + $this->assertStringContainsString( + '(' . count_user_posts( $author->ID ) . ')', + coauthors_wp_list_authors( + array( + 'echo' => false, + 'optioncount' => true, + ) + ) + ); + } + + /** + * Checks all the co-authors of the blog with show_fullname option. + */ + public function test_list_authors_with_show_fullname_arg_enabled() { + $author = $this->create_author(); + $post = $this->create_post( $author ); + + $args = array( + 'echo' => false, + 'show_fullname' => true, + ); + + $this->assertStringContainsString( $author->display_name, coauthors_wp_list_authors( $args ) ); + + $user = $this->factory()->user->create_and_get( + array( + 'first_name' => 'First', + 'last_name' => 'Last', + ) + ); + + $this->factory()->post->create( + array( + 'post_author' => $user->ID, + ) + ); + + $this->assertStringContainsString( "{$user->user_firstname} {$user->user_lastname}", coauthors_wp_list_authors( $args ) ); + } + + /** + * Checks all the co-authors of the blog with hide_empty option. + */ + public function test_list_authors_with_hide_empty_arg_enabled() { + + global $coauthors_plus; + + $coauthors_plus->guest_authors->create( + array( + 'user_login' => 'author2', + 'display_name' => 'author2', + ) + ); + + $this->assertStringContainsString( + 'author2', + coauthors_wp_list_authors( + array( + 'echo' => false, + 'hide_empty' => false, + ) + ) + ); + } + + /** + * Checks all the co-authors of the blog with feed option. + */ + public function test_list_authors_with_feed_arg_enabled() { + $author = $this->create_author(); + $post = $this->create_post( $author ); + + $feed_text = 'link to feed'; + $coauthors = coauthors_wp_list_authors( + array( + 'echo' => false, + 'feed' => $feed_text, + ) + ); + + $this->assertStringContainsString( esc_url( get_author_feed_link( $author->ID ) ), $coauthors ); + $this->assertStringContainsString( $feed_text, $coauthors ); + } + + /** + * Checks all the co-authors of the blog with feed_image option. + */ + public function test_list_authors_with_feed_image_arg_enabled() { + $author = $this->create_author(); + $post = $this->create_post( $author ); + + $feed_image = WP_TESTS_DOMAIN . '/path/to/a/graphic.png'; + $coauthors = coauthors_wp_list_authors( + array( + 'echo' => false, + 'feed_image' => $feed_image, + ) + ); + + $this->assertStringContainsString( esc_url( get_author_feed_link( $author->ID ) ), $coauthors ); + $this->assertStringContainsString( $feed_image, $coauthors ); + } + + /** + * Checks all the co-authors of the blog with feed_type option. + */ + public function test_list_authors_with_feed_type_arg_enabled() { + $author = $this->create_author(); + $post = $this->create_post( $author ); + + $feed_type = 'atom'; + $feed_text = 'link to feed'; + $coauthors = coauthors_wp_list_authors( + array( + 'echo' => false, + 'feed_type' => $feed_type, + 'feed' => $feed_text, + ) + ); + + $this->assertStringContainsString( esc_url( get_author_feed_link( $author->ID, $feed_type ) ), $coauthors ); + $this->assertStringContainsString( $feed_type, $coauthors ); + $this->assertStringContainsString( $feed_text, $coauthors ); + } + + /** + * Checks all the co-authors of the blog with style option. + */ + public function test_list_authors_with_style_arg_enabled() { + + $coauthors = coauthors_wp_list_authors( + array( + 'echo' => false, + 'style' => 'none', + ) + ); + + $this->assertStringNotContainsString( '
  • ', $coauthors ); + $this->assertStringNotContainsString( '
  • ', $coauthors ); + } + + /** + * Checks all the co-authors of the blog with html option. + */ + public function test_list_authors_with_html_arg_enabled() { + global $coauthors_plus; + + $author = $this->create_author(); + $editor = $this->create_editor(); + $post = $this->create_post( $author ); + + $args = array( + 'echo' => false, + 'html' => false, + ); + + $this->assertEquals( $author->display_name, coauthors_wp_list_authors( $args ) ); + + $coauthors_plus->add_coauthors( $post->ID, array( $editor->user_login ), true ); + + $this->assertEquals( "{$author->display_name}, {$editor->display_name}", coauthors_wp_list_authors( $args ) ); + } + + /** + * Checks all the co-authors of the blog with guest_authors_only option. + */ + public function test_list_authors_with_guest_authors_only_arg_enabled() { + + global $coauthors_plus; + + $author = $this->create_author(); + $post = $this->create_post( $author ); + + $args = array( + 'echo' => false, + 'guest_authors_only' => true, + ); + + $this->assertEmpty( coauthors_wp_list_authors( $args ) ); + + $guest_author_id = $coauthors_plus->guest_authors->create( + array( + 'user_login' => 'author2', + 'display_name' => 'author2', + ) + ); + + $this->assertEmpty( coauthors_wp_list_authors( $args ) ); + + $guest_author = $coauthors_plus->guest_authors->get_guest_author_by( 'id', $guest_author_id ); + + $coauthors_plus->add_coauthors( $post->ID, array( $guest_author->user_login ), true ); + + $this->assertStringContainsString( $guest_author->display_name, coauthors_wp_list_authors( $args ) ); + } +} diff --git a/tests/Integration/TemplateTagsTest.php b/tests/Integration/TemplateTagsTest.php index 3d873fa9..73452ebe 100644 --- a/tests/Integration/TemplateTagsTest.php +++ b/tests/Integration/TemplateTagsTest.php @@ -552,255 +552,6 @@ public function test_get_the_coauthor_meta() { $post = $post_backup; } - /** - * Checks all the co-authors of the blog with default args. - * - * @covers ::coauthors_wp_list_authors() - */ - public function test_coauthors_wp_list_authors_for_default_args() { - - global $coauthors_plus; - - $args = array( - 'echo' => false, - ); - - $coauthors = coauthors_wp_list_authors( $args ); - - $this->assertStringContainsString( 'href="' . get_author_posts_url( $this->author1->ID, $this->author1->user_nicename ) . '"', $coauthors, 'Author link not found.' ); - $this->assertStringContainsString( $this->author1->display_name, $coauthors, 'Author name not found.' ); - - $coauthors = coauthors_wp_list_authors( $args ); - - $this->assertStringNotContainsString( 'href="' . get_author_posts_url( $this->editor1->ID, $this->editor1->user_nicename ) . '"', $coauthors ); - $this->assertStringNotContainsString( $this->editor1->display_name, $coauthors ); - - $coauthors_plus->add_coauthors( $this->post->ID, array( $this->editor1->user_login ), true ); - - $coauthors = coauthors_wp_list_authors( $args ); - - $this->assertStringContainsString( 'href="' . get_author_posts_url( $this->author1->ID, $this->author1->user_nicename ) . '"', $coauthors, 'Main author link not found.' ); - $this->assertStringContainsString( $this->author1->display_name, $coauthors, 'Main author name not found.' ); - - // Here we are checking author name should not be more than one time. - // Asserting ">{$this->author1->display_name}<" because "$this->author1->display_name" can be multiple times like in href, title, etc. - $this->assertEquals( 1, substr_count( $coauthors, ">{$this->author1->display_name}<" ) ); - - $this->assertStringContainsString( '
  • ', $coauthors, 'Coauthors name separator is not matched.' ); - $this->assertStringContainsString( 'href="' . get_author_posts_url( $this->editor1->ID, $this->editor1->user_nicename ) . '"', $coauthors, 'Coauthor link not found.' ); - $this->assertStringContainsString( $this->editor1->display_name, $coauthors, 'Coauthor name not found.' ); - - // Here we are checking editor name should not be more than one time. - // Asserting ">{$this->editor1->display_name}<" because "$this->editor1->display_name" can be multiple times like in href, title, etc. - $this->assertEquals( 1, substr_count( $coauthors, ">{$this->editor1->display_name}<" ) ); - } - - /** - * Checks all the co-authors of the blog with optioncount option. - * - * @covers ::coauthors_wp_list_authors() - */ - public function test_coauthors_wp_list_authors_for_optioncount() { - - $this->assertStringContainsString( - '(' . count_user_posts( $this->author1->ID ) . ')', - coauthors_wp_list_authors( - array( - 'echo' => false, - 'optioncount' => true, - ) - ) - ); - } - - /** - * Checks all the co-authors of the blog with show_fullname option. - * - * @covers ::coauthors_wp_list_authors() - */ - public function test_coauthors_wp_list_authors_for_show_fullname() { - - $args = array( - 'echo' => false, - 'show_fullname' => true, - ); - - $this->assertStringContainsString( $this->author1->display_name, coauthors_wp_list_authors( $args ) ); - - $user = $this->factory()->user->create_and_get( - array( - 'first_name' => 'First', - 'last_name' => 'Last', - ) - ); - - $this->factory()->post->create( - array( - 'post_author' => $user->ID, - ) - ); - - $this->assertStringContainsString( "{$user->user_firstname} {$user->user_lastname}", coauthors_wp_list_authors( $args ) ); - } - - /** - * Checks all the co-authors of the blog with hide_empty option. - * - * @covers ::coauthors_wp_list_authors() - */ - public function test_coauthors_wp_list_authors_for_hide_empty() { - - global $coauthors_plus; - - $coauthors_plus->guest_authors->create( - array( - 'user_login' => 'author2', - 'display_name' => 'author2', - ) - ); - - $this->assertStringContainsString( - 'author2', - coauthors_wp_list_authors( - array( - 'echo' => false, - 'hide_empty' => false, - ) - ) - ); - } - - /** - * Checks all the co-authors of the blog with feed option. - * - * @covers ::coauthors_wp_list_authors() - */ - public function test_coauthors_wp_list_authors_for_feed() { - - $feed_text = 'link to feed'; - $coauthors = coauthors_wp_list_authors( - array( - 'echo' => false, - 'feed' => $feed_text, - ) - ); - - $this->assertStringContainsString( esc_url( get_author_feed_link( $this->author1->ID ) ), $coauthors ); - $this->assertStringContainsString( $feed_text, $coauthors ); - } - - /** - * Checks all the co-authors of the blog with feed_image option. - * - * @covers ::coauthors_wp_list_authors() - */ - public function test_coauthors_wp_list_authors_for_feed_image() { - - $feed_image = WP_TESTS_DOMAIN . '/path/to/a/graphic.png'; - $coauthors = coauthors_wp_list_authors( - array( - 'echo' => false, - 'feed_image' => $feed_image, - ) - ); - - $this->assertStringContainsString( esc_url( get_author_feed_link( $this->author1->ID ) ), $coauthors ); - $this->assertStringContainsString( $feed_image, $coauthors ); - } - - /** - * Checks all the co-authors of the blog with feed_type option. - * - * @covers ::coauthors_wp_list_authors() - */ - public function test_coauthors_wp_list_authors_for_feed_type() { - - $feed_type = 'atom'; - $feed_text = 'link to feed'; - $coauthors = coauthors_wp_list_authors( - array( - 'echo' => false, - 'feed_type' => $feed_type, - 'feed' => $feed_text, - ) - ); - - $this->assertStringContainsString( esc_url( get_author_feed_link( $this->author1->ID, $feed_type ) ), $coauthors ); - $this->assertStringContainsString( $feed_type, $coauthors ); - $this->assertStringContainsString( $feed_text, $coauthors ); - } - - /** - * Checks all the co-authors of the blog with style option. - * - * @covers ::coauthors_wp_list_authors() - */ - public function test_coauthors_wp_list_authors_for_style() { - - $coauthors = coauthors_wp_list_authors( - array( - 'echo' => false, - 'style' => 'none', - ) - ); - - $this->assertStringNotContainsString( '
  • ', $coauthors ); - $this->assertStringNotContainsString( '
  • ', $coauthors ); - } - - /** - * Checks all the co-authors of the blog with html option. - * - * @covers ::coauthors_wp_list_authors() - */ - public function test_coauthors_wp_list_authors_for_html() { - - global $coauthors_plus; - - $args = array( - 'echo' => false, - 'html' => false, - ); - - $this->assertEquals( $this->author1->display_name, coauthors_wp_list_authors( $args ) ); - - $coauthors_plus->add_coauthors( $this->post->ID, array( $this->editor1->user_login ), true ); - - $this->assertEquals( "{$this->author1->display_name}, {$this->editor1->display_name}", coauthors_wp_list_authors( $args ) ); - } - - /** - * Checks all the co-authors of the blog with guest_authors_only option. - * - * @covers ::coauthors_wp_list_authors() - */ - public function test_coauthors_wp_list_authors_for_guest_authors_only() { - - global $coauthors_plus; - - $args = array( - 'echo' => false, - 'guest_authors_only' => true, - ); - - $this->assertEmpty( coauthors_wp_list_authors( $args ) ); - - $guest_author_id = $coauthors_plus->guest_authors->create( - array( - 'user_login' => 'author2', - 'display_name' => 'author2', - ) - ); - - $this->assertEmpty( coauthors_wp_list_authors( $args ) ); - - $guest_author = $coauthors_plus->guest_authors->get_guest_author_by( 'id', $guest_author_id ); - - $coauthors_plus->add_coauthors( $this->post->ID, array( $guest_author->user_login ), true ); - - $this->assertStringContainsString( $guest_author->display_name, coauthors_wp_list_authors( $args ) ); - } - /** * Checks co-author's avatar. * From 1f5c977ef0a88a9919a27cae0a3b27e719fd6fef Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Sun, 1 Oct 2023 18:23:05 +0100 Subject: [PATCH 13/13] Tests: Refactor coauthors_get_avatar() tests --- .../TemplateTags/CoauthorsGetAvatarTest.php | 77 ++++++++++++++++ tests/Integration/TemplateTagsTest.php | 89 ------------------- 2 files changed, 77 insertions(+), 89 deletions(-) create mode 100644 tests/Integration/TemplateTags/CoauthorsGetAvatarTest.php diff --git a/tests/Integration/TemplateTags/CoauthorsGetAvatarTest.php b/tests/Integration/TemplateTags/CoauthorsGetAvatarTest.php new file mode 100644 index 00000000..95c823bc --- /dev/null +++ b/tests/Integration/TemplateTags/CoauthorsGetAvatarTest.php @@ -0,0 +1,77 @@ +create_author(); + + $this->assertEmpty( coauthors_get_avatar( $author->ID ) ); + $this->assertEquals( preg_match( "|^[^$|", coauthors_get_avatar( $author ) ), 1 ); + } + + /** + * Checks co-author's avatar when author is a guest author. + */ + public function test_with_guest_author() { + global $coauthors_plus; + + $guest_author_id = $this->create_guest_author(); + $guest_author = $coauthors_plus->guest_authors->get_guest_author_by( 'id', $guest_author_id ); + $attachment_id = $this->factory()->attachment->create_upload_object( dirname( __DIR__ ) . '/fixtures/dummy-attachment.png', $guest_author_id ); + + $this->assertEquals( preg_match( "|^[^$|", coauthors_get_avatar( $guest_author ) ), 1 ); + + set_post_thumbnail( $guest_author->ID, $attachment_id ); + + $avatar = coauthors_get_avatar( $guest_author ); + $attachment_url = wp_get_attachment_url( $attachment_id ); + + // Checking for dummy-attachment instead of dummy-attachment.png, as filename might change to + // dummy-attachment-1.png, dummy-attachment-2.png, etc. when running multiple tests. + $this->assertStringContainsString( 'dummy-attachment', $avatar ); + $this->assertStringContainsString( $attachment_url, $avatar ); + } + + /** + * Checks co-author's avatar when user's email is not set somehow. + */ + public function test_with_guest_author_when_user_email_not_set() { + global $coauthors_plus; + + $guest_author_id = $this->create_guest_author(); + $guest_author = $coauthors_plus->guest_authors->get_guest_author_by( 'id', $guest_author_id ); + unset( $guest_author->user_email ); + + $this->assertEmpty( coauthors_get_avatar( $guest_author ) ); + } + + /** + * Checks co-author's avatar with size. + */ + public function test_with_author_and_size_arg() { + $author = $this->create_author(); + $size = '100'; + + $this->assertEquals( preg_match( "|^create_author(); + $alt = 'Test'; + + $this->assertEquals( preg_match( "|^$altassertEmpty( coauthors_get_avatar( $this->author1->ID ) ); - - $this->assertEquals( preg_match( "|^[^$|", coauthors_get_avatar( $this->author1 ) ), 1 ); - } - - /** - * Checks co-author's avatar when author is a guest author. - * - * @covers ::coauthors_get_avatar() - */ - public function test_coauthors_get_avatar_when_guest_author() { - - global $coauthors_plus; - - $guest_author_id = $coauthors_plus->guest_authors->create( - array( - 'user_login' => 'author2', - 'display_name' => 'author2', - ) - ); - - $guest_author = $coauthors_plus->guest_authors->get_guest_author_by( 'id', $guest_author_id ); - $attachment_id = $this->factory()->attachment->create_upload_object( __DIR__ . '/fixtures/dummy-attachment.png', $guest_author_id ); - - $this->assertEquals( preg_match( "|^[^$|", coauthors_get_avatar( $guest_author ) ), 1 ); - - set_post_thumbnail( $guest_author->ID, $attachment_id ); - - $avatar = coauthors_get_avatar( $guest_author ); - $attachment_url = wp_get_attachment_url( $attachment_id ); - - // Checking for dummy-attachment instead of dummy-attachment.png, as filename might change to - // dummy-attachment-1.png, dummy-attachment-2.png, etc. when running multiple tests. - $this->assertStringContainsString( 'dummy-attachment', $avatar ); - $this->assertStringContainsString( $attachment_url, $avatar ); - } - - /** - * Checks co-author's avatar when user's email is not set somehow. - * - * @covers ::coauthors_get_avatar() - */ - public function test_coauthors_get_avatar_when_user_email_not_set() { - - global $coauthors_plus; - - $guest_author_id = $coauthors_plus->guest_authors->create( - array( - 'user_login' => 'author2', - 'display_name' => 'author2', - ) - ); - - $guest_author = $coauthors_plus->guest_authors->get_guest_author_by( 'id', $guest_author_id ); - - unset( $guest_author->user_email ); - - $this->assertEmpty( coauthors_get_avatar( $guest_author ) ); - } - - /** - * Checks co-author's avatar with size. - * - * @covers ::coauthors_get_avatar() - */ - public function test_coauthors_get_avatar_size() { - - $size = '100'; - $this->assertEquals( preg_match( "|^author1, $size ) ), 1 ); - } - - /** - * Checks co-author's avatar with alt. - * - * @covers ::coauthors_get_avatar() - */ - public function test_coauthors_get_avatar_alt() { - - $alt = 'Test'; - $this->assertEquals( preg_match( "|^$altauthor1, 96, '', $alt ) ), 1 ); - } }