WP_Test_REST_Posts_Controller   D
last analyzed

Complexity

Total Complexity 129

Size/Duplication

Total Lines 1906
Duplicated Lines 23.71 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 452
loc 1906
rs 4.4102
c 0
b 0
f 0
wmc 129
lcom 1
cbo 2

115 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 17 1
A test_register_routes() 8 8 1
A test_context_param() 14 14 1
B test_registered_query_params() 0 28 1
A test_get_items() 0 6 1
A test_get_items_empty_query() 0 9 1
B test_get_items_author_query() 0 25 1
B test_get_items_author_exclude_query() 0 27 1
A test_get_items_include_query() 0 18 1
A test_get_items_exclude_query() 0 14 1
A test_get_items_slug_query() 0 11 1
A test_get_items_status_query() 0 19 1
A test_get_items_status_without_permissions() 16 16 2
A test_get_items_order_and_orderby() 0 18 1
A test_get_items_with_orderby_relevance() 0 9 1
A test_get_items_ignore_sticky_posts_by_default() 0 20 1
A test_get_items_offset_query() 0 18 1
A test_get_items_tags_query() 0 14 1
A test_get_items_tags_and_categories_query() 0 19 1
A test_get_items_sticky_query() 16 16 1
B test_get_items_sticky_with_post__in_query() 0 28 1
A test_get_items_not_sticky_query() 16 16 1
A test_get_items_sticky_with_post__not_in_query() 0 18 1
A test_get_items_search_query() 3 15 2
B test_get_items_pagination_headers() 0 78 2
A test_get_items_private_filter_query_var() 17 17 1
A test_get_items_invalid_per_page() 6 6 1
A test_get_items_invalid_posts_per_page_ignored() 3 11 2
A test_get_items_invalid_context() 6 6 1
A test_get_items_invalid_date() 7 7 1
A test_get_items_valid_date() 0 13 1
A test_get_item() 0 6 1
B test_get_item_links() 0 42 5
A test_get_item_links_no_author() 0 11 1
A test_get_post_without_permission() 0 11 1
A test_get_post_invalid_id() 0 6 1
A test_get_post_list_context_with_permission() 0 12 1
A test_get_post_list_context_without_permission() 10 10 1
A test_get_post_context_without_permission() 10 10 1
A test_get_post_with_password() 14 14 1
A test_get_post_with_password_using_password() 0 23 2
A test_get_post_with_password_using_incorrect_password() 12 12 1
A test_get_post_with_password_without_permission() 0 14 1
A test_get_item_read_permission_custom_post_status() 0 14 1
A test_prepare_item() 0 9 1
A test_create_item() 0 11 1
A test_rest_create_item() 0 11 1
A test_create_post_invalid_id() 0 12 1
A test_create_post_as_contributor() 0 12 1
A test_create_post_sticky() 0 15 1
A test_create_post_sticky_as_contributor() 0 13 1
A test_create_post_other_author_without_permission() 0 12 1
A test_create_post_without_permission() 0 12 1
A test_create_post_draft() 18 18 1
A test_create_post_private() 15 15 1
A test_create_post_private_without_permission() 0 18 1
A test_create_post_publish_without_permission() 0 17 1
A test_create_post_invalid_status() 0 12 1
A test_create_post_with_format() 15 15 1
A test_create_post_with_invalid_format() 0 12 1
B test_create_update_post_with_featured_media() 0 31 1
A test_create_post_invalid_author() 0 12 1
A test_create_post_invalid_author_without_permission() 0 12 1
A test_create_post_with_password() 0 13 1
A test_create_post_with_falsy_password() 0 14 1
A test_create_post_with_empty_string_password_and_sticky() 15 15 1
A test_create_post_with_password_and_sticky_fails() 0 13 1
A test_create_post_custom_date() 0 16 1
A test_create_post_custom_date_with_timezone() 0 20 1
A test_create_post_with_db_error() 0 21 1
A test_create_post_with_invalid_date() 0 12 1
A test_create_post_with_invalid_date_gmt() 0 12 1
A test_create_post_with_quotes_in_title() 12 12 1
A test_create_post_with_categories() 0 16 1
A test_create_post_with_invalid_categories() 0 15 1
A test_update_item() 20 20 1
A test_rest_update_post() 20 20 1
A test_rest_update_post_raw() 0 20 1
A test_update_post_without_extra_params() 0 14 1
A test_update_post_without_permission() 0 15 1
A test_update_post_sticky_as_contributor() 13 13 1
A test_update_post_invalid_id() 0 8 1
A test_update_post_invalid_route() 0 8 1
A test_update_post_with_format() 15 15 1
A test_update_post_with_invalid_format() 0 12 1
B test_update_post_ignore_readonly() 0 25 1
A test_update_post_with_invalid_date() 12 12 1
A test_update_post_with_invalid_date_gmt() 12 12 1
A test_update_post_slug() 15 15 1
A test_update_post_slug_accented_chars() 15 15 1
B test_update_post_sticky() 0 28 1
A test_update_post_excerpt() 0 12 1
A test_update_post_empty_excerpt() 0 12 1
A test_update_post_content() 0 12 1
A test_update_post_empty_content() 0 12 1
A test_update_post_with_password_and_sticky_fails() 13 13 1
A test_update_stick_post_with_password_fails() 0 14 1
A test_update_password_protected_post_with_sticky_fails() 14 14 1
A test_update_post_with_quotes_in_title() 0 12 1
B test_update_post_with_categories() 0 33 3
A test_update_post_with_empty_categories() 0 16 1
A test_delete_item() 12 12 1
A test_delete_item_skip_trash() 13 13 1
A test_delete_item_already_trashed() 9 9 1
A test_delete_post_invalid_id() 8 8 1
A test_delete_post_invalid_post_type() 0 9 1
A test_delete_post_without_permission() 0 8 1
A test_register_post_type_invalid_controller() 0 9 1
B test_get_item_schema() 0 29 1
A test_get_additional_field_registration() 0 53 1
B test_additional_field_update_errors() 28 28 1
A additional_field_get_callback() 0 3 1
A additional_field_update_callback() 0 6 2
A tearDown() 0 7 2
A error_insert_query() 0 6 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like WP_Test_REST_Posts_Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WP_Test_REST_Posts_Controller, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Unit tests covering WP_REST_Posts_Controller functionality.
5
 *
6
 * @package WordPress
7
 * @subpackage JSON API
8
 */
9
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Testcase {
10
11
	public function setUp() {
12
		parent::setUp();
13
14
		$this->post_id = $this->factory->post->create();
15
16
		$this->editor_id = $this->factory->user->create( array(
17
			'role' => 'editor',
18
		) );
19
		$this->author_id = $this->factory->user->create( array(
20
			'role' => 'author',
21
		) );
22
		$this->contributor_id = $this->factory->user->create( array(
23
			'role' => 'contributor',
24
		) );
25
26
		register_post_type( 'youseeme', array( 'supports' => array(), 'show_in_rest' => true ) );
27
	}
28
29 View Code Duplication
	public function test_register_routes() {
30
		$routes = $this->server->get_routes();
31
32
		$this->assertArrayHasKey( '/wp/v2/posts', $routes );
33
		$this->assertCount( 2, $routes['/wp/v2/posts'] );
34
		$this->assertArrayHasKey( '/wp/v2/posts/(?P<id>[\d]+)', $routes );
35
		$this->assertCount( 3, $routes['/wp/v2/posts/(?P<id>[\d]+)'] );
36
	}
37
38 View Code Duplication
	public function test_context_param() {
39
		// Collection
40
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
41
		$response = $this->server->dispatch( $request );
42
		$data = $response->get_data();
43
		$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
44
		$this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
45
		// Single
46
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id );
47
		$response = $this->server->dispatch( $request );
48
		$data = $response->get_data();
49
		$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
50
		$this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
51
	}
52
53
	public function test_registered_query_params() {
54
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
55
		$response = $this->server->dispatch( $request );
56
		$data = $response->get_data();
57
		$keys = array_keys( $data['endpoints'][0]['args'] );
58
		sort( $keys );
59
		$this->assertEquals( array(
60
			'after',
61
			'author',
62
			'author_exclude',
63
			'before',
64
			'categories',
65
			'context',
66
			'exclude',
67
			'filter',
68
			'include',
69
			'offset',
70
			'order',
71
			'orderby',
72
			'page',
73
			'per_page',
74
			'search',
75
			'slug',
76
			'status',
77
			'sticky',
78
			'tags',
79
			), $keys );
80
	}
81
82
	public function test_get_items() {
83
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
84
		$response = $this->server->dispatch( $request );
85
86
		$this->check_get_posts_response( $response );
87
	}
88
89
	/**
90
	 * A valid query that returns 0 results should return an empty JSON list.
91
	 *
92
	 * @issue 862
93
	 */
94
	public function test_get_items_empty_query() {
95
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
96
		$request->set_query_params( array(
97
			'filter' => array( 'year' => 2008 ),
98
		) );
99
		$response = $this->server->dispatch( $request );
100
		$this->assertEquals( array(), $response->get_data() );
101
		$this->assertEquals( 200, $response->get_status() );
102
	}
103
104
	public function test_get_items_author_query() {
105
		$this->factory->post->create( array( 'post_author' => $this->editor_id ) );
106
		$this->factory->post->create( array( 'post_author' => $this->author_id ) );
107
		// All 3 posts
108
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
109
		$response = $this->server->dispatch( $request );
110
		$this->assertEquals( 200, $response->get_status() );
111
		$this->assertEquals( 3, count( $response->get_data() ) );
112
		// 2 of 3 posts
113
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
114
		$request->set_param( 'author', array( $this->editor_id, $this->author_id ) );
115
		$response = $this->server->dispatch( $request );
116
		$this->assertEquals( 200, $response->get_status() );
117
		$data = $response->get_data();
118
		$this->assertEquals( 2, count( $data ) );
119
		$this->assertEqualSets( array( $this->editor_id, $this->author_id ), wp_list_pluck( $data, 'author' ) );
120
		// 1 of 3 posts
121
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
122
		$request->set_param( 'author', $this->editor_id );
123
		$response = $this->server->dispatch( $request );
124
		$this->assertEquals( 200, $response->get_status() );
125
		$data = $response->get_data();
126
		$this->assertEquals( 1, count( $data ) );
127
		$this->assertEquals( $this->editor_id, $data[0]['author'] );
128
	}
129
130
	public function test_get_items_author_exclude_query() {
131
		$this->factory->post->create( array( 'post_author' => $this->editor_id ) );
132
		$this->factory->post->create( array( 'post_author' => $this->author_id ) );
133
		// All 3 posts
134
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
135
		$response = $this->server->dispatch( $request );
136
		$this->assertEquals( 200, $response->get_status() );
137
		$this->assertEquals( 3, count( $response->get_data() ) );
138
		// 1 of 3 posts
139
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
140
		$request->set_param( 'author_exclude', array( $this->editor_id, $this->author_id ) );
141
		$response = $this->server->dispatch( $request );
142
		$this->assertEquals( 200, $response->get_status() );
143
		$data = $response->get_data();
144
		$this->assertEquals( 1, count( $data ) );
145
		$this->assertNotEquals( $this->editor_id, $data[0]['author'] );
146
		$this->assertNotEquals( $this->author_id, $data[0]['author'] );
147
		// 2 of 3 posts
148
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
149
		$request->set_param( 'author_exclude', $this->editor_id );
150
		$response = $this->server->dispatch( $request );
151
		$this->assertEquals( 200, $response->get_status() );
152
		$data = $response->get_data();
153
		$this->assertEquals( 2, count( $data ) );
154
		$this->assertNotEquals( $this->editor_id, $data[0]['author'] );
155
		$this->assertNotEquals( $this->editor_id, $data[1]['author'] );
156
	}
157
158
	public function test_get_items_include_query() {
159
		$id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
160
		$this->factory->post->create( array( 'post_status' => 'publish' ) );
161
		$id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
162
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
163
		// Orderby=>desc
164
		$request->set_param( 'include', array( $id1, $id3 ) );
165
		$response = $this->server->dispatch( $request );
166
		$data = $response->get_data();
167
		$this->assertEquals( 2, count( $data ) );
168
		$this->assertEquals( $id3, $data[0]['id'] );
169
		// Orderby=>include
170
		$request->set_param( 'orderby', 'include' );
171
		$response = $this->server->dispatch( $request );
172
		$data = $response->get_data();
173
		$this->assertEquals( 2, count( $data ) );
174
		$this->assertEquals( $id1, $data[0]['id'] );
175
	}
176
177
	public function test_get_items_exclude_query() {
178
		$id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
179
		$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
180
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
181
		$response = $this->server->dispatch( $request );
182
		$data = $response->get_data();
183
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) );
184
		$this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ) ) );
185
		$request->set_param( 'exclude', array( $id2 ) );
186
		$response = $this->server->dispatch( $request );
187
		$data = $response->get_data();
188
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) );
189
		$this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ) ) );
190
	}
191
192
	public function test_get_items_search_query() {
193 View Code Duplication
		for ( $i = 0;  $i < 5;  $i++ ) {
194
			$this->factory->post->create( array( 'post_status' => 'publish' ) );
195
		}
196
		$this->factory->post->create( array( 'post_title' => 'Search Result', 'post_status' => 'publish' ) );
197
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
198
		$response = $this->server->dispatch( $request );
199
		$this->assertEquals( 7, count( $response->get_data() ) );
200
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
201
		$request->set_param( 'search', 'Search Result' );
202
		$response = $this->server->dispatch( $request );
203
		$data = $response->get_data();
204
		$this->assertEquals( 1, count( $data ) );
205
		$this->assertEquals( 'Search Result', $data[0]['title']['rendered'] );
206
	}
207
208
	public function test_get_items_slug_query() {
209
		$this->factory->post->create( array( 'post_title' => 'Apple', 'post_status' => 'publish' ) );
210
		$this->factory->post->create( array( 'post_title' => 'Banana', 'post_status' => 'publish' ) );
211
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
212
		$request->set_param( 'slug', 'apple' );
213
		$response = $this->server->dispatch( $request );
214
		$this->assertEquals( 200, $response->get_status() );
215
		$data = $response->get_data();
216
		$this->assertEquals( 1, count( $data ) );
217
		$this->assertEquals( 'Apple', $data[0]['title']['rendered'] );
218
	}
219
220
	public function test_get_items_status_query() {
221
		wp_set_current_user( 0 );
222
		$this->factory->post->create( array( 'post_status' => 'draft' ) );
223
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
224
		$request->set_param( 'status', 'publish' );
225
		$response = $this->server->dispatch( $request );
226
		$this->assertEquals( 200, $response->get_status() );
227
		$this->assertEquals( 1, count( $response->get_data() ) );
228
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
229
		$request->set_param( 'status', 'draft' );
230
		$response = $this->server->dispatch( $request );
231
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
232
		wp_set_current_user( $this->editor_id );
233
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
234
		$request->set_param( 'status', 'draft' );
235
		$response = $this->server->dispatch( $request );
236
		$this->assertEquals( 200, $response->get_status() );
237
		$this->assertEquals( 1, count( $response->get_data() ) );
238
	}
239
240 View Code Duplication
	public function test_get_items_status_without_permissions() {
241
		$draft_id = $this->factory->post->create( array(
242
			'post_status' => 'draft',
243
		) );
244
		wp_set_current_user( 0 );
245
246
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
247
		$response = $this->server->dispatch( $request );
248
249
		$this->assertEquals( 200, $response->get_status() );
250
251
		$all_data = $response->get_data();
252
		foreach ( $all_data as $post ) {
253
			$this->assertNotEquals( $draft_id, $post['id'] );
254
		}
255
	}
256
257
	public function test_get_items_order_and_orderby() {
258
		$this->factory->post->create( array( 'post_title' => 'Apple Pie', 'post_status' => 'publish' ) );
259
		$this->factory->post->create( array( 'post_title' => 'Apple Sauce', 'post_status' => 'publish' ) );
260
		$this->factory->post->create( array( 'post_title' => 'Apple Cobbler', 'post_status' => 'publish' ) );
261
		$this->factory->post->create( array( 'post_title' => 'Apple Coffee Cake', 'post_status' => 'publish' ) );
262
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
263
		$request->set_param( 'search', 'Apple' );
264
		// order defaults to 'desc'
265
		$request->set_param( 'orderby', 'title' );
266
		$response = $this->server->dispatch( $request );
267
		$data = $response->get_data();
268
		$this->assertEquals( 'Apple Sauce', $data[0]['title']['rendered'] );
269
		// order=>asc
270
		$request->set_param( 'order', 'asc' );
271
		$response = $this->server->dispatch( $request );
272
		$data = $response->get_data();
273
		$this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] );
274
	}
275
276
	public function test_get_items_with_orderby_relevance() {
277
		$this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) );
278
		$this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) );
279
280
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
281
		$request->set_param( 'orderby', 'relevance' );
282
		$response = $this->server->dispatch( $request );
283
		$this->assertErrorResponse( 'rest_no_search_term_defined', $response, 400 );
284
	}
285
286
	public function test_get_items_ignore_sticky_posts_by_default() {
287
		$this->markTestSkipped( 'Broken, see https://github.com/WP-API/WP-API/issues/2210' );
288
		$post_id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_date' => '2015-01-01 12:00:00', 'post_date_gmt' => '2015-01-01 12:00:00' ) );
289
		$post_id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_date' => '2015-01-02 12:00:00', 'post_date_gmt' => '2015-01-02 12:00:00' ) );
290
		$post_id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_date' => '2015-01-03 12:00:00', 'post_date_gmt' => '2015-01-03 12:00:00' ) );
291
		stick_post( $post_id2 );
292
293
		// No stickies by default
294
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
295
		$response = $this->server->dispatch( $request );
296
		$data = $response->get_data();
297
		$this->assertEquals( array( $this->post_id, $post_id3, $post_id2, $post_id1 ), wp_list_pluck( $data, 'id' ) );
298
299
		// Permit stickies
300
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
301
		$request->set_param( 'filter', array( 'ignore_sticky_posts' => false ) );
302
		$response = $this->server->dispatch( $request );
303
		$data = $response->get_data();
304
		$this->assertEquals( array( $post_id2, $this->post_id, $post_id3, $post_id1 ), wp_list_pluck( $data, 'id' ) );
305
	}
306
307
	public function test_get_items_offset_query() {
308
		$id1 = $this->post_id;
0 ignored issues
show
Unused Code introduced by
$id1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
309
		$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
0 ignored issues
show
Unused Code introduced by
$id2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
310
		$id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
0 ignored issues
show
Unused Code introduced by
$id3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
311
		$id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
0 ignored issues
show
Unused Code introduced by
$id4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
312
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
313
		$request->set_param( 'offset', 1 );
314
		$response = $this->server->dispatch( $request );
315
		$this->assertCount( 3, $response->get_data() );
316
		// 'offset' works with 'per_page'
317
		$request->set_param( 'per_page', 2 );
318
		$response = $this->server->dispatch( $request );
319
		$this->assertCount( 2, $response->get_data() );
320
		// 'offset' takes priority over 'page'
321
		$request->set_param( 'page', 3 );
322
		$response = $this->server->dispatch( $request );
323
		$this->assertCount( 2, $response->get_data() );
324
	}
325
326
	public function test_get_items_tags_query() {
327
		$id1 = $this->post_id;
328
		$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
0 ignored issues
show
Unused Code introduced by
$id2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
329
		$id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
0 ignored issues
show
Unused Code introduced by
$id3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
330
		$id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
0 ignored issues
show
Unused Code introduced by
$id4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
331
		$tag = wp_insert_term( 'My Tag', 'post_tag' );
332
333
		wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
334
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
335
		$request->set_param( 'tags', array( $tag['term_id'] ) );
336
337
		$response = $this->server->dispatch( $request );
338
		$this->assertCount( 1, $response->get_data() );
339
	}
340
341
	public function test_get_items_tags_and_categories_query() {
342
		$id1 = $this->post_id;
343
		$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
344
		$id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
0 ignored issues
show
Unused Code introduced by
$id3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
345
		$id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
0 ignored issues
show
Unused Code introduced by
$id4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
346
		$tag = wp_insert_term( 'My Tag', 'post_tag' );
347
		$category = wp_insert_term( 'My Category', 'category' );
348
349
		wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
350
		wp_set_object_terms( $id2, array( $tag['term_id'] ), 'post_tag' );
351
		wp_set_object_terms( $id1, array( $category['term_id'] ), 'category' );
352
353
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
354
		$request->set_param( 'tags', array( $tag['term_id'] ) );
355
		$request->set_param( 'categories', array( $category['term_id'] ) );
356
357
		$response = $this->server->dispatch( $request );
358
		$this->assertCount( 1, $response->get_data() );
359
	}
360
361 View Code Duplication
	public function test_get_items_sticky_query() {
362
		$id1 = $this->post_id;
0 ignored issues
show
Unused Code introduced by
$id1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
363
		$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
364
365
		update_option( 'sticky_posts', array( $id2 ) );
366
367
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
368
		$request->set_param( 'sticky', true );
369
370
		$response = $this->server->dispatch( $request );
371
		$this->assertCount( 1, $response->get_data() );
372
373
		$posts = $response->get_data();
374
		$post = $posts[0];
375
		$this->assertEquals( $id2, $post['id'] );
376
	}
377
378
	public function test_get_items_sticky_with_post__in_query() {
379
		$id1 = $this->post_id;
380
		$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
381
		$id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
0 ignored issues
show
Unused Code introduced by
$id3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
382
383
		update_option( 'sticky_posts', array( $id2 ) );
384
385
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
386
		$request->set_param( 'sticky', true );
387
		$request->set_param( 'include', array( $id1 ) );
388
389
		$response = $this->server->dispatch( $request );
390
		$this->assertCount( 0, $response->get_data() );
391
392
		update_option( 'sticky_posts', array( $id1, $id2 ) );
393
394
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
395
		$request->set_param( 'sticky', true );
396
		$request->set_param( 'include', array( $id1 ) );
397
398
		$response = $this->server->dispatch( $request );
399
400
		$this->assertCount( 1, $response->get_data() );
401
402
		$posts = $response->get_data();
403
		$post = $posts[0];
404
		$this->assertEquals( $id1, $post['id'] );
405
	}
406
407 View Code Duplication
	public function test_get_items_not_sticky_query() {
408
		$id1 = $this->post_id;
409
		$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
410
411
		update_option( 'sticky_posts', array( $id2 ) );
412
413
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
414
		$request->set_param( 'sticky', false );
415
416
		$response = $this->server->dispatch( $request );
417
		$this->assertCount( 1, $response->get_data() );
418
419
		$posts = $response->get_data();
420
		$post = $posts[0];
421
		$this->assertEquals( $id1, $post['id'] );
422
	}
423
424
	public function test_get_items_sticky_with_post__not_in_query() {
425
		$id1 = $this->post_id;
426
		$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
427
		$id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
428
429
		update_option( 'sticky_posts', array( $id2 ) );
430
431
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
432
		$request->set_param( 'sticky', false );
433
		$request->set_param( 'exclude', array( $id3 ) );
434
435
		$response = $this->server->dispatch( $request );
436
		$this->assertCount( 1, $response->get_data() );
437
438
		$posts = $response->get_data();
439
		$post = $posts[0];
440
		$this->assertEquals( $id1, $post['id'] );
441
	}
442
443
	/**
444
	 * @group test
445
	 */
446
	public function test_get_items_pagination_headers() {
447
		// Start of the index
448
		for ( $i = 0; $i < 49; $i++ ) {
449
			$this->factory->post->create( array(
450
				'post_title'   => "Post {$i}",
451
				) );
452
		}
453
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
454
		$response = $this->server->dispatch( $request );
455
		$headers = $response->get_headers();
456
		$this->assertEquals( 50, $headers['X-WP-Total'] );
457
		$this->assertEquals( 5, $headers['X-WP-TotalPages'] );
458
		$next_link = add_query_arg( array(
459
			'page'    => 2,
460
			), rest_url( '/wp/v2/posts' ) );
461
		$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
462
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
463
		// 3rd page
464
		$this->factory->post->create( array(
465
				'post_title'   => 'Post 51',
466
				) );
467
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
468
		$request->set_param( 'page', 3 );
469
		$response = $this->server->dispatch( $request );
470
		$headers = $response->get_headers();
471
		$this->assertEquals( 51, $headers['X-WP-Total'] );
472
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
473
		$prev_link = add_query_arg( array(
474
			'page'    => 2,
475
			), rest_url( '/wp/v2/posts' ) );
476
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
477
		$next_link = add_query_arg( array(
478
			'page'    => 4,
479
			), rest_url( '/wp/v2/posts' ) );
480
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
481
		// Last page
482
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
483
		$request->set_param( 'page', 6 );
484
		$response = $this->server->dispatch( $request );
485
		$headers = $response->get_headers();
486
		$this->assertEquals( 51, $headers['X-WP-Total'] );
487
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
488
		$prev_link = add_query_arg( array(
489
			'page'    => 5,
490
			), rest_url( '/wp/v2/posts' ) );
491
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
492
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
493
		// Out of bounds
494
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
495
		$request->set_param( 'page', 8 );
496
		$response = $this->server->dispatch( $request );
497
		$headers = $response->get_headers();
498
		$this->assertEquals( 51, $headers['X-WP-Total'] );
499
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
500
		$prev_link = add_query_arg( array(
501
			'page'    => 6,
502
			), rest_url( '/wp/v2/posts' ) );
503
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
504
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
505
506
		// With query params.
507
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
508
		$request->set_query_params( array( 'per_page' => 5, 'page' => 2 ) );
509
		$response = $this->server->dispatch( $request );
510
		$headers = $response->get_headers();
511
		$this->assertEquals( 51, $headers['X-WP-Total'] );
512
		$this->assertEquals( 11, $headers['X-WP-TotalPages'] );
513
		$prev_link = add_query_arg( array(
514
			'per_page' => 5,
515
			'page'     => 1,
516
			), rest_url( '/wp/v2/posts' ) );
517
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
518
		$next_link = add_query_arg( array(
519
			'per_page' => 5,
520
			'page'     => 3,
521
			), rest_url( '/wp/v2/posts' ) );
522
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
523
	}
524
525 View Code Duplication
	public function test_get_items_private_filter_query_var() {
526
		// Private query vars inaccessible to unauthorized users
527
		wp_set_current_user( 0 );
528
		$draft_id = $this->factory->post->create( array( 'post_status' => 'draft' ) );
529
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
530
		$request->set_param( 'filter', array( 'post_status' => 'draft' ) );
531
		$response = $this->server->dispatch( $request );
532
		$data = $response->get_data();
533
		$this->assertCount( 1, $data );
534
		$this->assertEquals( $this->post_id, $data[0]['id'] );
535
		// But they are accessible to authorized users
536
		wp_set_current_user( $this->editor_id );
537
		$response = $this->server->dispatch( $request );
538
		$data = $response->get_data();
539
		$this->assertCount( 1, $data );
540
		$this->assertEquals( $draft_id, $data[0]['id'] );
541
	}
542
543 View Code Duplication
	public function test_get_items_invalid_per_page() {
544
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
545
		$request->set_query_params( array( 'per_page' => -1 ) );
546
		$response = $this->server->dispatch( $request );
547
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
548
	}
549
550
	public function test_get_items_invalid_posts_per_page_ignored() {
551
		// This test ensures that filter[posts_per_page] is ignored, and that -1
552
		// cannot be used to sidestep per_page's valid range to retrieve all posts
553 View Code Duplication
		for ( $i = 0; $i < 20; $i++ ) {
554
			$this->factory->post->create( array( 'post_status' => 'publish' ) );
555
		}
556
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
557
		$request->set_query_params( array( 'filter' => array( 'posts_per_page' => -1 ) ) );
558
		$response = $this->server->dispatch( $request );
559
		$this->assertCount( 10, $response->get_data() );
560
	}
561
562 View Code Duplication
	public function test_get_items_invalid_context() {
563
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
564
		$request->set_param( 'context', 'banana' );
565
		$response = $this->server->dispatch( $request );
566
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
567
	}
568
569 View Code Duplication
	public function test_get_items_invalid_date() {
570
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
571
		$request->set_param( 'after', rand_str() );
572
		$request->set_param( 'before', rand_str() );
573
		$response = $this->server->dispatch( $request );
574
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
575
	}
576
577
	public function test_get_items_valid_date() {
578
		$post1 = $this->factory->post->create( array( 'post_date' => '2016-01-15T00:00:00Z' ) );
0 ignored issues
show
Unused Code introduced by
$post1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
579
		$post2 = $this->factory->post->create( array( 'post_date' => '2016-01-16T00:00:00Z' ) );
580
		$post3 = $this->factory->post->create( array( 'post_date' => '2016-01-17T00:00:00Z' ) );
0 ignored issues
show
Unused Code introduced by
$post3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
581
582
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
583
		$request->set_param( 'after', '2016-01-15T00:00:00Z' );
584
		$request->set_param( 'before', '2016-01-17T00:00:00Z' );
585
		$response = $this->server->dispatch( $request );
586
		$data = $response->get_data();
587
		$this->assertCount( 1, $data );
588
		$this->assertEquals( $post2, $data[0]['id'] );
589
	}
590
591
	public function test_get_item() {
592
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
593
		$response = $this->server->dispatch( $request );
594
595
		$this->check_get_post_response( $response, 'view' );
596
	}
597
598
	public function test_get_item_links() {
599
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
600
		$response = $this->server->dispatch( $request );
601
602
		$links = $response->get_links();
603
604
		$this->assertEquals( rest_url( '/wp/v2/posts/' . $this->post_id ), $links['self'][0]['href'] );
605
		$this->assertEquals( rest_url( '/wp/v2/posts' ), $links['collection'][0]['href'] );
606
607
		$this->assertEquals( rest_url( '/wp/v2/types/' . get_post_type( $this->post_id ) ), $links['about'][0]['href'] );
608
609
		$replies_url = rest_url( '/wp/v2/comments' );
610
		$replies_url = add_query_arg( 'post', $this->post_id, $replies_url );
611
		$this->assertEquals( $replies_url, $links['replies'][0]['href'] );
612
613
		$this->assertEquals( rest_url( '/wp/v2/posts/' . $this->post_id . '/revisions' ), $links['version-history'][0]['href'] );
614
615
		$attachments_url = rest_url( '/wp/v2/media' );
616
		$attachments_url = add_query_arg( 'parent', $this->post_id, $attachments_url );
617
		$this->assertEquals( $attachments_url, $links['https://api.w.org/attachment'][0]['href'] );
618
619
		$term_links = $links['https://api.w.org/term'];
620
		$tag_link = $cat_link = $format_link = null;
621
		foreach ( $term_links as $link ) {
622
			if ( 'post_tag' === $link['attributes']['taxonomy'] ) {
623
				$tag_link = $link;
624
			} elseif ( 'category' === $link['attributes']['taxonomy'] ) {
625
				$cat_link = $link;
626
			} else if ( 'post_format' === $link['attributes']['taxonomy'] ) {
627
				$format_link = $link;
628
			}
629
		}
630
		$this->assertNotEmpty( $tag_link );
631
		$this->assertNotEmpty( $cat_link );
632
		$this->assertNull( $format_link );
633
634
		$tags_url = add_query_arg( 'post', $this->post_id, rest_url( '/wp/v2/tags' ) );
635
		$this->assertEquals( $tags_url, $tag_link['href'] );
636
637
		$category_url = add_query_arg( 'post', $this->post_id, rest_url( '/wp/v2/categories' ) );
638
		$this->assertEquals( $category_url, $cat_link['href'] );
639
	}
640
641
	public function test_get_item_links_no_author() {
642
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
643
		$response = $this->server->dispatch( $request );
644
		$links = $response->get_links();
645
		$this->assertFalse( isset( $links['author'] ) );
646
		wp_update_post( array( 'ID' => $this->post_id, 'post_author' => $this->author_id ) );
647
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
648
		$response = $this->server->dispatch( $request );
649
		$links = $response->get_links();
650
		$this->assertEquals( rest_url( '/wp/v2/users/' . $this->author_id ), $links['author'][0]['href'] );
651
	}
652
653
	public function test_get_post_without_permission() {
654
		$draft_id = $this->factory->post->create( array(
655
			'post_status' => 'draft',
656
		) );
657
		wp_set_current_user( 0 );
658
659
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $draft_id ) );
660
		$response = $this->server->dispatch( $request );
661
662
		$this->assertErrorResponse( 'rest_forbidden', $response, 403 );
663
	}
664
665
	public function test_get_post_invalid_id() {
666
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
667
		$response = $this->server->dispatch( $request );
668
669
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
670
	}
671
672
	public function test_get_post_list_context_with_permission() {
673
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
674
		$request->set_query_params( array(
675
			'context' => 'edit',
676
		) );
677
678
		wp_set_current_user( $this->editor_id );
679
680
		$response = $this->server->dispatch( $request );
681
682
		$this->check_get_posts_response( $response, 'edit' );
683
	}
684
685 View Code Duplication
	public function test_get_post_list_context_without_permission() {
686
		wp_set_current_user( 0 );
687
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
688
		$request->set_query_params( array(
689
			'context' => 'edit',
690
		) );
691
		$response = $this->server->dispatch( $request );
692
693
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
694
	}
695
696 View Code Duplication
	public function test_get_post_context_without_permission() {
697
		wp_set_current_user( 0 );
698
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
699
		$request->set_query_params( array(
700
			'context' => 'edit',
701
		) );
702
		$response = $this->server->dispatch( $request );
703
704
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
705
	}
706
707 View Code Duplication
	public function test_get_post_with_password() {
708
		$post_id = $this->factory->post->create( array(
709
			'post_password' => '$inthebananastand',
710
		) );
711
712
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
713
		$response = $this->server->dispatch( $request );
714
715
		$this->check_get_post_response( $response, 'view' );
716
717
		$data = $response->get_data();
718
		$this->assertTrue( $data['content']['protected'] );
719
		$this->assertTrue( $data['excerpt']['protected'] );
720
	}
721
722
	public function test_get_post_with_password_using_password() {
723
		global $wp_version;
724
		if ( version_compare( $wp_version, '4.7-alpha', '<' ) ) {
725
			return $this->markTestSkipped( 'WordPress < 4.6 does not support filtering passwords for posts.' );
726
		}
727
728
		$post_id = $this->factory->post->create( array(
729
			'post_password' => '$inthebananastand',
730
			'post_content'  => 'Some secret content.',
731
			'post_excerpt'  => 'Some secret excerpt.',
732
		) );
733
734
		$post = get_post( $post_id );
735
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
736
		$request->set_param( 'password', '$inthebananastand' );
737
		$response = $this->server->dispatch( $request );
738
739
		$this->check_get_post_response( $response, 'view' );
740
741
		$data = $response->get_data();
742
		$this->assertEquals( wpautop( $post->post_content ), $data['content']['rendered'] );
743
		$this->assertEquals( wpautop( $post->post_excerpt ), $data['excerpt']['rendered'] );
744
	}
745
746 View Code Duplication
	public function test_get_post_with_password_using_incorrect_password() {
747
		$post_id = $this->factory->post->create( array(
748
			'post_password' => '$inthebananastand',
749
		) );
750
751
		$post = get_post( $post_id );
0 ignored issues
show
Unused Code introduced by
$post is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
752
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
753
		$request->set_param( 'password', 'wrongpassword' );
754
		$response = $this->server->dispatch( $request );
755
756
		$this->assertErrorResponse( 'rest_post_incorrect_password', $response, 403 );
757
	}
758
759
	public function test_get_post_with_password_without_permission() {
760
		$post_id = $this->factory->post->create( array(
761
			'post_password' => '$inthebananastand',
762
			'post_content'  => 'Some secret content.',
763
			'post_excerpt'  => 'Some secret excerpt.',
764
		) );
765
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
766
		$response = $this->server->dispatch( $request );
767
		$data = $response->get_data();
768
		$this->check_get_post_response( $response, 'view' );
769
		$this->assertEquals( '', $data['content']['rendered'] );
770
		$this->assertEquals( '', $data['excerpt']['rendered'] );
771
772
	}
773
774
	public function test_get_item_read_permission_custom_post_status() {
775
		register_post_status( 'testpubstatus', array( 'public' => true ) );
776
		register_post_status( 'testprivtatus', array( 'public' => false ) );
777
		// Public status
778
		wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'testpubstatus' ) );
779
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
780
		$response = $this->server->dispatch( $request );
781
		$this->assertEquals( 200, $response->get_status() );
782
		// Private status
783
		wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'testprivtatus' ) );
784
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
785
		$response = $this->server->dispatch( $request );
786
		$this->assertEquals( 403, $response->get_status() );
787
	}
788
789
	public function test_prepare_item() {
790
		wp_set_current_user( $this->editor_id );
791
792
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
793
		$request->set_query_params( array( 'context' => 'edit' ) );
794
		$response = $this->server->dispatch( $request );
795
796
		$this->check_get_post_response( $response, 'edit' );
797
	}
798
799
	public function test_create_item() {
800
		wp_set_current_user( $this->editor_id );
801
802
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
803
		$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
804
		$params = $this->set_post_data();
805
		$request->set_body_params( $params );
806
		$response = $this->server->dispatch( $request );
807
808
		$this->check_create_post_response( $response );
809
	}
810
811
	public function test_rest_create_item() {
812
		wp_set_current_user( $this->editor_id );
813
814
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
815
		$request->add_header( 'content-type', 'application/json' );
816
		$params = $this->set_post_data();
817
		$request->set_body( wp_json_encode( $params ) );
818
		$response = $this->server->dispatch( $request );
819
820
		$this->check_create_post_response( $response );
821
	}
822
823
	public function test_create_post_invalid_id() {
824
		wp_set_current_user( $this->editor_id );
825
826
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
827
		$params = $this->set_post_data( array(
828
			'id' => '3',
829
		) );
830
		$request->set_body_params( $params );
831
		$response = $this->server->dispatch( $request );
832
833
		$this->assertErrorResponse( 'rest_post_exists', $response, 400 );
834
	}
835
836
	public function test_create_post_as_contributor() {
837
		wp_set_current_user( $this->contributor_id );
838
839
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
840
		$params = $this->set_post_data(array(
841
			'status' => 'pending',
842
		));
843
844
		$request->set_body_params( $params );
845
		$response = $this->server->dispatch( $request );
846
		$this->check_create_post_response( $response );
847
	}
848
849
	public function test_create_post_sticky() {
850
		wp_set_current_user( $this->editor_id );
851
852
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
853
		$params = $this->set_post_data( array(
854
			'sticky' => true,
855
		) );
856
		$request->set_body_params( $params );
857
		$response = $this->server->dispatch( $request );
858
859
		$new_data = $response->get_data();
860
		$this->assertEquals( true, $new_data['sticky'] );
861
		$post = get_post( $new_data['id'] );
862
		$this->assertEquals( true, is_sticky( $post->ID ) );
863
	}
864
865
	public function test_create_post_sticky_as_contributor() {
866
		wp_set_current_user( $this->contributor_id );
867
868
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
869
		$params = $this->set_post_data( array(
870
			'sticky' => true,
871
			'status' => 'pending',
872
		) );
873
		$request->set_body_params( $params );
874
		$response = $this->server->dispatch( $request );
875
876
		$this->assertErrorResponse( 'rest_cannot_assign_sticky', $response, 403 );
877
	}
878
879
	public function test_create_post_other_author_without_permission() {
880
		wp_set_current_user( $this->author_id );
881
882
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
883
		$params = $this->set_post_data(array(
884
			'author' => $this->editor_id,
885
		));
886
		$request->set_body_params( $params );
887
		$response = $this->server->dispatch( $request );
888
889
		$this->assertErrorResponse( 'rest_cannot_edit_others', $response, 403 );
890
	}
891
892
	public function test_create_post_without_permission() {
893
		wp_set_current_user( 0 );
894
895
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
896
		$params = $this->set_post_data( array(
897
			'status' => 'draft',
898
		) );
899
		$request->set_body_params( $params );
900
		$response = $this->server->dispatch( $request );
901
902
		$this->assertErrorResponse( 'rest_cannot_create', $response, 401 );
903
	}
904
905 View Code Duplication
	public function test_create_post_draft() {
906
		wp_set_current_user( $this->editor_id );
907
908
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
909
		$params = $this->set_post_data( array(
910
			'status' => 'draft',
911
		) );
912
		$request->set_body_params( $params );
913
		$response = $this->server->dispatch( $request );
914
915
		$data = $response->get_data();
916
		$new_post = get_post( $data['id'] );
917
		$this->assertEquals( 'draft', $data['status'] );
918
		$this->assertEquals( 'draft', $new_post->post_status );
919
		// Confirm dates are null
920
		$this->assertNull( $data['date_gmt'] );
921
		$this->assertNull( $data['modified_gmt'] );
922
	}
923
924 View Code Duplication
	public function test_create_post_private() {
925
		wp_set_current_user( $this->editor_id );
926
927
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
928
		$params = $this->set_post_data( array(
929
			'status' => 'private',
930
		) );
931
		$request->set_body_params( $params );
932
		$response = $this->server->dispatch( $request );
933
934
		$data = $response->get_data();
935
		$new_post = get_post( $data['id'] );
936
		$this->assertEquals( 'private', $data['status'] );
937
		$this->assertEquals( 'private', $new_post->post_status );
938
	}
939
940
	public function test_create_post_private_without_permission() {
941
		wp_set_current_user( $this->author_id );
942
		$user = wp_get_current_user();
943
		$user->add_cap( 'publish_posts', false );
944
		// Flush capabilities, https://core.trac.wordpress.org/ticket/28374
945
		$user->get_role_caps();
946
		$user->update_user_level_from_caps();
947
948
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
949
		$params = $this->set_post_data( array(
950
			'status' => 'private',
951
			'author' => $this->author_id,
952
		) );
953
		$request->set_body_params( $params );
954
		$response = $this->server->dispatch( $request );
955
956
		$this->assertErrorResponse( 'rest_cannot_publish', $response, 403 );
957
	}
958
959
	public function test_create_post_publish_without_permission() {
960
		wp_set_current_user( $this->author_id );
961
		$user = wp_get_current_user();
962
		$user->add_cap( 'publish_posts', false );
963
		// Flush capabilities, https://core.trac.wordpress.org/ticket/28374
964
		$user->get_role_caps();
965
		$user->update_user_level_from_caps();
966
967
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
968
		$params = $this->set_post_data( array(
969
			'status' => 'publish',
970
		) );
971
		$request->set_body_params( $params );
972
		$response = $this->server->dispatch( $request );
973
974
		$this->assertErrorResponse( 'rest_cannot_publish', $response, 403 );
975
	}
976
977
	public function test_create_post_invalid_status() {
978
		wp_set_current_user( $this->editor_id );
979
980
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
981
		$params = $this->set_post_data( array(
982
			'status' => 'teststatus',
983
		) );
984
		$request->set_body_params( $params );
985
		$response = $this->server->dispatch( $request );
986
987
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
988
	}
989
990 View Code Duplication
	public function test_create_post_with_format() {
991
		wp_set_current_user( $this->editor_id );
992
993
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
994
		$params = $this->set_post_data( array(
995
			'format' => 'gallery',
996
		) );
997
		$request->set_body_params( $params );
998
		$response = $this->server->dispatch( $request );
999
1000
		$data = $response->get_data();
1001
		$new_post = get_post( $data['id'] );
1002
		$this->assertEquals( 'gallery', $data['format'] );
1003
		$this->assertEquals( 'gallery', get_post_format( $new_post->ID ) );
1004
	}
1005
1006
	public function test_create_post_with_invalid_format() {
1007
		wp_set_current_user( $this->editor_id );
1008
1009
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1010
		$params = $this->set_post_data( array(
1011
			'format' => 'testformat',
1012
		) );
1013
		$request->set_body_params( $params );
1014
		$response = $this->server->dispatch( $request );
1015
1016
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1017
	}
1018
1019
	public function test_create_update_post_with_featured_media() {
1020
1021
		$file = DIR_TESTDATA . '/images/canola.jpg';
1022
		$this->attachment_id = $this->factory->attachment->create_object( $file, 0, array(
1023
			'post_mime_type' => 'image/jpeg',
1024
			'menu_order' => rand( 1, 100 ),
1025
		) );
1026
1027
		wp_set_current_user( $this->editor_id );
1028
1029
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1030
		$params = $this->set_post_data( array(
1031
			'featured_media' => $this->attachment_id,
1032
		) );
1033
		$request->set_body_params( $params );
1034
		$response = $this->server->dispatch( $request );
1035
		$data = $response->get_data();
1036
		$new_post = get_post( $data['id'] );
1037
		$this->assertEquals( $this->attachment_id, $data['featured_media'] );
1038
		$this->assertEquals( $this->attachment_id, (int) get_post_thumbnail_id( $new_post->ID ) );
1039
1040
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $new_post->ID );
1041
		$params = $this->set_post_data( array(
1042
			'featured_media' => 0,
1043
		) );
1044
		$request->set_body_params( $params );
1045
		$response = $this->server->dispatch( $request );
1046
		$data = $response->get_data();
1047
		$this->assertEquals( 0, $data['featured_media'] );
1048
		$this->assertEquals( 0, (int) get_post_thumbnail_id( $new_post->ID ) );
1049
	}
1050
1051
	public function test_create_post_invalid_author() {
1052
		wp_set_current_user( $this->editor_id );
1053
1054
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1055
		$params = $this->set_post_data( array(
1056
			'author' => -1,
1057
		) );
1058
		$request->set_body_params( $params );
1059
		$response = $this->server->dispatch( $request );
1060
1061
		$this->assertErrorResponse( 'rest_invalid_author', $response, 400 );
1062
	}
1063
1064
	public function test_create_post_invalid_author_without_permission() {
1065
		wp_set_current_user( $this->author_id );
1066
1067
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1068
		$params = $this->set_post_data( array(
1069
			'author' => $this->editor_id,
1070
		) );
1071
		$request->set_body_params( $params );
1072
		$response = $this->server->dispatch( $request );
1073
1074
		$this->assertErrorResponse( 'rest_cannot_edit_others', $response, 403 );
1075
	}
1076
1077
	public function test_create_post_with_password() {
1078
		wp_set_current_user( $this->editor_id );
1079
1080
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1081
		$params = $this->set_post_data( array(
1082
			'password' => 'testing',
1083
		) );
1084
		$request->set_body_params( $params );
1085
		$response = $this->server->dispatch( $request );
1086
1087
		$data = $response->get_data();
1088
		$this->assertEquals( 'testing', $data['password'] );
1089
	}
1090
1091
	public function test_create_post_with_falsy_password() {
1092
		wp_set_current_user( $this->editor_id );
1093
1094
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1095
		$params = $this->set_post_data( array(
1096
			'password' => '0',
1097
		) );
1098
		$request->set_body_params( $params );
1099
		$response = $this->server->dispatch( $request );
1100
1101
		$data = $response->get_data();
1102
1103
		$this->assertEquals( '0', $data['password'] );
1104
	}
1105
1106 View Code Duplication
	public function test_create_post_with_empty_string_password_and_sticky() {
1107
		wp_set_current_user( $this->editor_id );
1108
1109
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1110
		$params = $this->set_post_data( array(
1111
			'password' => '',
1112
			'sticky'   => true,
1113
		) );
1114
		$request->set_body_params( $params );
1115
		$response = $this->server->dispatch( $request );
1116
1117
		$this->assertEquals( 201, $response->get_status() );
1118
		$data = $response->get_data();
1119
		$this->assertEquals( '', $data['password'] );
1120
	}
1121
1122
	public function test_create_post_with_password_and_sticky_fails() {
1123
		wp_set_current_user( $this->editor_id );
1124
1125
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1126
		$params = $this->set_post_data( array(
1127
			'password' => '123',
1128
			'sticky'   => true,
1129
		) );
1130
		$request->set_body_params( $params );
1131
		$response = $this->server->dispatch( $request );
1132
1133
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1134
	}
1135
1136
	public function test_create_post_custom_date() {
1137
		wp_set_current_user( $this->editor_id );
1138
1139
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1140
		$params = $this->set_post_data( array(
1141
			'date' => '2010-01-01T02:00:00Z',
1142
		) );
1143
		$request->set_body_params( $params );
1144
		$response = $this->server->dispatch( $request );
1145
1146
		$data = $response->get_data();
1147
		$new_post = get_post( $data['id'] );
1148
		$time = gmmktime( 2, 0, 0, 1, 1, 2010 );
1149
		$this->assertEquals( '2010-01-01T02:00:00', $data['date'] );
1150
		$this->assertEquals( $time, strtotime( $new_post->post_date ) );
1151
	}
1152
1153
	public function test_create_post_custom_date_with_timezone() {
1154
		wp_set_current_user( $this->editor_id );
1155
1156
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1157
		$params = $this->set_post_data( array(
1158
			'date' => '2010-01-01T02:00:00-10:00',
1159
		) );
1160
		$request->set_body_params( $params );
1161
		$response = $this->server->dispatch( $request );
1162
1163
		$data = $response->get_data();
1164
		$new_post = get_post( $data['id'] );
1165
		$time = gmmktime( 12, 0, 0, 1, 1, 2010 );
1166
1167
		$this->assertEquals( '2010-01-01T12:00:00', $data['date'] );
1168
		$this->assertEquals( '2010-01-01T12:00:00', $data['modified'] );
1169
1170
		$this->assertEquals( $time, strtotime( $new_post->post_date ) );
1171
		$this->assertEquals( $time, strtotime( $new_post->post_modified ) );
1172
	}
1173
1174
	public function test_create_post_with_db_error() {
1175
		wp_set_current_user( $this->editor_id );
1176
1177
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1178
		$params  = $this->set_post_data( array() );
1179
		$request->set_body_params( $params );
1180
1181
		/**
1182
		 * Disable showing error as the below is going to intentionally
1183
		 * trigger a DB error.
1184
		 */
1185
		global $wpdb;
1186
		$wpdb->suppress_errors = true;
1187
		add_filter( 'query', array( $this, 'error_insert_query' ) );
1188
1189
		$response = $this->server->dispatch( $request );
1190
		remove_filter( 'query', array( $this, 'error_insert_query' ) );
1191
		$wpdb->show_errors = true;
1192
1193
		$this->assertErrorResponse( 'db_insert_error', $response, 500 );
1194
	}
1195
1196
	public function test_create_post_with_invalid_date() {
1197
		wp_set_current_user( $this->editor_id );
1198
1199
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1200
		$params = $this->set_post_data( array(
1201
			'date' => '2010-60-01T02:00:00Z',
1202
		) );
1203
		$request->set_body_params( $params );
1204
		$response = $this->server->dispatch( $request );
1205
1206
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1207
	}
1208
1209
	public function test_create_post_with_invalid_date_gmt() {
1210
		wp_set_current_user( $this->editor_id );
1211
1212
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1213
		$params = $this->set_post_data( array(
1214
			'date_gmt' => '2010-60-01T02:00:00',
1215
		) );
1216
		$request->set_body_params( $params );
1217
		$response = $this->server->dispatch( $request );
1218
1219
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1220
	}
1221
1222 View Code Duplication
	public function test_create_post_with_quotes_in_title() {
1223
		wp_set_current_user( $this->editor_id );
1224
1225
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1226
		$params = $this->set_post_data( array(
1227
			'title' => "Rob O'Rourke's Diary",
1228
		) );
1229
		$request->set_body_params( $params );
1230
		$response = $this->server->dispatch( $request );
1231
		$new_data = $response->get_data();
1232
		$this->assertEquals( "Rob O'Rourke's Diary", $new_data['title']['raw'] );
1233
	}
1234
1235
	public function test_create_post_with_categories() {
1236
		wp_set_current_user( $this->editor_id );
1237
		$category = wp_insert_term( 'Test Category', 'category' );
1238
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1239
		$params = $this->set_post_data( array(
1240
			'password'   => 'testing',
1241
			'categories' => array(
1242
				$category['term_id']
1243
			),
1244
		) );
1245
		$request->set_body_params( $params );
1246
		$response = $this->server->dispatch( $request );
1247
1248
		$data = $response->get_data();
1249
		$this->assertEquals( array( $category['term_id'] ), $data['categories'] );
1250
	}
1251
1252
	public function test_create_post_with_invalid_categories() {
1253
		wp_set_current_user( $this->editor_id );
1254
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1255
		$params = $this->set_post_data( array(
1256
			'password'   => 'testing',
1257
			'categories' => array(
1258
				REST_TESTS_IMPOSSIBLY_HIGH_NUMBER
1259
			),
1260
		) );
1261
		$request->set_body_params( $params );
1262
		$response = $this->server->dispatch( $request );
1263
1264
		$data = $response->get_data();
1265
		$this->assertEquals( array(), $data['categories'] );
1266
	}
1267
1268 View Code Duplication
	public function test_update_item() {
1269
		wp_set_current_user( $this->editor_id );
1270
1271
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1272
		$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
1273
		$params = $this->set_post_data();
1274
		$request->set_body_params( $params );
1275
		$response = $this->server->dispatch( $request );
1276
1277
		$this->check_update_post_response( $response );
1278
		$new_data = $response->get_data();
1279
		$this->assertEquals( $this->post_id, $new_data['id'] );
1280
		$this->assertEquals( $params['title'], $new_data['title']['raw'] );
1281
		$this->assertEquals( $params['content'], $new_data['content']['raw'] );
1282
		$this->assertEquals( $params['excerpt'], $new_data['excerpt']['raw'] );
1283
		$post = get_post( $this->post_id );
1284
		$this->assertEquals( $params['title'], $post->post_title );
1285
		$this->assertEquals( $params['content'], $post->post_content );
1286
		$this->assertEquals( $params['excerpt'], $post->post_excerpt );
1287
	}
1288
1289 View Code Duplication
	public function test_rest_update_post() {
1290
		wp_set_current_user( $this->editor_id );
1291
1292
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1293
		$request->add_header( 'content-type', 'application/json' );
1294
		$params = $this->set_post_data();
1295
		$request->set_body( wp_json_encode( $params ) );
1296
		$response = $this->server->dispatch( $request );
1297
1298
		$this->check_update_post_response( $response );
1299
		$new_data = $response->get_data();
1300
		$this->assertEquals( $this->post_id, $new_data['id'] );
1301
		$this->assertEquals( $params['title'], $new_data['title']['raw'] );
1302
		$this->assertEquals( $params['content'], $new_data['content']['raw'] );
1303
		$this->assertEquals( $params['excerpt'], $new_data['excerpt']['raw'] );
1304
		$post = get_post( $this->post_id );
1305
		$this->assertEquals( $params['title'], $post->post_title );
1306
		$this->assertEquals( $params['content'], $post->post_content );
1307
		$this->assertEquals( $params['excerpt'], $post->post_excerpt );
1308
	}
1309
1310
	public function test_rest_update_post_raw() {
1311
		wp_set_current_user( $this->editor_id );
1312
1313
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1314
		$request->add_header( 'content-type', 'application/json' );
1315
		$params = $this->set_raw_post_data();
1316
		$request->set_body( wp_json_encode( $params ) );
1317
		$response = $this->server->dispatch( $request );
1318
1319
		$this->check_update_post_response( $response );
1320
		$new_data = $response->get_data();
1321
		$this->assertEquals( $this->post_id, $new_data['id'] );
1322
		$this->assertEquals( $params['title']['raw'], $new_data['title']['raw'] );
1323
		$this->assertEquals( $params['content']['raw'], $new_data['content']['raw'] );
1324
		$this->assertEquals( $params['excerpt']['raw'], $new_data['excerpt']['raw'] );
1325
		$post = get_post( $this->post_id );
1326
		$this->assertEquals( $params['title']['raw'], $post->post_title );
1327
		$this->assertEquals( $params['content']['raw'], $post->post_content );
1328
		$this->assertEquals( $params['excerpt']['raw'], $post->post_excerpt );
1329
	}
1330
1331
	public function test_update_post_without_extra_params() {
1332
		wp_set_current_user( $this->editor_id );
1333
1334
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1335
		$params = $this->set_post_data();
1336
		unset( $params['type'] );
1337
		unset( $params['name'] );
1338
		unset( $params['author'] );
1339
		unset( $params['status'] );
1340
		$request->set_body_params( $params );
1341
		$response = $this->server->dispatch( $request );
1342
1343
		$this->check_update_post_response( $response );
1344
	}
1345
1346
	public function test_update_post_without_permission() {
1347
		wp_set_current_user( $this->editor_id );
1348
		$user = wp_get_current_user();
1349
		$user->add_cap( 'edit_published_posts', false );
1350
		// Flush capabilities, https://core.trac.wordpress.org/ticket/28374
1351
		$user->get_role_caps();
1352
		$user->update_user_level_from_caps();
1353
1354
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1355
		$params = $this->set_post_data();
1356
		$request->set_body_params( $params );
1357
		$response = $this->server->dispatch( $request );
1358
1359
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
1360
	}
1361
1362 View Code Duplication
	public function test_update_post_sticky_as_contributor() {
1363
		wp_set_current_user( $this->contributor_id );
1364
1365
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1366
		$params = $this->set_post_data( array(
1367
			'sticky' => true,
1368
			'status' => 'pending',
1369
		) );
1370
		$request->set_body_params( $params );
1371
		$response = $this->server->dispatch( $request );
1372
1373
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
1374
	}
1375
1376
	public function test_update_post_invalid_id() {
1377
		wp_set_current_user( $this->editor_id );
1378
1379
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
1380
		$response = $this->server->dispatch( $request );
1381
1382
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1383
	}
1384
1385
	public function test_update_post_invalid_route() {
1386
		wp_set_current_user( $this->editor_id );
1387
1388
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', $this->post_id ) );
1389
		$response = $this->server->dispatch( $request );
1390
1391
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1392
	}
1393
1394 View Code Duplication
	public function test_update_post_with_format() {
1395
		wp_set_current_user( $this->editor_id );
1396
1397
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1398
		$params = $this->set_post_data( array(
1399
			'format' => 'gallery',
1400
		) );
1401
		$request->set_body_params( $params );
1402
		$response = $this->server->dispatch( $request );
1403
1404
		$data = $response->get_data();
1405
		$new_post = get_post( $data['id'] );
1406
		$this->assertEquals( 'gallery', $data['format'] );
1407
		$this->assertEquals( 'gallery', get_post_format( $new_post->ID ) );
1408
	}
1409
1410
	public function test_update_post_with_invalid_format() {
1411
		wp_set_current_user( $this->editor_id );
1412
1413
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1414
		$params = $this->set_post_data( array(
1415
			'format' => 'testformat',
1416
		) );
1417
		$request->set_body_params( $params );
1418
		$response = $this->server->dispatch( $request );
1419
1420
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1421
	}
1422
1423
	public function test_update_post_ignore_readonly() {
1424
		wp_set_current_user( $this->editor_id );
1425
1426
		$new_content = rand_str();
1427
		$expected_modified = current_time( 'mysql' );
1428
1429
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1430
		$params = $this->set_post_data( array(
1431
			'modified' => '2010-06-01T02:00:00Z',
1432
			'content'  => $new_content,
1433
		) );
1434
		$request->set_body_params( $params );
1435
		$response = $this->server->dispatch( $request );
1436
1437
		// The readonly modified param should be ignored, request should be a success.
1438
		$data = $response->get_data();
1439
		$new_post = get_post( $data['id'] );
1440
1441
		$this->assertEquals( $new_content, $data['content']['raw'] );
1442
		$this->assertEquals( $new_content, $new_post->post_content );
1443
1444
		// The modified date should equal the current time.
1445
		$this->assertEquals( date( 'Y-m-d', strtotime( mysql_to_rfc3339( $expected_modified ) ) ), date( 'Y-m-d', strtotime( $data['modified'] ) ) );
1446
		$this->assertEquals( date( 'Y-m-d', strtotime( $expected_modified ) ), date( 'Y-m-d', strtotime( $new_post->post_modified ) ) );
1447
	}
1448
1449 View Code Duplication
	public function test_update_post_with_invalid_date() {
1450
		wp_set_current_user( $this->editor_id );
1451
1452
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1453
		$params = $this->set_post_data( array(
1454
			'date' => rand_str(),
1455
		) );
1456
		$request->set_body_params( $params );
1457
		$response = $this->server->dispatch( $request );
1458
1459
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1460
	}
1461
1462 View Code Duplication
	public function test_update_post_with_invalid_date_gmt() {
1463
		wp_set_current_user( $this->editor_id );
1464
1465
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1466
		$params = $this->set_post_data( array(
1467
			'date_gmt' => rand_str(),
1468
		) );
1469
		$request->set_body_params( $params );
1470
		$response = $this->server->dispatch( $request );
1471
1472
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1473
	}
1474
1475 View Code Duplication
	public function test_update_post_slug() {
1476
		wp_set_current_user( $this->editor_id );
1477
1478
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1479
		$params = $this->set_post_data( array(
1480
			'slug' => 'sample-slug',
1481
		) );
1482
		$request->set_body_params( $params );
1483
		$response = $this->server->dispatch( $request );
1484
1485
		$new_data = $response->get_data();
1486
		$this->assertEquals( 'sample-slug', $new_data['slug'] );
1487
		$post = get_post( $new_data['id'] );
1488
		$this->assertEquals( 'sample-slug', $post->post_name );
1489
	}
1490
1491 View Code Duplication
	public function test_update_post_slug_accented_chars() {
1492
		wp_set_current_user( $this->editor_id );
1493
1494
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1495
		$params = $this->set_post_data( array(
1496
			'slug' => 'tęst-acceńted-chäræcters',
1497
		) );
1498
		$request->set_body_params( $params );
1499
		$response = $this->server->dispatch( $request );
1500
1501
		$new_data = $response->get_data();
1502
		$this->assertEquals( 'test-accented-charaecters', $new_data['slug'] );
1503
		$post = get_post( $new_data['id'] );
1504
		$this->assertEquals( 'test-accented-charaecters', $post->post_name );
1505
	}
1506
1507
	public function test_update_post_sticky() {
1508
		wp_set_current_user( $this->editor_id );
1509
1510
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1511
		$params = $this->set_post_data( array(
1512
			'sticky' => true,
1513
		) );
1514
		$request->set_body_params( $params );
1515
		$response = $this->server->dispatch( $request );
1516
1517
		$new_data = $response->get_data();
1518
		$this->assertEquals( true, $new_data['sticky'] );
1519
		$post = get_post( $new_data['id'] );
1520
		$this->assertEquals( true, is_sticky( $post->ID ) );
1521
1522
		// Updating another field shouldn't change sticky status
1523
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1524
		$params = $this->set_post_data( array(
1525
			'title'       => 'This should not reset sticky',
1526
		) );
1527
		$request->set_body_params( $params );
1528
		$response = $this->server->dispatch( $request );
1529
1530
		$new_data = $response->get_data();
1531
		$this->assertEquals( true, $new_data['sticky'] );
1532
		$post = get_post( $new_data['id'] );
1533
		$this->assertEquals( true, is_sticky( $post->ID ) );
1534
	}
1535
1536
	public function test_update_post_excerpt() {
1537
		wp_set_current_user( $this->editor_id );
1538
1539
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1540
		$request->set_body_params( array(
1541
			'excerpt' => 'An Excerpt',
1542
		) );
1543
1544
		$response = $this->server->dispatch( $request );
1545
		$new_data = $response->get_data();
1546
		$this->assertEquals( 'An Excerpt', $new_data['excerpt']['raw'] );
1547
	}
1548
1549
	public function test_update_post_empty_excerpt() {
1550
		wp_set_current_user( $this->editor_id );
1551
1552
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1553
		$request->set_body_params( array(
1554
			'excerpt' => '',
1555
		) );
1556
1557
		$response = $this->server->dispatch( $request );
1558
		$new_data = $response->get_data();
1559
		$this->assertEquals( '', $new_data['excerpt']['raw'] );
1560
	}
1561
1562
	public function test_update_post_content() {
1563
		wp_set_current_user( $this->editor_id );
1564
1565
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1566
		$request->set_body_params( array(
1567
			'content' => 'Some Content',
1568
		) );
1569
1570
		$response = $this->server->dispatch( $request );
1571
		$new_data = $response->get_data();
1572
		$this->assertEquals( 'Some Content', $new_data['content']['raw'] );
1573
	}
1574
1575
	public function test_update_post_empty_content() {
1576
		wp_set_current_user( $this->editor_id );
1577
1578
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1579
		$request->set_body_params( array(
1580
			'content' => '',
1581
		) );
1582
1583
		$response = $this->server->dispatch( $request );
1584
		$new_data = $response->get_data();
1585
		$this->assertEquals( '', $new_data['content']['raw'] );
1586
	}
1587
1588 View Code Duplication
	public function test_update_post_with_password_and_sticky_fails() {
1589
		wp_set_current_user( $this->editor_id );
1590
1591
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1592
		$params = $this->set_post_data( array(
1593
			'password' => '123',
1594
			'sticky'   => true,
1595
		) );
1596
		$request->set_body_params( $params );
1597
		$response = $this->server->dispatch( $request );
1598
1599
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1600
	}
1601
1602
	public function test_update_stick_post_with_password_fails() {
1603
		wp_set_current_user( $this->editor_id );
1604
1605
		stick_post( $this->post_id );
1606
1607
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1608
		$params = $this->set_post_data( array(
1609
			'password' => '123',
1610
		) );
1611
		$request->set_body_params( $params );
1612
		$response = $this->server->dispatch( $request );
1613
1614
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1615
	}
1616
1617 View Code Duplication
	public function test_update_password_protected_post_with_sticky_fails() {
1618
		wp_set_current_user( $this->editor_id );
1619
1620
		wp_update_post( array( 'ID' => $this->post_id, 'post_password' => '123' ) );
1621
1622
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1623
		$params = $this->set_post_data( array(
1624
			'sticky' => true,
1625
		) );
1626
		$request->set_body_params( $params );
1627
		$response = $this->server->dispatch( $request );
1628
1629
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1630
	}
1631
1632
	public function test_update_post_with_quotes_in_title() {
1633
		wp_set_current_user( $this->editor_id );
1634
1635
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1636
		$params = $this->set_post_data( array(
1637
			'title' => "Rob O'Rourke's Diary",
1638
		) );
1639
		$request->set_body_params( $params );
1640
		$response = $this->server->dispatch( $request );
1641
		$new_data = $response->get_data();
1642
		$this->assertEquals( "Rob O'Rourke's Diary", $new_data['title']['raw'] );
1643
	}
1644
1645
	public function test_update_post_with_categories() {
1646
1647
		wp_set_current_user( $this->editor_id );
1648
		$category = wp_insert_term( 'Test Category', 'category' );
1649
1650
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1651
		$params = $this->set_post_data( array(
1652
			'title' => 'Tester',
1653
			'categories' => array(
1654
				$category['term_id'],
1655
			),
1656
		) );
1657
		$request->set_body_params( $params );
1658
		$response = $this->server->dispatch( $request );
1659
		$new_data = $response->get_data();
1660
		$this->assertEquals( array( $category['term_id'] ), $new_data['categories'] );
1661
		$categories_path = '';
1662
		$links = $response->get_links();
1663
		foreach ( $links['https://api.w.org/term'] as $link ) {
1664
			if ( 'category' === $link['attributes']['taxonomy'] ) {
1665
				$categories_path = $link['href'];
1666
			}
1667
		}
1668
		$query = parse_url( $categories_path, PHP_URL_QUERY );
1669
		parse_str( $query, $args );
1670
		$request = new WP_REST_Request( 'GET', $args['rest_route'] );
1671
		unset( $args['rest_route'] );
1672
		$request->set_query_params( $args );
1673
		$response = $this->server->dispatch( $request );
1674
		$data = $response->get_data();
1675
		$this->assertCount( 1, $data );
1676
		$this->assertEquals( 'Test Category', $data[0]['name'] );
1677
	}
1678
1679
	public function test_update_post_with_empty_categories() {
1680
1681
		wp_set_current_user( $this->editor_id );
1682
		$category = wp_insert_term( 'Test Category', 'category' );
1683
		wp_set_object_terms( $this->post_id, $category['term_id'], 'category' );
1684
1685
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1686
		$params = $this->set_post_data( array(
1687
			'title' => 'Tester',
1688
			'categories' => array(),
1689
		) );
1690
		$request->set_body_params( $params );
1691
		$response = $this->server->dispatch( $request );
1692
		$new_data = $response->get_data();
1693
		$this->assertEquals( array(), $new_data['categories'] );
1694
	}
1695
1696 View Code Duplication
	public function test_delete_item() {
1697
		$post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
1698
		wp_set_current_user( $this->editor_id );
1699
1700
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
1701
		$response = $this->server->dispatch( $request );
1702
1703
		$this->assertNotInstanceOf( 'WP_Error', $response );
1704
		$this->assertEquals( 200, $response->get_status() );
1705
		$data = $response->get_data();
1706
		$this->assertEquals( 'Deleted post', $data['title']['raw'] );
1707
	}
1708
1709 View Code Duplication
	public function test_delete_item_skip_trash() {
1710
		$post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
1711
		wp_set_current_user( $this->editor_id );
1712
1713
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
1714
		$request['force'] = true;
1715
		$response = $this->server->dispatch( $request );
1716
1717
		$this->assertNotInstanceOf( 'WP_Error', $response );
1718
		$this->assertEquals( 200, $response->get_status() );
1719
		$data = $response->get_data();
1720
		$this->assertEquals( 'Deleted post', $data['title']['raw'] );
1721
	}
1722
1723 View Code Duplication
	public function test_delete_item_already_trashed() {
1724
		$post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
1725
		wp_set_current_user( $this->editor_id );
1726
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
1727
		$response = $this->server->dispatch( $request );
1728
		$this->assertEquals( 200, $response->get_status() );
1729
		$response = $this->server->dispatch( $request );
1730
		$this->assertErrorResponse( 'rest_already_trashed', $response, 410 );
1731
	}
1732
1733 View Code Duplication
	public function test_delete_post_invalid_id() {
1734
		wp_set_current_user( $this->editor_id );
1735
1736
		$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
1737
		$response = $this->server->dispatch( $request );
1738
1739
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1740
	}
1741
1742
	public function test_delete_post_invalid_post_type() {
1743
		$page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
1744
		wp_set_current_user( $this->editor_id );
1745
1746
		$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $page_id );
1747
		$response = $this->server->dispatch( $request );
1748
1749
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1750
	}
1751
1752
	public function test_delete_post_without_permission() {
1753
		wp_set_current_user( $this->author_id );
1754
1755
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1756
		$response = $this->server->dispatch( $request );
1757
1758
		$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
1759
	}
1760
1761
	public function test_register_post_type_invalid_controller() {
1762
1763
		register_post_type( 'invalid-controller', array( 'show_in_rest' => true, 'rest_controller_class' => 'Fake_Class_Baba' ) );
1764
		create_initial_rest_routes();
1765
		$routes = $this->server->get_routes();
1766
		$this->assertFalse( isset( $routes['/wp/v2/invalid-controller'] ) );
1767
		_unregister_post_type( 'invalid-controller' );
1768
1769
	}
1770
1771
	public function test_get_item_schema() {
1772
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
1773
		$response = $this->server->dispatch( $request );
1774
		$data = $response->get_data();
1775
		$properties = $data['schema']['properties'];
1776
		$this->assertEquals( 22, count( $properties ) );
1777
		$this->assertArrayHasKey( 'author', $properties );
1778
		$this->assertArrayHasKey( 'comment_status', $properties );
1779
		$this->assertArrayHasKey( 'content', $properties );
1780
		$this->assertArrayHasKey( 'date', $properties );
1781
		$this->assertArrayHasKey( 'date_gmt', $properties );
1782
		$this->assertArrayHasKey( 'excerpt', $properties );
1783
		$this->assertArrayHasKey( 'featured_media', $properties );
1784
		$this->assertArrayHasKey( 'guid', $properties );
1785
		$this->assertArrayHasKey( 'format', $properties );
1786
		$this->assertArrayHasKey( 'id', $properties );
1787
		$this->assertArrayHasKey( 'link', $properties );
1788
		$this->assertArrayHasKey( 'modified', $properties );
1789
		$this->assertArrayHasKey( 'modified_gmt', $properties );
1790
		$this->assertArrayHasKey( 'password', $properties );
1791
		$this->assertArrayHasKey( 'ping_status', $properties );
1792
		$this->assertArrayHasKey( 'slug', $properties );
1793
		$this->assertArrayHasKey( 'status', $properties );
1794
		$this->assertArrayHasKey( 'sticky', $properties );
1795
		$this->assertArrayHasKey( 'title', $properties );
1796
		$this->assertArrayHasKey( 'type', $properties );
1797
		$this->assertArrayHasKey( 'tags', $properties );
1798
		$this->assertArrayHasKey( 'categories', $properties );
1799
	}
1800
1801
	public function test_get_additional_field_registration() {
1802
1803
		$schema = array(
1804
			'type'        => 'integer',
1805
			'description' => 'Some integer of mine',
1806
			'enum'        => array( 1, 2, 3, 4 ),
1807
			'context'     => array( 'view', 'edit' ),
1808
		);
1809
1810
		register_rest_field( 'post', 'my_custom_int', array(
1811
			'schema'          => $schema,
1812
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
1813
			'update_callback' => array( $this, 'additional_field_update_callback' ),
1814
		) );
1815
1816
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
1817
1818
		$response = $this->server->dispatch( $request );
1819
		$data = $response->get_data();
1820
1821
		$this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
1822
		$this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
1823
1824
		wp_set_current_user( 1 );
1825
1826
		$post_id = $this->factory->post->create();
1827
1828
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
1829
1830
		$response = $this->server->dispatch( $request );
1831
		$this->assertArrayHasKey( 'my_custom_int', $response->data );
1832
1833
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $post_id );
1834
		$request->set_body_params(array(
1835
			'my_custom_int' => 123,
1836
		));
1837
1838
		$response = $this->server->dispatch( $request );
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1839
		$this->assertEquals( 123, get_post_meta( $post_id, 'my_custom_int', true ) );
1840
1841
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1842
		$request->set_body_params(array(
1843
			'my_custom_int' => 123,
1844
			'title' => 'hello',
1845
		));
1846
1847
		$response = $this->server->dispatch( $request );
1848
1849
		$this->assertEquals( 123, $response->data['my_custom_int'] );
1850
1851
		global $wp_rest_additional_fields;
1852
		$wp_rest_additional_fields = array();
1853
	}
1854
1855 View Code Duplication
	public function test_additional_field_update_errors() {
1856
		$schema = array(
1857
			'type'        => 'integer',
1858
			'description' => 'Some integer of mine',
1859
			'enum'        => array( 1, 2, 3, 4 ),
1860
			'context'     => array( 'view', 'edit' ),
1861
		);
1862
1863
		register_rest_field( 'post', 'my_custom_int', array(
1864
			'schema'          => $schema,
1865
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
1866
			'update_callback' => array( $this, 'additional_field_update_callback' ),
1867
		) );
1868
1869
		wp_set_current_user( $this->editor_id );
1870
		// Check for error on update.
1871
		$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1872
		$request->set_body_params( array(
1873
			'my_custom_int' => 'returnError',
1874
		) );
1875
1876
		$response = $this->server->dispatch( $request );
1877
1878
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1879
1880
		global $wp_rest_additional_fields;
1881
		$wp_rest_additional_fields = array();
1882
	}
1883
1884
	public function additional_field_get_callback( $object ) {
1885
		return get_post_meta( $object['id'], 'my_custom_int', true );
1886
	}
1887
1888
	public function additional_field_update_callback( $value, $post ) {
1889
		if ( 'returnError' === $value ) {
1890
			return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
1891
		}
1892
		update_post_meta( $post->ID, 'my_custom_int', $value );
1893
	}
1894
1895
	public function tearDown() {
1896
		_unregister_post_type( 'youseeeme' );
1897
		if ( isset( $this->attachment_id ) ) {
1898
			$this->remove_added_uploads();
1899
		}
1900
		parent::tearDown();
1901
	}
1902
1903
	/**
1904
	 * Internal function used to disable an insert query which
1905
	 * will trigger a wpdb error for testing purposes.
1906
	 */
1907
	public function error_insert_query( $query ) {
1908
		if ( strpos( $query, 'INSERT' ) === 0 ) {
1909
			$query = '],';
1910
		}
1911
		return $query;
1912
	}
1913
1914
}
1915