admin管理员组文章数量:1320661
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?
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?
2 Answers
Reset to default 1to 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,
]);
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:
- The method.
- The route.
- 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']
);
}
}
本文标签: testingRest API in integration testsfiltering by slug not working
版权声明:本文标题:testing - Rest API in integration tests - filtering by slug not working? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742064835a2418772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
{{URL}}/wp-json/wp/v2/pages?slug=api-docs
where URL variable changes based on the environment, and is the URL of the app (www.example
) – dingo_d Commented Jan 2, 2020 at 13:07