Completed
Branch develop (a2474f)
by
unknown
02:30
created

test_get_items_sticky_with_post__in_query()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
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
		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 filter params.
507
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
508
		$request->set_query_params( array( 'filter' => array( 'posts_per_page' => 5, 'paged' => 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
			'page'    => 1,
515
			), rest_url( '/wp/v2/posts' ) );
516
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
517
		$next_link = add_query_arg( array(
518
			'page'    => 3,
519
			), rest_url( '/wp/v2/posts' ) );
520
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
521
	}
522
523 View Code Duplication
	public function test_get_items_private_filter_query_var() {
524
		// Private query vars inaccessible to unauthorized users
525
		wp_set_current_user( 0 );
526
		$draft_id = $this->factory->post->create( array( 'post_status' => 'draft' ) );
527
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
528
		$request->set_param( 'filter', array( 'post_status' => 'draft' ) );
529
		$response = $this->server->dispatch( $request );
530
		$data = $response->get_data();
531
		$this->assertCount( 1, $data );
532
		$this->assertEquals( $this->post_id, $data[0]['id'] );
533
		// But they are accessible to authorized users
534
		wp_set_current_user( $this->editor_id );
535
		$response = $this->server->dispatch( $request );
536
		$data = $response->get_data();
537
		$this->assertCount( 1, $data );
538
		$this->assertEquals( $draft_id, $data[0]['id'] );
539
	}
540
541 View Code Duplication
	public function test_get_items_invalid_context() {
542
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
543
		$request->set_param( 'context', 'banana' );
544
		$response = $this->server->dispatch( $request );
545
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
546
	}
547
548 View Code Duplication
	public function test_get_items_invalid_date() {
549
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
550
		$request->set_param( 'after', rand_str() );
551
		$request->set_param( 'before', rand_str() );
552
		$response = $this->server->dispatch( $request );
553
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
554
	}
555
556
	public function test_get_items_valid_date() {
557
		$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...
558
		$post2 = $this->factory->post->create( array( 'post_date' => '2016-01-16T00:00:00Z' ) );
559
		$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...
560
561
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
562
		$request->set_param( 'after', '2016-01-15T00:00:00Z' );
563
		$request->set_param( 'before', '2016-01-17T00:00:00Z' );
564
		$response = $this->server->dispatch( $request );
565
		$data = $response->get_data();
566
		$this->assertCount( 1, $data );
567
		$this->assertEquals( $post2, $data[0]['id'] );
568
	}
569
570
	public function test_get_item() {
571
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
572
		$response = $this->server->dispatch( $request );
573
574
		$this->check_get_post_response( $response, 'view' );
575
	}
576
577
	public function test_get_item_links() {
578
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
579
		$response = $this->server->dispatch( $request );
580
581
		$links = $response->get_links();
582
583
		$this->assertEquals( rest_url( '/wp/v2/posts/' . $this->post_id ), $links['self'][0]['href'] );
584
		$this->assertEquals( rest_url( '/wp/v2/posts' ), $links['collection'][0]['href'] );
585
586
		$this->assertEquals( rest_url( '/wp/v2/types/' . get_post_type( $this->post_id ) ), $links['about'][0]['href'] );
587
588
		$replies_url = rest_url( '/wp/v2/comments' );
589
		$replies_url = add_query_arg( 'post', $this->post_id, $replies_url );
590
		$this->assertEquals( $replies_url, $links['replies'][0]['href'] );
591
592
		$this->assertEquals( rest_url( '/wp/v2/posts/' . $this->post_id . '/revisions' ), $links['version-history'][0]['href'] );
593
594
		$attachments_url = rest_url( '/wp/v2/media' );
595
		$attachments_url = add_query_arg( 'parent', $this->post_id, $attachments_url );
596
		$this->assertEquals( $attachments_url, $links['https://api.w.org/attachment'][0]['href'] );
597
598
		$term_links = $links['https://api.w.org/term'];
599
		$tag_link = $cat_link = $format_link = null;
600
		foreach ( $term_links as $link ) {
601
			if ( 'post_tag' === $link['attributes']['taxonomy'] ) {
602
				$tag_link = $link;
603
			} elseif ( 'category' === $link['attributes']['taxonomy'] ) {
604
				$cat_link = $link;
605
			} else if ( 'post_format' === $link['attributes']['taxonomy'] ) {
606
				$format_link = $link;
607
			}
608
		}
609
		$this->assertNotEmpty( $tag_link );
610
		$this->assertNotEmpty( $cat_link );
611
		$this->assertNull( $format_link );
612
613
		$tags_url = add_query_arg( 'post', $this->post_id, rest_url( '/wp/v2/tags' ) );
614
		$this->assertEquals( $tags_url, $tag_link['href'] );
615
616
		$category_url = add_query_arg( 'post', $this->post_id, rest_url( '/wp/v2/categories' ) );
617
		$this->assertEquals( $category_url, $cat_link['href'] );
618
	}
619
620
	public function test_get_item_links_no_author() {
621
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
622
		$response = $this->server->dispatch( $request );
623
		$links = $response->get_links();
624
		$this->assertFalse( isset( $links['author'] ) );
625
		wp_update_post( array( 'ID' => $this->post_id, 'post_author' => $this->author_id ) );
626
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
627
		$response = $this->server->dispatch( $request );
628
		$links = $response->get_links();
629
		$this->assertEquals( rest_url( '/wp/v2/users/' . $this->author_id ), $links['author'][0]['href'] );
630
	}
631
632
	public function test_get_post_without_permission() {
633
		$draft_id = $this->factory->post->create( array(
634
			'post_status' => 'draft',
635
		) );
636
		wp_set_current_user( 0 );
637
638
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $draft_id ) );
639
		$response = $this->server->dispatch( $request );
640
641
		$this->assertErrorResponse( 'rest_forbidden', $response, 403 );
642
	}
643
644
	public function test_get_post_invalid_id() {
645
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
646
		$response = $this->server->dispatch( $request );
647
648
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
649
	}
650
651
	public function test_get_post_list_context_with_permission() {
652
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
653
		$request->set_query_params( array(
654
			'context' => 'edit',
655
		) );
656
657
		wp_set_current_user( $this->editor_id );
658
659
		$response = $this->server->dispatch( $request );
660
661
		$this->check_get_posts_response( $response, 'edit' );
662
	}
663
664 View Code Duplication
	public function test_get_post_list_context_without_permission() {
665
		wp_set_current_user( 0 );
666
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
667
		$request->set_query_params( array(
668
			'context' => 'edit',
669
		) );
670
		$response = $this->server->dispatch( $request );
671
672
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
673
	}
674
675 View Code Duplication
	public function test_get_post_context_without_permission() {
676
		wp_set_current_user( 0 );
677
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
678
		$request->set_query_params( array(
679
			'context' => 'edit',
680
		) );
681
		$response = $this->server->dispatch( $request );
682
683
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
684
	}
685
686
	public function test_get_post_with_password() {
687
		$post_id = $this->factory->post->create( array(
688
			'post_password' => '$inthebananastand',
689
		) );
690
691
		wp_set_current_user( $this->editor_id );
692
693
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
694
		$response = $this->server->dispatch( $request );
695
696
		$this->check_get_post_response( $response, 'view' );
697
	}
698
699
	public function test_get_post_with_password_without_permission() {
700
		$post_id = $this->factory->post->create( array(
701
			'post_password' => '$inthebananastand',
702
		) );
703
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
704
		$response = $this->server->dispatch( $request );
705
706
		$this->assertErrorResponse( 'rest_forbidden', $response, 403 );
707
	}
708
709
	public function test_get_item_read_permission_custom_post_status() {
710
		register_post_status( 'testpubstatus', array( 'public' => true ) );
711
		register_post_status( 'testprivtatus', array( 'public' => false ) );
712
		// Public status
713
		wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'testpubstatus' ) );
714
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
715
		$response = $this->server->dispatch( $request );
716
		$this->assertEquals( 200, $response->get_status() );
717
		// Private status
718
		wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'testprivtatus' ) );
719
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
720
		$response = $this->server->dispatch( $request );
721
		$this->assertEquals( 403, $response->get_status() );
722
	}
723
724
	public function test_prepare_item() {
725
		wp_set_current_user( $this->editor_id );
726
727
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
728
		$request->set_query_params( array( 'context' => 'edit' ) );
729
		$response = $this->server->dispatch( $request );
730
731
		$this->check_get_post_response( $response, 'edit' );
732
	}
733
734
	public function test_create_item() {
735
		wp_set_current_user( $this->editor_id );
736
737
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
738
		$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
739
		$params = $this->set_post_data();
740
		$request->set_body_params( $params );
741
		$response = $this->server->dispatch( $request );
742
743
		$this->check_create_post_response( $response );
744
	}
745
746
	public function test_rest_create_item() {
747
		wp_set_current_user( $this->editor_id );
748
749
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
750
		$request->add_header( 'content-type', 'application/json' );
751
		$params = $this->set_post_data();
752
		$request->set_body( wp_json_encode( $params ) );
753
		$response = $this->server->dispatch( $request );
754
755
		$this->check_create_post_response( $response );
756
	}
757
758
	public function test_create_post_invalid_id() {
759
		wp_set_current_user( $this->editor_id );
760
761
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
762
		$params = $this->set_post_data( array(
763
			'id' => '3',
764
		) );
765
		$request->set_body_params( $params );
766
		$response = $this->server->dispatch( $request );
767
768
		$this->assertErrorResponse( 'rest_post_exists', $response, 400 );
769
	}
770
771
	public function test_create_post_as_contributor() {
772
		wp_set_current_user( $this->contributor_id );
773
774
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
775
		$params = $this->set_post_data(array(
776
			'status' => 'pending',
777
		));
778
779
		$request->set_body_params( $params );
780
		$response = $this->server->dispatch( $request );
781
		$this->check_create_post_response( $response );
782
	}
783
784
	public function test_create_post_sticky() {
785
		wp_set_current_user( $this->editor_id );
786
787
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
788
		$params = $this->set_post_data( array(
789
			'sticky' => true,
790
		) );
791
		$request->set_body_params( $params );
792
		$response = $this->server->dispatch( $request );
793
794
		$new_data = $response->get_data();
795
		$this->assertEquals( true, $new_data['sticky'] );
796
		$post = get_post( $new_data['id'] );
797
		$this->assertEquals( true, is_sticky( $post->ID ) );
798
	}
799
800
	public function test_create_post_sticky_as_contributor() {
801
		wp_set_current_user( $this->contributor_id );
802
803
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
804
		$params = $this->set_post_data( array(
805
			'sticky' => true,
806
			'status' => 'pending',
807
		) );
808
		$request->set_body_params( $params );
809
		$response = $this->server->dispatch( $request );
810
811
		$this->assertErrorResponse( 'rest_cannot_assign_sticky', $response, 403 );
812
	}
813
814
	public function test_create_post_other_author_without_permission() {
815
		wp_set_current_user( $this->author_id );
816
817
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
818
		$params = $this->set_post_data(array(
819
			'author' => $this->editor_id,
820
		));
821
		$request->set_body_params( $params );
822
		$response = $this->server->dispatch( $request );
823
824
		$this->assertErrorResponse( 'rest_cannot_edit_others', $response, 403 );
825
	}
826
827
	public function test_create_post_without_permission() {
828
		wp_set_current_user( 0 );
829
830
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
831
		$params = $this->set_post_data( array(
832
			'status' => 'draft',
833
		) );
834
		$request->set_body_params( $params );
835
		$response = $this->server->dispatch( $request );
836
837
		$this->assertErrorResponse( 'rest_cannot_create', $response, 401 );
838
	}
839
840 View Code Duplication
	public function test_create_post_draft() {
841
		wp_set_current_user( $this->editor_id );
842
843
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
844
		$params = $this->set_post_data( array(
845
			'status' => 'draft',
846
		) );
847
		$request->set_body_params( $params );
848
		$response = $this->server->dispatch( $request );
849
850
		$data = $response->get_data();
851
		$new_post = get_post( $data['id'] );
852
		$this->assertEquals( 'draft', $data['status'] );
853
		$this->assertEquals( 'draft', $new_post->post_status );
854
		// Confirm dates are null
855
		$this->assertNull( $data['date_gmt'] );
856
		$this->assertNull( $data['modified_gmt'] );
857
	}
858
859 View Code Duplication
	public function test_create_post_private() {
860
		wp_set_current_user( $this->editor_id );
861
862
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
863
		$params = $this->set_post_data( array(
864
			'status' => 'private',
865
		) );
866
		$request->set_body_params( $params );
867
		$response = $this->server->dispatch( $request );
868
869
		$data = $response->get_data();
870
		$new_post = get_post( $data['id'] );
871
		$this->assertEquals( 'private', $data['status'] );
872
		$this->assertEquals( 'private', $new_post->post_status );
873
	}
874
875
	public function test_create_post_private_without_permission() {
876
		wp_set_current_user( $this->author_id );
877
		$user = wp_get_current_user();
878
		$user->add_cap( 'publish_posts', false );
879
		// Flush capabilities, https://core.trac.wordpress.org/ticket/28374
880
		$user->get_role_caps();
881
		$user->update_user_level_from_caps();
882
883
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
884
		$params = $this->set_post_data( array(
885
			'status' => 'private',
886
			'author' => $this->author_id,
887
		) );
888
		$request->set_body_params( $params );
889
		$response = $this->server->dispatch( $request );
890
891
		$this->assertErrorResponse( 'rest_cannot_publish', $response, 403 );
892
	}
893
894
	public function test_create_post_publish_without_permission() {
895
		wp_set_current_user( $this->author_id );
896
		$user = wp_get_current_user();
897
		$user->add_cap( 'publish_posts', false );
898
		// Flush capabilities, https://core.trac.wordpress.org/ticket/28374
899
		$user->get_role_caps();
900
		$user->update_user_level_from_caps();
901
902
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
903
		$params = $this->set_post_data( array(
904
			'status' => 'publish',
905
		) );
906
		$request->set_body_params( $params );
907
		$response = $this->server->dispatch( $request );
908
909
		$this->assertErrorResponse( 'rest_cannot_publish', $response, 403 );
910
	}
911
912
	public function test_create_post_invalid_status() {
913
		wp_set_current_user( $this->editor_id );
914
915
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
916
		$params = $this->set_post_data( array(
917
			'status' => 'teststatus',
918
		) );
919
		$request->set_body_params( $params );
920
		$response = $this->server->dispatch( $request );
921
922
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
923
	}
924
925 View Code Duplication
	public function test_create_post_with_format() {
926
		wp_set_current_user( $this->editor_id );
927
928
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
929
		$params = $this->set_post_data( array(
930
			'format' => 'gallery',
931
		) );
932
		$request->set_body_params( $params );
933
		$response = $this->server->dispatch( $request );
934
935
		$data = $response->get_data();
936
		$new_post = get_post( $data['id'] );
937
		$this->assertEquals( 'gallery', $data['format'] );
938
		$this->assertEquals( 'gallery', get_post_format( $new_post->ID ) );
939
	}
940
941
	public function test_create_post_with_invalid_format() {
942
		wp_set_current_user( $this->editor_id );
943
944
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
945
		$params = $this->set_post_data( array(
946
			'format' => 'testformat',
947
		) );
948
		$request->set_body_params( $params );
949
		$response = $this->server->dispatch( $request );
950
951
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
952
	}
953
954
	public function test_create_update_post_with_featured_media() {
955
956
		$file = DIR_TESTDATA . '/images/canola.jpg';
957
		$this->attachment_id = $this->factory->attachment->create_object( $file, 0, array(
958
			'post_mime_type' => 'image/jpeg',
959
			'menu_order' => rand( 1, 100 ),
960
		) );
961
962
		wp_set_current_user( $this->editor_id );
963
964
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
965
		$params = $this->set_post_data( array(
966
			'featured_media' => $this->attachment_id,
967
		) );
968
		$request->set_body_params( $params );
969
		$response = $this->server->dispatch( $request );
970
		$data = $response->get_data();
971
		$new_post = get_post( $data['id'] );
972
		$this->assertEquals( $this->attachment_id, $data['featured_media'] );
973
		$this->assertEquals( $this->attachment_id, (int) get_post_thumbnail_id( $new_post->ID ) );
974
975
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $new_post->ID );
976
		$params = $this->set_post_data( array(
977
			'featured_media' => 0,
978
		) );
979
		$request->set_body_params( $params );
980
		$response = $this->server->dispatch( $request );
981
		$data = $response->get_data();
982
		$this->assertEquals( 0, $data['featured_media'] );
983
		$this->assertEquals( 0, (int) get_post_thumbnail_id( $new_post->ID ) );
984
	}
985
986
	public function test_create_post_invalid_author() {
987
		wp_set_current_user( $this->editor_id );
988
989
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
990
		$params = $this->set_post_data( array(
991
			'author' => -1,
992
		) );
993
		$request->set_body_params( $params );
994
		$response = $this->server->dispatch( $request );
995
996
		$this->assertErrorResponse( 'rest_invalid_author', $response, 400 );
997
	}
998
999
	public function test_create_post_invalid_author_without_permission() {
1000
		wp_set_current_user( $this->author_id );
1001
1002
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1003
		$params = $this->set_post_data( array(
1004
			'author' => $this->editor_id,
1005
		) );
1006
		$request->set_body_params( $params );
1007
		$response = $this->server->dispatch( $request );
1008
1009
		$this->assertErrorResponse( 'rest_cannot_edit_others', $response, 403 );
1010
	}
1011
1012
	public function test_create_post_with_password() {
1013
		wp_set_current_user( $this->editor_id );
1014
1015
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1016
		$params = $this->set_post_data( array(
1017
			'password' => 'testing',
1018
		) );
1019
		$request->set_body_params( $params );
1020
		$response = $this->server->dispatch( $request );
1021
1022
		$data = $response->get_data();
1023
		$this->assertEquals( 'testing', $data['password'] );
1024
	}
1025
1026
	public function test_create_post_with_password_without_permission() {
1027
		wp_set_current_user( $this->author_id );
1028
		$user = wp_get_current_user();
1029
		$user->add_cap( 'publish_posts', false );
1030
		// Flush capabilities, https://core.trac.wordpress.org/ticket/28374
1031
		$user->get_role_caps();
1032
		$user->update_user_level_from_caps();
1033
1034
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1035
		$params = $this->set_post_data( array(
1036
			'password' => 'testing',
1037
			'author'   => $this->author_id,
1038
			'status'   => 'draft',
1039
		) );
1040
		$request->set_body_params( $params );
1041
		$response = $this->server->dispatch( $request );
1042
1043
		$this->assertErrorResponse( 'rest_cannot_publish', $response, 403 );
1044
	}
1045
1046
	public function test_create_post_with_falsy_password() {
1047
		wp_set_current_user( $this->editor_id );
1048
1049
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1050
		$params = $this->set_post_data( array(
1051
			'password' => '0',
1052
		) );
1053
		$request->set_body_params( $params );
1054
		$response = $this->server->dispatch( $request );
1055
1056
		$data = $response->get_data();
1057
1058
		$this->assertEquals( '0', $data['password'] );
1059
	}
1060
1061 View Code Duplication
	public function test_create_post_with_empty_string_password_and_sticky() {
1062
		wp_set_current_user( $this->editor_id );
1063
1064
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1065
		$params = $this->set_post_data( array(
1066
			'password' => '',
1067
			'sticky'   => true,
1068
		) );
1069
		$request->set_body_params( $params );
1070
		$response = $this->server->dispatch( $request );
1071
1072
		$this->assertEquals( 201, $response->get_status() );
1073
		$data = $response->get_data();
1074
		$this->assertEquals( '', $data['password'] );
1075
	}
1076
1077
	public function test_create_post_with_password_and_sticky_fails() {
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' => '123',
1083
			'sticky'   => true,
1084
		) );
1085
		$request->set_body_params( $params );
1086
		$response = $this->server->dispatch( $request );
1087
1088
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1089
	}
1090
1091
	public function test_create_post_custom_date() {
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
			'date' => '2010-01-01T02:00:00Z',
1097
		) );
1098
		$request->set_body_params( $params );
1099
		$response = $this->server->dispatch( $request );
1100
1101
		$data = $response->get_data();
1102
		$new_post = get_post( $data['id'] );
1103
		$time = gmmktime( 2, 0, 0, 1, 1, 2010 );
1104
		$this->assertEquals( '2010-01-01T02:00:00', $data['date'] );
1105
		$this->assertEquals( $time, strtotime( $new_post->post_date ) );
1106
	}
1107
1108
	public function test_create_post_custom_date_with_timezone() {
1109
		wp_set_current_user( $this->editor_id );
1110
1111
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1112
		$params = $this->set_post_data( array(
1113
			'date' => '2010-01-01T02:00:00-10:00',
1114
		) );
1115
		$request->set_body_params( $params );
1116
		$response = $this->server->dispatch( $request );
1117
1118
		$data = $response->get_data();
1119
		$new_post = get_post( $data['id'] );
1120
		$time = gmmktime( 12, 0, 0, 1, 1, 2010 );
1121
1122
		$this->assertEquals( '2010-01-01T12:00:00', $data['date'] );
1123
		$this->assertEquals( '2010-01-01T12:00:00', $data['modified'] );
1124
1125
		$this->assertEquals( $time, strtotime( $new_post->post_date ) );
1126
		$this->assertEquals( $time, strtotime( $new_post->post_modified ) );
1127
	}
1128
1129
	public function test_create_post_with_db_error() {
1130
		wp_set_current_user( $this->editor_id );
1131
1132
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1133
		$params  = $this->set_post_data( array() );
1134
		$request->set_body_params( $params );
1135
1136
		/**
1137
		 * Disable showing error as the below is going to intentionally
1138
		 * trigger a DB error.
1139
		 */
1140
		global $wpdb;
1141
		$wpdb->suppress_errors = true;
1142
		add_filter( 'query', array( $this, 'error_insert_query' ) );
1143
1144
		$response = $this->server->dispatch( $request );
1145
		remove_filter( 'query', array( $this, 'error_insert_query' ) );
1146
		$wpdb->show_errors = true;
1147
1148
		$this->assertErrorResponse( 'db_insert_error', $response, 500 );
1149
	}
1150
1151
	public function test_create_post_with_invalid_date() {
1152
		wp_set_current_user( $this->editor_id );
1153
1154
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1155
		$params = $this->set_post_data( array(
1156
			'date' => '2010-60-01T02:00:00Z',
1157
		) );
1158
		$request->set_body_params( $params );
1159
		$response = $this->server->dispatch( $request );
1160
1161
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1162
	}
1163
1164
	public function test_create_post_with_invalid_date_gmt() {
1165
		wp_set_current_user( $this->editor_id );
1166
1167
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1168
		$params = $this->set_post_data( array(
1169
			'date_gmt' => '2010-60-01T02:00:00',
1170
		) );
1171
		$request->set_body_params( $params );
1172
		$response = $this->server->dispatch( $request );
1173
1174
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1175
	}
1176
1177 View Code Duplication
	public function test_create_post_with_quotes_in_title() {
1178
		wp_set_current_user( $this->editor_id );
1179
1180
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1181
		$params = $this->set_post_data( array(
1182
			'title' => "Rob O'Rourke's Diary",
1183
		) );
1184
		$request->set_body_params( $params );
1185
		$response = $this->server->dispatch( $request );
1186
		$new_data = $response->get_data();
1187
		$this->assertEquals( "Rob O'Rourke's Diary", $new_data['title']['raw'] );
1188
	}
1189
1190
	public function test_create_post_with_categories() {
1191
		wp_set_current_user( $this->editor_id );
1192
		$category = wp_insert_term( 'Test Category', 'category' );
1193
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1194
		$params = $this->set_post_data( array(
1195
			'password'   => 'testing',
1196
			'categories' => array(
1197
				$category['term_id']
1198
			),
1199
		) );
1200
		$request->set_body_params( $params );
1201
		$response = $this->server->dispatch( $request );
1202
1203
		$data = $response->get_data();
1204
		$this->assertEquals( array( $category['term_id'] ), $data['categories'] );
1205
	}
1206
1207
	public function test_create_post_with_invalid_categories() {
1208
		wp_set_current_user( $this->editor_id );
1209
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1210
		$params = $this->set_post_data( array(
1211
			'password'   => 'testing',
1212
			'categories' => array(
1213
				REST_TESTS_IMPOSSIBLY_HIGH_NUMBER
1214
			),
1215
		) );
1216
		$request->set_body_params( $params );
1217
		$response = $this->server->dispatch( $request );
1218
1219
		$data = $response->get_data();
1220
		$this->assertEquals( array(), $data['categories'] );
1221
	}
1222
1223 View Code Duplication
	public function test_update_item() {
1224
		wp_set_current_user( $this->editor_id );
1225
1226
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1227
		$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
1228
		$params = $this->set_post_data();
1229
		$request->set_body_params( $params );
1230
		$response = $this->server->dispatch( $request );
1231
1232
		$this->check_update_post_response( $response );
1233
		$new_data = $response->get_data();
1234
		$this->assertEquals( $this->post_id, $new_data['id'] );
1235
		$this->assertEquals( $params['title'], $new_data['title']['raw'] );
1236
		$this->assertEquals( $params['content'], $new_data['content']['raw'] );
1237
		$this->assertEquals( $params['excerpt'], $new_data['excerpt']['raw'] );
1238
		$post = get_post( $this->post_id );
1239
		$this->assertEquals( $params['title'], $post->post_title );
1240
		$this->assertEquals( $params['content'], $post->post_content );
1241
		$this->assertEquals( $params['excerpt'], $post->post_excerpt );
1242
	}
1243
1244 View Code Duplication
	public function test_rest_update_post() {
1245
		wp_set_current_user( $this->editor_id );
1246
1247
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1248
		$request->add_header( 'content-type', 'application/json' );
1249
		$params = $this->set_post_data();
1250
		$request->set_body( wp_json_encode( $params ) );
1251
		$response = $this->server->dispatch( $request );
1252
1253
		$this->check_update_post_response( $response );
1254
		$new_data = $response->get_data();
1255
		$this->assertEquals( $this->post_id, $new_data['id'] );
1256
		$this->assertEquals( $params['title'], $new_data['title']['raw'] );
1257
		$this->assertEquals( $params['content'], $new_data['content']['raw'] );
1258
		$this->assertEquals( $params['excerpt'], $new_data['excerpt']['raw'] );
1259
		$post = get_post( $this->post_id );
1260
		$this->assertEquals( $params['title'], $post->post_title );
1261
		$this->assertEquals( $params['content'], $post->post_content );
1262
		$this->assertEquals( $params['excerpt'], $post->post_excerpt );
1263
	}
1264
1265
	public function test_rest_update_post_raw() {
1266
		wp_set_current_user( $this->editor_id );
1267
1268
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1269
		$request->add_header( 'content-type', 'application/json' );
1270
		$params = $this->set_raw_post_data();
1271
		$request->set_body( wp_json_encode( $params ) );
1272
		$response = $this->server->dispatch( $request );
1273
1274
		$this->check_update_post_response( $response );
1275
		$new_data = $response->get_data();
1276
		$this->assertEquals( $this->post_id, $new_data['id'] );
1277
		$this->assertEquals( $params['title']['raw'], $new_data['title']['raw'] );
1278
		$this->assertEquals( $params['content']['raw'], $new_data['content']['raw'] );
1279
		$this->assertEquals( $params['excerpt']['raw'], $new_data['excerpt']['raw'] );
1280
		$post = get_post( $this->post_id );
1281
		$this->assertEquals( $params['title']['raw'], $post->post_title );
1282
		$this->assertEquals( $params['content']['raw'], $post->post_content );
1283
		$this->assertEquals( $params['excerpt']['raw'], $post->post_excerpt );
1284
	}
1285
1286
	public function test_update_post_without_extra_params() {
1287
		wp_set_current_user( $this->editor_id );
1288
1289
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1290
		$params = $this->set_post_data();
1291
		unset( $params['type'] );
1292
		unset( $params['name'] );
1293
		unset( $params['author'] );
1294
		unset( $params['status'] );
1295
		$request->set_body_params( $params );
1296
		$response = $this->server->dispatch( $request );
1297
1298
		$this->check_update_post_response( $response );
1299
	}
1300
1301
	public function test_update_post_without_permission() {
1302
		wp_set_current_user( $this->editor_id );
1303
		$user = wp_get_current_user();
1304
		$user->add_cap( 'edit_published_posts', false );
1305
		// Flush capabilities, https://core.trac.wordpress.org/ticket/28374
1306
		$user->get_role_caps();
1307
		$user->update_user_level_from_caps();
1308
1309
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1310
		$params = $this->set_post_data();
1311
		$request->set_body_params( $params );
1312
		$response = $this->server->dispatch( $request );
1313
1314
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
1315
	}
1316
1317 View Code Duplication
	public function test_update_post_sticky_as_contributor() {
1318
		wp_set_current_user( $this->contributor_id );
1319
1320
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1321
		$params = $this->set_post_data( array(
1322
			'sticky' => true,
1323
			'status' => 'pending',
1324
		) );
1325
		$request->set_body_params( $params );
1326
		$response = $this->server->dispatch( $request );
1327
1328
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
1329
	}
1330
1331
	public function test_update_post_invalid_id() {
1332
		wp_set_current_user( $this->editor_id );
1333
1334
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
1335
		$response = $this->server->dispatch( $request );
1336
1337
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1338
	}
1339
1340
	public function test_update_post_invalid_route() {
1341
		wp_set_current_user( $this->editor_id );
1342
1343
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', $this->post_id ) );
1344
		$response = $this->server->dispatch( $request );
1345
1346
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1347
	}
1348
1349 View Code Duplication
	public function test_update_post_with_format() {
1350
		wp_set_current_user( $this->editor_id );
1351
1352
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1353
		$params = $this->set_post_data( array(
1354
			'format' => 'gallery',
1355
		) );
1356
		$request->set_body_params( $params );
1357
		$response = $this->server->dispatch( $request );
1358
1359
		$data = $response->get_data();
1360
		$new_post = get_post( $data['id'] );
1361
		$this->assertEquals( 'gallery', $data['format'] );
1362
		$this->assertEquals( 'gallery', get_post_format( $new_post->ID ) );
1363
	}
1364
1365
	public function test_update_post_with_invalid_format() {
1366
		wp_set_current_user( $this->editor_id );
1367
1368
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1369
		$params = $this->set_post_data( array(
1370
			'format' => 'testformat',
1371
		) );
1372
		$request->set_body_params( $params );
1373
		$response = $this->server->dispatch( $request );
1374
1375
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1376
	}
1377
1378
	public function test_update_post_ignore_readonly() {
1379
		wp_set_current_user( $this->editor_id );
1380
1381
		$new_content = rand_str();
1382
		$expected_modified = current_time( 'mysql' );
1383
1384
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1385
		$params = $this->set_post_data( array(
1386
			'modified' => '2010-06-01T02:00:00Z',
1387
			'content'  => $new_content,
1388
		) );
1389
		$request->set_body_params( $params );
1390
		$response = $this->server->dispatch( $request );
1391
1392
		// The readonly modified param should be ignored, request should be a success.
1393
		$data = $response->get_data();
1394
		$new_post = get_post( $data['id'] );
1395
1396
		$this->assertEquals( $new_content, $data['content']['raw'] );
1397
		$this->assertEquals( $new_content, $new_post->post_content );
1398
1399
		// The modified date should equal the current time.
1400
		$this->assertEquals( date( 'Y-m-d', strtotime( mysql_to_rfc3339( $expected_modified ) ) ), date( 'Y-m-d', strtotime( $data['modified'] ) ) );
1401
		$this->assertEquals( date( 'Y-m-d', strtotime( $expected_modified ) ), date( 'Y-m-d', strtotime( $new_post->post_modified ) ) );
1402
	}
1403
1404 View Code Duplication
	public function test_update_post_with_invalid_date() {
1405
		wp_set_current_user( $this->editor_id );
1406
1407
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1408
		$params = $this->set_post_data( array(
1409
			'date' => rand_str(),
1410
		) );
1411
		$request->set_body_params( $params );
1412
		$response = $this->server->dispatch( $request );
1413
1414
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1415
	}
1416
1417 View Code Duplication
	public function test_update_post_with_invalid_date_gmt() {
1418
		wp_set_current_user( $this->editor_id );
1419
1420
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1421
		$params = $this->set_post_data( array(
1422
			'date_gmt' => rand_str(),
1423
		) );
1424
		$request->set_body_params( $params );
1425
		$response = $this->server->dispatch( $request );
1426
1427
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1428
	}
1429
1430 View Code Duplication
	public function test_update_post_slug() {
1431
		wp_set_current_user( $this->editor_id );
1432
1433
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1434
		$params = $this->set_post_data( array(
1435
			'slug' => 'sample-slug',
1436
		) );
1437
		$request->set_body_params( $params );
1438
		$response = $this->server->dispatch( $request );
1439
1440
		$new_data = $response->get_data();
1441
		$this->assertEquals( 'sample-slug', $new_data['slug'] );
1442
		$post = get_post( $new_data['id'] );
1443
		$this->assertEquals( 'sample-slug', $post->post_name );
1444
	}
1445
1446 View Code Duplication
	public function test_update_post_slug_accented_chars() {
1447
		wp_set_current_user( $this->editor_id );
1448
1449
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1450
		$params = $this->set_post_data( array(
1451
			'slug' => 'tęst-acceńted-chäræcters',
1452
		) );
1453
		$request->set_body_params( $params );
1454
		$response = $this->server->dispatch( $request );
1455
1456
		$new_data = $response->get_data();
1457
		$this->assertEquals( 'test-accented-charaecters', $new_data['slug'] );
1458
		$post = get_post( $new_data['id'] );
1459
		$this->assertEquals( 'test-accented-charaecters', $post->post_name );
1460
	}
1461
1462
	public function test_update_post_sticky() {
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
			'sticky' => true,
1468
		) );
1469
		$request->set_body_params( $params );
1470
		$response = $this->server->dispatch( $request );
1471
1472
		$new_data = $response->get_data();
1473
		$this->assertEquals( true, $new_data['sticky'] );
1474
		$post = get_post( $new_data['id'] );
1475
		$this->assertEquals( true, is_sticky( $post->ID ) );
1476
1477
		// Updating another field shouldn't change sticky status
1478
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1479
		$params = $this->set_post_data( array(
1480
			'title'       => 'This should not reset sticky',
1481
		) );
1482
		$request->set_body_params( $params );
1483
		$response = $this->server->dispatch( $request );
1484
1485
		$new_data = $response->get_data();
1486
		$this->assertEquals( true, $new_data['sticky'] );
1487
		$post = get_post( $new_data['id'] );
1488
		$this->assertEquals( true, is_sticky( $post->ID ) );
1489
	}
1490
1491
	public function test_update_post_excerpt() {
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
		$request->set_body_params( array(
1496
			'excerpt' => 'An Excerpt',
1497
		) );
1498
1499
		$response = $this->server->dispatch( $request );
1500
		$new_data = $response->get_data();
1501
		$this->assertEquals( 'An Excerpt', $new_data['excerpt']['raw'] );
1502
	}
1503
1504
	public function test_update_post_empty_excerpt() {
1505
		wp_set_current_user( $this->editor_id );
1506
1507
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1508
		$request->set_body_params( array(
1509
			'excerpt' => '',
1510
		) );
1511
1512
		$response = $this->server->dispatch( $request );
1513
		$new_data = $response->get_data();
1514
		$this->assertEquals( '', $new_data['excerpt']['raw'] );
1515
	}
1516
1517
	public function test_update_post_content() {
1518
		wp_set_current_user( $this->editor_id );
1519
1520
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1521
		$request->set_body_params( array(
1522
			'content' => 'Some Content',
1523
		) );
1524
1525
		$response = $this->server->dispatch( $request );
1526
		$new_data = $response->get_data();
1527
		$this->assertEquals( 'Some Content', $new_data['content']['raw'] );
1528
	}
1529
1530
	public function test_update_post_empty_content() {
1531
		wp_set_current_user( $this->editor_id );
1532
1533
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1534
		$request->set_body_params( array(
1535
			'content' => '',
1536
		) );
1537
1538
		$response = $this->server->dispatch( $request );
1539
		$new_data = $response->get_data();
1540
		$this->assertEquals( '', $new_data['content']['raw'] );
1541
	}
1542
1543 View Code Duplication
	public function test_update_post_with_password_and_sticky_fails() {
1544
		wp_set_current_user( $this->editor_id );
1545
1546
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1547
		$params = $this->set_post_data( array(
1548
			'password' => '123',
1549
			'sticky'   => true,
1550
		) );
1551
		$request->set_body_params( $params );
1552
		$response = $this->server->dispatch( $request );
1553
1554
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1555
	}
1556
1557
	public function test_update_stick_post_with_password_fails() {
1558
		wp_set_current_user( $this->editor_id );
1559
1560
		stick_post( $this->post_id );
1561
1562
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1563
		$params = $this->set_post_data( array(
1564
			'password' => '123',
1565
		) );
1566
		$request->set_body_params( $params );
1567
		$response = $this->server->dispatch( $request );
1568
1569
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1570
	}
1571
1572 View Code Duplication
	public function test_update_password_protected_post_with_sticky_fails() {
1573
		wp_set_current_user( $this->editor_id );
1574
1575
		wp_update_post( array( 'ID' => $this->post_id, 'post_password' => '123' ) );
1576
1577
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1578
		$params = $this->set_post_data( array(
1579
			'sticky' => true,
1580
		) );
1581
		$request->set_body_params( $params );
1582
		$response = $this->server->dispatch( $request );
1583
1584
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1585
	}
1586
1587
	public function test_update_post_with_quotes_in_title() {
1588
		wp_set_current_user( $this->editor_id );
1589
1590
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1591
		$params = $this->set_post_data( array(
1592
			'title' => "Rob O'Rourke's Diary",
1593
		) );
1594
		$request->set_body_params( $params );
1595
		$response = $this->server->dispatch( $request );
1596
		$new_data = $response->get_data();
1597
		$this->assertEquals( "Rob O'Rourke's Diary", $new_data['title']['raw'] );
1598
	}
1599
1600
	public function test_update_post_with_categories() {
1601
1602
		wp_set_current_user( $this->editor_id );
1603
		$category = wp_insert_term( 'Test Category', 'category' );
1604
1605
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1606
		$params = $this->set_post_data( array(
1607
			'title' => 'Tester',
1608
			'categories' => array(
1609
				$category['term_id'],
1610
			),
1611
		) );
1612
		$request->set_body_params( $params );
1613
		$response = $this->server->dispatch( $request );
1614
		$new_data = $response->get_data();
1615
		$this->assertEquals( array( $category['term_id'] ), $new_data['categories'] );
1616
		$categories_path = '';
1617
		$links = $response->get_links();
1618
		foreach ( $links['https://api.w.org/term'] as $link ) {
1619
			if ( 'category' === $link['attributes']['taxonomy'] ) {
1620
				$categories_path = $link['href'];
1621
			}
1622
		}
1623
		$query = parse_url( $categories_path, PHP_URL_QUERY );
1624
		parse_str( $query, $args );
1625
		$request = new WP_REST_Request( 'GET', $args['rest_route'] );
1626
		unset( $args['rest_route'] );
1627
		$request->set_query_params( $args );
1628
		$response = $this->server->dispatch( $request );
1629
		$data = $response->get_data();
1630
		$this->assertCount( 1, $data );
1631
		$this->assertEquals( 'Test Category', $data[0]['name'] );
1632
	}
1633
1634
	public function test_update_post_with_empty_categories() {
1635
1636
		wp_set_current_user( $this->editor_id );
1637
		$category = wp_insert_term( 'Test Category', 'category' );
1638
		wp_set_object_terms( $this->post_id, $category['term_id'], 'category' );
1639
1640
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1641
		$params = $this->set_post_data( array(
1642
			'title' => 'Tester',
1643
			'categories' => array(),
1644
		) );
1645
		$request->set_body_params( $params );
1646
		$response = $this->server->dispatch( $request );
1647
		$new_data = $response->get_data();
1648
		$this->assertEquals( array(), $new_data['categories'] );
1649
	}
1650
1651 View Code Duplication
	public function test_delete_item() {
1652
		$post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
1653
		wp_set_current_user( $this->editor_id );
1654
1655
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
1656
		$response = $this->server->dispatch( $request );
1657
1658
		$this->assertNotInstanceOf( 'WP_Error', $response );
1659
		$this->assertEquals( 200, $response->get_status() );
1660
		$data = $response->get_data();
1661
		$this->assertEquals( 'Deleted post', $data['title']['raw'] );
1662
	}
1663
1664 View Code Duplication
	public function test_delete_item_skip_trash() {
1665
		$post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
1666
		wp_set_current_user( $this->editor_id );
1667
1668
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
1669
		$request['force'] = true;
1670
		$response = $this->server->dispatch( $request );
1671
1672
		$this->assertNotInstanceOf( 'WP_Error', $response );
1673
		$this->assertEquals( 200, $response->get_status() );
1674
		$data = $response->get_data();
1675
		$this->assertEquals( 'Deleted post', $data['title']['raw'] );
1676
	}
1677
1678 View Code Duplication
	public function test_delete_item_already_trashed() {
1679
		$post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
1680
		wp_set_current_user( $this->editor_id );
1681
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
1682
		$response = $this->server->dispatch( $request );
1683
		$this->assertEquals( 200, $response->get_status() );
1684
		$response = $this->server->dispatch( $request );
1685
		$this->assertErrorResponse( 'rest_already_trashed', $response, 410 );
1686
	}
1687
1688 View Code Duplication
	public function test_delete_post_invalid_id() {
1689
		wp_set_current_user( $this->editor_id );
1690
1691
		$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
1692
		$response = $this->server->dispatch( $request );
1693
1694
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1695
	}
1696
1697
	public function test_delete_post_invalid_post_type() {
1698
		$page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
1699
		wp_set_current_user( $this->editor_id );
1700
1701
		$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $page_id );
1702
		$response = $this->server->dispatch( $request );
1703
1704
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1705
	}
1706
1707
	public function test_delete_post_without_permission() {
1708
		wp_set_current_user( $this->author_id );
1709
1710
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1711
		$response = $this->server->dispatch( $request );
1712
1713
		$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
1714
	}
1715
1716
	public function test_register_post_type_invalid_controller() {
1717
1718
		register_post_type( 'invalid-controller', array( 'show_in_rest' => true, 'rest_controller_class' => 'Fake_Class_Baba' ) );
1719
		create_initial_rest_routes();
1720
		$routes = $this->server->get_routes();
1721
		$this->assertFalse( isset( $routes['/wp/v2/invalid-controller'] ) );
1722
		_unregister_post_type( 'invalid-controller' );
1723
1724
	}
1725
1726 View Code Duplication
	public function test_get_item_schema() {
1727
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
1728
		$response = $this->server->dispatch( $request );
1729
		$data = $response->get_data();
1730
		$properties = $data['schema']['properties'];
1731
		$this->assertEquals( 22, count( $properties ) );
1732
		$this->assertArrayHasKey( 'author', $properties );
1733
		$this->assertArrayHasKey( 'comment_status', $properties );
1734
		$this->assertArrayHasKey( 'content', $properties );
1735
		$this->assertArrayHasKey( 'date', $properties );
1736
		$this->assertArrayHasKey( 'date_gmt', $properties );
1737
		$this->assertArrayHasKey( 'excerpt', $properties );
1738
		$this->assertArrayHasKey( 'featured_media', $properties );
1739
		$this->assertArrayHasKey( 'guid', $properties );
1740
		$this->assertArrayHasKey( 'format', $properties );
1741
		$this->assertArrayHasKey( 'id', $properties );
1742
		$this->assertArrayHasKey( 'link', $properties );
1743
		$this->assertArrayHasKey( 'modified', $properties );
1744
		$this->assertArrayHasKey( 'modified_gmt', $properties );
1745
		$this->assertArrayHasKey( 'password', $properties );
1746
		$this->assertArrayHasKey( 'ping_status', $properties );
1747
		$this->assertArrayHasKey( 'slug', $properties );
1748
		$this->assertArrayHasKey( 'status', $properties );
1749
		$this->assertArrayHasKey( 'sticky', $properties );
1750
		$this->assertArrayHasKey( 'title', $properties );
1751
		$this->assertArrayHasKey( 'type', $properties );
1752
		$this->assertArrayHasKey( 'tags', $properties );
1753
		$this->assertArrayHasKey( 'categories', $properties );
1754
	}
1755
1756
	public function test_get_additional_field_registration() {
1757
1758
		$schema = array(
1759
			'type'        => 'integer',
1760
			'description' => 'Some integer of mine',
1761
			'enum'        => array( 1, 2, 3, 4 ),
1762
			'context'     => array( 'view', 'edit' ),
1763
		);
1764
1765
		register_rest_field( 'post', 'my_custom_int', array(
1766
			'schema'          => $schema,
1767
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
1768
			'update_callback' => array( $this, 'additional_field_update_callback' ),
1769
		) );
1770
1771
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
1772
1773
		$response = $this->server->dispatch( $request );
1774
		$data = $response->get_data();
1775
1776
		$this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
1777
		$this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
1778
1779
		wp_set_current_user( 1 );
1780
1781
		$post_id = $this->factory->post->create();
1782
1783
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
1784
1785
		$response = $this->server->dispatch( $request );
1786
		$this->assertArrayHasKey( 'my_custom_int', $response->data );
1787
1788
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $post_id );
1789
		$request->set_body_params(array(
1790
			'my_custom_int' => 123,
1791
		));
1792
1793
		$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...
1794
		$this->assertEquals( 123, get_post_meta( $post_id, 'my_custom_int', true ) );
1795
1796
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1797
		$request->set_body_params(array(
1798
			'my_custom_int' => 123,
1799
			'title' => 'hello',
1800
		));
1801
1802
		$response = $this->server->dispatch( $request );
1803
1804
		$this->assertEquals( 123, $response->data['my_custom_int'] );
1805
1806
		global $wp_rest_additional_fields;
1807
		$wp_rest_additional_fields = array();
1808
	}
1809
1810 View Code Duplication
	public function test_additional_field_update_errors() {
1811
		$schema = array(
1812
			'type'        => 'integer',
1813
			'description' => 'Some integer of mine',
1814
			'enum'        => array( 1, 2, 3, 4 ),
1815
			'context'     => array( 'view', 'edit' ),
1816
		);
1817
1818
		register_rest_field( 'post', 'my_custom_int', array(
1819
			'schema'          => $schema,
1820
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
1821
			'update_callback' => array( $this, 'additional_field_update_callback' ),
1822
		) );
1823
1824
		wp_set_current_user( $this->editor_id );
1825
		// Check for error on update.
1826
		$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1827
		$request->set_body_params( array(
1828
			'my_custom_int' => 'returnError',
1829
		) );
1830
1831
		$response = $this->server->dispatch( $request );
1832
1833
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1834
1835
		global $wp_rest_additional_fields;
1836
		$wp_rest_additional_fields = array();
1837
	}
1838
1839
	public function additional_field_get_callback( $object ) {
1840
		return get_post_meta( $object['id'], 'my_custom_int', true );
1841
	}
1842
1843
	public function additional_field_update_callback( $value, $post ) {
1844
		if ( 'returnError' === $value ) {
1845
			return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
1846
		}
1847
		update_post_meta( $post->ID, 'my_custom_int', $value );
1848
	}
1849
1850
	public function tearDown() {
1851
		_unregister_post_type( 'youseeeme' );
1852
		if ( isset( $this->attachment_id ) ) {
1853
			$this->remove_added_uploads();
1854
		}
1855
		parent::tearDown();
1856
	}
1857
1858
	/**
1859
	 * Internal function used to disable an insert query which
1860
	 * will trigger a wpdb error for testing purposes.
1861
	 */
1862
	public function error_insert_query( $query ) {
1863
		if ( strpos( $query, 'INSERT' ) === 0 ) {
1864
			$query = '],';
1865
		}
1866
		return $query;
1867
	}
1868
1869
}
1870