1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class WP_Test_REST_Controller extends WP_Test_REST_TestCase { |
4
|
|
|
|
5
|
|
|
public function setUp() { |
6
|
|
|
parent::setUp(); |
7
|
|
|
$this->request = new WP_REST_Request( 'GET', '/wp/v2/testroute', array( |
8
|
|
|
'args' => array( |
9
|
|
|
'someinteger' => array( |
10
|
|
|
'type' => 'integer', |
11
|
|
|
), |
12
|
|
|
'someboolean' => array( |
13
|
|
|
'type' => 'boolean', |
14
|
|
|
), |
15
|
|
|
'somestring' => array( |
16
|
|
|
'type' => 'string', |
17
|
|
|
), |
18
|
|
|
'someenum' => array( |
19
|
|
|
'type' => 'string', |
20
|
|
|
'enum' => array( 'a' ), |
21
|
|
|
), |
22
|
|
|
'somedate' => array( |
23
|
|
|
'type' => 'string', |
24
|
|
|
'format' => 'date-time', |
25
|
|
|
), |
26
|
|
|
'someemail' => array( |
27
|
|
|
'type' => 'string', |
28
|
|
|
'format' => 'email', |
29
|
|
|
), |
30
|
|
|
), |
31
|
|
|
)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function test_validate_schema_type_integer() { |
35
|
|
|
|
36
|
|
|
$this->assertTrue( |
37
|
|
|
rest_validate_request_arg( '123', $this->request, 'someinteger' ) |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$this->assertErrorResponse( |
41
|
|
|
'rest_invalid_param', |
42
|
|
|
rest_validate_request_arg( 'abc', $this->request, 'someinteger' ) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function test_validate_schema_type_boolean() { |
47
|
|
|
|
48
|
|
|
$this->assertTrue( |
49
|
|
|
rest_validate_request_arg( true, $this->request, 'someboolean' ) |
50
|
|
|
); |
51
|
|
|
$this->assertTrue( |
52
|
|
|
rest_validate_request_arg( false, $this->request, 'someboolean' ) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$this->assertErrorResponse( |
56
|
|
|
'rest_invalid_param', |
57
|
|
|
rest_validate_request_arg( 'true', $this->request, 'someboolean' ) |
58
|
|
|
); |
59
|
|
|
$this->assertErrorResponse( |
60
|
|
|
'rest_invalid_param', |
61
|
|
|
rest_validate_request_arg( '123', $this->request, 'someboolean' ) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function test_validate_schema_type_string() { |
66
|
|
|
|
67
|
|
|
$this->assertTrue( |
68
|
|
|
rest_validate_request_arg( '123', $this->request, 'somestring' ) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$this->assertErrorResponse( |
72
|
|
|
'rest_invalid_param', |
73
|
|
|
rest_validate_request_arg( array( 'foo' => 'bar' ), $this->request, 'somestring' ) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function test_validate_schema_enum() { |
78
|
|
|
|
79
|
|
|
$this->assertTrue( |
80
|
|
|
rest_validate_request_arg( 'a', $this->request, 'someenum' ) |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$this->assertErrorResponse( |
84
|
|
|
'rest_invalid_param', |
85
|
|
|
rest_validate_request_arg( 'd', $this->request, 'someenum' ) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function test_validate_schema_format_email() { |
90
|
|
|
|
91
|
|
|
$this->assertTrue( |
92
|
|
|
rest_validate_request_arg( '[email protected]', $this->request, 'someemail' ) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$this->assertErrorResponse( |
96
|
|
|
'rest_invalid_email', |
97
|
|
|
rest_validate_request_arg( 'd', $this->request, 'someemail' ) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function test_validate_schema_format_date_time() { |
102
|
|
|
|
103
|
|
|
$this->assertTrue( |
104
|
|
|
rest_validate_request_arg( '2010-01-01T12:00:00', $this->request, 'somedate' ) |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$this->assertErrorResponse( |
108
|
|
|
'rest_invalid_date', |
109
|
|
|
rest_validate_request_arg( '2010-18-18T12:00:00', $this->request, 'somedate' ) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
public function test_get_endpoint_args_for_item_schema_description() { |
114
|
|
|
$controller = new WP_REST_Test_Controller(); |
115
|
|
|
$args = $controller->get_endpoint_args_for_item_schema(); |
116
|
|
|
$this->assertEquals( 'A pretty string.', $args['somestring']['description'] ); |
117
|
|
|
$this->assertFalse( isset( $args['someinteger']['description'] ) ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function test_get_endpoint_args_for_item_schema_arg_options() { |
121
|
|
|
|
122
|
|
|
$controller = new WP_REST_Test_Controller(); |
123
|
|
|
$args = $controller->get_endpoint_args_for_item_schema(); |
124
|
|
|
|
125
|
|
|
$this->assertFalse( $args['someargoptions']['required'] ); |
126
|
|
|
$this->assertEquals( '__return_true', $args['someargoptions']['sanitize_callback'] ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function test_get_endpoint_args_for_item_schema_default_value() { |
130
|
|
|
|
131
|
|
|
$controller = new WP_REST_Test_Controller(); |
132
|
|
|
|
133
|
|
|
$args = $controller->get_endpoint_args_for_item_schema(); |
134
|
|
|
|
135
|
|
|
$this->assertEquals( 'a', $args['somedefault']['default'] ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public $rest_the_post_filter_apply_count = 0; |
139
|
|
|
|
140
|
|
|
public function test_get_post() { |
141
|
|
|
$post_id = $this->factory()->post->create( array( 'post_title' => 'Original' ) ); |
142
|
|
|
$controller = new WP_REST_Test_Controller(); |
143
|
|
|
|
144
|
|
|
$post = $controller->get_post( $post_id ); |
145
|
|
|
$this->assertEquals( 'Original', $post->post_title ); |
146
|
|
|
|
147
|
|
|
$filter_apply_count = $this->rest_the_post_filter_apply_count; |
148
|
|
|
add_filter( 'rest_the_post', array( $this, 'filter_rest_the_post_for_test_get_post' ), 10, 2 ); |
149
|
|
|
$post = $controller->get_post( $post_id ); |
150
|
|
|
$this->assertEquals( 'Overridden', $post->post_title ); |
151
|
|
|
$this->assertEquals( 1 + $filter_apply_count, $this->rest_the_post_filter_apply_count ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function filter_rest_the_post_for_test_get_post( $post, $post_id ) { |
155
|
|
|
$this->assertInstanceOf( 'WP_Post', $post ); |
156
|
|
|
$this->assertInternalType( 'int', $post_id ); |
157
|
|
|
$post->post_title = 'Overridden'; |
158
|
|
|
$this->rest_the_post_filter_apply_count += 1; |
159
|
|
|
return $post; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|