Completed
Branch develop (f3647e)
by
unknown
02:47
created

test_get_items_order_and_orderby()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 1
eloc 15
c 3
b 0
f 1
nc 1
nop 0
dl 0
loc 18
rs 9.4285
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
			'tags',
78
			), $keys );
79
	}
80
81
	public function test_get_items() {
82
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
83
		$response = $this->server->dispatch( $request );
84
85
		$this->check_get_posts_response( $response );
86
	}
87
88
	/**
89
	 * A valid query that returns 0 results should return an empty JSON list.
90
	 *
91
	 * @issue 862
92
	 */
93
	public function test_get_items_empty_query() {
94
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
95
		$request->set_query_params( array(
96
			'filter' => array( 'year' => 2008 ),
97
		) );
98
		$response = $this->server->dispatch( $request );
99
		$this->assertEquals( array(), $response->get_data() );
100
		$this->assertEquals( 200, $response->get_status() );
101
	}
102
103
	public function test_get_items_author_query() {
104
		$this->factory->post->create( array( 'post_author' => $this->editor_id ) );
105
		$this->factory->post->create( array( 'post_author' => $this->author_id ) );
106
		// All 3 posts
107
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
108
		$response = $this->server->dispatch( $request );
109
		$this->assertEquals( 200, $response->get_status() );
110
		$this->assertEquals( 3, count( $response->get_data() ) );
111
		// 2 of 3 posts
112
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
113
		$request->set_param( 'author', array( $this->editor_id, $this->author_id ) );
114
		$response = $this->server->dispatch( $request );
115
		$this->assertEquals( 200, $response->get_status() );
116
		$data = $response->get_data();
117
		$this->assertEquals( 2, count( $data ) );
118
		$this->assertEqualSets( array( $this->editor_id, $this->author_id ), wp_list_pluck( $data, 'author' ) );
119
		// 1 of 3 posts
120
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
121
		$request->set_param( 'author', $this->editor_id );
122
		$response = $this->server->dispatch( $request );
123
		$this->assertEquals( 200, $response->get_status() );
124
		$data = $response->get_data();
125
		$this->assertEquals( 1, count( $data ) );
126
		$this->assertEquals( $this->editor_id, $data[0]['author'] );
127
	}
128
129
	public function test_get_items_author_exclude_query() {
130
		$this->factory->post->create( array( 'post_author' => $this->editor_id ) );
131
		$this->factory->post->create( array( 'post_author' => $this->author_id ) );
132
		// All 3 posts
133
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
134
		$response = $this->server->dispatch( $request );
135
		$this->assertEquals( 200, $response->get_status() );
136
		$this->assertEquals( 3, count( $response->get_data() ) );
137
		// 1 of 3 posts
138
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
139
		$request->set_param( 'author_exclude', array( $this->editor_id, $this->author_id ) );
140
		$response = $this->server->dispatch( $request );
141
		$this->assertEquals( 200, $response->get_status() );
142
		$data = $response->get_data();
143
		$this->assertEquals( 1, count( $data ) );
144
		$this->assertNotEquals( $this->editor_id, $data[0]['author'] );
145
		$this->assertNotEquals( $this->author_id, $data[0]['author'] );
146
		// 2 of 3 posts
147
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
148
		$request->set_param( 'author_exclude', $this->editor_id );
149
		$response = $this->server->dispatch( $request );
150
		$this->assertEquals( 200, $response->get_status() );
151
		$data = $response->get_data();
152
		$this->assertEquals( 2, count( $data ) );
153
		$this->assertNotEquals( $this->editor_id, $data[0]['author'] );
154
		$this->assertNotEquals( $this->editor_id, $data[1]['author'] );
155
	}
156
157
	public function test_get_items_include_query() {
158
		$id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
159
		$this->factory->post->create( array( 'post_status' => 'publish' ) );
160
		$id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
161
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
162
		// Orderby=>desc
163
		$request->set_param( 'include', array( $id1, $id3 ) );
164
		$response = $this->server->dispatch( $request );
165
		$data = $response->get_data();
166
		$this->assertEquals( 2, count( $data ) );
167
		$this->assertEquals( $id3, $data[0]['id'] );
168
		// Orderby=>include
169
		$request->set_param( 'orderby', 'include' );
170
		$response = $this->server->dispatch( $request );
171
		$data = $response->get_data();
172
		$this->assertEquals( 2, count( $data ) );
173
		$this->assertEquals( $id1, $data[0]['id'] );
174
	}
175
176
	public function test_get_items_exclude_query() {
177
		$id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
178
		$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
179
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
180
		$response = $this->server->dispatch( $request );
181
		$data = $response->get_data();
182
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) );
183
		$this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ) ) );
184
		$request->set_param( 'exclude', array( $id2 ) );
185
		$response = $this->server->dispatch( $request );
186
		$data = $response->get_data();
187
		$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) );
188
		$this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ) ) );
189
	}
190
191
	public function test_get_items_search_query() {
192
		for ( $i = 0;  $i < 5;  $i++ ) {
193
			$this->factory->post->create( array( 'post_status' => 'publish' ) );
194
		}
195
		$this->factory->post->create( array( 'post_title' => 'Search Result', 'post_status' => 'publish' ) );
196
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
197
		$response = $this->server->dispatch( $request );
198
		$this->assertEquals( 7, count( $response->get_data() ) );
199
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
200
		$request->set_param( 'search', 'Search Result' );
201
		$response = $this->server->dispatch( $request );
202
		$data = $response->get_data();
203
		$this->assertEquals( 1, count( $data ) );
204
		$this->assertEquals( 'Search Result', $data[0]['title']['rendered'] );
205
	}
206
207
	public function test_get_items_slug_query() {
208
		$this->factory->post->create( array( 'post_title' => 'Apple', 'post_status' => 'publish' ) );
209
		$this->factory->post->create( array( 'post_title' => 'Banana', 'post_status' => 'publish' ) );
210
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
211
		$request->set_param( 'slug', 'apple' );
212
		$response = $this->server->dispatch( $request );
213
		$this->assertEquals( 200, $response->get_status() );
214
		$data = $response->get_data();
215
		$this->assertEquals( 1, count( $data ) );
216
		$this->assertEquals( 'Apple', $data[0]['title']['rendered'] );
217
	}
218
219
	public function test_get_items_status_query() {
220
		wp_set_current_user( 0 );
221
		$this->factory->post->create( array( 'post_status' => 'draft' ) );
222
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
223
		$request->set_param( 'status', 'publish' );
224
		$response = $this->server->dispatch( $request );
225
		$this->assertEquals( 200, $response->get_status() );
226
		$this->assertEquals( 1, count( $response->get_data() ) );
227
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
228
		$request->set_param( 'status', 'draft' );
229
		$response = $this->server->dispatch( $request );
230
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
231
		wp_set_current_user( $this->editor_id );
232
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
233
		$request->set_param( 'status', 'draft' );
234
		$response = $this->server->dispatch( $request );
235
		$this->assertEquals( 200, $response->get_status() );
236
		$this->assertEquals( 1, count( $response->get_data() ) );
237
	}
238
239 View Code Duplication
	public function test_get_items_status_without_permissions() {
240
		$draft_id = $this->factory->post->create( array(
241
			'post_status' => 'draft',
242
		) );
243
		wp_set_current_user( 0 );
244
245
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
246
		$response = $this->server->dispatch( $request );
247
248
		$this->assertEquals( 200, $response->get_status() );
249
250
		$all_data = $response->get_data();
251
		foreach ( $all_data as $post ) {
252
			$this->assertNotEquals( $draft_id, $post['id'] );
253
		}
254
	}
255
256
	public function test_get_items_order_and_orderby() {
257
		$this->factory->post->create( array( 'post_title' => 'Apple Pie', 'post_status' => 'publish' ) );
258
		$this->factory->post->create( array( 'post_title' => 'Apple Sauce', 'post_status' => 'publish' ) );
259
		$this->factory->post->create( array( 'post_title' => 'Apple Cobbler', 'post_status' => 'publish' ) );
260
		$this->factory->post->create( array( 'post_title' => 'Apple Coffee Cake', 'post_status' => 'publish' ) );
261
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
262
		$request->set_param( 'search', 'Apple' );
263
		// order defaults to 'desc'
264
		$request->set_param( 'orderby', 'title' );
265
		$response = $this->server->dispatch( $request );
266
		$data = $response->get_data();
267
		$this->assertEquals( 'Apple Sauce', $data[0]['title']['rendered'] );
268
		// order=>desc
269
		$request->set_param( 'order', 'asc' );
270
		$response = $this->server->dispatch( $request );
271
		$data = $response->get_data();
272
		$this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] );
273
	}
274
275
	public function test_get_items_with_orderby_relevance() {
276
		$this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) );
277
		$this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) );
278
279
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
280
		$request->set_param( 'orderby', 'relevance' );
281
		$response = $this->server->dispatch( $request );
282
		$this->assertErrorResponse( 'rest_no_search_term_defined', $response, 400 );
283
	}
284
285
	public function test_get_items_ignore_sticky_posts_by_default() {
286
		$this->markTestSkipped( 'Broken, see https://github.com/WP-API/WP-API/issues/2210' );
287
		$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' ) );
288
		$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' ) );
289
		$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' ) );
290
		stick_post( $post_id2 );
291
292
		// No stickies by default
293
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
294
		$response = $this->server->dispatch( $request );
295
		$data = $response->get_data();
296
		$this->assertEquals( array( $this->post_id, $post_id3, $post_id2, $post_id1 ), wp_list_pluck( $data, 'id' ) );
297
298
		// Permit stickies
299
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
300
		$request->set_param( 'filter', array( 'ignore_sticky_posts' => false ) );
301
		$response = $this->server->dispatch( $request );
302
		$data = $response->get_data();
303
		$this->assertEquals( array( $post_id2, $this->post_id, $post_id3, $post_id1 ), wp_list_pluck( $data, 'id' ) );
304
	}
305
306
	public function test_get_items_offset_query() {
307
		$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...
308
		$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...
309
		$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...
310
		$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...
311
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
312
		$request->set_param( 'offset', 1 );
313
		$response = $this->server->dispatch( $request );
314
		$this->assertCount( 3, $response->get_data() );
315
		// 'offset' works with 'per_page'
316
		$request->set_param( 'per_page', 2 );
317
		$response = $this->server->dispatch( $request );
318
		$this->assertCount( 2, $response->get_data() );
319
		// 'offset' takes priority over 'page'
320
		$request->set_param( 'page', 3 );
321
		$response = $this->server->dispatch( $request );
322
		$this->assertCount( 2, $response->get_data() );
323
	}
324
325
	public function test_get_items_tags_query() {
326
		$id1 = $this->post_id;
327
		$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...
328
		$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...
329
		$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...
330
		$tag = wp_insert_term( 'My Tag', 'post_tag' );
331
332
		wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
333
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
334
		$request->set_param( 'tags', array( $tag['term_id'] ) );
335
336
		$response = $this->server->dispatch( $request );
337
		$this->assertCount( 1, $response->get_data() );
338
	}
339
340
	public function test_get_items_tags_and_categories_query() {
341
		$id1 = $this->post_id;
342
		$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
343
		$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...
344
		$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...
345
		$tag = wp_insert_term( 'My Tag', 'post_tag' );
346
		$category = wp_insert_term( 'My Category', 'category' );
347
348
		wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
349
		wp_set_object_terms( $id2, array( $tag['term_id'] ), 'post_tag' );
350
		wp_set_object_terms( $id1, array( $category['term_id'] ), 'category' );
351
352
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
353
		$request->set_param( 'tags', array( $tag['term_id'] ) );
354
		$request->set_param( 'categories', array( $category['term_id'] ) );
355
356
		$response = $this->server->dispatch( $request );
357
		$this->assertCount( 1, $response->get_data() );
358
	}
359
360
	/**
361
	 * @group test
362
	 */
363
	public function test_get_items_pagination_headers() {
364
		// Start of the index
365
		for ( $i = 0; $i < 49; $i++ ) {
366
			$this->factory->post->create( array(
367
				'post_title'   => "Post {$i}",
368
				) );
369
		}
370
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
371
		$response = $this->server->dispatch( $request );
372
		$headers = $response->get_headers();
373
		$this->assertEquals( 50, $headers['X-WP-Total'] );
374
		$this->assertEquals( 5, $headers['X-WP-TotalPages'] );
375
		$next_link = add_query_arg( array(
376
			'page'    => 2,
377
			), rest_url( '/wp/v2/posts' ) );
378
		$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
379
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
380
		// 3rd page
381
		$this->factory->post->create( array(
382
				'post_title'   => 'Post 51',
383
				) );
384
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
385
		$request->set_param( 'page', 3 );
386
		$response = $this->server->dispatch( $request );
387
		$headers = $response->get_headers();
388
		$this->assertEquals( 51, $headers['X-WP-Total'] );
389
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
390
		$prev_link = add_query_arg( array(
391
			'page'    => 2,
392
			), rest_url( '/wp/v2/posts' ) );
393
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
394
		$next_link = add_query_arg( array(
395
			'page'    => 4,
396
			), rest_url( '/wp/v2/posts' ) );
397
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
398
		// Last page
399
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
400
		$request->set_param( 'page', 6 );
401
		$response = $this->server->dispatch( $request );
402
		$headers = $response->get_headers();
403
		$this->assertEquals( 51, $headers['X-WP-Total'] );
404
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
405
		$prev_link = add_query_arg( array(
406
			'page'    => 5,
407
			), rest_url( '/wp/v2/posts' ) );
408
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
409
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
410
		// Out of bounds
411
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
412
		$request->set_param( 'page', 8 );
413
		$response = $this->server->dispatch( $request );
414
		$headers = $response->get_headers();
415
		$this->assertEquals( 51, $headers['X-WP-Total'] );
416
		$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
417
		$prev_link = add_query_arg( array(
418
			'page'    => 6,
419
			), rest_url( '/wp/v2/posts' ) );
420
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
421
		$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
422
423
		// With filter params.
424
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
425
		$request->set_query_params( array( 'filter' => array( 'posts_per_page' => 5, 'paged' => 2 ) ) );
426
		$response = $this->server->dispatch( $request );
427
		$headers = $response->get_headers();
428
		$this->assertEquals( 51, $headers['X-WP-Total'] );
429
		$this->assertEquals( 11, $headers['X-WP-TotalPages'] );
430
		$prev_link = add_query_arg( array(
431
			'page'    => 1,
432
			), rest_url( '/wp/v2/posts' ) );
433
		$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
434
		$next_link = add_query_arg( array(
435
			'page'    => 3,
436
			), rest_url( '/wp/v2/posts' ) );
437
		$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
438
	}
439
440 View Code Duplication
	public function test_get_items_private_filter_query_var() {
441
		// Private query vars inaccessible to unauthorized users
442
		wp_set_current_user( 0 );
443
		$draft_id = $this->factory->post->create( array( 'post_status' => 'draft' ) );
444
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
445
		$request->set_param( 'filter', array( 'post_status' => 'draft' ) );
446
		$response = $this->server->dispatch( $request );
447
		$data = $response->get_data();
448
		$this->assertCount( 1, $data );
449
		$this->assertEquals( $this->post_id, $data[0]['id'] );
450
		// But they are accessible to authorized users
451
		wp_set_current_user( $this->editor_id );
452
		$response = $this->server->dispatch( $request );
453
		$data = $response->get_data();
454
		$this->assertCount( 1, $data );
455
		$this->assertEquals( $draft_id, $data[0]['id'] );
456
	}
457
458 View Code Duplication
	public function test_get_items_invalid_context() {
459
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
460
		$request->set_param( 'context', 'banana' );
461
		$response = $this->server->dispatch( $request );
462
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
463
	}
464
465 View Code Duplication
	public function test_get_items_invalid_date() {
466
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
467
		$request->set_param( 'after', rand_str() );
468
		$request->set_param( 'before', rand_str() );
469
		$response = $this->server->dispatch( $request );
470
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
471
	}
472
473
	public function test_get_items_valid_date() {
474
		$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...
475
		$post2 = $this->factory->post->create( array( 'post_date' => '2016-01-16T00:00:00Z' ) );
476
		$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...
477
478
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
479
		$request->set_param( 'after', '2016-01-15T00:00:00Z' );
480
		$request->set_param( 'before', '2016-01-17T00:00:00Z' );
481
		$response = $this->server->dispatch( $request );
482
		$data = $response->get_data();
483
		$this->assertCount( 1, $data );
484
		$this->assertEquals( $post2, $data[0]['id'] );
485
	}
486
487
	public function test_get_item() {
488
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
489
		$response = $this->server->dispatch( $request );
490
491
		$this->check_get_post_response( $response, 'view' );
492
	}
493
494
	public function test_get_item_links() {
495
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
496
		$response = $this->server->dispatch( $request );
497
498
		$links = $response->get_links();
499
500
		$this->assertEquals( rest_url( '/wp/v2/posts/' . $this->post_id ), $links['self'][0]['href'] );
501
		$this->assertEquals( rest_url( '/wp/v2/posts' ), $links['collection'][0]['href'] );
502
503
		$this->assertEquals( rest_url( '/wp/v2/types/' . get_post_type( $this->post_id ) ), $links['about'][0]['href'] );
504
505
		$replies_url = rest_url( '/wp/v2/comments' );
506
		$replies_url = add_query_arg( 'post', $this->post_id, $replies_url );
507
		$this->assertEquals( $replies_url, $links['replies'][0]['href'] );
508
509
		$this->assertEquals( rest_url( '/wp/v2/posts/' . $this->post_id . '/revisions' ), $links['version-history'][0]['href'] );
510
511
		$attachments_url = rest_url( '/wp/v2/media' );
512
		$attachments_url = add_query_arg( 'parent', $this->post_id, $attachments_url );
513
		$this->assertEquals( $attachments_url, $links['https://api.w.org/attachment'][0]['href'] );
514
515
		$term_links = $links['https://api.w.org/term'];
516
		$tag_link = $cat_link = $format_link = null;
517
		foreach ( $term_links as $link ) {
518
			if ( 'post_tag' === $link['attributes']['taxonomy'] ) {
519
				$tag_link = $link;
520
			} elseif ( 'category' === $link['attributes']['taxonomy'] ) {
521
				$cat_link = $link;
522
			} else if ( 'post_format' === $link['attributes']['taxonomy'] ) {
523
				$format_link = $link;
524
			}
525
		}
526
		$this->assertNotEmpty( $tag_link );
527
		$this->assertNotEmpty( $cat_link );
528
		$this->assertNull( $format_link );
529
530
		$tags_url = add_query_arg( 'post', $this->post_id, rest_url( '/wp/v2/tags' ) );
531
		$this->assertEquals( $tags_url, $tag_link['href'] );
532
533
		$category_url = add_query_arg( 'post', $this->post_id, rest_url( '/wp/v2/categories' ) );
534
		$this->assertEquals( $category_url, $cat_link['href'] );
535
	}
536
537
	public function test_get_item_links_no_author() {
538
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
539
		$response = $this->server->dispatch( $request );
540
		$links = $response->get_links();
541
		$this->assertFalse( isset( $links['author'] ) );
542
		wp_update_post( array( 'ID' => $this->post_id, 'post_author' => $this->author_id ) );
543
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
544
		$response = $this->server->dispatch( $request );
545
		$links = $response->get_links();
546
		$this->assertEquals( rest_url( '/wp/v2/users/' . $this->author_id ), $links['author'][0]['href'] );
547
	}
548
549
	public function test_get_post_without_permission() {
550
		$draft_id = $this->factory->post->create( array(
551
			'post_status' => 'draft',
552
		) );
553
		wp_set_current_user( 0 );
554
555
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $draft_id ) );
556
		$response = $this->server->dispatch( $request );
557
558
		$this->assertErrorResponse( 'rest_forbidden', $response, 403 );
559
	}
560
561
	public function test_get_post_invalid_id() {
562
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
563
		$response = $this->server->dispatch( $request );
564
565
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
566
	}
567
568
	public function test_get_post_list_context_with_permission() {
569
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
570
		$request->set_query_params( array(
571
			'context' => 'edit',
572
		) );
573
574
		wp_set_current_user( $this->editor_id );
575
576
		$response = $this->server->dispatch( $request );
577
578
		$this->check_get_posts_response( $response, 'edit' );
579
	}
580
581 View Code Duplication
	public function test_get_post_list_context_without_permission() {
582
		wp_set_current_user( 0 );
583
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
584
		$request->set_query_params( array(
585
			'context' => 'edit',
586
		) );
587
		$response = $this->server->dispatch( $request );
588
589
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
590
	}
591
592 View Code Duplication
	public function test_get_post_context_without_permission() {
593
		wp_set_current_user( 0 );
594
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
595
		$request->set_query_params( array(
596
			'context' => 'edit',
597
		) );
598
		$response = $this->server->dispatch( $request );
599
600
		$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
601
	}
602
603
	public function test_get_post_with_password() {
604
		$post_id = $this->factory->post->create( array(
605
			'post_password' => '$inthebananastand',
606
		) );
607
608
		wp_set_current_user( $this->editor_id );
609
610
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
611
		$response = $this->server->dispatch( $request );
612
613
		$this->check_get_post_response( $response, 'view' );
614
	}
615
616
	public function test_get_post_with_password_without_permission() {
617
		$post_id = $this->factory->post->create( array(
618
			'post_password' => '$inthebananastand',
619
		) );
620
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
621
		$response = $this->server->dispatch( $request );
622
623
		$this->assertErrorResponse( 'rest_forbidden', $response, 403 );
624
	}
625
626
	public function test_get_item_read_permission_custom_post_status() {
627
		register_post_status( 'testpubstatus', array( 'public' => true ) );
628
		register_post_status( 'testprivtatus', array( 'public' => false ) );
629
		// Public status
630
		wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'testpubstatus' ) );
631
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
632
		$response = $this->server->dispatch( $request );
633
		$this->assertEquals( 200, $response->get_status() );
634
		// Private status
635
		wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'testprivtatus' ) );
636
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
637
		$response = $this->server->dispatch( $request );
638
		$this->assertEquals( 403, $response->get_status() );
639
	}
640
641
	public function test_prepare_item() {
642
		wp_set_current_user( $this->editor_id );
643
644
		$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
645
		$request->set_query_params( array( 'context' => 'edit' ) );
646
		$response = $this->server->dispatch( $request );
647
648
		$this->check_get_post_response( $response, 'edit' );
649
	}
650
651
	public function test_create_item() {
652
		wp_set_current_user( $this->editor_id );
653
654
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
655
		$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
656
		$params = $this->set_post_data();
657
		$request->set_body_params( $params );
658
		$response = $this->server->dispatch( $request );
659
660
		$this->check_create_post_response( $response );
661
	}
662
663
	public function test_rest_create_item() {
664
		wp_set_current_user( $this->editor_id );
665
666
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
667
		$request->add_header( 'content-type', 'application/json' );
668
		$params = $this->set_post_data();
669
		$request->set_body( wp_json_encode( $params ) );
670
		$response = $this->server->dispatch( $request );
671
672
		$this->check_create_post_response( $response );
673
	}
674
675
	public function test_create_post_invalid_id() {
676
		wp_set_current_user( $this->editor_id );
677
678
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
679
		$params = $this->set_post_data( array(
680
			'id' => '3',
681
		) );
682
		$request->set_body_params( $params );
683
		$response = $this->server->dispatch( $request );
684
685
		$this->assertErrorResponse( 'rest_post_exists', $response, 400 );
686
	}
687
688
	public function test_create_post_as_contributor() {
689
		wp_set_current_user( $this->contributor_id );
690
691
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
692
		$params = $this->set_post_data(array(
693
			'status' => 'pending',
694
		));
695
696
		$request->set_body_params( $params );
697
		$response = $this->server->dispatch( $request );
698
		$this->check_create_post_response( $response );
699
	}
700
701
	public function test_create_post_sticky() {
702
		wp_set_current_user( $this->editor_id );
703
704
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
705
		$params = $this->set_post_data( array(
706
			'sticky' => true,
707
		) );
708
		$request->set_body_params( $params );
709
		$response = $this->server->dispatch( $request );
710
711
		$new_data = $response->get_data();
712
		$this->assertEquals( true, $new_data['sticky'] );
713
		$post = get_post( $new_data['id'] );
714
		$this->assertEquals( true, is_sticky( $post->ID ) );
715
	}
716
717
	public function test_create_post_sticky_as_contributor() {
718
		wp_set_current_user( $this->contributor_id );
719
720
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
721
		$params = $this->set_post_data( array(
722
			'sticky' => true,
723
			'status' => 'pending',
724
		) );
725
		$request->set_body_params( $params );
726
		$response = $this->server->dispatch( $request );
727
728
		$this->assertErrorResponse( 'rest_cannot_assign_sticky', $response, 403 );
729
	}
730
731
	public function test_create_post_other_author_without_permission() {
732
		wp_set_current_user( $this->author_id );
733
734
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
735
		$params = $this->set_post_data(array(
736
			'author' => $this->editor_id,
737
		));
738
		$request->set_body_params( $params );
739
		$response = $this->server->dispatch( $request );
740
741
		$this->assertErrorResponse( 'rest_cannot_edit_others', $response, 403 );
742
	}
743
744
	public function test_create_post_without_permission() {
745
		wp_set_current_user( 0 );
746
747
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
748
		$params = $this->set_post_data( array(
749
			'status' => 'draft',
750
		) );
751
		$request->set_body_params( $params );
752
		$response = $this->server->dispatch( $request );
753
754
		$this->assertErrorResponse( 'rest_cannot_create', $response, 401 );
755
	}
756
757 View Code Duplication
	public function test_create_post_draft() {
758
		wp_set_current_user( $this->editor_id );
759
760
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
761
		$params = $this->set_post_data( array(
762
			'status' => 'draft',
763
		) );
764
		$request->set_body_params( $params );
765
		$response = $this->server->dispatch( $request );
766
767
		$data = $response->get_data();
768
		$new_post = get_post( $data['id'] );
769
		$this->assertEquals( 'draft', $data['status'] );
770
		$this->assertEquals( 'draft', $new_post->post_status );
771
		// Confirm dates are null
772
		$this->assertNull( $data['date_gmt'] );
773
		$this->assertNull( $data['modified_gmt'] );
774
	}
775
776 View Code Duplication
	public function test_create_post_private() {
777
		wp_set_current_user( $this->editor_id );
778
779
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
780
		$params = $this->set_post_data( array(
781
			'status' => 'private',
782
		) );
783
		$request->set_body_params( $params );
784
		$response = $this->server->dispatch( $request );
785
786
		$data = $response->get_data();
787
		$new_post = get_post( $data['id'] );
788
		$this->assertEquals( 'private', $data['status'] );
789
		$this->assertEquals( 'private', $new_post->post_status );
790
	}
791
792
	public function test_create_post_private_without_permission() {
793
		wp_set_current_user( $this->author_id );
794
		$user = wp_get_current_user();
795
		$user->add_cap( 'publish_posts', false );
796
		// Flush capabilities, https://core.trac.wordpress.org/ticket/28374
797
		$user->get_role_caps();
798
		$user->update_user_level_from_caps();
799
800
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
801
		$params = $this->set_post_data( array(
802
			'status' => 'private',
803
			'author' => $this->author_id,
804
		) );
805
		$request->set_body_params( $params );
806
		$response = $this->server->dispatch( $request );
807
808
		$this->assertErrorResponse( 'rest_cannot_publish', $response, 403 );
809
	}
810
811
	public function test_create_post_publish_without_permission() {
812
		wp_set_current_user( $this->author_id );
813
		$user = wp_get_current_user();
814
		$user->add_cap( 'publish_posts', false );
815
		// Flush capabilities, https://core.trac.wordpress.org/ticket/28374
816
		$user->get_role_caps();
817
		$user->update_user_level_from_caps();
818
819
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
820
		$params = $this->set_post_data( array(
821
			'status' => 'publish',
822
		) );
823
		$request->set_body_params( $params );
824
		$response = $this->server->dispatch( $request );
825
826
		$this->assertErrorResponse( 'rest_cannot_publish', $response, 403 );
827
	}
828
829
	public function test_create_post_invalid_status() {
830
		wp_set_current_user( $this->editor_id );
831
832
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
833
		$params = $this->set_post_data( array(
834
			'status' => 'teststatus',
835
		) );
836
		$request->set_body_params( $params );
837
		$response = $this->server->dispatch( $request );
838
839
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
840
	}
841
842 View Code Duplication
	public function test_create_post_with_format() {
843
		wp_set_current_user( $this->editor_id );
844
845
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
846
		$params = $this->set_post_data( array(
847
			'format' => 'gallery',
848
		) );
849
		$request->set_body_params( $params );
850
		$response = $this->server->dispatch( $request );
851
852
		$data = $response->get_data();
853
		$new_post = get_post( $data['id'] );
854
		$this->assertEquals( 'gallery', $data['format'] );
855
		$this->assertEquals( 'gallery', get_post_format( $new_post->ID ) );
856
	}
857
858
	public function test_create_post_with_invalid_format() {
859
		wp_set_current_user( $this->editor_id );
860
861
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
862
		$params = $this->set_post_data( array(
863
			'format' => 'testformat',
864
		) );
865
		$request->set_body_params( $params );
866
		$response = $this->server->dispatch( $request );
867
868
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
869
	}
870
871
	public function test_create_update_post_with_featured_media() {
872
873
		$file = DIR_TESTDATA . '/images/canola.jpg';
874
		$this->attachment_id = $this->factory->attachment->create_object( $file, 0, array(
875
			'post_mime_type' => 'image/jpeg',
876
			'menu_order' => rand( 1, 100 ),
877
		) );
878
879
		wp_set_current_user( $this->editor_id );
880
881
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
882
		$params = $this->set_post_data( array(
883
			'featured_media' => $this->attachment_id,
884
		) );
885
		$request->set_body_params( $params );
886
		$response = $this->server->dispatch( $request );
887
		$data = $response->get_data();
888
		$new_post = get_post( $data['id'] );
889
		$this->assertEquals( $this->attachment_id, $data['featured_media'] );
890
		$this->assertEquals( $this->attachment_id, (int) get_post_thumbnail_id( $new_post->ID ) );
891
892
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $new_post->ID );
893
		$params = $this->set_post_data( array(
894
			'featured_media' => 0,
895
		) );
896
		$request->set_body_params( $params );
897
		$response = $this->server->dispatch( $request );
898
		$data = $response->get_data();
899
		$this->assertEquals( 0, $data['featured_media'] );
900
		$this->assertEquals( 0, (int) get_post_thumbnail_id( $new_post->ID ) );
901
	}
902
903
	public function test_create_post_invalid_author() {
904
		wp_set_current_user( $this->editor_id );
905
906
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
907
		$params = $this->set_post_data( array(
908
			'author' => -1,
909
		) );
910
		$request->set_body_params( $params );
911
		$response = $this->server->dispatch( $request );
912
913
		$this->assertErrorResponse( 'rest_invalid_author', $response, 400 );
914
	}
915
916
	public function test_create_post_invalid_author_without_permission() {
917
		wp_set_current_user( $this->author_id );
918
919
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
920
		$params = $this->set_post_data( array(
921
			'author' => $this->editor_id,
922
		) );
923
		$request->set_body_params( $params );
924
		$response = $this->server->dispatch( $request );
925
926
		$this->assertErrorResponse( 'rest_cannot_edit_others', $response, 403 );
927
	}
928
929
	public function test_create_post_with_password() {
930
		wp_set_current_user( $this->editor_id );
931
932
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
933
		$params = $this->set_post_data( array(
934
			'password' => 'testing',
935
		) );
936
		$request->set_body_params( $params );
937
		$response = $this->server->dispatch( $request );
938
939
		$data = $response->get_data();
940
		$this->assertEquals( 'testing', $data['password'] );
941
	}
942
943
	public function test_create_post_with_password_without_permission() {
944
		wp_set_current_user( $this->author_id );
945
		$user = wp_get_current_user();
946
		$user->add_cap( 'publish_posts', false );
947
		// Flush capabilities, https://core.trac.wordpress.org/ticket/28374
948
		$user->get_role_caps();
949
		$user->update_user_level_from_caps();
950
951
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
952
		$params = $this->set_post_data( array(
953
			'password' => 'testing',
954
			'author'   => $this->author_id,
955
			'status'   => 'draft',
956
		) );
957
		$request->set_body_params( $params );
958
		$response = $this->server->dispatch( $request );
959
960
		$this->assertErrorResponse( 'rest_cannot_publish', $response, 403 );
961
	}
962
963
	public function test_create_post_with_falsy_password() {
964
		wp_set_current_user( $this->editor_id );
965
966
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
967
		$params = $this->set_post_data( array(
968
			'password' => '0',
969
		) );
970
		$request->set_body_params( $params );
971
		$response = $this->server->dispatch( $request );
972
973
		$data = $response->get_data();
974
975
		$this->assertEquals( '0', $data['password'] );
976
	}
977
978 View Code Duplication
	public function test_create_post_with_empty_string_password_and_sticky() {
979
		wp_set_current_user( $this->editor_id );
980
981
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
982
		$params = $this->set_post_data( array(
983
			'password' => '',
984
			'sticky'   => true,
985
		) );
986
		$request->set_body_params( $params );
987
		$response = $this->server->dispatch( $request );
988
989
		$this->assertEquals( 201, $response->get_status() );
990
		$data = $response->get_data();
991
		$this->assertEquals( '', $data['password'] );
992
	}
993
994
	public function test_create_post_with_password_and_sticky_fails() {
995
		wp_set_current_user( $this->editor_id );
996
997
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
998
		$params = $this->set_post_data( array(
999
			'password' => '123',
1000
			'sticky'   => true,
1001
		) );
1002
		$request->set_body_params( $params );
1003
		$response = $this->server->dispatch( $request );
1004
1005
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1006
	}
1007
1008
	public function test_create_post_custom_date() {
1009
		wp_set_current_user( $this->editor_id );
1010
1011
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1012
		$params = $this->set_post_data( array(
1013
			'date' => '2010-01-01T02:00:00Z',
1014
		) );
1015
		$request->set_body_params( $params );
1016
		$response = $this->server->dispatch( $request );
1017
1018
		$data = $response->get_data();
1019
		$new_post = get_post( $data['id'] );
1020
		$time = gmmktime( 2, 0, 0, 1, 1, 2010 );
1021
		$this->assertEquals( '2010-01-01T02:00:00', $data['date'] );
1022
		$this->assertEquals( $time, strtotime( $new_post->post_date ) );
1023
	}
1024
1025
	public function test_create_post_custom_date_with_timezone() {
1026
		wp_set_current_user( $this->editor_id );
1027
1028
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1029
		$params = $this->set_post_data( array(
1030
			'date' => '2010-01-01T02:00:00-10:00',
1031
		) );
1032
		$request->set_body_params( $params );
1033
		$response = $this->server->dispatch( $request );
1034
1035
		$data = $response->get_data();
1036
		$new_post = get_post( $data['id'] );
1037
		$time = gmmktime( 12, 0, 0, 1, 1, 2010 );
1038
1039
		$this->assertEquals( '2010-01-01T12:00:00', $data['date'] );
1040
		$this->assertEquals( '2010-01-01T12:00:00', $data['modified'] );
1041
1042
		$this->assertEquals( $time, strtotime( $new_post->post_date ) );
1043
		$this->assertEquals( $time, strtotime( $new_post->post_modified ) );
1044
	}
1045
1046
	public function test_create_post_with_db_error() {
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
		$request->set_body_params( $params );
1052
1053
		/**
1054
		 * Disable showing error as the below is going to intentionally
1055
		 * trigger a DB error.
1056
		 */
1057
		global $wpdb;
1058
		$wpdb->suppress_errors = true;
1059
		add_filter( 'query', array( $this, 'error_insert_query' ) );
1060
1061
		$response = $this->server->dispatch( $request );
1062
		remove_filter( 'query', array( $this, 'error_insert_query' ) );
1063
		$wpdb->show_errors = true;
1064
1065
		$this->assertErrorResponse( 'db_insert_error', $response, 500 );
1066
	}
1067
1068
	public function test_create_post_with_invalid_date() {
1069
		wp_set_current_user( $this->editor_id );
1070
1071
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1072
		$params = $this->set_post_data( array(
1073
			'date' => '2010-60-01T02:00:00Z',
1074
		) );
1075
		$request->set_body_params( $params );
1076
		$response = $this->server->dispatch( $request );
1077
1078
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1079
	}
1080
1081
	public function test_create_post_with_invalid_date_gmt() {
1082
		wp_set_current_user( $this->editor_id );
1083
1084
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1085
		$params = $this->set_post_data( array(
1086
			'date_gmt' => '2010-60-01T02:00:00',
1087
		) );
1088
		$request->set_body_params( $params );
1089
		$response = $this->server->dispatch( $request );
1090
1091
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1092
	}
1093
1094 View Code Duplication
	public function test_create_post_with_quotes_in_title() {
1095
		wp_set_current_user( $this->editor_id );
1096
1097
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1098
		$params = $this->set_post_data( array(
1099
			'title' => "Rob O'Rourke's Diary",
1100
		) );
1101
		$request->set_body_params( $params );
1102
		$response = $this->server->dispatch( $request );
1103
		$new_data = $response->get_data();
1104
		$this->assertEquals( "Rob O'Rourke's Diary", $new_data['title']['raw'] );
1105
	}
1106
1107
	public function test_create_post_with_categories() {
1108
		wp_set_current_user( $this->editor_id );
1109
		$category = wp_insert_term( 'Test Category', 'category' );
1110
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1111
		$params = $this->set_post_data( array(
1112
			'password'   => 'testing',
1113
			'categories' => array(
1114
				$category['term_id']
1115
			),
1116
		) );
1117
		$request->set_body_params( $params );
1118
		$response = $this->server->dispatch( $request );
1119
1120
		$data = $response->get_data();
1121
		$this->assertEquals( array( $category['term_id'] ), $data['categories'] );
1122
	}
1123
1124
	public function test_create_post_with_invalid_categories() {
1125
		wp_set_current_user( $this->editor_id );
1126
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1127
		$params = $this->set_post_data( array(
1128
			'password'   => 'testing',
1129
			'categories' => array(
1130
				REST_TESTS_IMPOSSIBLY_HIGH_NUMBER
1131
			),
1132
		) );
1133
		$request->set_body_params( $params );
1134
		$response = $this->server->dispatch( $request );
1135
1136
		$data = $response->get_data();
1137
		$this->assertEquals( array(), $data['categories'] );
1138
	}
1139
1140 View Code Duplication
	public function test_update_item() {
1141
		wp_set_current_user( $this->editor_id );
1142
1143
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1144
		$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
1145
		$params = $this->set_post_data();
1146
		$request->set_body_params( $params );
1147
		$response = $this->server->dispatch( $request );
1148
1149
		$this->check_update_post_response( $response );
1150
		$new_data = $response->get_data();
1151
		$this->assertEquals( $this->post_id, $new_data['id'] );
1152
		$this->assertEquals( $params['title'], $new_data['title']['raw'] );
1153
		$this->assertEquals( $params['content'], $new_data['content']['raw'] );
1154
		$this->assertEquals( $params['excerpt'], $new_data['excerpt']['raw'] );
1155
		$post = get_post( $this->post_id );
1156
		$this->assertEquals( $params['title'], $post->post_title );
1157
		$this->assertEquals( $params['content'], $post->post_content );
1158
		$this->assertEquals( $params['excerpt'], $post->post_excerpt );
1159
	}
1160
1161 View Code Duplication
	public function test_rest_update_post() {
1162
		wp_set_current_user( $this->editor_id );
1163
1164
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1165
		$request->add_header( 'content-type', 'application/json' );
1166
		$params = $this->set_post_data();
1167
		$request->set_body( wp_json_encode( $params ) );
1168
		$response = $this->server->dispatch( $request );
1169
1170
		$this->check_update_post_response( $response );
1171
		$new_data = $response->get_data();
1172
		$this->assertEquals( $this->post_id, $new_data['id'] );
1173
		$this->assertEquals( $params['title'], $new_data['title']['raw'] );
1174
		$this->assertEquals( $params['content'], $new_data['content']['raw'] );
1175
		$this->assertEquals( $params['excerpt'], $new_data['excerpt']['raw'] );
1176
		$post = get_post( $this->post_id );
1177
		$this->assertEquals( $params['title'], $post->post_title );
1178
		$this->assertEquals( $params['content'], $post->post_content );
1179
		$this->assertEquals( $params['excerpt'], $post->post_excerpt );
1180
	}
1181
1182
	public function test_rest_update_post_raw() {
1183
		wp_set_current_user( $this->editor_id );
1184
1185
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1186
		$request->add_header( 'content-type', 'application/json' );
1187
		$params = $this->set_raw_post_data();
1188
		$request->set_body( wp_json_encode( $params ) );
1189
		$response = $this->server->dispatch( $request );
1190
1191
		$this->check_update_post_response( $response );
1192
		$new_data = $response->get_data();
1193
		$this->assertEquals( $this->post_id, $new_data['id'] );
1194
		$this->assertEquals( $params['title']['raw'], $new_data['title']['raw'] );
1195
		$this->assertEquals( $params['content']['raw'], $new_data['content']['raw'] );
1196
		$this->assertEquals( $params['excerpt']['raw'], $new_data['excerpt']['raw'] );
1197
		$post = get_post( $this->post_id );
1198
		$this->assertEquals( $params['title']['raw'], $post->post_title );
1199
		$this->assertEquals( $params['content']['raw'], $post->post_content );
1200
		$this->assertEquals( $params['excerpt']['raw'], $post->post_excerpt );
1201
	}
1202
1203
	public function test_update_post_without_extra_params() {
1204
		wp_set_current_user( $this->editor_id );
1205
1206
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1207
		$params = $this->set_post_data();
1208
		unset( $params['type'] );
1209
		unset( $params['name'] );
1210
		unset( $params['author'] );
1211
		unset( $params['status'] );
1212
		$request->set_body_params( $params );
1213
		$response = $this->server->dispatch( $request );
1214
1215
		$this->check_update_post_response( $response );
1216
	}
1217
1218
	public function test_update_post_without_permission() {
1219
		wp_set_current_user( $this->editor_id );
1220
		$user = wp_get_current_user();
1221
		$user->add_cap( 'edit_published_posts', false );
1222
		// Flush capabilities, https://core.trac.wordpress.org/ticket/28374
1223
		$user->get_role_caps();
1224
		$user->update_user_level_from_caps();
1225
1226
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1227
		$params = $this->set_post_data();
1228
		$request->set_body_params( $params );
1229
		$response = $this->server->dispatch( $request );
1230
1231
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
1232
	}
1233
1234 View Code Duplication
	public function test_update_post_sticky_as_contributor() {
1235
		wp_set_current_user( $this->contributor_id );
1236
1237
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1238
		$params = $this->set_post_data( array(
1239
			'sticky' => true,
1240
			'status' => 'pending',
1241
		) );
1242
		$request->set_body_params( $params );
1243
		$response = $this->server->dispatch( $request );
1244
1245
		$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
1246
	}
1247
1248
	public function test_update_post_invalid_id() {
1249
		wp_set_current_user( $this->editor_id );
1250
1251
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
1252
		$response = $this->server->dispatch( $request );
1253
1254
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1255
	}
1256
1257
	public function test_update_post_invalid_route() {
1258
		wp_set_current_user( $this->editor_id );
1259
1260
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', $this->post_id ) );
1261
		$response = $this->server->dispatch( $request );
1262
1263
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1264
	}
1265
1266 View Code Duplication
	public function test_update_post_with_format() {
1267
		wp_set_current_user( $this->editor_id );
1268
1269
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1270
		$params = $this->set_post_data( array(
1271
			'format' => 'gallery',
1272
		) );
1273
		$request->set_body_params( $params );
1274
		$response = $this->server->dispatch( $request );
1275
1276
		$data = $response->get_data();
1277
		$new_post = get_post( $data['id'] );
1278
		$this->assertEquals( 'gallery', $data['format'] );
1279
		$this->assertEquals( 'gallery', get_post_format( $new_post->ID ) );
1280
	}
1281
1282
	public function test_update_post_with_invalid_format() {
1283
		wp_set_current_user( $this->editor_id );
1284
1285
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1286
		$params = $this->set_post_data( array(
1287
			'format' => 'testformat',
1288
		) );
1289
		$request->set_body_params( $params );
1290
		$response = $this->server->dispatch( $request );
1291
1292
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1293
	}
1294
1295
	public function test_update_post_ignore_readonly() {
1296
		wp_set_current_user( $this->editor_id );
1297
1298
		$new_content = rand_str();
1299
		$expected_modified = current_time( 'mysql' );
1300
1301
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1302
		$params = $this->set_post_data( array(
1303
			'modified' => '2010-06-01T02:00:00Z',
1304
			'content'  => $new_content,
1305
		) );
1306
		$request->set_body_params( $params );
1307
		$response = $this->server->dispatch( $request );
1308
1309
		// The readonly modified param should be ignored, request should be a success.
1310
		$data = $response->get_data();
1311
		$new_post = get_post( $data['id'] );
1312
1313
		$this->assertEquals( $new_content, $data['content']['raw'] );
1314
		$this->assertEquals( $new_content, $new_post->post_content );
1315
1316
		// The modified date should equal the current time.
1317
		$this->assertEquals( date( 'Y-m-d', strtotime( mysql_to_rfc3339( $expected_modified ) ) ), date( 'Y-m-d', strtotime( $data['modified'] ) ) );
1318
		$this->assertEquals( date( 'Y-m-d', strtotime( $expected_modified ) ), date( 'Y-m-d', strtotime( $new_post->post_modified ) ) );
1319
	}
1320
1321 View Code Duplication
	public function test_update_post_with_invalid_date() {
1322
		wp_set_current_user( $this->editor_id );
1323
1324
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1325
		$params = $this->set_post_data( array(
1326
			'date' => rand_str(),
1327
		) );
1328
		$request->set_body_params( $params );
1329
		$response = $this->server->dispatch( $request );
1330
1331
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1332
	}
1333
1334 View Code Duplication
	public function test_update_post_with_invalid_date_gmt() {
1335
		wp_set_current_user( $this->editor_id );
1336
1337
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1338
		$params = $this->set_post_data( array(
1339
			'date_gmt' => rand_str(),
1340
		) );
1341
		$request->set_body_params( $params );
1342
		$response = $this->server->dispatch( $request );
1343
1344
		$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1345
	}
1346
1347 View Code Duplication
	public function test_update_post_slug() {
1348
		wp_set_current_user( $this->editor_id );
1349
1350
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1351
		$params = $this->set_post_data( array(
1352
			'slug' => 'sample-slug',
1353
		) );
1354
		$request->set_body_params( $params );
1355
		$response = $this->server->dispatch( $request );
1356
1357
		$new_data = $response->get_data();
1358
		$this->assertEquals( 'sample-slug', $new_data['slug'] );
1359
		$post = get_post( $new_data['id'] );
1360
		$this->assertEquals( 'sample-slug', $post->post_name );
1361
	}
1362
1363
	public function test_update_post_sticky() {
1364
		wp_set_current_user( $this->editor_id );
1365
1366
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1367
		$params = $this->set_post_data( array(
1368
			'sticky' => true,
1369
		) );
1370
		$request->set_body_params( $params );
1371
		$response = $this->server->dispatch( $request );
1372
1373
		$new_data = $response->get_data();
1374
		$this->assertEquals( true, $new_data['sticky'] );
1375
		$post = get_post( $new_data['id'] );
1376
		$this->assertEquals( true, is_sticky( $post->ID ) );
1377
1378
		// Updating another field shouldn't change sticky status
1379
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1380
		$params = $this->set_post_data( array(
1381
			'title'       => 'This should not reset sticky',
1382
		) );
1383
		$request->set_body_params( $params );
1384
		$response = $this->server->dispatch( $request );
1385
1386
		$new_data = $response->get_data();
1387
		$this->assertEquals( true, $new_data['sticky'] );
1388
		$post = get_post( $new_data['id'] );
1389
		$this->assertEquals( true, is_sticky( $post->ID ) );
1390
	}
1391
1392
	public function test_update_post_excerpt() {
1393
		wp_set_current_user( $this->editor_id );
1394
1395
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1396
		$request->set_body_params( array(
1397
			'excerpt' => 'An Excerpt',
1398
		) );
1399
1400
		$response = $this->server->dispatch( $request );
1401
		$new_data = $response->get_data();
1402
		$this->assertEquals( 'An Excerpt', $new_data['excerpt']['raw'] );
1403
	}
1404
1405
	public function test_update_post_empty_excerpt() {
1406
		wp_set_current_user( $this->editor_id );
1407
1408
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1409
		$request->set_body_params( array(
1410
			'excerpt' => '',
1411
		) );
1412
1413
		$response = $this->server->dispatch( $request );
1414
		$new_data = $response->get_data();
1415
		$this->assertEquals( '', $new_data['excerpt']['raw'] );
1416
	}
1417
1418
	public function test_update_post_content() {
1419
		wp_set_current_user( $this->editor_id );
1420
1421
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1422
		$request->set_body_params( array(
1423
			'content' => 'Some Content',
1424
		) );
1425
1426
		$response = $this->server->dispatch( $request );
1427
		$new_data = $response->get_data();
1428
		$this->assertEquals( 'Some Content', $new_data['content']['raw'] );
1429
	}
1430
1431
	public function test_update_post_empty_content() {
1432
		wp_set_current_user( $this->editor_id );
1433
1434
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1435
		$request->set_body_params( array(
1436
			'content' => '',
1437
		) );
1438
1439
		$response = $this->server->dispatch( $request );
1440
		$new_data = $response->get_data();
1441
		$this->assertEquals( '', $new_data['content']['raw'] );
1442
	}
1443
1444 View Code Duplication
	public function test_update_post_with_password_and_sticky_fails() {
1445
		wp_set_current_user( $this->editor_id );
1446
1447
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1448
		$params = $this->set_post_data( array(
1449
			'password' => '123',
1450
			'sticky'   => true,
1451
		) );
1452
		$request->set_body_params( $params );
1453
		$response = $this->server->dispatch( $request );
1454
1455
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1456
	}
1457
1458
	public function test_update_stick_post_with_password_fails() {
1459
		wp_set_current_user( $this->editor_id );
1460
1461
		stick_post( $this->post_id );
1462
1463
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1464
		$params = $this->set_post_data( array(
1465
			'password' => '123',
1466
		) );
1467
		$request->set_body_params( $params );
1468
		$response = $this->server->dispatch( $request );
1469
1470
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1471
	}
1472
1473 View Code Duplication
	public function test_update_password_protected_post_with_sticky_fails() {
1474
		wp_set_current_user( $this->editor_id );
1475
1476
		wp_update_post( array( 'ID' => $this->post_id, 'post_password' => '123' ) );
1477
1478
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1479
		$params = $this->set_post_data( array(
1480
			'sticky' => true,
1481
		) );
1482
		$request->set_body_params( $params );
1483
		$response = $this->server->dispatch( $request );
1484
1485
		$this->assertErrorResponse( 'rest_invalid_field', $response, 400 );
1486
	}
1487
1488
	public function test_update_post_with_quotes_in_title() {
1489
		wp_set_current_user( $this->editor_id );
1490
1491
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1492
		$params = $this->set_post_data( array(
1493
			'title' => "Rob O'Rourke's Diary",
1494
		) );
1495
		$request->set_body_params( $params );
1496
		$response = $this->server->dispatch( $request );
1497
		$new_data = $response->get_data();
1498
		$this->assertEquals( "Rob O'Rourke's Diary", $new_data['title']['raw'] );
1499
	}
1500
1501
	public function test_update_post_with_categories() {
1502
1503
		wp_set_current_user( $this->editor_id );
1504
		$category = wp_insert_term( 'Test Category', 'category' );
1505
1506
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1507
		$params = $this->set_post_data( array(
1508
			'title' => 'Tester',
1509
			'categories' => array(
1510
				$category['term_id'],
1511
			),
1512
		) );
1513
		$request->set_body_params( $params );
1514
		$response = $this->server->dispatch( $request );
1515
		$new_data = $response->get_data();
1516
		$this->assertEquals( array( $category['term_id'] ), $new_data['categories'] );
1517
		$categories_path = '';
1518
		$links = $response->get_links();
1519
		foreach ( $links['https://api.w.org/term'] as $link ) {
1520
			if ( 'category' === $link['attributes']['taxonomy'] ) {
1521
				$categories_path = $link['href'];
1522
			}
1523
		}
1524
		$query = parse_url( $categories_path, PHP_URL_QUERY );
1525
		parse_str( $query, $args );
1526
		$request = new WP_REST_Request( 'GET', $args['rest_route'] );
1527
		unset( $args['rest_route'] );
1528
		$request->set_query_params( $args );
1529
		$response = $this->server->dispatch( $request );
1530
		$data = $response->get_data();
1531
		$this->assertCount( 1, $data );
1532
		$this->assertEquals( 'Test Category', $data[0]['name'] );
1533
	}
1534
1535
	public function test_update_post_with_empty_categories() {
1536
1537
		wp_set_current_user( $this->editor_id );
1538
		$category = wp_insert_term( 'Test Category', 'category' );
1539
		wp_set_object_terms( $this->post_id, $category['term_id'], 'category' );
1540
1541
		$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1542
		$params = $this->set_post_data( array(
1543
			'title' => 'Tester',
1544
			'categories' => array(),
1545
		) );
1546
		$request->set_body_params( $params );
1547
		$response = $this->server->dispatch( $request );
1548
		$new_data = $response->get_data();
1549
		$this->assertEquals( array(), $new_data['categories'] );
1550
	}
1551
1552 View Code Duplication
	public function test_delete_item() {
1553
		$post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
1554
		wp_set_current_user( $this->editor_id );
1555
1556
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
1557
		$response = $this->server->dispatch( $request );
1558
1559
		$this->assertNotInstanceOf( 'WP_Error', $response );
1560
		$this->assertEquals( 200, $response->get_status() );
1561
		$data = $response->get_data();
1562
		$this->assertEquals( 'Deleted post', $data['title']['raw'] );
1563
	}
1564
1565 View Code Duplication
	public function test_delete_item_skip_trash() {
1566
		$post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
1567
		wp_set_current_user( $this->editor_id );
1568
1569
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
1570
		$request['force'] = true;
1571
		$response = $this->server->dispatch( $request );
1572
1573
		$this->assertNotInstanceOf( 'WP_Error', $response );
1574
		$this->assertEquals( 200, $response->get_status() );
1575
		$data = $response->get_data();
1576
		$this->assertEquals( 'Deleted post', $data['title']['raw'] );
1577
	}
1578
1579 View Code Duplication
	public function test_delete_item_already_trashed() {
1580
		$post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
1581
		wp_set_current_user( $this->editor_id );
1582
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
1583
		$response = $this->server->dispatch( $request );
1584
		$this->assertEquals( 200, $response->get_status() );
1585
		$response = $this->server->dispatch( $request );
1586
		$this->assertErrorResponse( 'rest_already_trashed', $response, 410 );
1587
	}
1588
1589 View Code Duplication
	public function test_delete_post_invalid_id() {
1590
		wp_set_current_user( $this->editor_id );
1591
1592
		$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
1593
		$response = $this->server->dispatch( $request );
1594
1595
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1596
	}
1597
1598
	public function test_delete_post_invalid_post_type() {
1599
		$page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
1600
		wp_set_current_user( $this->editor_id );
1601
1602
		$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $page_id );
1603
		$response = $this->server->dispatch( $request );
1604
1605
		$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
1606
	}
1607
1608
	public function test_delete_post_without_permission() {
1609
		wp_set_current_user( $this->author_id );
1610
1611
		$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
1612
		$response = $this->server->dispatch( $request );
1613
1614
		$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
1615
	}
1616
1617
	public function test_register_post_type_invalid_controller() {
1618
1619
		register_post_type( 'invalid-controller', array( 'show_in_rest' => true, 'rest_controller_class' => 'Fake_Class_Baba' ) );
1620
		create_initial_rest_routes();
1621
		$routes = $this->server->get_routes();
1622
		$this->assertFalse( isset( $routes['/wp/v2/invalid-controller'] ) );
1623
		_unregister_post_type( 'invalid-controller' );
1624
1625
	}
1626
1627 View Code Duplication
	public function test_get_item_schema() {
1628
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
1629
		$response = $this->server->dispatch( $request );
1630
		$data = $response->get_data();
1631
		$properties = $data['schema']['properties'];
1632
		$this->assertEquals( 22, count( $properties ) );
1633
		$this->assertArrayHasKey( 'author', $properties );
1634
		$this->assertArrayHasKey( 'comment_status', $properties );
1635
		$this->assertArrayHasKey( 'content', $properties );
1636
		$this->assertArrayHasKey( 'date', $properties );
1637
		$this->assertArrayHasKey( 'date_gmt', $properties );
1638
		$this->assertArrayHasKey( 'excerpt', $properties );
1639
		$this->assertArrayHasKey( 'featured_media', $properties );
1640
		$this->assertArrayHasKey( 'guid', $properties );
1641
		$this->assertArrayHasKey( 'format', $properties );
1642
		$this->assertArrayHasKey( 'id', $properties );
1643
		$this->assertArrayHasKey( 'link', $properties );
1644
		$this->assertArrayHasKey( 'modified', $properties );
1645
		$this->assertArrayHasKey( 'modified_gmt', $properties );
1646
		$this->assertArrayHasKey( 'password', $properties );
1647
		$this->assertArrayHasKey( 'ping_status', $properties );
1648
		$this->assertArrayHasKey( 'slug', $properties );
1649
		$this->assertArrayHasKey( 'status', $properties );
1650
		$this->assertArrayHasKey( 'sticky', $properties );
1651
		$this->assertArrayHasKey( 'title', $properties );
1652
		$this->assertArrayHasKey( 'type', $properties );
1653
		$this->assertArrayHasKey( 'tags', $properties );
1654
		$this->assertArrayHasKey( 'categories', $properties );
1655
	}
1656
1657
	public function test_get_additional_field_registration() {
1658
1659
		$schema = array(
1660
			'type'        => 'integer',
1661
			'description' => 'Some integer of mine',
1662
			'enum'        => array( 1, 2, 3, 4 ),
1663
			'context'     => array( 'view', 'edit' ),
1664
		);
1665
1666
		register_rest_field( 'post', 'my_custom_int', array(
1667
			'schema'          => $schema,
1668
			'get_callback'    => array( $this, 'additional_field_get_callback' ),
1669
			'update_callback' => array( $this, 'additional_field_update_callback' ),
1670
		) );
1671
1672
		$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
1673
1674
		$response = $this->server->dispatch( $request );
1675
		$data = $response->get_data();
1676
1677
		$this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
1678
		$this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
1679
1680
		wp_set_current_user( 1 );
1681
1682
		$post_id = $this->factory->post->create();
1683
1684
		$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
1685
1686
		$response = $this->server->dispatch( $request );
1687
		$this->assertArrayHasKey( 'my_custom_int', $response->data );
1688
1689
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $post_id );
1690
		$request->set_body_params(array(
1691
			'my_custom_int' => 123,
1692
		));
1693
1694
		$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...
1695
		$this->assertEquals( 123, get_post_meta( $post_id, 'my_custom_int', true ) );
1696
1697
		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
1698
		$request->set_body_params(array(
1699
			'my_custom_int' => 123,
1700
			'title' => 'hello',
1701
		));
1702
1703
		$response = $this->server->dispatch( $request );
1704
1705
		$this->assertEquals( 123, $response->data['my_custom_int'] );
1706
1707
		global $wp_rest_additional_fields;
1708
		$wp_rest_additional_fields = array();
1709
	}
1710
1711
	public function additional_field_get_callback( $object ) {
1712
		return get_post_meta( $object['id'], 'my_custom_int', true );
1713
	}
1714
1715
	public function additional_field_update_callback( $value, $post ) {
1716
		update_post_meta( $post->ID, 'my_custom_int', $value );
1717
	}
1718
1719
	public function tearDown() {
1720
		_unregister_post_type( 'youseeeme' );
1721
		if ( isset( $this->attachment_id ) ) {
1722
			$this->remove_added_uploads();
1723
		}
1724
		parent::tearDown();
1725
	}
1726
1727
	/**
1728
	 * Internal function used to disable an insert query which
1729
	 * will trigger a wpdb error for testing purposes.
1730
	 */
1731
	public function error_insert_query( $query ) {
1732
		if ( strpos( $query, 'INSERT' ) === 0 ) {
1733
			$query = '],';
1734
		}
1735
		return $query;
1736
	}
1737
1738
}
1739