From 2f6875a5b4207dd85d056b548ac5fd0c3a461401 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Wed, 27 Nov 2024 15:00:02 +0200 Subject: [PATCH 01/17] Create integration test about quering for seo scores without any data yet created --- .../Scores/SEO_Scores_Route_Test.php | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php new file mode 100644 index 00000000000..7ef3ae02e41 --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -0,0 +1,71 @@ +score_results_repository = Mockery::mock( SEO_Score_Results_Repository::class ); + + $this->instance = new SEO_Scores_Route( $this->score_results_repository ); + + $user = $this->factory->user->create_and_get(); + $user->add_cap( 'wpseo_manage_options' ); + + \wp_set_current_user( $user->ID ); + } + + /** + * Tests the get_scores method. + * + * @covers ::get_scores + * + * @return void + */ + public function test_get_seo_scores() { + $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); + $request->set_param( 'contentType', 'post' ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( + WP_REST_Response::class, + $response + ); + + $response_data = $response->get_data(); + + $this->assertIsArray( $response_data ); + $this->assertIsArray( $response_data['scores'] ); + $this->assertIsFloat( $response_data['queryTime'] ); + $this->assertIsBool( $response_data['cacheUsed'] ); + } +} From 1545f6ecd683191073dcb325295659295a16f85a Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Wed, 27 Nov 2024 15:09:37 +0200 Subject: [PATCH 02/17] Fix deprecation in PHP 8.2 about dynamic properties --- .../User_Interface/Scores/SEO_Scores_Route_Test.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php index 7ef3ae02e41..b56d626841c 100644 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -19,6 +19,13 @@ */ final class SEO_Scores_Route_Test extends TestCase { + /** + * The instance to test. + * + * @var SEO_Scores_Route + */ + private $instance; + /** * Holds the SEO_Score_Results_Repository instance. * From 9e187468183e3204234252cf3b0bdd47e0e28248 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Thu, 28 Nov 2024 12:38:55 +0200 Subject: [PATCH 03/17] Manually add all covered methods --- .../Scores/SEO_Scores_Route_Test.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php index b56d626841c..99385a8d467 100644 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -54,6 +54,23 @@ public function set_up() { * Tests the get_scores method. * * @covers ::get_scores + * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\SEO_Score_Results\SEO_Score_Results_Repository::get_score_results + * @covers Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\SEO_Score_Results\SEO_Score_Results_Collector::get_score_results + * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\Current_Scores_Repository::get_current_scores + * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\Current_Scores_Repository::get_current_score_links + * @covers Yoast\WP\SEO\Dashboard\Infrastructure\Score_Groups\Score_Group_Link_Collector::get_view_link + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Score_Result\Score_Result::to_array + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Scores_List::to_array + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score::get_name + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score::get_amount + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score::get_links_to_array + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_name + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_min_score + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_max_score + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_position + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_filter_key + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_filter_value + * @covers Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Type::get_name * * @return void */ From b6243625fb056a57ec195cb130fc29184173eb37 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Thu, 28 Nov 2024 15:04:55 +0200 Subject: [PATCH 04/17] Add assertions for multiple created posts --- .../Scores/SEO_Scores_Route_Test.php | 95 ++++++++++++++++++- 1 file changed, 90 insertions(+), 5 deletions(-) diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php index 99385a8d467..bfc1ce14370 100644 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -72,18 +72,37 @@ public function set_up() { * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_filter_value * @covers Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Type::get_name * + * @dataProvider data_provider_get_seo_scores + * + * @param array> $inserted_posts The posts to be insterted. + * @param array $expected_amounts The amounts of the scores that are expected to be returned. + * * @return void */ - public function test_get_seo_scores() { + public function test_get_seo_scores( $inserted_posts, $expected_amounts ) { $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); $request->set_param( 'contentType', 'post' ); + $request->set_param( 'taxonomy', 'category' ); + $request->set_param( 'term', 1 ); + + foreach ( $inserted_posts as $key => $post ) { + $meta_input = []; + foreach ( $post['meta_input'] as $meta_key => $meta_value ) { + $meta_input[ $meta_key ] = $meta_value; + } + \wp_insert_post( + [ + 'post_title' => 'Test Post' . $key, + 'post_status' => 'publish', + 'post_category' => [ 1 ], + 'meta_input' => $meta_input, + ] + ); + } $response = \rest_get_server()->dispatch( $request ); - $this->assertInstanceOf( - WP_REST_Response::class, - $response - ); + $this->assertInstanceOf( WP_REST_Response::class, $response ); $response_data = $response->get_data(); @@ -91,5 +110,71 @@ public function test_get_seo_scores() { $this->assertIsArray( $response_data['scores'] ); $this->assertIsFloat( $response_data['queryTime'] ); $this->assertIsBool( $response_data['cacheUsed'] ); + + $this->assertEquals( $response_data['scores'][0]['name'], 'good' ); + $this->assertEquals( $response_data['scores'][0]['amount'], $expected_amounts['good'] ); + + $this->assertEquals( $response_data['scores'][1]['name'], 'ok' ); + $this->assertEquals( $response_data['scores'][1]['amount'], $expected_amounts['ok'] ); + + $this->assertEquals( $response_data['scores'][2]['name'], 'bad' ); + $this->assertEquals( $response_data['scores'][2]['amount'], $expected_amounts['bad'] ); + + $this->assertEquals( $response_data['scores'][3]['name'], 'notAnalyzed' ); + $this->assertEquals( $response_data['scores'][3]['amount'], $expected_amounts['notAnalyzed'] ); + } + + /** + * Data provider for test_get_seo_scores. + * + * @return array + */ + public static function data_provider_get_seo_scores() { + yield 'No posts insterted' => [ + 'inserted_posts' => [], + 'expected_amounts' => [ + 'good' => 0, + 'ok' => 0, + 'bad' => 0, + 'notAnalyzed' => 0, + ], + ]; + yield 'Multiple posts of all sorts of SEO scores' => [ + 'inserted_posts' => [ + [ + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '30', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '10', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '50', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '80', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'meta_input' => [], + ], + ], + 'expected_amounts' => [ + 'good' => 1, + 'ok' => 1, + 'bad' => 2, + 'notAnalyzed' => 1, + ], + ]; } } From f763cc54b5ee5ae8cf2ba3954b16b3fbc494bfed Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Thu, 28 Nov 2024 16:16:27 +0200 Subject: [PATCH 05/17] Test cases where an invalid content type is passed in the API request --- .../Scores/SEO_Scores_Route_Test.php | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php index bfc1ce14370..fd7df56d973 100644 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -50,10 +50,74 @@ public function set_up() { \wp_set_current_user( $user->ID ); } + /** + * Tests the get_scores by sending a non existing content type. + * + * @covers ::get_scores + * @covers ::get_content_type + * + * @return void + */ + public function test_get_seo_scores_with_non_existing_content_type() { + $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); + $request->set_param( 'contentType', 'not-existing-content-type' ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid content type.' ); + } + + /** + * Tests the get_scores by sending an excluded-from-indexable-creation content type. + * + * @covers ::get_scores + * @covers ::get_content_type + * + * @return void + */ + public function test_get_seo_scores_with_excluded_content_type() { + $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); + $request->set_param( 'contentType', 'post' ); + + \add_filter( 'wpseo_indexable_excluded_post_types', [ $this, 'filter_exclude_post' ] ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid content type.' ); + + \remove_filter( 'wpseo_editor_specific_replace_vars', [ $this, 'filter_exclude_post' ] ); + } + + /** + * Filter function to exclude posts from indexable creation. + * + * @param array $excluded_post_types The excluded post types before the filter. + * + * @return array The excluded post types after the filter. + */ + public function filter_exclude_post( $excluded_post_types ) { + $excluded_post_types[] = 'post'; + + return $excluded_post_types; + } + /** * Tests the get_scores method. * * @covers ::get_scores + * @covers ::get_content_type + * @covers ::get_taxonomy + * @covers ::get_validated_term_id * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\SEO_Score_Results\SEO_Score_Results_Repository::get_score_results * @covers Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\SEO_Score_Results\SEO_Score_Results_Collector::get_score_results * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\Current_Scores_Repository::get_current_scores From 80bb13f15656d87879f8d48964370b23781c813a Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 29 Nov 2024 11:58:10 +0200 Subject: [PATCH 06/17] Add more tests for the routes --- .../Scores/SEO_Scores_Route_Test.php | 228 +++++++++++++++--- 1 file changed, 192 insertions(+), 36 deletions(-) diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php index fd7df56d973..2d87acaa878 100644 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -77,6 +77,8 @@ public function test_get_seo_scores_with_non_existing_content_type() { * * @covers ::get_scores * @covers ::get_content_type + * @covers Yoast\WP\SEO\Dashboard\Infrastructure\Content_Types\Content_Types_Collector::get_content_types + * @covers Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Types_List::add * * @return void */ @@ -95,7 +97,7 @@ public function test_get_seo_scores_with_excluded_content_type() { $this->assertSame( 400, $response->status ); $this->assertSame( $response_data['error'], 'Invalid content type.' ); - \remove_filter( 'wpseo_editor_specific_replace_vars', [ $this, 'filter_exclude_post' ] ); + \remove_filter( 'wpseo_indexable_excluded_post_types', [ $this, 'filter_exclude_post' ] ); } /** @@ -111,6 +113,85 @@ public function filter_exclude_post( $excluded_post_types ) { return $excluded_post_types; } + /** + * Tests the get_scores by sending a non filtering taxonomy for this content type. + * + * @covers ::get_taxonomy + * @covers Yoast\WP\SEO\Dashboard\Application\Taxonomies\Taxonomies_Repository::get_content_type_taxonomy + * + * @return void + */ + public function test_get_seo_scores_with_non_filtering_taxonomy() { + $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); + $request->set_param( 'contentType', 'post' ); + $request->set_param( 'taxonomy', 'post_tag' ); + $request->set_param( 'term', 'irrelevant' ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid taxonomy.' ); + } + + /** + * Tests the get_scores by sending an invalid term for this taxonomy and content type. + * + * @covers ::get_validated_term_id + * + * @return void + */ + public function test_get_seo_scores_with_invalid_term() { + $tag_id = \wp_insert_term( + 'Test tag', + 'post_tag', + [ + 'slug' => 'test-tag', + ] + ); + + $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); + $request->set_param( 'contentType', 'post' ); + $request->set_param( 'taxonomy', 'category' ); + $request->set_param( 'term', $tag_id['term_id'] ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid term.' ); + } + + /** + * Tests trying to get_scores with an unauthorized user. + * + * @covers ::permission_manage_options + * + * @return void + */ + public function test_get_seo_scores_with_not_priviliged_user() { + $user = $this->factory->user->create_and_get( [ 'role' => 'author' ] ); + \wp_set_current_user( $user->ID ); + + $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); + $request->set_param( 'contentType', 'post' ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( $response_data['code'], 'rest_forbidden' ); + $this->assertSame( $response_data['data']['status'], 403 ); + } + /** * Tests the get_scores method. * @@ -138,16 +219,20 @@ public function filter_exclude_post( $excluded_post_types ) { * * @dataProvider data_provider_get_seo_scores * - * @param array> $inserted_posts The posts to be insterted. - * @param array $expected_amounts The amounts of the scores that are expected to be returned. + * @param array> $inserted_posts The posts to be insterted. + * @param array $taxonomy_filter An array of term IDs and slugs. + * @param array $expected_amounts The amounts of the scores that are expected to be returned. * * @return void */ - public function test_get_seo_scores( $inserted_posts, $expected_amounts ) { + public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expected_amounts ) { $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); $request->set_param( 'contentType', 'post' ); - $request->set_param( 'taxonomy', 'category' ); - $request->set_param( 'term', 1 ); + + if ( ! empty( $taxonomy_filter ) ) { + $request->set_param( 'taxonomy', 'category' ); + $request->set_param( 'term', $taxonomy_filter['term_id'] ); + } foreach ( $inserted_posts as $key => $post ) { $meta_input = []; @@ -158,7 +243,7 @@ public function test_get_seo_scores( $inserted_posts, $expected_amounts ) { [ 'post_title' => 'Test Post' . $key, 'post_status' => 'publish', - 'post_category' => [ 1 ], + 'post_category' => [ $post['post_category'] ], 'meta_input' => $meta_input, ] ); @@ -186,6 +271,12 @@ public function test_get_seo_scores( $inserted_posts, $expected_amounts ) { $this->assertEquals( $response_data['scores'][3]['name'], 'notAnalyzed' ); $this->assertEquals( $response_data['scores'][3]['amount'], $expected_amounts['notAnalyzed'] ); + + $link_suffix = ( ! empty( $taxonomy_filter ) ) ? '&category_name=' . $taxonomy_filter['term_slug'] : ''; + $this->assertEquals( $response_data['scores'][0]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=post&seo_filter=good' . $link_suffix ); + $this->assertEquals( $response_data['scores'][1]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=post&seo_filter=ok' . $link_suffix ); + $this->assertEquals( $response_data['scores'][2]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=post&seo_filter=bad' . $link_suffix ); + $this->assertEquals( $response_data['scores'][3]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=post&seo_filter=na' . $link_suffix ); } /** @@ -194,8 +285,87 @@ public function test_get_seo_scores( $inserted_posts, $expected_amounts ) { * @return array */ public static function data_provider_get_seo_scores() { + $term = \wp_insert_term( + 'Test category', + 'category', + [ + 'slug' => 'test-category', + ] + ); + + $term_id = $term['term_id']; + $term_slug = 'test-category'; + + $inserted_posts_in_multiple_terms = [ + [ + 'post_category' => 1, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '30', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'post_category' => $term_id, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '30', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'post_category' => 1, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '10', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'post_category' => $term_id, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '10', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'post_category' => 1, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '50', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'post_category' => $term_id, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '50', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'post_category' => 1, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '80', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'post_category' => $term_id, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '80', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'post_category' => 1, + 'meta_input' => [], + ], + [ + 'post_category' => $term_id, + 'meta_input' => [], + ], + ]; + yield 'No posts insterted' => [ 'inserted_posts' => [], + 'taxonomy_filter' => [], 'expected_amounts' => [ 'good' => 0, 'ok' => 0, @@ -203,35 +373,11 @@ public static function data_provider_get_seo_scores() { 'notAnalyzed' => 0, ], ]; - yield 'Multiple posts of all sorts of SEO scores' => [ - 'inserted_posts' => [ - [ - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '30', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '10', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '50', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '80', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'meta_input' => [], - ], + yield 'Multiple posts of all sorts of SEO scores from term with ID 1' => [ + 'inserted_posts' => $inserted_posts_in_multiple_terms, + 'taxonomy_filter' => [ + 'term_id' => $term_id, + 'term_slug' => $term_slug, ], 'expected_amounts' => [ 'good' => 1, @@ -240,5 +386,15 @@ public static function data_provider_get_seo_scores() { 'notAnalyzed' => 1, ], ]; + yield 'Multiple posts of all sorts of SEO scores from all terms' => [ + 'inserted_posts' => $inserted_posts_in_multiple_terms, + 'taxonomy_filter' => [], + 'expected_amounts' => [ + 'good' => 2, + 'ok' => 2, + 'bad' => 4, + 'notAnalyzed' => 2, + ], + ]; } } From cf9a13d2579d2dd14c2b65e49eff38526f63779b Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 29 Nov 2024 12:29:40 +0200 Subject: [PATCH 07/17] Use factories instead of wp_inster_* functions --- .../Scores/SEO_Scores_Route_Test.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php index 2d87acaa878..9c4a9357b75 100644 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -239,7 +239,8 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte foreach ( $post['meta_input'] as $meta_key => $meta_value ) { $meta_input[ $meta_key ] = $meta_value; } - \wp_insert_post( + + $this->factory()->post->create( [ 'post_title' => 'Test Post' . $key, 'post_status' => 'publish', @@ -285,15 +286,17 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte * @return array */ public static function data_provider_get_seo_scores() { - $term = \wp_insert_term( - 'Test category', - 'category', + $term = self::factory()->term->create_and_get( [ - 'slug' => 'test-category', + 'name' => 'Test category', + 'taxonomy' => 'category', + [ + 'slug' => 'test-category', + ], ] ); - $term_id = $term['term_id']; + $term_id = $term->term_id; $term_slug = 'test-category'; $inserted_posts_in_multiple_terms = [ From 47c9ac500a9bb4e12c25f785ed49f6b291fa7617 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 29 Nov 2024 12:36:35 +0200 Subject: [PATCH 08/17] Temporarily log response to help troubleshooting CI --- .../Scores/SEO_Scores_Route_Test.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php index 9c4a9357b75..b50a4ef5aeb 100644 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -3,6 +3,7 @@ // @phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- This namespace should reflect the namespace of the original class. namespace Yoast\WP\SEO\Tests\WP\Dashboard\User_Interface\Scores; +use Yoast\WP\SEO\Repositories\Indexable_Repository; use Mockery; use WP_REST_Request; use WP_REST_Response; @@ -230,6 +231,7 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte $request->set_param( 'contentType', 'post' ); if ( ! empty( $taxonomy_filter ) ) { + error_log( print_r( $taxonomy_filter, true ) ); $request->set_param( 'taxonomy', 'category' ); $request->set_param( 'term', $taxonomy_filter['term_id'] ); } @@ -242,7 +244,7 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte $this->factory()->post->create( [ - 'post_title' => 'Test Post' . $key, + 'post_title' => 'Test Post ' . $key, 'post_status' => 'publish', 'post_category' => [ $post['post_category'] ], 'meta_input' => $meta_input, @@ -250,12 +252,24 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte ); } + error_log(print_r(get_posts( [ 'post_type' => 'post', 'numberposts' => -1 ] ), true)); + $post_indexables = YoastSEO()->classes->get( Indexable_Repository::class )->find_all_with_type_and_sub_type( 'post', 'post'); + + foreach ( $post_indexables as $indexable ) { + error_log(print_r($indexable->id, true)); + error_log(print_r($indexable->permalink, true)); + error_log(print_r($indexable->type, true)); + error_log(print_r($indexable->object_sub_type, true)); + } + $response = \rest_get_server()->dispatch( $request ); $this->assertInstanceOf( WP_REST_Response::class, $response ); $response_data = $response->get_data(); + error_log( print_r( $response_data, true ) ); + $this->assertIsArray( $response_data ); $this->assertIsArray( $response_data['scores'] ); $this->assertIsFloat( $response_data['queryTime'] ); From d69ebf0e4f93a68c66978114a87e88c8bffb6001 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 29 Nov 2024 13:33:12 +0200 Subject: [PATCH 09/17] Remove logging for troubleshooting --- .../Scores/SEO_Scores_Route_Test.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php index b50a4ef5aeb..e86e264d567 100644 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -3,7 +3,6 @@ // @phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- This namespace should reflect the namespace of the original class. namespace Yoast\WP\SEO\Tests\WP\Dashboard\User_Interface\Scores; -use Yoast\WP\SEO\Repositories\Indexable_Repository; use Mockery; use WP_REST_Request; use WP_REST_Response; @@ -231,7 +230,6 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte $request->set_param( 'contentType', 'post' ); if ( ! empty( $taxonomy_filter ) ) { - error_log( print_r( $taxonomy_filter, true ) ); $request->set_param( 'taxonomy', 'category' ); $request->set_param( 'term', $taxonomy_filter['term_id'] ); } @@ -252,24 +250,12 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte ); } - error_log(print_r(get_posts( [ 'post_type' => 'post', 'numberposts' => -1 ] ), true)); - $post_indexables = YoastSEO()->classes->get( Indexable_Repository::class )->find_all_with_type_and_sub_type( 'post', 'post'); - - foreach ( $post_indexables as $indexable ) { - error_log(print_r($indexable->id, true)); - error_log(print_r($indexable->permalink, true)); - error_log(print_r($indexable->type, true)); - error_log(print_r($indexable->object_sub_type, true)); - } - $response = \rest_get_server()->dispatch( $request ); $this->assertInstanceOf( WP_REST_Response::class, $response ); $response_data = $response->get_data(); - error_log( print_r( $response_data, true ) ); - $this->assertIsArray( $response_data ); $this->assertIsArray( $response_data['scores'] ); $this->assertIsFloat( $response_data['queryTime'] ); From 1cdc87bf67e894ece95d89cbc5342840017f6f59 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 29 Nov 2024 13:35:54 +0200 Subject: [PATCH 10/17] Fix tests with no term ID included --- .../Scores/SEO_Scores_Route_Test.php | 58 +++++++++++++------ 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php index e86e264d567..3a5554cb9cb 100644 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -100,19 +100,6 @@ public function test_get_seo_scores_with_excluded_content_type() { \remove_filter( 'wpseo_indexable_excluded_post_types', [ $this, 'filter_exclude_post' ] ); } - /** - * Filter function to exclude posts from indexable creation. - * - * @param array $excluded_post_types The excluded post types before the filter. - * - * @return array The excluded post types after the filter. - */ - public function filter_exclude_post( $excluded_post_types ) { - $excluded_post_types[] = 'post'; - - return $excluded_post_types; - } - /** * Tests the get_scores by sending a non filtering taxonomy for this content type. * @@ -226,8 +213,10 @@ public function test_get_seo_scores_with_not_priviliged_user() { * @return void */ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expected_amounts ) { + $this->register_blog_post_type(); + $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); - $request->set_param( 'contentType', 'post' ); + $request->set_param( 'contentType', 'blog-post' ); if ( ! empty( $taxonomy_filter ) ) { $request->set_param( 'taxonomy', 'category' ); @@ -244,6 +233,7 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte [ 'post_title' => 'Test Post ' . $key, 'post_status' => 'publish', + 'post_type' => 'blog-post', 'post_category' => [ $post['post_category'] ], 'meta_input' => $meta_input, ] @@ -274,10 +264,10 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte $this->assertEquals( $response_data['scores'][3]['amount'], $expected_amounts['notAnalyzed'] ); $link_suffix = ( ! empty( $taxonomy_filter ) ) ? '&category_name=' . $taxonomy_filter['term_slug'] : ''; - $this->assertEquals( $response_data['scores'][0]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=post&seo_filter=good' . $link_suffix ); - $this->assertEquals( $response_data['scores'][1]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=post&seo_filter=ok' . $link_suffix ); - $this->assertEquals( $response_data['scores'][2]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=post&seo_filter=bad' . $link_suffix ); - $this->assertEquals( $response_data['scores'][3]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=post&seo_filter=na' . $link_suffix ); + $this->assertEquals( $response_data['scores'][0]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=good' . $link_suffix ); + $this->assertEquals( $response_data['scores'][1]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=ok' . $link_suffix ); + $this->assertEquals( $response_data['scores'][2]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=bad' . $link_suffix ); + $this->assertEquals( $response_data['scores'][3]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=na' . $link_suffix ); } /** @@ -366,7 +356,7 @@ public static function data_provider_get_seo_scores() { ], ]; - yield 'No posts insterted' => [ + yield 'No posts inserted' => [ 'inserted_posts' => [], 'taxonomy_filter' => [], 'expected_amounts' => [ @@ -400,4 +390,34 @@ public static function data_provider_get_seo_scores() { ], ]; } + + /** + * Filter function to exclude posts from indexable creation. + * + * @param array $excluded_post_types The excluded post types before the filter. + * + * @return array The excluded post types after the filter. + */ + public function filter_exclude_post( $excluded_post_types ) { + $excluded_post_types[] = 'post'; + + return $excluded_post_types; + } + + /** + * Action function to register new blog post post type. + * + * @return void + */ + public function register_blog_post_type() { + $args = [ + 'label' => 'Blog posts', + 'public' => true, + 'has_archive' => true, + 'show_in_rest' => true, + 'taxonomies' => [ 'category' ], + ]; + + \register_post_type( 'blog-post', $args ); + } } From e30b6196ae2bd9ec5cb54bfe77e94cd007e61ea5 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 29 Nov 2024 13:46:30 +0200 Subject: [PATCH 11/17] Temporarily log again to help troubleshooting CI --- .../user-interface/scores/abstract-scores-route.php | 5 +++++ .../User_Interface/Scores/SEO_Scores_Route_Test.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/dashboard/user-interface/scores/abstract-scores-route.php b/src/dashboard/user-interface/scores/abstract-scores-route.php index e711003bc93..08262f5dc16 100644 --- a/src/dashboard/user-interface/scores/abstract-scores-route.php +++ b/src/dashboard/user-interface/scores/abstract-scores-route.php @@ -245,12 +245,17 @@ protected function get_validated_term_id( ?int $term_id, ?Taxonomy $taxonomy ): throw new Exception( 'Taxonomy needs a provided term.', 400 ); } + error_log( 'term_id: ' . $term_id ); if ( $term_id !== null ) { $term = \get_term( $term_id ); + error_log( print_r( $term, true ) ); if ( ! $term || \is_wp_error( $term ) ) { throw new Exception( 'Invalid term.', 400 ); } + error_log( print_r( $taxonomy, true ) ); + error_log( 'taxonomy name: ' . $taxonomy->get_name() ); + error_log( 'term->taxonomy: ' . $term->taxonomy ); if ( $taxonomy !== null && $term->taxonomy !== $taxonomy->get_name() ) { throw new Exception( 'Invalid term.', 400 ); } diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php index 3a5554cb9cb..d08490ff139 100644 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -286,7 +286,7 @@ public static function data_provider_get_seo_scores() { ] ); - $term_id = $term->term_id; + $term_id = (int) $term->term_id; $term_slug = 'test-category'; $inserted_posts_in_multiple_terms = [ From c2a397b24bc61264faa68bf412cb047b3fcc21d5 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 29 Nov 2024 14:00:48 +0200 Subject: [PATCH 12/17] Try and fix the tests with taxonomy filtering --- .../scores/abstract-scores-route.php | 5 -- .../Scores/SEO_Scores_Route_Test.php | 86 +++++++++---------- 2 files changed, 42 insertions(+), 49 deletions(-) diff --git a/src/dashboard/user-interface/scores/abstract-scores-route.php b/src/dashboard/user-interface/scores/abstract-scores-route.php index 08262f5dc16..e711003bc93 100644 --- a/src/dashboard/user-interface/scores/abstract-scores-route.php +++ b/src/dashboard/user-interface/scores/abstract-scores-route.php @@ -245,17 +245,12 @@ protected function get_validated_term_id( ?int $term_id, ?Taxonomy $taxonomy ): throw new Exception( 'Taxonomy needs a provided term.', 400 ); } - error_log( 'term_id: ' . $term_id ); if ( $term_id !== null ) { $term = \get_term( $term_id ); - error_log( print_r( $term, true ) ); if ( ! $term || \is_wp_error( $term ) ) { throw new Exception( 'Invalid term.', 400 ); } - error_log( print_r( $taxonomy, true ) ); - error_log( 'taxonomy name: ' . $taxonomy->get_name() ); - error_log( 'term->taxonomy: ' . $term->taxonomy ); if ( $taxonomy !== null && $term->taxonomy !== $taxonomy->get_name() ) { throw new Exception( 'Invalid term.', 400 ); } diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php index d08490ff139..ace9a18e75b 100644 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php @@ -206,21 +206,34 @@ public function test_get_seo_scores_with_not_priviliged_user() { * * @dataProvider data_provider_get_seo_scores * - * @param array> $inserted_posts The posts to be insterted. - * @param array $taxonomy_filter An array of term IDs and slugs. - * @param array $expected_amounts The amounts of the scores that are expected to be returned. + * @param array> $inserted_posts The posts to be insterted. + * @param bool $taxonomy_filter Whether there's a taxonomy filter. + * @param array $expected_amounts The amounts of the scores that are expected to be returned. * * @return void */ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expected_amounts ) { $this->register_blog_post_type(); + $term = self::factory()->term->create_and_get( + [ + 'name' => 'Test category', + 'taxonomy' => 'category', + [ + 'slug' => 'test-category', + ], + ] + ); + + $term_id = (int) $term->term_id; + $term_slug = 'test-category'; + $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); $request->set_param( 'contentType', 'blog-post' ); if ( ! empty( $taxonomy_filter ) ) { $request->set_param( 'taxonomy', 'category' ); - $request->set_param( 'term', $taxonomy_filter['term_id'] ); + $request->set_param( 'term', $term_id ); } foreach ( $inserted_posts as $key => $post ) { @@ -234,7 +247,7 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte 'post_title' => 'Test Post ' . $key, 'post_status' => 'publish', 'post_type' => 'blog-post', - 'post_category' => [ $post['post_category'] ], + 'post_category' => ( $post['custom_category'] ) ? [ $term_id ] : [ 1 ], 'meta_input' => $meta_input, ] ); @@ -263,7 +276,7 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte $this->assertEquals( $response_data['scores'][3]['name'], 'notAnalyzed' ); $this->assertEquals( $response_data['scores'][3]['amount'], $expected_amounts['notAnalyzed'] ); - $link_suffix = ( ! empty( $taxonomy_filter ) ) ? '&category_name=' . $taxonomy_filter['term_slug'] : ''; + $link_suffix = ( $taxonomy_filter ) ? '&category_name=' . $term_slug : ''; $this->assertEquals( $response_data['scores'][0]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=good' . $link_suffix ); $this->assertEquals( $response_data['scores'][1]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=ok' . $link_suffix ); $this->assertEquals( $response_data['scores'][2]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=bad' . $link_suffix ); @@ -276,89 +289,77 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte * @return array */ public static function data_provider_get_seo_scores() { - $term = self::factory()->term->create_and_get( - [ - 'name' => 'Test category', - 'taxonomy' => 'category', - [ - 'slug' => 'test-category', - ], - ] - ); - - $term_id = (int) $term->term_id; - $term_slug = 'test-category'; $inserted_posts_in_multiple_terms = [ [ - 'post_category' => 1, - 'meta_input' => [ + 'custom_category' => false, + 'meta_input' => [ '_yoast_wpseo_linkdex' => '30', '_yoast_wpseo_focuskw' => 'test', ], ], [ - 'post_category' => $term_id, - 'meta_input' => [ + 'custom_category' => true, + 'meta_input' => [ '_yoast_wpseo_linkdex' => '30', '_yoast_wpseo_focuskw' => 'test', ], ], [ - 'post_category' => 1, - 'meta_input' => [ + 'custom_category' => false, + 'meta_input' => [ '_yoast_wpseo_linkdex' => '10', '_yoast_wpseo_focuskw' => 'test', ], ], [ - 'post_category' => $term_id, - 'meta_input' => [ + 'custom_category' => true, + 'meta_input' => [ '_yoast_wpseo_linkdex' => '10', '_yoast_wpseo_focuskw' => 'test', ], ], [ - 'post_category' => 1, - 'meta_input' => [ + 'custom_category' => false, + 'meta_input' => [ '_yoast_wpseo_linkdex' => '50', '_yoast_wpseo_focuskw' => 'test', ], ], [ - 'post_category' => $term_id, - 'meta_input' => [ + 'custom_category' => true, + 'meta_input' => [ '_yoast_wpseo_linkdex' => '50', '_yoast_wpseo_focuskw' => 'test', ], ], [ - 'post_category' => 1, - 'meta_input' => [ + 'custom_category' => false, + 'meta_input' => [ '_yoast_wpseo_linkdex' => '80', '_yoast_wpseo_focuskw' => 'test', ], ], [ - 'post_category' => $term_id, - 'meta_input' => [ + 'custom_category' => true, + 'meta_input' => [ '_yoast_wpseo_linkdex' => '80', '_yoast_wpseo_focuskw' => 'test', ], ], [ - 'post_category' => 1, - 'meta_input' => [], + 'custom_category' => false, + 'meta_input' => [], ], [ - 'post_category' => $term_id, - 'meta_input' => [], + 'custom_category' => true, + 'meta_input' => [], ], ]; yield 'No posts inserted' => [ 'inserted_posts' => [], - 'taxonomy_filter' => [], + 'taxonomy_filter' => false, 'expected_amounts' => [ 'good' => 0, 'ok' => 0, @@ -368,10 +369,7 @@ public static function data_provider_get_seo_scores() { ]; yield 'Multiple posts of all sorts of SEO scores from term with ID 1' => [ 'inserted_posts' => $inserted_posts_in_multiple_terms, - 'taxonomy_filter' => [ - 'term_id' => $term_id, - 'term_slug' => $term_slug, - ], + 'taxonomy_filter' => true, 'expected_amounts' => [ 'good' => 1, 'ok' => 1, @@ -381,7 +379,7 @@ public static function data_provider_get_seo_scores() { ]; yield 'Multiple posts of all sorts of SEO scores from all terms' => [ 'inserted_posts' => $inserted_posts_in_multiple_terms, - 'taxonomy_filter' => [], + 'taxonomy_filter' => false, 'expected_amounts' => [ 'good' => 2, 'ok' => 2, From 351bdc12d46792a7fbb70d39307a1a2beff9a34f Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 6 Dec 2024 11:36:06 +0200 Subject: [PATCH 13/17] Create separate test classes for each set of covers --- .../SEO_Scores/Abstract_SEO_Scores_Test.php | 45 ++ .../SEO_Scores/Excluded_Content_Type_Test.php | 55 +++ .../SEO_Scores/Get_Scores_Test.php | 243 ++++++++++ .../SEO_Scores/Invalid_Term_Test.php | 45 ++ .../Non_Existing_Content_Type_Test.php | 36 ++ .../Non_Filtering_Taxonomy_Test.php | 38 ++ .../SEO_Scores/Not_Priviliged_User_Test.php | 38 ++ .../Scores/SEO_Scores_Route_Test.php | 421 ------------------ 8 files changed, 500 insertions(+), 421 deletions(-) create mode 100644 tests/WP/Dashboard/User_Interface/SEO_Scores/Abstract_SEO_Scores_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/SEO_Scores/Excluded_Content_Type_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/SEO_Scores/Invalid_Term_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/SEO_Scores/Non_Existing_Content_Type_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/SEO_Scores/Non_Filtering_Taxonomy_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/SEO_Scores/Not_Priviliged_User_Test.php delete mode 100644 tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php diff --git a/tests/WP/Dashboard/User_Interface/SEO_Scores/Abstract_SEO_Scores_Test.php b/tests/WP/Dashboard/User_Interface/SEO_Scores/Abstract_SEO_Scores_Test.php new file mode 100644 index 00000000000..b28973adc3e --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/SEO_Scores/Abstract_SEO_Scores_Test.php @@ -0,0 +1,45 @@ +score_results_repository = Mockery::mock( SEO_Score_Results_Repository::class ); + + $this->instance = new SEO_Scores_Route( $this->score_results_repository ); + + $user = $this->factory->user->create_and_get(); + $user->add_cap( 'wpseo_manage_options' ); + + \wp_set_current_user( $user->ID ); + } +} diff --git a/tests/WP/Dashboard/User_Interface/SEO_Scores/Excluded_Content_Type_Test.php b/tests/WP/Dashboard/User_Interface/SEO_Scores/Excluded_Content_Type_Test.php new file mode 100644 index 00000000000..af00501da23 --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/SEO_Scores/Excluded_Content_Type_Test.php @@ -0,0 +1,55 @@ +set_param( 'contentType', 'post' ); + + \add_filter( 'wpseo_indexable_excluded_post_types', [ $this, 'filter_exclude_post' ] ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid content type.' ); + + \remove_filter( 'wpseo_indexable_excluded_post_types', [ $this, 'filter_exclude_post' ] ); + } + + /** + * Filter function to exclude posts from indexable creation. + * + * @param array $excluded_post_types The excluded post types before the filter. + * + * @return array The excluded post types after the filter. + */ + public function filter_exclude_post( $excluded_post_types ) { + $excluded_post_types[] = 'post'; + + return $excluded_post_types; + } +} diff --git a/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php b/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php new file mode 100644 index 00000000000..de4757496a3 --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php @@ -0,0 +1,243 @@ +> $inserted_posts The posts to be insterted. + * @param bool $taxonomy_filter Whether there's a taxonomy filter. + * @param array $expected_amounts The amounts of the scores that are expected to be returned. + * + * @return void + */ + public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expected_amounts ) { + $this->register_blog_post_type(); + + $term = self::factory()->term->create_and_get( + [ + 'name' => 'Test category', + 'taxonomy' => 'category', + [ + 'slug' => 'test-category', + ], + ] + ); + + $term_id = (int) $term->term_id; + $term_slug = 'test-category'; + + $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); + $request->set_param( 'contentType', 'blog-post' ); + + if ( ! empty( $taxonomy_filter ) ) { + $request->set_param( 'taxonomy', 'category' ); + $request->set_param( 'term', $term_id ); + } + + foreach ( $inserted_posts as $key => $post ) { + $meta_input = []; + foreach ( $post['meta_input'] as $meta_key => $meta_value ) { + $meta_input[ $meta_key ] = $meta_value; + } + + $this->factory()->post->create( + [ + 'post_title' => 'Test Post ' . $key, + 'post_status' => 'publish', + 'post_type' => 'blog-post', + 'post_category' => ( $post['custom_category'] ) ? [ $term_id ] : [ 1 ], + 'meta_input' => $meta_input, + ] + ); + } + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertIsArray( $response_data ); + $this->assertIsArray( $response_data['scores'] ); + $this->assertIsFloat( $response_data['queryTime'] ); + $this->assertIsBool( $response_data['cacheUsed'] ); + + $this->assertEquals( $response_data['scores'][0]['name'], 'good' ); + $this->assertEquals( $response_data['scores'][0]['amount'], $expected_amounts['good'] ); + + $this->assertEquals( $response_data['scores'][1]['name'], 'ok' ); + $this->assertEquals( $response_data['scores'][1]['amount'], $expected_amounts['ok'] ); + + $this->assertEquals( $response_data['scores'][2]['name'], 'bad' ); + $this->assertEquals( $response_data['scores'][2]['amount'], $expected_amounts['bad'] ); + + $this->assertEquals( $response_data['scores'][3]['name'], 'notAnalyzed' ); + $this->assertEquals( $response_data['scores'][3]['amount'], $expected_amounts['notAnalyzed'] ); + + $link_suffix = ( $taxonomy_filter ) ? '&category_name=' . $term_slug : ''; + $this->assertEquals( $response_data['scores'][0]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=good' . $link_suffix ); + $this->assertEquals( $response_data['scores'][1]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=ok' . $link_suffix ); + $this->assertEquals( $response_data['scores'][2]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=bad' . $link_suffix ); + $this->assertEquals( $response_data['scores'][3]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=na' . $link_suffix ); + + \unregister_post_type( 'blog-post' ); + } + + /** + * Data provider for test_get_seo_scores. + * + * @return array + */ + public static function data_provider_get_seo_scores() { + + $inserted_posts_in_multiple_terms = [ + [ + 'custom_category' => false, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '30', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'custom_category' => true, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '30', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'custom_category' => false, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '10', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'custom_category' => true, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '10', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'custom_category' => false, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '50', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'custom_category' => true, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '50', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'custom_category' => false, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '80', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'custom_category' => true, + 'meta_input' => [ + '_yoast_wpseo_linkdex' => '80', + '_yoast_wpseo_focuskw' => 'test', + ], + ], + [ + 'custom_category' => false, + 'meta_input' => [], + ], + [ + 'custom_category' => true, + 'meta_input' => [], + ], + ]; + + yield 'No posts inserted' => [ + 'inserted_posts' => [], + 'taxonomy_filter' => false, + 'expected_amounts' => [ + 'good' => 0, + 'ok' => 0, + 'bad' => 0, + 'notAnalyzed' => 0, + ], + ]; + yield 'Multiple posts of all sorts of SEO scores from term with ID 1' => [ + 'inserted_posts' => $inserted_posts_in_multiple_terms, + 'taxonomy_filter' => true, + 'expected_amounts' => [ + 'good' => 1, + 'ok' => 1, + 'bad' => 2, + 'notAnalyzed' => 1, + ], + ]; + yield 'Multiple posts of all sorts of SEO scores from all terms' => [ + 'inserted_posts' => $inserted_posts_in_multiple_terms, + 'taxonomy_filter' => false, + 'expected_amounts' => [ + 'good' => 2, + 'ok' => 2, + 'bad' => 4, + 'notAnalyzed' => 2, + ], + ]; + } + + /** + * Action function to register new blog post post type. + * + * @return void + */ + public function register_blog_post_type() { + $args = [ + 'label' => 'Blog posts', + 'public' => true, + 'has_archive' => true, + 'show_in_rest' => true, + 'taxonomies' => [ 'category' ], + ]; + + \register_post_type( 'blog-post', $args ); + } +} diff --git a/tests/WP/Dashboard/User_Interface/SEO_Scores/Invalid_Term_Test.php b/tests/WP/Dashboard/User_Interface/SEO_Scores/Invalid_Term_Test.php new file mode 100644 index 00000000000..d149bac86a6 --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/SEO_Scores/Invalid_Term_Test.php @@ -0,0 +1,45 @@ + 'test-tag', + ] + ); + + $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); + $request->set_param( 'contentType', 'post' ); + $request->set_param( 'taxonomy', 'category' ); + $request->set_param( 'term', $tag_id['term_id'] ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid term.' ); + } +} diff --git a/tests/WP/Dashboard/User_Interface/SEO_Scores/Non_Existing_Content_Type_Test.php b/tests/WP/Dashboard/User_Interface/SEO_Scores/Non_Existing_Content_Type_Test.php new file mode 100644 index 00000000000..c0917b5d42d --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/SEO_Scores/Non_Existing_Content_Type_Test.php @@ -0,0 +1,36 @@ +set_param( 'contentType', 'not-existing-content-type' ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid content type.' ); + } +} diff --git a/tests/WP/Dashboard/User_Interface/SEO_Scores/Non_Filtering_Taxonomy_Test.php b/tests/WP/Dashboard/User_Interface/SEO_Scores/Non_Filtering_Taxonomy_Test.php new file mode 100644 index 00000000000..0f1a834702a --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/SEO_Scores/Non_Filtering_Taxonomy_Test.php @@ -0,0 +1,38 @@ +set_param( 'contentType', 'post' ); + $request->set_param( 'taxonomy', 'post_tag' ); + $request->set_param( 'term', 'irrelevant' ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid taxonomy.' ); + } +} diff --git a/tests/WP/Dashboard/User_Interface/SEO_Scores/Not_Priviliged_User_Test.php b/tests/WP/Dashboard/User_Interface/SEO_Scores/Not_Priviliged_User_Test.php new file mode 100644 index 00000000000..8cc6831bbcc --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/SEO_Scores/Not_Priviliged_User_Test.php @@ -0,0 +1,38 @@ +factory->user->create_and_get( [ 'role' => 'author' ] ); + \wp_set_current_user( $user->ID ); + + $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); + $request->set_param( 'contentType', 'post' ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( $response_data['code'], 'rest_forbidden' ); + $this->assertSame( $response_data['data']['status'], 403 ); + } +} diff --git a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php b/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php deleted file mode 100644 index ace9a18e75b..00000000000 --- a/tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php +++ /dev/null @@ -1,421 +0,0 @@ -score_results_repository = Mockery::mock( SEO_Score_Results_Repository::class ); - - $this->instance = new SEO_Scores_Route( $this->score_results_repository ); - - $user = $this->factory->user->create_and_get(); - $user->add_cap( 'wpseo_manage_options' ); - - \wp_set_current_user( $user->ID ); - } - - /** - * Tests the get_scores by sending a non existing content type. - * - * @covers ::get_scores - * @covers ::get_content_type - * - * @return void - */ - public function test_get_seo_scores_with_non_existing_content_type() { - $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); - $request->set_param( 'contentType', 'not-existing-content-type' ); - - $response = \rest_get_server()->dispatch( $request ); - - $this->assertInstanceOf( WP_REST_Response::class, $response ); - - $response_data = $response->get_data(); - - $this->assertSame( 400, $response->status ); - $this->assertSame( $response_data['error'], 'Invalid content type.' ); - } - - /** - * Tests the get_scores by sending an excluded-from-indexable-creation content type. - * - * @covers ::get_scores - * @covers ::get_content_type - * @covers Yoast\WP\SEO\Dashboard\Infrastructure\Content_Types\Content_Types_Collector::get_content_types - * @covers Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Types_List::add - * - * @return void - */ - public function test_get_seo_scores_with_excluded_content_type() { - $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); - $request->set_param( 'contentType', 'post' ); - - \add_filter( 'wpseo_indexable_excluded_post_types', [ $this, 'filter_exclude_post' ] ); - - $response = \rest_get_server()->dispatch( $request ); - - $this->assertInstanceOf( WP_REST_Response::class, $response ); - - $response_data = $response->get_data(); - - $this->assertSame( 400, $response->status ); - $this->assertSame( $response_data['error'], 'Invalid content type.' ); - - \remove_filter( 'wpseo_indexable_excluded_post_types', [ $this, 'filter_exclude_post' ] ); - } - - /** - * Tests the get_scores by sending a non filtering taxonomy for this content type. - * - * @covers ::get_taxonomy - * @covers Yoast\WP\SEO\Dashboard\Application\Taxonomies\Taxonomies_Repository::get_content_type_taxonomy - * - * @return void - */ - public function test_get_seo_scores_with_non_filtering_taxonomy() { - $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); - $request->set_param( 'contentType', 'post' ); - $request->set_param( 'taxonomy', 'post_tag' ); - $request->set_param( 'term', 'irrelevant' ); - - $response = \rest_get_server()->dispatch( $request ); - - $this->assertInstanceOf( WP_REST_Response::class, $response ); - - $response_data = $response->get_data(); - - $this->assertSame( 400, $response->status ); - $this->assertSame( $response_data['error'], 'Invalid taxonomy.' ); - } - - /** - * Tests the get_scores by sending an invalid term for this taxonomy and content type. - * - * @covers ::get_validated_term_id - * - * @return void - */ - public function test_get_seo_scores_with_invalid_term() { - $tag_id = \wp_insert_term( - 'Test tag', - 'post_tag', - [ - 'slug' => 'test-tag', - ] - ); - - $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); - $request->set_param( 'contentType', 'post' ); - $request->set_param( 'taxonomy', 'category' ); - $request->set_param( 'term', $tag_id['term_id'] ); - - $response = \rest_get_server()->dispatch( $request ); - - $this->assertInstanceOf( WP_REST_Response::class, $response ); - - $response_data = $response->get_data(); - - $this->assertSame( 400, $response->status ); - $this->assertSame( $response_data['error'], 'Invalid term.' ); - } - - /** - * Tests trying to get_scores with an unauthorized user. - * - * @covers ::permission_manage_options - * - * @return void - */ - public function test_get_seo_scores_with_not_priviliged_user() { - $user = $this->factory->user->create_and_get( [ 'role' => 'author' ] ); - \wp_set_current_user( $user->ID ); - - $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); - $request->set_param( 'contentType', 'post' ); - - $response = \rest_get_server()->dispatch( $request ); - - $this->assertInstanceOf( WP_REST_Response::class, $response ); - - $response_data = $response->get_data(); - - $this->assertSame( $response_data['code'], 'rest_forbidden' ); - $this->assertSame( $response_data['data']['status'], 403 ); - } - - /** - * Tests the get_scores method. - * - * @covers ::get_scores - * @covers ::get_content_type - * @covers ::get_taxonomy - * @covers ::get_validated_term_id - * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\SEO_Score_Results\SEO_Score_Results_Repository::get_score_results - * @covers Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\SEO_Score_Results\SEO_Score_Results_Collector::get_score_results - * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\Current_Scores_Repository::get_current_scores - * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\Current_Scores_Repository::get_current_score_links - * @covers Yoast\WP\SEO\Dashboard\Infrastructure\Score_Groups\Score_Group_Link_Collector::get_view_link - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Score_Result\Score_Result::to_array - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Scores_List::to_array - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score::get_name - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score::get_amount - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score::get_links_to_array - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_name - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_min_score - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_max_score - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_position - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_filter_key - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface::get_filter_value - * @covers Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Type::get_name - * - * @dataProvider data_provider_get_seo_scores - * - * @param array> $inserted_posts The posts to be insterted. - * @param bool $taxonomy_filter Whether there's a taxonomy filter. - * @param array $expected_amounts The amounts of the scores that are expected to be returned. - * - * @return void - */ - public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expected_amounts ) { - $this->register_blog_post_type(); - - $term = self::factory()->term->create_and_get( - [ - 'name' => 'Test category', - 'taxonomy' => 'category', - [ - 'slug' => 'test-category', - ], - ] - ); - - $term_id = (int) $term->term_id; - $term_slug = 'test-category'; - - $request = new WP_REST_Request( 'GET', '/yoast/v1/seo_scores' ); - $request->set_param( 'contentType', 'blog-post' ); - - if ( ! empty( $taxonomy_filter ) ) { - $request->set_param( 'taxonomy', 'category' ); - $request->set_param( 'term', $term_id ); - } - - foreach ( $inserted_posts as $key => $post ) { - $meta_input = []; - foreach ( $post['meta_input'] as $meta_key => $meta_value ) { - $meta_input[ $meta_key ] = $meta_value; - } - - $this->factory()->post->create( - [ - 'post_title' => 'Test Post ' . $key, - 'post_status' => 'publish', - 'post_type' => 'blog-post', - 'post_category' => ( $post['custom_category'] ) ? [ $term_id ] : [ 1 ], - 'meta_input' => $meta_input, - ] - ); - } - - $response = \rest_get_server()->dispatch( $request ); - - $this->assertInstanceOf( WP_REST_Response::class, $response ); - - $response_data = $response->get_data(); - - $this->assertIsArray( $response_data ); - $this->assertIsArray( $response_data['scores'] ); - $this->assertIsFloat( $response_data['queryTime'] ); - $this->assertIsBool( $response_data['cacheUsed'] ); - - $this->assertEquals( $response_data['scores'][0]['name'], 'good' ); - $this->assertEquals( $response_data['scores'][0]['amount'], $expected_amounts['good'] ); - - $this->assertEquals( $response_data['scores'][1]['name'], 'ok' ); - $this->assertEquals( $response_data['scores'][1]['amount'], $expected_amounts['ok'] ); - - $this->assertEquals( $response_data['scores'][2]['name'], 'bad' ); - $this->assertEquals( $response_data['scores'][2]['amount'], $expected_amounts['bad'] ); - - $this->assertEquals( $response_data['scores'][3]['name'], 'notAnalyzed' ); - $this->assertEquals( $response_data['scores'][3]['amount'], $expected_amounts['notAnalyzed'] ); - - $link_suffix = ( $taxonomy_filter ) ? '&category_name=' . $term_slug : ''; - $this->assertEquals( $response_data['scores'][0]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=good' . $link_suffix ); - $this->assertEquals( $response_data['scores'][1]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=ok' . $link_suffix ); - $this->assertEquals( $response_data['scores'][2]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=bad' . $link_suffix ); - $this->assertEquals( $response_data['scores'][3]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=na' . $link_suffix ); - } - - /** - * Data provider for test_get_seo_scores. - * - * @return array - */ - public static function data_provider_get_seo_scores() { - - $inserted_posts_in_multiple_terms = [ - [ - 'custom_category' => false, - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '30', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'custom_category' => true, - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '30', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'custom_category' => false, - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '10', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'custom_category' => true, - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '10', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'custom_category' => false, - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '50', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'custom_category' => true, - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '50', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'custom_category' => false, - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '80', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'custom_category' => true, - 'meta_input' => [ - '_yoast_wpseo_linkdex' => '80', - '_yoast_wpseo_focuskw' => 'test', - ], - ], - [ - 'custom_category' => false, - 'meta_input' => [], - ], - [ - 'custom_category' => true, - 'meta_input' => [], - ], - ]; - - yield 'No posts inserted' => [ - 'inserted_posts' => [], - 'taxonomy_filter' => false, - 'expected_amounts' => [ - 'good' => 0, - 'ok' => 0, - 'bad' => 0, - 'notAnalyzed' => 0, - ], - ]; - yield 'Multiple posts of all sorts of SEO scores from term with ID 1' => [ - 'inserted_posts' => $inserted_posts_in_multiple_terms, - 'taxonomy_filter' => true, - 'expected_amounts' => [ - 'good' => 1, - 'ok' => 1, - 'bad' => 2, - 'notAnalyzed' => 1, - ], - ]; - yield 'Multiple posts of all sorts of SEO scores from all terms' => [ - 'inserted_posts' => $inserted_posts_in_multiple_terms, - 'taxonomy_filter' => false, - 'expected_amounts' => [ - 'good' => 2, - 'ok' => 2, - 'bad' => 4, - 'notAnalyzed' => 2, - ], - ]; - } - - /** - * Filter function to exclude posts from indexable creation. - * - * @param array $excluded_post_types The excluded post types before the filter. - * - * @return array The excluded post types after the filter. - */ - public function filter_exclude_post( $excluded_post_types ) { - $excluded_post_types[] = 'post'; - - return $excluded_post_types; - } - - /** - * Action function to register new blog post post type. - * - * @return void - */ - public function register_blog_post_type() { - $args = [ - 'label' => 'Blog posts', - 'public' => true, - 'has_archive' => true, - 'show_in_rest' => true, - 'taxonomies' => [ 'category' ], - ]; - - \register_post_type( 'blog-post', $args ); - } -} From 091d6d98c3493e727550be1e40854085ac1758a8 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 6 Dec 2024 12:31:55 +0200 Subject: [PATCH 14/17] Add tests for readability route --- .../Abstract_Readability_Scores_Test.php | 45 ++++ .../Excluded_Content_Type_Test.php | 55 ++++ .../Readability_Scores/Get_Scores_Test.php | 235 ++++++++++++++++++ .../Readability_Scores/Invalid_Term_Test.php | 45 ++++ .../Non_Existing_Content_Type_Test.php | 36 +++ .../Non_Filtering_Taxonomy_Test.php | 38 +++ .../Not_Priviliged_User_Test.php | 38 +++ 7 files changed, 492 insertions(+) create mode 100644 tests/WP/Dashboard/User_Interface/Readability_Scores/Abstract_Readability_Scores_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/Readability_Scores/Excluded_Content_Type_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/Readability_Scores/Invalid_Term_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/Readability_Scores/Non_Existing_Content_Type_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/Readability_Scores/Non_Filtering_Taxonomy_Test.php create mode 100644 tests/WP/Dashboard/User_Interface/Readability_Scores/Not_Priviliged_User_Test.php diff --git a/tests/WP/Dashboard/User_Interface/Readability_Scores/Abstract_Readability_Scores_Test.php b/tests/WP/Dashboard/User_Interface/Readability_Scores/Abstract_Readability_Scores_Test.php new file mode 100644 index 00000000000..c11a3e8973f --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/Readability_Scores/Abstract_Readability_Scores_Test.php @@ -0,0 +1,45 @@ +score_results_repository = Mockery::mock( Readability_Score_Results_Repository::class ); + + $this->instance = new Readability_Scores_Route( $this->score_results_repository ); + + $user = $this->factory->user->create_and_get(); + $user->add_cap( 'wpseo_manage_options' ); + + \wp_set_current_user( $user->ID ); + } +} diff --git a/tests/WP/Dashboard/User_Interface/Readability_Scores/Excluded_Content_Type_Test.php b/tests/WP/Dashboard/User_Interface/Readability_Scores/Excluded_Content_Type_Test.php new file mode 100644 index 00000000000..cae35bd222d --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/Readability_Scores/Excluded_Content_Type_Test.php @@ -0,0 +1,55 @@ +set_param( 'contentType', 'post' ); + + \add_filter( 'wpseo_indexable_excluded_post_types', [ $this, 'filter_exclude_post' ] ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid content type.' ); + + \remove_filter( 'wpseo_indexable_excluded_post_types', [ $this, 'filter_exclude_post' ] ); + } + + /** + * Filter function to exclude posts from indexable creation. + * + * @param array $excluded_post_types The excluded post types before the filter. + * + * @return array The excluded post types after the filter. + */ + public function filter_exclude_post( $excluded_post_types ) { + $excluded_post_types[] = 'post'; + + return $excluded_post_types; + } +} diff --git a/tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php b/tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php new file mode 100644 index 00000000000..68335088168 --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php @@ -0,0 +1,235 @@ +> $inserted_posts The posts to be insterted. + * @param bool $taxonomy_filter Whether there's a taxonomy filter. + * @param array $expected_amounts The amounts of the scores that are expected to be returned. + * + * @return void + */ + public function test_get_readability_scores( $inserted_posts, $taxonomy_filter, $expected_amounts ) { + $this->register_blog_post_type(); + + $term = self::factory()->term->create_and_get( + [ + 'name' => 'Test category', + 'taxonomy' => 'category', + [ + 'slug' => 'test-category', + ], + ] + ); + + $term_id = (int) $term->term_id; + $term_slug = 'test-category'; + + $request = new WP_REST_Request( 'GET', '/yoast/v1/readability_scores' ); + $request->set_param( 'contentType', 'blog-post' ); + + if ( ! empty( $taxonomy_filter ) ) { + $request->set_param( 'taxonomy', 'category' ); + $request->set_param( 'term', $term_id ); + } + + foreach ( $inserted_posts as $key => $post ) { + $meta_input = []; + foreach ( $post['meta_input'] as $meta_key => $meta_value ) { + $meta_input[ $meta_key ] = $meta_value; + } + + $this->factory()->post->create( + [ + 'post_title' => 'Test Post ' . $key, + 'post_status' => 'publish', + 'post_type' => 'blog-post', + 'post_category' => ( $post['custom_category'] ) ? [ $term_id ] : [ 1 ], + 'meta_input' => $meta_input, + ] + ); + } + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertIsArray( $response_data ); + $this->assertIsArray( $response_data['scores'] ); + $this->assertIsFloat( $response_data['queryTime'] ); + $this->assertIsBool( $response_data['cacheUsed'] ); + + $this->assertEquals( $response_data['scores'][0]['name'], 'good' ); + $this->assertEquals( $response_data['scores'][0]['amount'], $expected_amounts['good'] ); + + $this->assertEquals( $response_data['scores'][1]['name'], 'ok' ); + $this->assertEquals( $response_data['scores'][1]['amount'], $expected_amounts['ok'] ); + + $this->assertEquals( $response_data['scores'][2]['name'], 'bad' ); + $this->assertEquals( $response_data['scores'][2]['amount'], $expected_amounts['bad'] ); + + $this->assertEquals( $response_data['scores'][3]['name'], 'notAnalyzed' ); + $this->assertEquals( $response_data['scores'][3]['amount'], $expected_amounts['notAnalyzed'] ); + + $link_suffix = ( $taxonomy_filter ) ? '&category_name=' . $term_slug : ''; + $this->assertEquals( $response_data['scores'][0]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&readability_filter=good' . $link_suffix ); + $this->assertEquals( $response_data['scores'][1]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&readability_filter=ok' . $link_suffix ); + $this->assertEquals( $response_data['scores'][2]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&readability_filter=bad' . $link_suffix ); + $this->assertEquals( $response_data['scores'][3]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&readability_filter=na' . $link_suffix ); + + \unregister_post_type( 'blog-post' ); + } + + /** + * Data provider for test_get_readability_scores. + * + * @return array + */ + public static function data_provider_get_readability_scores() { + + $inserted_posts_in_multiple_terms = [ + [ + 'custom_category' => false, + 'meta_input' => [ + '_yoast_wpseo_content_score' => '30', + ], + ], + [ + 'custom_category' => true, + 'meta_input' => [ + '_yoast_wpseo_content_score' => '30', + ], + ], + [ + 'custom_category' => false, + 'meta_input' => [ + '_yoast_wpseo_content_score' => '10', + ], + ], + [ + 'custom_category' => true, + 'meta_input' => [ + '_yoast_wpseo_content_score' => '10', + ], + ], + [ + 'custom_category' => false, + 'meta_input' => [ + '_yoast_wpseo_content_score' => '50', + ], + ], + [ + 'custom_category' => true, + 'meta_input' => [ + '_yoast_wpseo_content_score' => '50', + ], + ], + [ + 'custom_category' => false, + 'meta_input' => [ + '_yoast_wpseo_content_score' => '80', + ], + ], + [ + 'custom_category' => true, + 'meta_input' => [ + '_yoast_wpseo_content_score' => '80', + ], + ], + [ + 'custom_category' => false, + 'meta_input' => [], + ], + [ + 'custom_category' => true, + 'meta_input' => [], + ], + ]; + + yield 'No posts inserted' => [ + 'inserted_posts' => [], + 'taxonomy_filter' => false, + 'expected_amounts' => [ + 'good' => 0, + 'ok' => 0, + 'bad' => 0, + 'notAnalyzed' => 0, + ], + ]; + yield 'Multiple posts of all sorts of SEO scores from term with ID 1' => [ + 'inserted_posts' => $inserted_posts_in_multiple_terms, + 'taxonomy_filter' => true, + 'expected_amounts' => [ + 'good' => 1, + 'ok' => 1, + 'bad' => 2, + 'notAnalyzed' => 1, + ], + ]; + yield 'Multiple posts of all sorts of SEO scores from all terms' => [ + 'inserted_posts' => $inserted_posts_in_multiple_terms, + 'taxonomy_filter' => false, + 'expected_amounts' => [ + 'good' => 2, + 'ok' => 2, + 'bad' => 4, + 'notAnalyzed' => 2, + ], + ]; + } + + /** + * Action function to register new blog post post type. + * + * @return void + */ + public function register_blog_post_type() { + $args = [ + 'label' => 'Blog posts', + 'public' => true, + 'has_archive' => true, + 'show_in_rest' => true, + 'taxonomies' => [ 'category' ], + ]; + + \register_post_type( 'blog-post', $args ); + } +} diff --git a/tests/WP/Dashboard/User_Interface/Readability_Scores/Invalid_Term_Test.php b/tests/WP/Dashboard/User_Interface/Readability_Scores/Invalid_Term_Test.php new file mode 100644 index 00000000000..cdd06e1b4d9 --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/Readability_Scores/Invalid_Term_Test.php @@ -0,0 +1,45 @@ + 'test-tag', + ] + ); + + $request = new WP_REST_Request( 'GET', '/yoast/v1/readability_scores' ); + $request->set_param( 'contentType', 'post' ); + $request->set_param( 'taxonomy', 'category' ); + $request->set_param( 'term', $tag_id['term_id'] ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid term.' ); + } +} diff --git a/tests/WP/Dashboard/User_Interface/Readability_Scores/Non_Existing_Content_Type_Test.php b/tests/WP/Dashboard/User_Interface/Readability_Scores/Non_Existing_Content_Type_Test.php new file mode 100644 index 00000000000..53b326ff921 --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/Readability_Scores/Non_Existing_Content_Type_Test.php @@ -0,0 +1,36 @@ +set_param( 'contentType', 'not-existing-content-type' ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid content type.' ); + } +} diff --git a/tests/WP/Dashboard/User_Interface/Readability_Scores/Non_Filtering_Taxonomy_Test.php b/tests/WP/Dashboard/User_Interface/Readability_Scores/Non_Filtering_Taxonomy_Test.php new file mode 100644 index 00000000000..702622eaf45 --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/Readability_Scores/Non_Filtering_Taxonomy_Test.php @@ -0,0 +1,38 @@ +set_param( 'contentType', 'post' ); + $request->set_param( 'taxonomy', 'post_tag' ); + $request->set_param( 'term', 'irrelevant' ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( 400, $response->status ); + $this->assertSame( $response_data['error'], 'Invalid taxonomy.' ); + } +} diff --git a/tests/WP/Dashboard/User_Interface/Readability_Scores/Not_Priviliged_User_Test.php b/tests/WP/Dashboard/User_Interface/Readability_Scores/Not_Priviliged_User_Test.php new file mode 100644 index 00000000000..a72a5159b51 --- /dev/null +++ b/tests/WP/Dashboard/User_Interface/Readability_Scores/Not_Priviliged_User_Test.php @@ -0,0 +1,38 @@ +factory->user->create_and_get( [ 'role' => 'author' ] ); + \wp_set_current_user( $user->ID ); + + $request = new WP_REST_Request( 'GET', '/yoast/v1/readability_scores' ); + $request->set_param( 'contentType', 'post' ); + + $response = \rest_get_server()->dispatch( $request ); + + $this->assertInstanceOf( WP_REST_Response::class, $response ); + + $response_data = $response->get_data(); + + $this->assertSame( $response_data['code'], 'rest_forbidden' ); + $this->assertSame( $response_data['data']['status'], 403 ); + } +} From e13d046f8a66dbd1c9097b19e08830b73e9dd7d1 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 6 Dec 2024 12:47:20 +0200 Subject: [PATCH 15/17] Fix wrong covers tag --- .../User_Interface/Readability_Scores/Get_Scores_Test.php | 2 +- .../WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php b/tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php index 68335088168..cbcd8f3f11b 100644 --- a/tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php +++ b/tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php @@ -19,7 +19,7 @@ * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\Current_Scores_Repository::get_current_scores * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\Current_Scores_Repository::get_current_score_links * @covers Yoast\WP\SEO\Dashboard\Infrastructure\Score_Groups\Score_Group_Link_Collector::get_view_link - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Score_Result\Score_Result::to_array + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Score_Result::to_array * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Scores_List::to_array * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score::get_name * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score::get_amount diff --git a/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php b/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php index de4757496a3..db0fd867d86 100644 --- a/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php +++ b/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php @@ -19,7 +19,7 @@ * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\Current_Scores_Repository::get_current_scores * @covers Yoast\WP\SEO\Dashboard\Application\Score_Results\Current_Scores_Repository::get_current_score_links * @covers Yoast\WP\SEO\Dashboard\Infrastructure\Score_Groups\Score_Group_Link_Collector::get_view_link - * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Score_Result\Score_Result::to_array + * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Score_Result::to_array * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Scores_List::to_array * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score::get_name * @covers Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score::get_amount From 8f5b1fd18f90973bcf22f145b2b0adb24733ccfa Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Fri, 6 Dec 2024 15:21:01 +0200 Subject: [PATCH 16/17] Troubleshoot failing tests --- .../User_Interface/Readability_Scores/Get_Scores_Test.php | 8 +++++++- .../User_Interface/SEO_Scores/Get_Scores_Test.php | 8 +++++++- tests/WP/Sitemaps/Post_Type_Sitemap_Provider_Test.php | 7 ++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php b/tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php index cbcd8f3f11b..3e839f4d8aa 100644 --- a/tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php +++ b/tests/WP/Dashboard/User_Interface/Readability_Scores/Get_Scores_Test.php @@ -69,13 +69,14 @@ public function test_get_readability_scores( $inserted_posts, $taxonomy_filter, $request->set_param( 'term', $term_id ); } + $new_ids = []; foreach ( $inserted_posts as $key => $post ) { $meta_input = []; foreach ( $post['meta_input'] as $meta_key => $meta_value ) { $meta_input[ $meta_key ] = $meta_value; } - $this->factory()->post->create( + $new_ids[] = self::factory()->post->create( [ 'post_title' => 'Test Post ' . $key, 'post_status' => 'publish', @@ -115,7 +116,12 @@ public function test_get_readability_scores( $inserted_posts, $taxonomy_filter, $this->assertEquals( $response_data['scores'][2]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&readability_filter=bad' . $link_suffix ); $this->assertEquals( $response_data['scores'][3]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&readability_filter=na' . $link_suffix ); + // Clean up. + foreach ( $new_ids as $new_id ) { + \wp_delete_post( $new_id, true ); + } \unregister_post_type( 'blog-post' ); + \wp_delete_term( $term_id, 'category' ); } /** diff --git a/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php b/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php index db0fd867d86..917a021c8f8 100644 --- a/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php +++ b/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php @@ -69,13 +69,14 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte $request->set_param( 'term', $term_id ); } + $new_ids = []; foreach ( $inserted_posts as $key => $post ) { $meta_input = []; foreach ( $post['meta_input'] as $meta_key => $meta_value ) { $meta_input[ $meta_key ] = $meta_value; } - $this->factory()->post->create( + $new_ids[] = self::factory()->post->create( [ 'post_title' => 'Test Post ' . $key, 'post_status' => 'publish', @@ -115,7 +116,12 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte $this->assertEquals( $response_data['scores'][2]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=bad' . $link_suffix ); $this->assertEquals( $response_data['scores'][3]['links']['view'], 'http://example.org/wp-admin/edit.php?post_status=publish&post_type=blog-post&seo_filter=na' . $link_suffix ); + // Clean up. + foreach ( $new_ids as $new_id ) { + $result = \wp_delete_post( $new_id, true ); + } \unregister_post_type( 'blog-post' ); + \wp_delete_term( $term_id, 'category' ); } /** diff --git a/tests/WP/Sitemaps/Post_Type_Sitemap_Provider_Test.php b/tests/WP/Sitemaps/Post_Type_Sitemap_Provider_Test.php index 68668c8595c..9951461b712 100644 --- a/tests/WP/Sitemaps/Post_Type_Sitemap_Provider_Test.php +++ b/tests/WP/Sitemaps/Post_Type_Sitemap_Provider_Test.php @@ -102,9 +102,9 @@ public function test_get_index_links_multiple_entries_non_paged() { */ public function test_get_index_links_empty_bucket() { - $this->factory->post->create(); - $this->excluded_posts = [ $this->factory->post->create() ]; // Remove this post. - $this->factory->post->create(); + self::factory()->post->create( [ 'post_date' => '2024-01-01 00:00:01' ] ); + $this->excluded_posts = [ self::factory()->post->create( [ 'post_date' => '2024-01-01 00:00:02' ] ) ]; // Remove this post. + self::factory()->post->create( [ 'post_date' => '2024-01-01 00:00:03' ] ); \add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', [ $this, 'exclude_post' ] ); \add_filter( 'wpseo_sitemap_entries_per_page', [ $this, 'return_one' ] ); @@ -114,6 +114,7 @@ public function test_get_index_links_empty_bucket() { // Set the page to the second one, which should not contain an entry, but should exist. \set_query_var( 'sitemap_n', '2' ); + // Load the sitemap. $sitemaps = new Sitemaps_Double(); From 2a64b3588c57e4e3bff3c4f2d6a8e7105707aec3 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Mon, 9 Dec 2024 16:22:05 +0200 Subject: [PATCH 17/17] Fix PHPCS --- .../User_Interface/SEO_Scores/Get_Scores_Test.php | 2 +- tests/WP/Sitemaps/Post_Type_Sitemap_Provider_Test.php | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php b/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php index 917a021c8f8..f642b75befb 100644 --- a/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php +++ b/tests/WP/Dashboard/User_Interface/SEO_Scores/Get_Scores_Test.php @@ -118,7 +118,7 @@ public function test_get_seo_scores( $inserted_posts, $taxonomy_filter, $expecte // Clean up. foreach ( $new_ids as $new_id ) { - $result = \wp_delete_post( $new_id, true ); + \wp_delete_post( $new_id, true ); } \unregister_post_type( 'blog-post' ); \wp_delete_term( $term_id, 'category' ); diff --git a/tests/WP/Sitemaps/Post_Type_Sitemap_Provider_Test.php b/tests/WP/Sitemaps/Post_Type_Sitemap_Provider_Test.php index 9951461b712..c768615f2a2 100644 --- a/tests/WP/Sitemaps/Post_Type_Sitemap_Provider_Test.php +++ b/tests/WP/Sitemaps/Post_Type_Sitemap_Provider_Test.php @@ -102,9 +102,9 @@ public function test_get_index_links_multiple_entries_non_paged() { */ public function test_get_index_links_empty_bucket() { - self::factory()->post->create( [ 'post_date' => '2024-01-01 00:00:01' ] ); - $this->excluded_posts = [ self::factory()->post->create( [ 'post_date' => '2024-01-01 00:00:02' ] ) ]; // Remove this post. - self::factory()->post->create( [ 'post_date' => '2024-01-01 00:00:03' ] ); + self::factory()->post->create( [ 'post_date' => '2024-01-01 00:00:01' ] ); + $this->excluded_posts = [ self::factory()->post->create( [ 'post_date' => '2024-01-01 00:00:02' ] ) ]; // Remove this post. + self::factory()->post->create( [ 'post_date' => '2024-01-01 00:00:03' ] ); \add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', [ $this, 'exclude_post' ] ); \add_filter( 'wpseo_sitemap_entries_per_page', [ $this, 'return_one' ] ); @@ -114,7 +114,6 @@ public function test_get_index_links_empty_bucket() { // Set the page to the second one, which should not contain an entry, but should exist. \set_query_var( 'sitemap_n', '2' ); - // Load the sitemap. $sitemaps = new Sitemaps_Double();