-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtestcase-rest-post-type-controller.php
More file actions
317 lines (270 loc) · 11.2 KB
/
testcase-rest-post-type-controller.php
File metadata and controls
317 lines (270 loc) · 11.2 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
abstract class WP_Test_REST_Post_Type_Controller_Testcase extends WP_Test_REST_Controller_Testcase {
protected function check_post_data( $post, $data, $context, $links ) {
$post_type_obj = get_post_type_object( $post->post_type );
// Standard fields.
$this->assertEquals( $post->ID, $data['id'] );
$this->assertEquals( $post->post_name, $data['slug'] );
$this->assertEquals( get_permalink( $post->ID ), $data['link'] );
if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
$post_date_gmt = gmdate( 'Y-m-d H:i:s', strtotime( $post->post_date ) - ( get_option( 'gmt_offset' ) * 3600 ) );
$this->assertEquals( mysql_to_rfc3339( $post_date_gmt ), $data['date_gmt'] );
} else {
$this->assertEquals( mysql_to_rfc3339( $post->post_date_gmt ), $data['date_gmt'] );
}
$this->assertEquals( mysql_to_rfc3339( $post->post_date ), $data['date'] );
if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) {
$post_modified_gmt = gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * 3600 ) );
$this->assertEquals( mysql_to_rfc3339( $post_modified_gmt ), $data['modified_gmt'] );
} else {
$this->assertEquals( mysql_to_rfc3339( $post->post_modified_gmt ), $data['modified_gmt'] );
}
$this->assertEquals( mysql_to_rfc3339( $post->post_modified ), $data['modified'] );
// Author.
if ( post_type_supports( $post->post_type, 'author' ) ) {
$this->assertEquals( $post->post_author, $data['author'] );
} else {
$this->assertEmpty( $data['author'] );
}
// Post parent.
if ( $post_type_obj->hierarchical ) {
$this->assertArrayHasKey( 'parent', $data );
if ( $post->post_parent ) {
if ( is_int( $data['parent'] ) ) {
$this->assertEquals( $post->post_parent, $data['parent'] );
} else {
$this->assertEquals( $post->post_parent, $data['parent']['id'] );
$this->check_get_post_response( $data['parent'], get_post( $data['parent']['id'] ), 'view-parent' );
}
} else {
$this->assertEmpty( $data['parent'] );
}
} else {
$this->assertFalse( isset( $data['parent'] ) );
}
// Page attributes.
if ( $post_type_obj->hierarchical && post_type_supports( $post->post_type, 'page-attributes' ) ) {
$this->assertEquals( $post->menu_order, $data['menu_order'] );
} else {
$this->assertFalse( isset( $data['menu_order'] ) );
}
// Comments.
if ( post_type_supports( $post->post_type, 'comments' ) ) {
$this->assertEquals( $post->comment_status, $data['comment_status'] );
$this->assertEquals( $post->ping_status, $data['ping_status'] );
} else {
$this->assertFalse( isset( $data['comment_status'] ) );
$this->assertFalse( isset( $data['ping_status'] ) );
}
if ( 'post' === $post->post_type ) {
$this->assertEquals( is_sticky( $post->ID ), $data['sticky'] );
}
if ( 'post' === $post->post_type && 'edit' === $context ) {
$this->assertEquals( $post->post_password, $data['password'] );
}
if ( 'page' === $post->post_type ) {
$this->assertEquals( get_page_template_slug( $post->ID ), $data['template'] );
}
if ( post_type_supports( $post->post_type, 'thumbnail' ) ) {
$this->assertEquals( (int) get_post_thumbnail_id( $post->ID ), $data['featured_media'] );
} else {
$this->assertFalse( isset( $data['featured_media'] ) );
}
// Check post format.
if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
$post_format = get_post_format( $post->ID );
if ( empty( $post_format ) ) {
$this->assertEquals( 'standard', $data['format'] );
} else {
$this->assertEquals( get_post_format( $post->ID ), $data['format'] );
}
} else {
$this->assertFalse( isset( $data['format'] ) );
}
// Check filtered values.
if ( post_type_supports( $post->post_type, 'title' ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
$this->assertEquals( get_the_title( $post->ID ), $data['title']['rendered'] );
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
if ( 'edit' === $context ) {
$this->assertEquals( $post->post_title, $data['title']['raw'] );
} else {
$this->assertFalse( isset( $data['title']['raw'] ) );
}
} else {
$this->assertFalse( isset( $data['title'] ) );
}
if ( post_type_supports( $post->post_type, 'editor' ) ) {
// TODO: Apply content filter for more accurate testing.
if ( ! $post->post_password ) {
$this->assertEquals( wpautop( $post->post_content ), $data['content']['rendered'] );
}
if ( 'edit' === $context ) {
$this->assertEquals( $post->post_content, $data['content']['raw'] );
} else {
$this->assertFalse( isset( $data['content']['raw'] ) );
}
} else {
$this->assertFalse( isset( $data['content'] ) );
}
if ( post_type_supports( $post->post_type, 'excerpt' ) ) {
if ( empty( $post->post_password ) ) {
// TODO: Apply excerpt filter for more accurate testing.
$this->assertEquals( wpautop( $post->post_excerpt ), $data['excerpt']['rendered'] );
} else {
// TODO: Better testing for excerpts for password protected posts.
}
if ( 'edit' === $context ) {
$this->assertEquals( $post->post_excerpt, $data['excerpt']['raw'] );
} else {
$this->assertFalse( isset( $data['excerpt']['raw'] ) );
}
} else {
$this->assertFalse( isset( $data['excerpt'] ) );
}
$this->assertEquals( $post->post_status, $data['status'] );
$this->assertEquals( $post->guid, $data['guid']['rendered'] );
if ( 'edit' === $context ) {
$this->assertEquals( $post->guid, $data['guid']['raw'] );
}
$taxonomies = wp_list_filter( get_object_taxonomies( $post->post_type, 'objects' ), array( 'show_in_rest' => true ) );
foreach ( $taxonomies as $taxonomy ) {
$this->assertTrue( isset( $data[ $taxonomy->rest_base ] ) );
$terms = wp_get_object_terms( $post->ID, $taxonomy->name, array( 'fields' => 'ids' ) );
sort( $terms );
sort( $data[ $taxonomy->rest_base ] );
$this->assertEquals( $terms, $data[ $taxonomy->rest_base ] );
}
// Test links.
if ( $links ) {
$links = test_rest_expand_compact_links( $links );
$post_type = get_post_type_object( $data['type'] );
$this->assertEquals( $links['self'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base . '/' . $data['id'] ) );
$this->assertEquals( $links['collection'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base ) );
$this->assertEquals( $links['about'][0]['href'], rest_url( 'wp/v2/types/' . $data['type'] ) );
if ( post_type_supports( $post->post_type, 'author' ) && $data['author'] ) {
$this->assertEquals( $links['author'][0]['href'], rest_url( 'wp/v2/users/' . $data['author'] ) );
}
if ( post_type_supports( $post->post_type, 'comments' ) ) {
$this->assertEquals( $links['replies'][0]['href'], add_query_arg( 'post', $data['id'], rest_url( 'wp/v2/comments' ) ) );
}
if ( post_type_supports( $post->post_type, 'revisions' ) ) {
$this->assertEquals( $links['version-history'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base . '/' . $data['id'] . '/revisions' ) );
}
if ( $post_type->hierarchical && ! empty( $data['parent'] ) ) {
$this->assertEquals( $links['up'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base . '/' . $data['parent'] ) );
}
if ( ! in_array( $data['type'], array( 'attachment', 'nav_menu_item', 'revision' ), true ) ) {
$this->assertEquals( $links['https://api.w.org/attachment'][0]['href'], add_query_arg( 'parent', $data['id'], rest_url( 'wp/v2/media' ) ) );
}
if ( ! empty( $data['featured_media'] ) ) {
$this->assertEquals( $links['https://api.w.org/featuredmedia'][0]['href'], rest_url( 'wp/v2/media/' . $data['featured_media'] ) );
}
$num = 0;
foreach ( $taxonomies as $key => $taxonomy ) {
$this->assertEquals( $taxonomy->name, $links['https://api.w.org/term'][ $num ]['attributes']['taxonomy'] );
$this->assertEquals( add_query_arg( 'post', $data['id'], rest_url( 'wp/v2/' . $taxonomy->rest_base ) ), $links['https://api.w.org/term'][ $num ]['href'] );
$num++;
}
}
}
protected function check_get_posts_response( $response, $context = 'view' ) {
$this->assertNotWPError( $response );
$response = rest_ensure_response( $response );
$this->assertEquals( 200, $response->get_status() );
$headers = $response->get_headers();
$this->assertArrayHasKey( 'X-WP-Total', $headers );
$this->assertArrayHasKey( 'X-WP-TotalPages', $headers );
$all_data = $response->get_data();
foreach ( $all_data as $data ) {
$post = get_post( $data['id'] );
// As the links for the post are "response_links" format in the data array,
// we have to pull them out and parse them.
$links = $data['_links'];
foreach ( $links as &$links_array ) {
foreach ( $links_array as &$link ) {
$attributes = array_diff_key(
$link,
array(
'href' => 1,
'name' => 1,
)
);
$link = array_diff_key( $link, $attributes );
$link['attributes'] = $attributes;
}
}
$this->check_post_data( $post, $data, $context, $links );
}
}
protected function check_get_post_response( $response, $context = 'view' ) {
$this->assertNotWPError( $response );
$response = rest_ensure_response( $response );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$post = get_post( $data['id'] );
$this->check_post_data( $post, $data, $context, $response->get_links() );
}
protected function check_create_post_response( $response ) {
$this->assertNotWPError( $response );
$response = rest_ensure_response( $response );
$this->assertEquals( 201, $response->get_status() );
$headers = $response->get_headers();
$this->assertArrayHasKey( 'Location', $headers );
$data = $response->get_data();
$post = get_post( $data['id'] );
$this->check_post_data( $post, $data, 'edit', $response->get_links() );
}
protected function check_update_post_response( $response ) {
$this->assertNotWPError( $response );
$response = rest_ensure_response( $response );
$this->assertEquals( 200, $response->get_status() );
$headers = $response->get_headers();
$this->assertArrayNotHasKey( 'Location', $headers );
$data = $response->get_data();
$post = get_post( $data['id'] );
$this->check_post_data( $post, $data, 'edit', $response->get_links() );
}
protected function set_post_data( $args = array() ) {
$defaults = array(
'title' => 'Post Title',
'content' => 'Post content',
'excerpt' => 'Post excerpt',
'name' => 'test',
'status' => 'publish',
'author' => get_current_user_id(),
'type' => 'post',
);
return wp_parse_args( $args, $defaults );
}
protected function set_raw_post_data( $args = array() ) {
return wp_parse_args(
$args,
$this->set_post_data(
array(
'title' => array(
'raw' => 'Post Title',
),
'content' => array(
'raw' => 'Post content',
),
'excerpt' => array(
'raw' => 'Post excerpt',
),
)
)
);
}
/**
* Overwrite the default protected title format.
*
* By default WordPress will show password protected posts with a title of
* "Protected: %s", as the REST API communicates the protected status of a post
* in a machine readable format, we remove the "Protected: " prefix.
*
* @return string
*/
public function protected_title_format() {
return '%s';
}
}