Editor: Adds pagination and ordering support to WP_REST_Pattern_Directory_Controller.

Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller` by allow listing `'per_page'`, `'page'`, `'offset'`, `'order'`, and `'orderby'` query parameters. This change enables pagination and ordering features in the pattern directory explorer by using the same sort as wordpress.org/patterns.

Reference:
* [https://github.com/WordPress/gutenberg/pull/45293 Gutenberg PR 45293]

Follow-up to [55098], [51206], [51021].

Props ntsekouras, ryelle, arrasel403, hellofromTonya, ironprogrammer, mukesh27, robinwpdeveloper.
Fixes #57501.

git-svn-id: https://develop.svn.wordpress.org/trunk@55132 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork
2023-01-24 16:52:42 +00:00
parent 1f28106a14
commit 119ec3240e
3 changed files with 326 additions and 26 deletions

View File

@@ -30,6 +30,15 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
*/
private static $controller;
/**
* List of URLs captured.
*
* @since 6.2.0
*
* @var string[]
*/
protected static $http_request_urls;
/**
* Set up class test fixtures.
*
@@ -44,9 +53,30 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
)
);
self::$http_request_urls = array();
static::$controller = new WP_REST_Pattern_Directory_Controller();
}
/**
* Tear down after class.
*
* @since 6.2.0
*/
public static function wpTearDownAfterClass() {
self::delete_user( self::$contributor_id );
}
/**
* Clear the captured request URLs after each test.
*
* @since 6.2.0
*/
public function tear_down() {
self::$http_request_urls = array();
parent::tear_down();
}
/**
* Asserts that the pattern matches the expected response schema.
*
@@ -310,6 +340,176 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
$this->assertSame( 'modified the cache', $patterns[0] );
}
/**
* Tests if the provided query args are passed through to the wp.org API.
*
* @since 6.2.0
*
* @ticket 57501
*
* @covers WP_REST_Pattern_Directory_Controller::get_items
*
* @dataProvider data_get_items_query_args
*
* @param string $param Query parameter name (ex, page).
* @param mixed $value Query value to test.
* @param bool $is_error Whether this value should error or not.
* @param mixed $expected Expected value (or expected error code).
*/
public function test_get_items_query_args( $param, $value, $is_error, $expected ) {
wp_set_current_user( self::$contributor_id );
add_filter( 'pre_http_request', array( $this, 'mock_request_to_apiwporg_url' ), 10, 3 );
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
if ( $value ) {
$request->set_query_params( array( $param => $value ) );
}
$response = rest_do_request( $request );
$data = $response->get_data();
if ( $is_error ) {
$this->assertSame( $expected, $data['code'], 'Response error code does not match' );
$this->assertStringContainsString( $param, $data['message'], 'Response error message does not match' );
} else {
$this->assertCount( 1, self::$http_request_urls, 'The number of HTTP Request URLs is not 1' );
$this->assertStringContainsString( $param . '=' . $expected, self::$http_request_urls[0], 'The param and/or value do not match' );
}
}
/**
* Data provider.
*
* return array[]
*/
public function data_get_items_query_args() {
return array(
'per_page default' => array(
'param' => 'per_page',
'value' => false,
'is_error' => false,
'expected' => 100,
),
'per_page custom-1' => array(
'param' => 'per_page',
'value' => 5,
'is_error' => false,
'expected' => 5,
),
'per_page custom-2' => array(
'param' => 'per_page',
'value' => 50,
'is_error' => false,
'expected' => 50,
),
'per_page invalid-1' => array(
'param' => 'per_page',
'value' => 200,
'is_error' => true,
'expected' => 'rest_invalid_param',
),
'per_page invalid-2' => array(
'param' => 'per_page',
'value' => 'abc',
'is_error' => true,
'expected' => 'rest_invalid_param',
),
'page default' => array(
'param' => 'page',
'value' => false,
'is_error' => false,
'expected' => 1,
),
'page custom' => array(
'param' => 'page',
'value' => 5,
'is_error' => false,
'expected' => 5,
),
'page invalid' => array(
'param' => 'page',
'value' => 'abc',
'is_error' => true,
'expected' => 'rest_invalid_param',
),
'offset custom' => array(
'param' => 'offset',
'value' => 5,
'is_error' => false,
'expected' => 5,
),
'offset invalid-1' => array(
'param' => 'offset',
'value' => 'abc',
'is_error' => true,
'expected' => 'rest_invalid_param',
),
'order default' => array(
'param' => 'order',
'value' => false,
'is_error' => false,
'expected' => 'desc',
),
'order custom' => array(
'param' => 'order',
'value' => 'asc',
'is_error' => false,
'expected' => 'asc',
),
'order invalid-1' => array(
'param' => 'order',
'value' => 10,
'is_error' => true,
'expected' => 'rest_invalid_param',
),
'order invalid-2' => array(
'param' => 'order',
'value' => 'fake',
'is_error' => true,
'expected' => 'rest_invalid_param',
),
'orderby default' => array(
'param' => 'orderby',
'value' => false,
'is_error' => false,
'expected' => 'date',
),
'orderby custom-1' => array(
'param' => 'orderby',
'value' => 'title',
'is_error' => false,
'expected' => 'title',
),
'orderby custom-2' => array(
'param' => 'orderby',
'value' => 'date',
'is_error' => false,
'expected' => 'date',
),
'orderby custom-3' => array(
'param' => 'orderby',
'value' => 'favorite_count',
'is_error' => false,
'expected' => 'favorite_count',
),
'orderby invalid-1' => array(
'param' => 'orderby',
'value' => 10,
'is_error' => true,
'expected' => 'rest_invalid_param',
),
'orderby invalid-2' => array(
'param' => 'orderby',
'value' => 'fake',
'is_error' => true,
'expected' => 'rest_invalid_param',
),
);
}
/**
* @doesNotPerformAssertions
*/
@@ -573,4 +773,33 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
3
);
}
/**
* Mock the request to wp.org URL to capture the URLs.
*
* @since 6.2.0
*
* @return array faux/mocked response.
*/
public function mock_request_to_apiwporg_url( $response, $args, $url ) {
if ( 'api.wordpress.org' !== wp_parse_url( $url, PHP_URL_HOST ) ) {
return $response;
}
self::$http_request_urls[] = $url;
// Return a response to prevent external API request.
$response = array(
'headers' => array(),
'response' => array(
'code' => 200,
'message' => 'OK',
),
'body' => '[]',
'cookies' => array(),
'filename' => null,
);
return $response;
}
}