diff --git a/.gitignore b/.gitignore index fbbb6ee8..2b9ec0d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ +/.phpunit.cache/ /node_modules/ /vendor/ /.phpcs.xml -/.phpunit.result.cache /composer.lock /phpcs.xml /phpunit.xml diff --git a/composer.json b/composer.json index 83ccf301..2b776753 100644 --- a/composer.json +++ b/composer.json @@ -49,6 +49,14 @@ } }, "scripts": { + "coverage": [ + "@putenv WP_MULTISITE=1", + "@php ./vendor/bin/phpunit --exclude=ms-excluded --coverage-html ./.phpunit.cache/coverage-html" + ], + "coverage-ci": [ + "@putenv WP_MULTISITE=1", + "@php ./vendor/bin/phpunit --exclude=ms-excluded" + ], "cs": [ "@php ./vendor/bin/phpcs -p -s -v -n . --standard=\"WordPress-VIP-Go\" --extensions=php --ignore=\"/vendor/*,/node_modules/*,/tests/*\"" ], @@ -65,11 +73,11 @@ "bash bin/install-wp-tests.sh wordpress_test root root localhost" ], "integration": [ - "@php ./vendor/bin/phpunit --exclude=ms-required" + "@php ./vendor/bin/phpunit --exclude=ms-required --no-coverage" ], "integration-ms": [ "@putenv WP_MULTISITE=1", - "@php ./vendor/bin/phpunit --exclude=ms-excluded" + "@php ./vendor/bin/phpunit --exclude=ms-excluded --no-coverage" ], "behat": "run-behat-tests", "behat-rerun": "rerun-behat-tests", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6a7a807a..7244c2b5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,19 +1,42 @@ - - - - ./tests/ - - + + + + + tests/Integration + + + + + + co-authors-plus.php + template-tags.php + upgrade.php + php + + + + php/integrations/yoast/class-coauthor.php + + php/integrations/amp/meta-author.php + + + + + + diff --git a/tests/test-author-queried-object.php b/tests/Integration/AuthorQueriedObjectTest.php similarity index 96% rename from tests/test-author-queried-object.php rename to tests/Integration/AuthorQueriedObjectTest.php index 2c0f8a26..77c68d2d 100644 --- a/tests/test-author-queried-object.php +++ b/tests/Integration/AuthorQueriedObjectTest.php @@ -3,7 +3,9 @@ * Test Co-Authors Plus' modifications of author queries */ -class Test_Author_Queried_Object extends CoAuthorsPlus_TestCase { +namespace Automattic\CoAuthorsPlus\Tests\Integration; + +class AuthorQueriedObjectTest extends TestCase { /** * On author pages, the queried object should only be set diff --git a/tests/test-author-queries.php b/tests/Integration/AuthorQueriesTest.php similarity index 94% rename from tests/test-author-queries.php rename to tests/Integration/AuthorQueriesTest.php index ecb2f878..d8d7296a 100644 --- a/tests/test-author-queries.php +++ b/tests/Integration/AuthorQueriesTest.php @@ -1,6 +1,8 @@ factory()->user->create( @@ -21,7 +23,7 @@ public function test__author_arg__user_is_post_author_query_as_post_author() { wp_set_current_user( $author_id ); - $query = new WP_Query( + $query = new \WP_Query( array( 'author' => $author_id, ) @@ -48,7 +50,7 @@ public function test__author_arg__user_is_post_author() { ); $this->_cap->add_coauthors( $post_id, array( $author->user_login ) ); - $query = new WP_Query( + $query = new \WP_Query( array( 'author' => $author_id, ) @@ -75,7 +77,7 @@ public function test__author_name_arg__user_is_post_author() { ); $this->_cap->add_coauthors( $post_id, array( $author->user_login ) ); - $query = new WP_Query( + $query = new \WP_Query( array( 'author_name' => $author->user_login, ) @@ -110,7 +112,7 @@ public function test__author_name_arg__user_is_coauthor() { ); $this->_cap->add_coauthors( $post_id, array( $author1->user_login, $author2->user_login ) ); - $query = new WP_Query( + $query = new \WP_Query( array( 'author_name' => $author2->user_login, ) @@ -145,7 +147,7 @@ public function test__author_arg__user_is_coauthor__author_arg() { ); $this->_cap->add_coauthors( $post_id, array( $author1->user_login, $author2->user_login ) ); - $query = new WP_Query( + $query = new \WP_Query( array( 'author' => $author2_id, ) @@ -173,7 +175,7 @@ public function test__author_name_arg_plus_tax_query__user_is_post_author() { $this->_cap->add_coauthors( $post_id, array( $author->user_login ) ); wp_set_post_terms( $post_id, 'test' ); - $query = new WP_Query( + $query = new \WP_Query( array( 'author_name' => $author->user_login, 'tag' => 'test', @@ -210,7 +212,7 @@ public function tests__author_name_arg_plus_tax_query__is_coauthor() { $this->_cap->add_coauthors( $post_id, array( $author1->user_login, $author2->user_login ) ); wp_set_post_terms( $post_id, 'test' ); - $query = new WP_Query( + $query = new \WP_Query( array( 'author_name' => $author2->user_login, 'tag' => 'test', diff --git a/tests/test-coauthors-plus.php b/tests/Integration/CoAuthorsPlusTest.php similarity index 97% rename from tests/test-coauthors-plus.php rename to tests/Integration/CoAuthorsPlusTest.php index cbe60ccb..90a2dfac 100644 --- a/tests/test-coauthors-plus.php +++ b/tests/Integration/CoAuthorsPlusTest.php @@ -1,6 +1,8 @@ get_coauthor_by( 'id', $guest_author_id ); - $this->assertInstanceOf( stdClass::class, $coauthor ); + $this->assertInstanceOf( \stdClass::class, $coauthor ); $this->assertTrue( property_exists( $coauthor, 'ID' ) ); $this->assertEquals( $guest_author_id, $coauthor->ID ); $this->assertEquals( 'guest-author', $coauthor->type ); @@ -97,7 +99,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->assertInstanceOf( \stdClass::class, $coauthor ); $this->assertTrue( property_exists( $coauthor, 'ID' ) ); $this->assertEquals( $guest_author_id, $coauthor->ID ); $this->assertEquals( 'guest-author', $coauthor->type ); @@ -118,26 +120,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->assertInstanceOf( \WP_User::class, $coauthor ); $this->assertTrue( property_exists( $coauthor, 'ID' ) ); $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->assertInstanceOf( \WP_User::class, $coauthor ); $this->assertTrue( property_exists( $coauthor->data, 'user_login' ) ); $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->assertInstanceOf( \WP_User::class, $coauthor ); $this->assertTrue( property_exists( $coauthor->data, 'user_nicename' ) ); $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->assertInstanceOf( \WP_User::class, $coauthor ); $this->assertTrue( property_exists( $coauthor->data, 'user_email' ) ); $this->assertEquals( $this->author1->user_email, $coauthor->user_email ); @@ -147,7 +149,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->assertInstanceOf( \stdClass::class, $coauthor ); $this->assertTrue( property_exists( $coauthor, 'linked_account' ) ); $this->assertEquals( $this->editor1->user_login, $coauthor->linked_account ); } @@ -519,7 +521,7 @@ public function test_get_author_term_using_caching() { $author_term = $coauthors_plus->get_author_term( $this->author1 ); $author_term_cached = wp_cache_get( $cache_key, 'co-authors-plus' ); - $this->assertInstanceOf( WP_Term::class, $author_term ); + $this->assertInstanceOf( \WP_Term::class, $author_term ); $this->assertEquals( $author_term, $author_term_cached ); } @@ -538,7 +540,7 @@ public function test_get_author_term_when_author_has_linked_account() { $author_term = $coauthors_plus->get_author_term( $coauthor ); - $this->assertInstanceOf( WP_Term::class, $author_term ); + $this->assertInstanceOf( \WP_Term::class, $author_term ); // Checks when term does not exist or deleted somehow. wp_delete_term( $author_term->term_id, $author_term->taxonomy ); @@ -566,7 +568,7 @@ public function test_get_author_term_when_author_has_not_linked_account() { $author_term = $coauthors_plus->get_author_term( $coauthor ); - $this->assertInstanceOf( WP_Term::class, $author_term ); + $this->assertInstanceOf( \WP_Term::class, $author_term ); // Checks when term does not exist or deleted somehow. wp_delete_term( $author_term->term_id, $author_term->taxonomy ); diff --git a/tests/test-coauthors-endpoint.php b/tests/Integration/EndpointsTest.php similarity index 90% rename from tests/test-coauthors-endpoint.php rename to tests/Integration/EndpointsTest.php index 1584627c..d412f9d4 100644 --- a/tests/test-coauthors-endpoint.php +++ b/tests/Integration/EndpointsTest.php @@ -1,11 +1,13 @@ set_url_params( array( 'q' => 'auth', @@ -208,7 +209,7 @@ public function test_get_coauthors_search_results() { 'Failed to assert that coauthors search returns results matching the query.' ); - $not_found_get_request = new WP_REST_Request( 'GET' ); + $not_found_get_request = new \WP_REST_Request( 'GET' ); $not_found_get_request->set_url_params( array( 'q' => 'nonexistent', @@ -224,7 +225,7 @@ public function test_get_coauthors_search_results() { } /** - * @covers ::get_coauthors() + * @covers \CoAuthors\API\Endpoints::get_coauthors */ public function test_authors_get_coauthors() { $test_post = $this->factory()->post->create_and_get( @@ -239,7 +240,7 @@ public function test_authors_get_coauthors() { $test_post_id = $test_post->ID; - $get_request = new WP_REST_Request( 'GET' ); + $get_request = new \WP_REST_Request( 'GET' ); $get_request->set_url_params( array( 'post_id' => $test_post_id, @@ -252,7 +253,7 @@ public function test_authors_get_coauthors() { } /** - * @covers ::update_coauthors() + * @covers \CoAuthors\API\Endpoints::update_coauthors */ public function test_update_coauthors() { @@ -270,7 +271,7 @@ public function test_update_coauthors() { $test_post_id = $test_post->ID; - $post_request = new WP_REST_Request( 'POST' ); + $post_request = new \WP_REST_Request( 'POST' ); $post_request->set_url_params( array( 'post_id' => $test_post_id, @@ -290,7 +291,7 @@ public function test_can_edit_coauthors() { ) ); - $request = new WP_REST_Request( + $request = new \WP_REST_Request( 'GET', '' ); @@ -319,7 +320,7 @@ public function test_can_edit_coauthors__with_post_param() { ) ); - $request = new WP_REST_Request( + $request = new \WP_REST_Request( 'GET', '' ); @@ -347,7 +348,7 @@ public function test_can_edit_coauthors__with_post_param() { } /** - * @covers ::remove_author_link() + * @covers \CoAuthors\API\Endpoints::remove_author_link */ public function test_remove_author_link() { @@ -359,7 +360,7 @@ public function test_remove_author_link() { ) ); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $test_post->ID ); + $request = new \WP_REST_Request( 'GET', '/wp/v2/posts/' . $test_post->ID ); wp_set_current_user( $this->editor1->ID ); @@ -390,7 +391,7 @@ public function test_remove_author_link() { } /** - * @covers ::modify_response() + * @covers \CoAuthors\API\Endpoints::modify_responses */ public function test_modify_response() { $this->_api->modify_responses(); diff --git a/tests/test-coauthors-guest-authors.php b/tests/Integration/GuestAuthorsTest.php similarity index 96% rename from tests/test-coauthors-guest-authors.php rename to tests/Integration/GuestAuthorsTest.php index e65627c3..84f2b218 100644 --- a/tests/test-coauthors-guest-authors.php +++ b/tests/Integration/GuestAuthorsTest.php @@ -1,8 +1,10 @@ get_guest_author_by( 'ID', $guest_author_id ); $guest_author_cached = wp_cache_get( $cache_key, $guest_author_obj::$cache_group ); - $this->assertInstanceOf( stdClass::class, $guest_author ); + $this->assertInstanceOf( \stdClass::class, $guest_author ); $this->assertEquals( $guest_author, $guest_author_cached ); } @@ -111,20 +113,20 @@ public function test_get_guest_author_by_with_different_keys() { // Checks guest author using ID. $guest_author = $guest_author_obj->get_guest_author_by( 'ID', $guest_author_id ); - $this->assertInstanceOf( stdClass::class, $guest_author ); + $this->assertInstanceOf( \stdClass::class, $guest_author ); $this->assertEquals( $guest_author_id, $guest_author->ID ); $this->assertEquals( $guest_author_obj->post_type, $guest_author->type ); // Checks guest author using user_nicename. $guest_author = $guest_author_obj->get_guest_author_by( 'user_nicename', $this->editor1->user_nicename ); - $this->assertInstanceOf( stdClass::class, $guest_author ); + $this->assertInstanceOf( \stdClass::class, $guest_author ); $this->assertEquals( $guest_author_obj->post_type, $guest_author->type ); // Checks guest author using linked_account. $guest_author = $guest_author_obj->get_guest_author_by( 'linked_account', $this->editor1->user_login ); - $this->assertInstanceOf( stdClass::class, $guest_author ); + $this->assertInstanceOf( \stdClass::class, $guest_author ); $this->assertEquals( $guest_author_obj->post_type, $guest_author->type ); } @@ -305,7 +307,7 @@ public function test_create_guest_author_from_user_id() { $guest_author_id = $guest_author_obj->create_guest_author_from_user_id( $this->editor1->ID ); $guest_author = $guest_author_obj->get_guest_author_by( 'ID', $guest_author_id ); - $this->assertInstanceOf( stdClass::class, $guest_author ); + $this->assertInstanceOf( \stdClass::class, $guest_author ); } /** @@ -388,11 +390,11 @@ public function test_handle_delete_guest_author_action_with_nonce() { try { $guest_author_obj->handle_delete_guest_author_action(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { $exception = $e; } - $this->assertInstanceOf( 'WPDieException', $exception ); + $this->assertInstanceOf( \WPDieException::class, $exception ); $this->assertStringContainsString( esc_html( $expected ), $exception->getMessage() ); // Checks when nonce is as expected. @@ -400,7 +402,7 @@ public function test_handle_delete_guest_author_action_with_nonce() { try { $guest_author_obj->handle_delete_guest_author_action(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { $exception = $e; } @@ -440,11 +442,11 @@ public function test_handle_delete_guest_author_action_with_list_users_capabilit try { $guest_author_obj->handle_delete_guest_author_action(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { $exception = $e; } - $this->assertInstanceOf( 'WPDieException', $exception ); + $this->assertInstanceOf( \WPDieException::class, $exception ); $this->assertStringContainsString( esc_html( $expected ), $exception->getMessage() ); // Checks when current user has list_users capability. @@ -455,7 +457,7 @@ public function test_handle_delete_guest_author_action_with_list_users_capabilit try { $guest_author_obj->handle_delete_guest_author_action(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { $exception = $e; } @@ -497,11 +499,11 @@ public function test_handle_delete_guest_author_action_with_guest_author_existen // Checks when guest author does not exist. try { $guest_author_obj->handle_delete_guest_author_action(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { $exception = $e; } - $this->assertInstanceOf( 'WPDieException', $exception ); + $this->assertInstanceOf( \WPDieException::class, $exception ); $this->assertStringContainsString( esc_html( $expected ), $exception->getMessage() ); // Checks when guest author exists. @@ -509,7 +511,7 @@ public function test_handle_delete_guest_author_action_with_guest_author_existen try { $guest_author_obj->handle_delete_guest_author_action(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { $exception = $e; } @@ -552,11 +554,11 @@ public function test_handle_delete_guest_author_action_with_reassign_not_as_expe try { $guest_author_obj->handle_delete_guest_author_action(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { $exception = $e; } - $this->assertInstanceOf( 'WPDieException', $exception ); + $this->assertInstanceOf( \WPDieException::class, $exception ); $this->assertStringContainsString( esc_html( $expected ), $exception->getMessage() ); // Restore current user from backup. @@ -596,7 +598,7 @@ public function test_handle_delete_guest_author_action_with_reassign_is_leave_as $guest_author_obj->handle_delete_guest_author_action(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { $this->assertStringContainsString( $guest_author_obj->parent_page, $e->getMessage() ); $this->assertStringContainsString( 'page=view-guest-authors', $e->getMessage() ); @@ -643,11 +645,11 @@ public function test_handle_delete_guest_author_action_with_reassign_is_reassign try { $guest_author_obj->handle_delete_guest_author_action(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { $exception = $e; } - $this->assertInstanceOf( 'WPDieException', $exception ); + $this->assertInstanceOf( \WPDieException::class, $exception ); $this->assertStringContainsString( esc_html( $expected ), $exception->getMessage() ); // When coauthor exists. @@ -659,7 +661,7 @@ public function test_handle_delete_guest_author_action_with_reassign_is_reassign $guest_author_obj->handle_delete_guest_author_action(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { //$this->assertStringContainsString( $guest_author_obj->parent_page, $e->getMessage() ); $this->assertStringContainsString( 'page=view-guest-authors', $e->getMessage() ); @@ -705,7 +707,7 @@ public function test_handle_delete_guest_author_action_with_reassign_is_remove_b $guest_author_obj->handle_delete_guest_author_action(); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { $this->assertStringContainsString( $guest_author_obj->parent_page, $e->getMessage() ); $this->assertStringContainsString( 'page=view-guest-authors', $e->getMessage() ); @@ -736,7 +738,7 @@ public function test_handle_delete_guest_author_action_with_reassign_is_remove_b **/ public function catch_redirect_destination( $location, $status ) { - throw new Exception( $location, $status ); + throw new \Exception( $location, $status ); } /** diff --git a/tests/test-manage-coauthors.php b/tests/Integration/ManageCoAuthorsTest.php similarity index 95% rename from tests/test-manage-coauthors.php rename to tests/Integration/ManageCoAuthorsTest.php index 9781c19b..5c340814 100644 --- a/tests/test-manage-coauthors.php +++ b/tests/Integration/ManageCoAuthorsTest.php @@ -1,6 +1,8 @@ guest_authors = new CoAuthors_Guest_Authors(); + $coauthors_plus->guest_authors = new \CoAuthors_Guest_Authors(); $coauthors_plus->guest_authors->create_guest_author_from_user_id( $this->author1 ); $new_data = $coauthors_plus->coauthors_set_post_author_field( $data, $post_array ); @@ -350,7 +352,7 @@ public function test_coauthors_set_post_author_field_when_guest_author_is_linked * * @see https://github.com/Automattic/Co-Authors-Plus/issues/198 * - * @covers ::coauthors_set_post_author_field() + * @covers \CoAuthors_Plus::coauthors_set_post_author_field */ public function test_coauthors_set_post_author_field_when_post_author_is_not_set() { @@ -388,7 +390,7 @@ public function test_coauthors_set_post_author_field_when_post_author_is_not_set * * @see https://github.com/Automattic/Co-Authors-Plus/issues/198 * - * @covers ::coauthors_update_post() + * @covers \CoAuthors_Plus::coauthors_update_post */ public function test_coauthors_update_post_when_post_type_is_attachment() { @@ -423,7 +425,7 @@ public function test_coauthors_update_post_when_post_type_is_attachment() { * * @see https://github.com/Automattic/Co-Authors-Plus/issues/198 * - * @covers ::coauthors_update_post() + * @covers \CoAuthors_Plus::coauthors_update_post */ public function test_coauthors_update_post_when_current_user_can_set_authors() { @@ -469,7 +471,7 @@ public function test_coauthors_update_post_when_current_user_can_set_authors() { * * @see https://github.com/Automattic/Co-Authors-Plus/issues/198 * - * @covers ::coauthors_update_post() + * @covers \CoAuthors_Plus::coauthors_update_post */ public function test_coauthors_update_post_when_post_has_not_author_terms() { diff --git a/tests/test-template-tags.php b/tests/Integration/TemplateTagsTest.php similarity index 99% rename from tests/test-template-tags.php rename to tests/Integration/TemplateTagsTest.php index 28bed1b3..933abb9a 100644 --- a/tests/test-template-tags.php +++ b/tests/Integration/TemplateTagsTest.php @@ -1,8 +1,10 @@ author1 = $this->factory()->user->create_and_get( array( diff --git a/tests/coauthorsplus-testcase.php b/tests/Integration/TestCase.php similarity index 74% rename from tests/coauthorsplus-testcase.php rename to tests/Integration/TestCase.php index 8b82d016..2834de18 100644 --- a/tests/coauthorsplus-testcase.php +++ b/tests/Integration/TestCase.php @@ -1,10 +1,13 @@