Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit Tests for 3004 changes #3022

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add Unit Tests for 3004 changes
  • Loading branch information
burhandodhy committed Sep 23, 2022
commit 4d14ec9acbd6c54ec6faf41aa7135c0e1be6c593
67 changes: 67 additions & 0 deletions tests/php/features/TestSearchOrdering.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,73 @@ public function testRestApiInit() {
remove_filter( 'rest_url', [ $this, 'filter_rest_url_for_leading_slash' ], 10, 2 );
}

/**
* Test API endpoints are accessible for users with `manage_options` capability.
*
* @since 4.4.0
*/
public function testUserWithManageOptionsCapabilityCanAccessAPI() {

global $wp_rest_server;
/** @var WP_REST_Server $wp_rest_server */
$wp_rest_server = new \WP_REST_Server;
do_action( 'rest_api_init', $wp_rest_server );

$request = new \WP_REST_Request( 'GET', '/elasticpress/v1/pointer_search' );
$request->set_query_params(
array(
's' => 'hello-world',
)
);
$response = $wp_rest_server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );


$request = new \WP_REST_Request( 'GET', '/elasticpress/v1/pointer_preview' );
$request->set_query_params(
array(
's' => 'hello-world',
)
);
$response = $wp_rest_server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
}

/**
* Test API endpoints are not accessible for users without `manage_options` capability.
*
* @since 4.4.0
*/
public function testUserWithOutManageOptionsCapabilityCanNotAccessAPI() {

// Set current user without `manage_options` capability.
wp_set_current_user( $this->factory()->user->create( array( 'role' => 'editor' ) ) );

global $wp_rest_server;
/** @var WP_REST_Server $wp_rest_server */
$wp_rest_server = new \WP_REST_Server;
do_action( 'rest_api_init', $wp_rest_server );

$request = new \WP_REST_Request( 'GET', '/elasticpress/v1/pointer_search' );
$request->set_query_params(
array(
's' => 'hello-world',
)
);
$response = $wp_rest_server->dispatch( $request );
$this->assertEquals( 403, $response->get_status() );


$request = new \WP_REST_Request( 'GET', '/elasticpress/v1/pointer_preview' );
$request->set_query_params(
array(
's' => 'hello-world',
)
);
$response = $wp_rest_server->dispatch( $request );
$this->assertEquals( 403, $response->get_status() );
}

public function filter_rest_url_for_leading_slash( $url, $path ) {
if ( is_multisite() || get_option( 'permalink_structure' ) ) {
return $url;
Expand Down