Rest API in integration tests - filtering by slug not working?

I have a bunch of custom fields in my REST API response, and I need to refactor the code for it, so I'm creating an integration test for it, just to make sure nothing breaks in the process of refactoring.

The testing suite was build using wp scaffold for plugin tests. The test looks like:

?php
/**
 * Class Api_Docs_Page
 *
 * @package My_Plugin\Routes\Endpoints
 */

namespace My_Plugin\Tests\Routes\Endpoints;

use WP_REST_Request;
use WP_UnitTestCase;

/**
 * Class that tests the /wp-json/wp/v2/pages?slug=api-docs response.
 */
class Api_Docs_Page extends WP_UnitTestCase {

  private $author_id;
  private $api_docs_page_id;

  /**
   * Test suite setUp method
   */
  public function setUp() {
    parent::setUp();

    // Set up pretty permalinks so that the rest route works with slugs.
    $this-set_permalink_structure( '/%postname%/' );
    flush_rewrite_rules();

    // The way the plugin is set up requires this to exist if we want to create a user using WP factories.
    $_REQUEST['_wpnonce_create-user'] = wp_create_nonce( 'create-user' );

    $this-author_id = $this-factory-user-create(
      [
        'user_email' = '[email protected]',
        'role' = 'administrator',
      ]
    );

    $this-api_docs_page_id = $this-factory-post-create(
      [
        'post_title' = 'API Docs',
        'post_type'  = 'page',
      ]
    );

    // ACF Field - because I'm still using ACF, don't judge me :p
    update_field( 'api_docs_page', $this-api_docs_page_id, 'options' );

    // Create the page and all the fields. Will come later on
  }

  /**
   * Test suite tearDown method
   */
  public function tearDown() {
    parent::tearDown();
  }

  /**
   * Test if the response is correct
   */
  public function test_api_docs_page_response() {
    // $request  = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    // $request  = new WP_REST_Request( 'GET', '/wp/v2/pages?slug=api-docs' );
    $request  = new WP_REST_Request( 'GET', "/wp/v2/pages/{$this-api_docs_page_id}" );
    $response = rest_get_server()-dispatch( $request );

    $page_data = $response-get_data();

    error_log( print_r( $page_data, true ) );

  }
}

Now, the $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); works, and I see the data when I run my test, all great.

This $request = new WP_REST_Request( 'GET', "/wp/v2/pages/{$this-api_docs_page_id}" ); also works, and I can see the page response I've created with my factory method.

But this

$request = new WP_REST_Request( 'GET', '/wp/v2/pages?slug=api-docs' );

Doesn't work and returns

(
    [code] = rest_no_route
    [message] = No route was found matching the URL and request method
    [data] = Array
        (
            [status] = 404
        )

)

The real response, when I try it in the postman works with the slug.

I've added the permalink structure in the setUp method but I'm not sure that helped.

Any idea why the slug lookup isn't working in my test?

Topic integration-tests rest-api testing Wordpress

Category Web


When doing a direct WP_REST_Request, the syntax is not the same as when interacting with the REST API through its HTTP interface.

So Kaperto's response was correct. Because WP_REST_Request accepts three arguments:

  1. The method.
  2. The route.
  3. The arguments.

If you add the arguments to the route, it will not be recognized by the REST API.

This is a passing test:

class Api_Docs_Page extends WP_UnitTestCase {
    public function test_api_docs_page_response() {
        $page_id = self::factory()->post->create(
            [
                'post_title' => 'API Docs',
                'post_type'  => 'page',
            ]
        );

        $request  = new WP_REST_Request(
            'GET',
            "/wp/v2/pages",
            [
                'slug' => 'api-docs'
            ]
        );

        $response = rest_get_server()->dispatch( $request );
        $page_data = $response->get_data();


        $this->assertNotEmpty( $page_data[0] );
        $this->assertSame(
            $page_id,
            $page_data[0]['id']
        );
    }
}

to add parameters when you use WP_REST_Request, you have to do that :

$request = new WP_REST_Request('GET', "/wp/v2/pages");

$request->set_query_params([
    "slug" => $this->api_docs_page_id,
]);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.