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 d9f7263 commit bc46edd
Showing 1 changed file with 138 additions and 10 deletions.
148 changes: 138 additions & 10 deletions tests/phpunit/tests/rest-api/rest-widgets-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,31 @@ public function test_get_items_no_widgets() {
}

/**
* @dataProvider data_readable_http_methods
* @ticket 41683
* @ticket 56481
*
* @param string $method The 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/widgets' );
$request = new WP_REST_Request( $method, '/wp/v2/widgets' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 );
}

/**
* 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 53915
*/
Expand Down Expand Up @@ -332,11 +348,15 @@ public function test_get_items_without_show_in_rest_are_removed_from_the_list()
}

/**
* @dataProvider data_readable_http_methods
* @ticket 41683
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_wrong_permission_author() {
public function test_get_items_wrong_permission_author( $method ) {
wp_set_current_user( self::$author_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/widgets' );
$request = new WP_REST_Request( $method, '/wp/v2/widgets' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 );
}
Expand Down Expand Up @@ -377,8 +397,9 @@ public function test_get_items() {

$request = new WP_REST_Request( 'GET', '/wp/v2/widgets' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$data = $this->remove_links( $data );
remove_filter( 'pre_http_request', array( $this, 'mocked_rss_response' ) );
$data = $response->get_data();
$data = $this->remove_links( $data );
$this->assertSameSets(
array(
array(
Expand Down Expand Up @@ -406,6 +427,54 @@ public function test_get_items() {
$wp_widget_factory->widgets['WP_Widget_RSS']->widget_options['show_instance_in_rest'] = true;
}

/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_with_head_request_should_not_prepare_widget_data( $method ) {
$block_content = '<!-- wp:paragraph --><p>Block test</p><!-- /wp:paragraph -->';

$this->setup_widget(
'rss',
1,
array(
'title' => 'RSS test',
'url' => 'https://wordpress.org/news/feed',
)
);
$this->setup_widget(
'block',
1,
array(
'content' => $block_content,
)
);
$this->setup_sidebar(
'sidebar-1',
array(
'name' => 'Test sidebar',
),
array( 'block-1', 'rss-1', 'testwidget' )
);

$request = new WP_REST_Request( 'HEAD', '/wp/v2/widgets' );

$hook_name = 'rest_prepare_post';
$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.' );
}

public function mocked_rss_response() {
$single_value_headers = array(
'Content-Type' => 'application/rss+xml; charset=UTF-8',
Expand Down Expand Up @@ -528,9 +597,64 @@ public function test_get_item() {
}

/**
* @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 ) {
$this->setup_widget(
'text',
1,
array(
'text' => 'Custom text test',
)
);
$this->setup_sidebar(
'sidebar-1',
array(
'name' => 'Test sidebar',
),
array( 'text-1' )
);

$request = new WP_REST_Request( $method, '/wp/v2/widgets/text-1' );

$hook_name = 'rest_prepare_widget';
$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.' );
}

/**
* @dataProvider data_readable_http_methods
* @ticket 41683
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_no_permission() {
public function test_get_item_no_permission( $method ) {
wp_set_current_user( 0 );

$this->setup_widget(
Expand All @@ -548,15 +672,19 @@ public function test_get_item_no_permission() {
array( 'text-1' )
);

$request = new WP_REST_Request( 'GET', '/wp/v2/widgets/text-1' );
$request = new WP_REST_Request( $method, '/wp/v2/widgets/text-1' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 );
}

/**
* @dataProvider data_readable_http_methods
* @ticket 41683
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_wrong_permission_author() {
public function test_get_item_wrong_permission_author( $method ) {
wp_set_current_user( self::$author_id );
$this->setup_widget(
'text',
Expand All @@ -572,7 +700,7 @@ public function test_get_item_wrong_permission_author() {
)
);

$request = new WP_REST_Request( 'GET', '/wp/v2/widgets/text-1' );
$request = new WP_REST_Request( $method, '/wp/v2/widgets/text-1' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 );
}
Expand Down

0 comments on commit bc46edd

Please sign in to comment.