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

Question

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_PluginRoutesEndpoints
 */

namespace My_PluginTestsRoutesEndpoints;

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' => 'test@test.com',
        '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?

0
, , dingo_d 2 years 2020-01-02T08:38:33-05:00 0 Answers 95 views 0

Leave an answer

Browse
Browse