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 30, 2024
1 parent 23e5b16 commit c109b0e
Showing 1 changed file with 117 additions and 12 deletions.
129 changes: 117 additions & 12 deletions tests/phpunit/tests/rest-api/rest-autosaves-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,26 +179,76 @@ public function test_get_items() {
$this->check_get_autosave_response( $data[0], $this->post_autosave );
}

public function test_get_items_no_permission() {
/**
* @ticket 56481
*/
public function test_get_items_with_head_request_should_not_prepare_post_data() {
$request = new WP_REST_Request( 'HEAD', '/wp/v2/posts/' . self::$post_id . '/autosaves' );

$hook_name = 'rest_prepare_autosave';
$filter = new MockAction();
$callback = array( $filter, 'filter' );

add_filter( $hook_name, $callback );
$response = rest_get_server()->dispatch( $request );
remove_filter( $hook_name, $callback );

$this->assertNotWPError( $response );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}

/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_no_permission( $method ) {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/autosaves' );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/autosaves' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
wp_set_current_user( self::$contributor_id );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
}

public function test_get_items_missing_parent() {
/**
* 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' ),
);
}

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

public function test_get_items_invalid_parent_post_type() {
/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_invalid_parent_post_type( $method ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/autosaves' );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$page_id . '/autosaves' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
}
Expand Down Expand Up @@ -230,6 +280,43 @@ public function test_get_item() {
$this->assertSame( self::$editor_id, $data['author'] );
}

/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_should_allow_adding_headers_via_filter( $method ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/autosaves/' . self::$autosave_post_id );

$hook_name = 'rest_prepare_autosave';
$filter = new MockAction();
$callback = array( $filter, 'filter' );
add_filter( $hook_name, $callback );
$header_filter = new class() {
public static function add_custom_header( $response ) {
$response->header( 'X-Test-Header', 'Test' );

return $response;
}
};
add_filter( $hook_name, array( $header_filter, 'add_custom_header' ) );
$response = rest_get_server()->dispatch( $request );
remove_filter( $hook_name, $callback );
remove_filter( $hook_name, array( $header_filter, 'add_custom_header' ) );

$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertSame( 1, $filter->get_call_count(), 'The "' . $hook_name . '" filter was not called when it should be for GET/HEAD requests.' );
$headers = $response->get_headers();
$this->assertArrayHasKey( 'X-Test-Header', $headers, 'The "X-Test-Header" header should be present in the response.' );
$this->assertSame( 'Test', $headers['X-Test-Header'], 'The "X-Test-Header" header value should be equal to "Test".' );
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}

public function test_get_item_embed_context() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/autosaves/' . self::$autosave_post_id );
Expand All @@ -248,23 +335,41 @@ public function test_get_item_embed_context() {
$this->assertSameSets( $fields, array_keys( $data ) );
}

public function test_get_item_no_permission() {
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/autosaves/' . self::$autosave_post_id );
/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_no_permission( $method ) {
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/autosaves/' . self::$autosave_post_id );
wp_set_current_user( self::$contributor_id );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
}

public function test_get_item_missing_parent() {
/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_missing_parent( $method ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/autosaves/' . self::$autosave_post_id );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/autosaves/' . self::$autosave_post_id );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
}

public function test_get_item_invalid_parent_post_type() {
/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_invalid_parent_post_type( $method ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/autosaves' );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$page_id . '/autosaves' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
}
Expand Down

0 comments on commit c109b0e

Please sign in to comment.