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 23, 2024
1 parent cdb9397 commit 6ef5d6a
Showing 1 changed file with 89 additions and 8 deletions.
97 changes: 89 additions & 8 deletions tests/phpunit/tests/rest-api/rest-block-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,47 +602,128 @@ public function test_get_item_schema() {
}

/**
* @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 ) {
$block_name = 'fake/test';
wp_set_current_user( self::$admin_id );

$hook_name = 'rest_prepare_block_type';
$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' ) );
$request = new WP_REST_Request( $method, '/wp/v2/block-types/' . $block_name );
$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 called when it should not be for 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.' );
}

/**
* 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' ),
);
}

/**
* @ticket 56481
*/
public function test_get_items_with_head_request_should_not_prepare_block_type_data() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'HEAD', '/wp/v2/block-types' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}

/**
* @dataProvider data_readable_http_methods
* @ticket 47620
* @ticket 56481
*
* @param string $method HTTP method to use.
*/
public function test_get_items_wrong_permission() {
public function test_get_items_wrong_permission( $method ) {
wp_set_current_user( self::$subscriber_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-types' );
$request = new WP_REST_Request( $method, '/wp/v2/block-types' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 403 );
}

/**
* @dataProvider data_readable_http_methods
* @ticket 47620
* @ticket 56481
*
* @param string $method HTTP method to use.
*/
public function test_get_item_wrong_permission() {
public function test_get_item_wrong_permission( $method ) {
wp_set_current_user( self::$subscriber_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-types/fake/test' );
$request = new WP_REST_Request( $method, '/wp/v2/block-types/fake/test' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 403 );
}

/**
* @dataProvider data_readable_http_methods
* @ticket 47620
* @ticket 56481
*
* @param string $method HTTP method to use.
*/
public function test_get_items_no_permission() {
public function test_get_items_no_permission( $method ) {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-types' );
$request = new WP_REST_Request( $method, '/wp/v2/block-types' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 );
}

/**
* @dataProvider data_readable_http_methods
* @ticket 47620
* @ticket 56481
*
* @param string $method HTTP method to use.
*/
public function test_get_item_no_permission() {
public function test_get_item_no_permission( $method ) {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-types/fake/test' );
$request = new WP_REST_Request( $method, '/wp/v2/block-types/fake/test' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 );
}

/**
* @dataProvider data_readable_http_methods
* @ticket 47620
* @ticket 56481
*
* @param string $method HTTP method to use.
*/
public function test_prepare_item() {
$registry = new WP_Block_Type_Registry();
Expand Down

0 comments on commit 6ef5d6a

Please sign in to comment.