Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-vlasenko committed Dec 26, 2024
1 parent be04dec commit f2ba32a
Showing 1 changed file with 84 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,33 @@ public function test_register_routes() {
}

/**
* @dataProvider data_readable_http_methods
* @ticket 58524
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_items
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_missing_parent() {
public function test_get_items_missing_parent( $method ) {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' );
$request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
}

/**
* Data provider intended to provide HTTP method names for testing GET and HEAD requests.
*
* @return array
*/
public static function data_readable_http_methods() {
return array(
'GET request' => array( 'GET' ),
'HEAD request' => array( 'HEAD' ),
);
}

/**
* Utility function to check the items in WP_REST_Global_Styles_Controller::get_items
* against the expected values.
Expand Down Expand Up @@ -327,16 +343,20 @@ public function test_get_item() {
}

/**
* @dataProvider data_readable_http_methods
* @ticket 59810
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_revision
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_invalid_revision_id_should_error() {
public function test_get_item_invalid_revision_id_should_error( $method ) {
wp_set_current_user( self::$admin_id );

$expected_error = 'rest_post_invalid_id';
$expected_status = 404;
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions/20000001' );
$request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions/20000001' );
$response = rest_get_server()->dispatch( $request );

$this->assertErrorResponse( $expected_error, $response, $expected_status );
Expand Down Expand Up @@ -419,14 +439,18 @@ public function test_get_item_schema() {
}

/**
* @dataProvider data_readable_http_methods
* @ticket 58524
* @ticket 60131
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_item_permissions_check
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_permissions_check() {
public function test_get_item_permissions_check( $method ) {
wp_set_current_user( self::$author_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );

$this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
Expand All @@ -435,21 +459,23 @@ public function test_get_item_permissions_check() {
/**
* Tests the pagination header of the first page.
*
* Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_pagination_header_of_the_first_page
*
* @dataProvider data_readable_http_methods
* @ticket 58524
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_items
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_pagination_header_of_the_first_page() {
public function test_get_items_pagination_header_of_the_first_page( $method ) {
wp_set_current_user( self::$admin_id );

$rest_route = '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions';
$per_page = 2;
$total_pages = (int) ceil( $this->total_revisions / $per_page );
$page = 1; // First page.

$request = new WP_REST_Request( 'GET', $rest_route );
$request = new WP_REST_Request( $method, $rest_route );
$request->set_query_params(
array(
'per_page' => $per_page,
Expand All @@ -476,19 +502,23 @@ public function test_get_items_pagination_header_of_the_first_page() {
*
* Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_pagination_header_of_the_last_page
*
* @dataProvider data_readable_http_methods
* @ticket 58524
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_items
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_pagination_header_of_the_last_page() {
public function test_get_items_pagination_header_of_the_last_page( $method ) {
wp_set_current_user( self::$admin_id );

$rest_route = '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions';
$per_page = 2;
$total_pages = (int) ceil( $this->total_revisions / $per_page );
$page = 2; // Last page.

$request = new WP_REST_Request( 'GET', $rest_route );
$request = new WP_REST_Request( $method, $rest_route );
$request->set_query_params(
array(
'per_page' => $per_page,
Expand All @@ -514,18 +544,22 @@ public function test_get_items_pagination_header_of_the_last_page() {
*
* Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_invalid_per_page_should_error
*
* @dataProvider data_readable_http_methods
* @ticket 58524
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_items
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_invalid_per_page_should_error() {
public function test_get_items_invalid_per_page_should_error( $method ) {
wp_set_current_user( self::$admin_id );

$per_page = -1; // Invalid number.
$expected_error = 'rest_invalid_param';
$expected_status = 400;

$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_param( 'per_page', $per_page );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( $expected_error, $response, $expected_status );
Expand All @@ -536,11 +570,15 @@ public function test_get_items_invalid_per_page_should_error() {
*
* Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_out_of_bounds_page_should_error
*
* @dataProvider data_readable_http_methods
* @ticket 58524
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_items
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_out_of_bounds_page_should_error() {
public function test_get_items_out_of_bounds_page_should_error( $method ) {
wp_set_current_user( self::$admin_id );

$per_page = 2;
Expand All @@ -549,7 +587,7 @@ public function test_get_items_out_of_bounds_page_should_error() {
$expected_error = 'rest_revision_invalid_page_number';
$expected_status = 400;

$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_query_params(
array(
'per_page' => $per_page,
Expand All @@ -565,19 +603,23 @@ public function test_get_items_out_of_bounds_page_should_error() {
*
* Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_invalid_max_pages_should_error
*
* @dataProvider data_readable_http_methods
* @ticket 58524
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_items
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_invalid_max_pages_should_error() {
public function test_get_items_invalid_max_pages_should_error( $method ) {
wp_set_current_user( self::$admin_id );

$per_page = 2;
$page = REST_TESTS_IMPOSSIBLY_HIGH_NUMBER; // Invalid number.
$expected_error = 'rest_revision_invalid_page_number';
$expected_status = 400;

$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_query_params(
array(
'per_page' => $per_page,
Expand Down Expand Up @@ -689,19 +731,23 @@ public function test_get_items_offset_should_take_priority_over_page() {
*
* Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_total_revisions_offset_should_return_empty_data
*
* @dataProvider data_readable_http_methods
* @ticket 58524
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_items
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_total_revisions_offset_should_return_empty_data() {
public function test_get_items_total_revisions_offset_should_return_empty_data( $method ) {
wp_set_current_user( self::$admin_id );

$per_page = 2;
$offset = $this->total_revisions;
$expected_error = 'rest_revision_invalid_offset_number';
$expected_status = 400;

$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_query_params(
array(
'offset' => $offset,
Expand All @@ -717,19 +763,23 @@ public function test_get_items_total_revisions_offset_should_return_empty_data()
*
* Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_out_of_bound_offset_should_error
*
* @dataProvider data_readable_http_methods
* @ticket 58524
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_items
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_out_of_bound_offset_should_error() {
public function test_get_items_out_of_bound_offset_should_error( $method ) {
wp_set_current_user( self::$admin_id );

$per_page = 2;
$offset = $this->total_revisions + 1;
$expected_error = 'rest_revision_invalid_offset_number';
$expected_status = 400;

$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_query_params(
array(
'offset' => $offset,
Expand All @@ -745,19 +795,23 @@ public function test_get_items_out_of_bound_offset_should_error() {
*
* Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_impossible_high_number_offset_should_error
*
* @dataProvider data_readable_http_methods
* @ticket 58524
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_items
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_impossible_high_number_offset_should_error() {
public function test_get_items_impossible_high_number_offset_should_error( $method ) {
wp_set_current_user( self::$admin_id );

$per_page = 2;
$offset = REST_TESTS_IMPOSSIBLY_HIGH_NUMBER;
$expected_error = 'rest_revision_invalid_offset_number';
$expected_status = 400;

$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_query_params(
array(
'offset' => $offset,
Expand All @@ -773,19 +827,23 @@ public function test_get_items_impossible_high_number_offset_should_error() {
*
* Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_invalid_offset_should_error
*
* @dataProvider data_readable_http_methods
* @ticket 58524
* @ticket 56481
*
* @covers WP_REST_Global_Styles_Controller::get_items
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_invalid_offset_should_error() {
public function test_get_items_invalid_offset_should_error( $method ) {
wp_set_current_user( self::$admin_id );

$per_page = 2;
$offset = 'moreplease';
$expected_error = 'rest_invalid_param';
$expected_status = 400;

$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_query_params(
array(
'offset' => $offset,
Expand Down

0 comments on commit f2ba32a

Please sign in to comment.