Skip to content

Commit

Permalink
update tests for the new 'label' field and add tests for the new sear…
Browse files Browse the repository at this point in the history
…ch media controller
  • Loading branch information
petitphp committed Feb 14, 2024
1 parent 87ebaf3 commit 2ebfe82
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
21 changes: 18 additions & 3 deletions tests/phpunit/includes/class-wp-rest-test-search-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,28 @@ class WP_REST_Test_Search_Handler extends WP_REST_Search_Handler {
public function __construct( $amount = 10 ) {
$this->type = 'test';

$this->subtypes = array( 'test_first_type', 'test_second_type' );
$this->subtypes = array( 'test_first_type', 'test_second_type' );
$test_first_type_object = new \WP_Post_Type(
'test_first_type',
array(
'labels' => array(
'name' => 'Test first types',
'singular_name' => 'Test first type',
)

Check failure on line 26 in tests/phpunit/includes/class-wp-rest-test-search-handler.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

There should be a comma after the last array item in a multi-line array.
)
);

$this->items = array();
for ( $i = 1; $i <= $amount; $i++ ) {
$subtype = $i > $amount / 2 ? 'test_second_type' : 'test_first_type';
for ( $i = 1; $i <= $amount; $i ++ ) {

Check failure on line 31 in tests/phpunit/includes/class-wp-rest-test-search-handler.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected no spaces between $i and the increment operator; 1 found
$subtype = $i > $amount / 2 ? 'test_second_type' : 'test_first_type';
$subtype_object = $i > $amount / 2 ? false : $test_first_type_object;

$this->items[ $i ] = (object) array(
'test_id' => $i,
'test_title' => sprintf( 'Title %d', $i ),
'test_url' => sprintf( home_url( '/tests/%d' ), $i ),
'test_type' => $subtype,
'test_label' => $subtype_object ? $subtype_object->labels->singular_name : $subtype,
);
}
}
Expand Down Expand Up @@ -82,6 +93,10 @@ public function prepare_item( $id, array $fields ) {
$data[ WP_REST_Search_Controller::PROP_SUBTYPE ] = $test->test_type;
}

if ( in_array( WP_REST_Search_Controller::PROP_LABEL, $fields, true ) ) {
$data[ WP_REST_Search_Controller::PROP_LABEL ] = $test->test_label;
}

return $data;
}

Expand Down
82 changes: 82 additions & 0 deletions tests/phpunit/tests/rest-api/rest-search-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class WP_Test_REST_Search_Controller extends WP_Test_REST_Controller_Testcase {
*/
private static $my_content_post_ids = array();

/**
* Attachments
*
* @var array
*/
private static $my_attachment_ids;

/**
* Categories.
*
Expand Down Expand Up @@ -90,6 +97,13 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
'name' => 'Test Tag',
)
);

self::$my_attachment_ids = $factory->attachment->create_many(
4,
array(
'post_title' => 'my-footitle',
)
);
}

/**
Expand Down Expand Up @@ -413,6 +427,7 @@ public function test_prepare_item() {
'url',
'type',
'subtype',
'label',
'_links',
),
array_keys( $data[0] )
Expand Down Expand Up @@ -458,6 +473,7 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'url', $properties );
$this->assertArrayHasKey( 'type', $properties );
$this->assertArrayHasKey( 'subtype', $properties );
$this->assertArrayHasKey( 'label', $properties );
}

/**
Expand Down Expand Up @@ -523,6 +539,7 @@ public function test_custom_search_handler_prepare_item() {
'url',
'type',
'subtype',
'label',
),
array_keys( $data )
);
Expand Down Expand Up @@ -888,4 +905,69 @@ public function test_get_items_search_terms_exclude_ids() {
wp_list_pluck( $response->get_data(), 'id' )
);
}

/**
* Search through attachments.
*
* @ticket xxxxx
*/
public function test_get_items_search_type_media() {
$response = $this->do_request_with_params(
array(
'per_page' => 100,
'type' => 'media',
)
);
$this->assertSame( 200, $response->get_status() );
$this->assertSameSets(
self::$my_attachment_ids,
wp_list_pluck( $response->get_data(), 'id' )
);
}

/**
* @ticket xxxxx
*/
public function test_label_field_contain_the_post_type_singular_name() {
$response = $this->do_request_with_params(
array(
'subtype' => 'page',
'per_page' => 1,
)
);

$response_data = $response->get_data();
$this->assertSame( 'Page', $response_data[0]['label'] );
}

/**
* @ticket xxxxx
*/
public function test_label_field_contain_the_taxonomy_name() {
$response = $this->do_request_with_params(
array(
'type' => 'term',
'subtype' => 'category',
'per_page' => 1,
)
);

$response_data = $response->get_data();
$this->assertSame( 'Category', $response_data[0]['label'] );
}

/**
* @ticket xxxxx
*/
public function test_label_field_contain_the_post_format_name() {
$response = $this->do_request_with_params(
array(
'type' => 'post-format',
'per_page' => 1,
)
);

$response_data = $response->get_data();
$this->assertSame( 'Aside', $response_data[0]['label'] );
}
}
3 changes: 2 additions & 1 deletion tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -9802,7 +9802,8 @@ mockedApiResponse.Schema = {
"enum": [
"post",
"term",
"post-format"
"post-format",
"media"
],
"required": false
},
Expand Down

0 comments on commit 2ebfe82

Please sign in to comment.