Skip to content

Commit

Permalink
Add more tests for the routes
Browse files Browse the repository at this point in the history
  • Loading branch information
leonidasmi committed Nov 29, 2024
1 parent f763cc5 commit 80bb13f
Showing 1 changed file with 192 additions and 36 deletions.
228 changes: 192 additions & 36 deletions tests/WP/Dashboard/User_Interface/Scores/SEO_Scores_Route_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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' ] );
}

/**
Expand All @@ -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.
*
Expand Down Expand Up @@ -138,16 +219,20 @@ public function filter_exclude_post( $excluded_post_types ) {
*
* @dataProvider data_provider_get_seo_scores
*
* @param array<array<string, string>> $inserted_posts The posts to be insterted.
* @param array<string, int> $expected_amounts The amounts of the scores that are expected to be returned.
* @param array<array<string,string>> $inserted_posts The posts to be insterted.
* @param array<string,string|int> $taxonomy_filter An array of term IDs and slugs.
* @param array<string,int> $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 = [];
Expand All @@ -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,
]
);
Expand Down Expand Up @@ -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 );
}

/**
Expand All @@ -194,44 +285,99 @@ public function test_get_seo_scores( $inserted_posts, $expected_amounts ) {
* @return array<string,bool>
*/
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,
'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' => [],
],
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,
Expand All @@ -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,
],
];
}
}

0 comments on commit 80bb13f

Please sign in to comment.