Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace CalderaLearn\RestSearch\Tests\Integration;
use CalderaLearn\RestSearch\FilterWPQuery;
use CalderaLearn\RestSearch\Tests\Mock\AlwaysFilterWPQuery;
class RestRequestTest extends RestAPITestCase
{
/**
* Ensures that REST API requests will be filtered
*
* @covers FilterWPQuery::callback()
*/
public function testShouldFilter()
{
//Create a request
$request = new \WP_REST_Request('GET', '/wp/v2/posts');
rest_api_loaded();
//Make sure the method returns true
$this->assertTrue(FilterWPQuery::shouldFilter());
}
/**
* Ensure that REST API response data was correctly altered
*
* @covers FilterWPQuery::shouldFilter();
* @covers FilterWPQuery::callback()
*/
public function testFilteringRESTRequest()
{
//Setup filter
AlwaysFilterWPQuery::addFilter();
$this->assertTrue(AlwaysFilterWPQuery::shouldFilter());
//Create a request
$request = new \WP_REST_Request('GET', '/wp/v2/posts');
//Dispatch request
$response = rest_get_server()->dispatch($request);
//Test response status
$this->assertSame(200, $response->get_status());
//Test the response data
//Use the mock data we have in our mock class as the expected values
$expected = FilterWPQuery::getPosts();
//Test that the expected results and the actual results are the same
$responseData = $response->get_data();
$this->assertTrue(is_array($responseData));
$this->assertSame(count($expected), count($responseData));
foreach ($responseData as $i => $responsePost) {
$this->assertTrue(isset($expected[$i]));
$this->assertSame($expected[$i]->post_title, $responsePost[ 'title' ][ 'rendered' ]);
}
}
}