集成测试中的REST API-按插件过滤不起作用?

时间:2020-01-02 作者:dingo_d

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\' => \'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?

2 个回复
SO网友:Kaperto

要在使用WP\\U REST\\U请求时添加参数,必须执行以下操作:

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

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

SO网友:Fränk

在进行直接WP_REST_Request, 语法与通过REST API的HTTP接口与REST API交互时的语法不同。

所以卡佩托的回答是正确的。因为WP_REST_Request 接受三个参数:

方法如果向路由添加参数,REST API将无法识别它。

这是一个合格测试:

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\']
        );
    }
}