WP_Test_REST_Posts_Controller::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Unit tests covering WP_REST_Posts_Controller functionality.
4
 *
5
 * @package    WordPress
6
 * @subpackage REST API
7
 */
8
9
/**
10
 * @group restapi
11
 */
12
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Testcase
13
{
14
    protected static $post_id;
15
16
    protected static $superadmin_id;
17
    protected static $editor_id;
18
    protected static $author_id;
19
    protected static $contributor_id;
20
21
    protected static $supported_formats;
22
23
    protected $forbidden_cat;
24
25
    public static function wpSetUpBeforeClass( $factory ) 
0 ignored issues
show
Coding Style introduced by
The function name wpSetUpBeforeClass is in camel caps, but expected wp_set_up_before_class instead as per the coding standard.
Loading history...
26
    {
27
        self::$post_id = $factory->post->create();
28
29
        self::$superadmin_id = $factory->user->create(
30
            array(
31
            'role'       => 'administrator',
32
            'user_login' => 'superadmin',
33
            ) 
34
        );
35
        self::$editor_id = $factory->user->create(
36
            array(
37
            'role' => 'editor',
38
            ) 
39
        );
40
        self::$author_id = $factory->user->create(
41
            array(
42
            'role' => 'author',
43
            ) 
44
        );
45
        self::$contributor_id = $factory->user->create(
46
            array(
47
            'role' => 'contributor',
48
            ) 
49
        );
50
51
        if (is_multisite() ) {
52
            update_site_option('site_admins', array( 'superadmin' ));
53
        }
54
55
        // Only support 'post' and 'gallery'
56
        self::$supported_formats = get_theme_support('post-formats');
57
        add_theme_support('post-formats', array( 'post', 'gallery' ));
58
    }
59
60
    public static function wpTearDownAfterClass() 
0 ignored issues
show
Coding Style introduced by
The function name wpTearDownAfterClass is in camel caps, but expected wp_tear_down_after_class instead as per the coding standard.
Loading history...
61
    {
62
        // Restore theme support for formats.
63
        if (self::$supported_formats ) {
64
            add_theme_support('post-formats', self::$supported_formats);
65
        } else {
66
            remove_theme_support('post-formats');
67
        }
68
69
        wp_delete_post(self::$post_id, true);
70
71
        self::delete_user(self::$editor_id);
72
        self::delete_user(self::$author_id);
73
        self::delete_user(self::$contributor_id);
74
    }
75
76
    public function setUp() 
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
77
    {
78
        parent::setUp();
79
        register_post_type('youseeme', array( 'supports' => array(), 'show_in_rest' => true ));
80
    }
81
82 View Code Duplication
    public function test_register_routes() 
83
    {
84
        $routes = $this->server->get_routes();
85
86
        $this->assertArrayHasKey('/wp/v2/posts', $routes);
87
        $this->assertCount(2, $routes['/wp/v2/posts']);
88
        $this->assertArrayHasKey('/wp/v2/posts/(?P<id>[\d]+)', $routes);
89
        $this->assertCount(3, $routes['/wp/v2/posts/(?P<id>[\d]+)']);
90
    }
91
92 View Code Duplication
    public function test_context_param() 
93
    {
94
        // Collection
95
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/posts');
96
        $response = $this->server->dispatch($request);
97
        $data = $response->get_data();
98
        $this->assertEquals('view', $data['endpoints'][0]['args']['context']['default']);
99
        $this->assertEquals(array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum']);
100
        // Single
101
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/posts/' . self::$post_id);
102
        $response = $this->server->dispatch($request);
103
        $data = $response->get_data();
104
        $this->assertEquals('view', $data['endpoints'][0]['args']['context']['default']);
105
        $this->assertEquals(array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum']);
106
    }
107
108 View Code Duplication
    public function test_registered_query_params() 
109
    {
110
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/posts');
111
        $response = $this->server->dispatch($request);
112
        $data = $response->get_data();
113
        $keys = array_keys($data['endpoints'][0]['args']);
114
        sort($keys);
115
        $this->assertEquals(
116
            array(
117
            'after',
118
            'author',
119
            'author_exclude',
120
            'before',
121
            'categories',
122
            'categories_exclude',
123
            'context',
124
            'exclude',
125
            'include',
126
            'offset',
127
            'order',
128
            'orderby',
129
            'page',
130
            'per_page',
131
            'search',
132
            'slug',
133
            'status',
134
            'sticky',
135
            'tags',
136
            'tags_exclude',
137
            ), $keys 
138
        );
139
    }
140
141
    public function test_get_items() 
142
    {
143
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
144
        $response = $this->server->dispatch($request);
145
146
        $this->check_get_posts_response($response);
147
    }
148
149
    /**
150
     * A valid query that returns 0 results should return an empty JSON list.
151
     *
152
     * @issue 862
153
     */
154
    public function test_get_items_empty_query() 
155
    {
156
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
157
        $request->set_query_params(
158
            array(
159
            'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
160
            ) 
161
        );
162
        $response = $this->server->dispatch($request);
163
164
        $this->assertEmpty($response->get_data());
165
        $this->assertEquals(200, $response->get_status());
166
    }
167
168
    public function test_get_items_author_query() 
169
    {
170
        $this->factory->post->create(array( 'post_author' => self::$editor_id ));
171
        $this->factory->post->create(array( 'post_author' => self::$author_id ));
172
        // All 3 posts
173
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
174
        $response = $this->server->dispatch($request);
175
        $this->assertEquals(200, $response->get_status());
176
        $this->assertEquals(3, count($response->get_data()));
177
        // 2 of 3 posts
178
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
179
        $request->set_param('author', array( self::$editor_id, self::$author_id ));
180
        $response = $this->server->dispatch($request);
181
        $this->assertEquals(200, $response->get_status());
182
        $data = $response->get_data();
183
        $this->assertEquals(2, count($data));
184
        $this->assertEqualSets(array( self::$editor_id, self::$author_id ), wp_list_pluck($data, 'author'));
185
        // 1 of 3 posts
186
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
187
        $request->set_param('author', self::$editor_id);
188
        $response = $this->server->dispatch($request);
189
        $this->assertEquals(200, $response->get_status());
190
        $data = $response->get_data();
191
        $this->assertEquals(1, count($data));
192
        $this->assertEquals(self::$editor_id, $data[0]['author']);
193
    }
194
195
    public function test_get_items_author_exclude_query() 
196
    {
197
        $this->factory->post->create(array( 'post_author' => self::$editor_id ));
198
        $this->factory->post->create(array( 'post_author' => self::$author_id ));
199
        // All 3 posts
200
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
201
        $response = $this->server->dispatch($request);
202
        $this->assertEquals(200, $response->get_status());
203
        $this->assertEquals(3, count($response->get_data()));
204
        // 1 of 3 posts
205
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
206
        $request->set_param('author_exclude', array( self::$editor_id, self::$author_id ));
207
        $response = $this->server->dispatch($request);
208
        $this->assertEquals(200, $response->get_status());
209
        $data = $response->get_data();
210
        $this->assertEquals(1, count($data));
211
        $this->assertNotEquals(self::$editor_id, $data[0]['author']);
212
        $this->assertNotEquals(self::$author_id, $data[0]['author']);
213
        // 2 of 3 posts
214
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
215
        $request->set_param('author_exclude', self::$editor_id);
216
        $response = $this->server->dispatch($request);
217
        $this->assertEquals(200, $response->get_status());
218
        $data = $response->get_data();
219
        $this->assertEquals(2, count($data));
220
        $this->assertNotEquals(self::$editor_id, $data[0]['author']);
221
        $this->assertNotEquals(self::$editor_id, $data[1]['author']);
222
        // invalid author_exclude errors
223
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
224
        $request->set_param('author_exclude', 'invalid');
225
        $response = $this->server->dispatch($request);
226
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
227
    }
228
229
    public function test_get_items_include_query() 
230
    {
231
        $id1 = $this->factory->post->create(array( 'post_status' => 'publish' ));
232
        $this->factory->post->create(array( 'post_status' => 'publish' ));
233
        $id3 = $this->factory->post->create(array( 'post_status' => 'publish' ));
234
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
235
        // Orderby=>desc
236
        $request->set_param('include', array( $id1, $id3 ));
237
        $response = $this->server->dispatch($request);
238
        $data = $response->get_data();
239
        $this->assertEquals(2, count($data));
240
        $this->assertEquals($id3, $data[0]['id']);
241
        // Orderby=>include
242
        $request->set_param('orderby', 'include');
243
        $response = $this->server->dispatch($request);
244
        $data = $response->get_data();
245
        $this->assertEquals(2, count($data));
246
        $this->assertEquals($id1, $data[0]['id']);
247
        // Invalid include should error
248
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
249
        $request->set_param('include', 'invalid');
250
        $response = $this->server->dispatch($request);
251
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
252
    }
253
254
    public function test_get_items_exclude_query() 
255
    {
256
        $id1 = $this->factory->post->create(array( 'post_status' => 'publish' ));
257
        $id2 = $this->factory->post->create(array( 'post_status' => 'publish' ));
258
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
259
        $response = $this->server->dispatch($request);
260
        $data = $response->get_data();
261
        $this->assertTrue(in_array($id1, wp_list_pluck($data, 'id'), true));
262
        $this->assertTrue(in_array($id2, wp_list_pluck($data, 'id'), true));
263
264
        $request->set_param('exclude', array( $id2 ));
265
        $response = $this->server->dispatch($request);
266
        $data = $response->get_data();
267
        $this->assertTrue(in_array($id1, wp_list_pluck($data, 'id'), true));
268
        $this->assertFalse(in_array($id2, wp_list_pluck($data, 'id'), true));
269
270
        $request->set_param('exclude', "$id2");
271
        $response = $this->server->dispatch($request);
272
        $data = $response->get_data();
273
        $this->assertTrue(in_array($id1, wp_list_pluck($data, 'id'), true));
274
        $this->assertFalse(in_array($id2, wp_list_pluck($data, 'id'), true));
275
276
        $request->set_param('exclude', 'invalid');
277
        $response = $this->server->dispatch($request);
278
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
279
    }
280
281
    public function test_get_items_search_query() 
282
    {
283
        for ( $i = 0;  $i < 5;  $i++ ) {
284
            $this->factory->post->create(array( 'post_status' => 'publish' ));
285
        }
286
        $this->factory->post->create(array( 'post_title' => 'Search Result', 'post_status' => 'publish' ));
287
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
288
        $response = $this->server->dispatch($request);
289
        $this->assertEquals(7, count($response->get_data()));
290
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
291
        $request->set_param('search', 'Search Result');
292
        $response = $this->server->dispatch($request);
293
        $data = $response->get_data();
294
        $this->assertEquals(1, count($data));
295
        $this->assertEquals('Search Result', $data[0]['title']['rendered']);
296
    }
297
298
    public function test_get_items_slug_query() 
299
    {
300
        $this->factory->post->create(array( 'post_title' => 'Apple', 'post_status' => 'publish' ));
301
        $this->factory->post->create(array( 'post_title' => 'Banana', 'post_status' => 'publish' ));
302
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
303
        $request->set_param('slug', 'apple');
304
        $response = $this->server->dispatch($request);
305
        $this->assertEquals(200, $response->get_status());
306
        $data = $response->get_data();
307
        $this->assertEquals(1, count($data));
308
        $this->assertEquals('Apple', $data[0]['title']['rendered']);
309
    }
310
311 View Code Duplication
    public function test_get_items_multiple_slugs_array_query() 
312
    {
313
        $this->factory->post->create(array( 'post_title' => 'Apple', 'post_status' => 'publish' ));
314
        $this->factory->post->create(array( 'post_title' => 'Banana', 'post_status' => 'publish' ));
315
        $this->factory->post->create(array( 'post_title' => 'Peach', 'post_status' => 'publish' ));
316
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
317
        $request->set_param('slug', array( 'banana', 'peach' ));
318
        $response = $this->server->dispatch($request);
319
        $this->assertEquals(200, $response->get_status());
320
        $data = $response->get_data();
321
        $this->assertEquals(2, count($data));
322
        $titles = array(
323
         $data[0]['title']['rendered'],
324
         $data[1]['title']['rendered'],
325
        );
326
        sort($titles);
327
        $this->assertEquals(array( 'Banana', 'Peach' ), $titles);
328
    }
329
330 View Code Duplication
    public function test_get_items_multiple_slugs_string_query() 
331
    {
332
        $this->factory->post->create(array( 'post_title' => 'Apple', 'post_status' => 'publish' ));
333
        $this->factory->post->create(array( 'post_title' => 'Banana', 'post_status' => 'publish' ));
334
        $this->factory->post->create(array( 'post_title' => 'Peach', 'post_status' => 'publish' ));
335
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
336
        $request->set_param('slug', 'apple,banana');
337
        $response = $this->server->dispatch($request);
338
        $this->assertEquals(200, $response->get_status());
339
        $data = $response->get_data();
340
        $this->assertEquals(2, count($data));
341
        $titles = array(
342
         $data[0]['title']['rendered'],
343
         $data[1]['title']['rendered'],
344
        );
345
        sort($titles);
346
        $this->assertEquals(array( 'Apple', 'Banana' ), $titles);
347
    }
348
349
    public function test_get_items_status_query() 
350
    {
351
        wp_set_current_user(0);
352
        $this->factory->post->create(array( 'post_status' => 'draft' ));
353
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
354
        $request->set_param('status', 'publish');
355
        $response = $this->server->dispatch($request);
356
        $this->assertEquals(200, $response->get_status());
357
        $this->assertEquals(1, count($response->get_data()));
358
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
359
        $request->set_param('status', 'draft');
360
        $response = $this->server->dispatch($request);
361
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
362
        wp_set_current_user(self::$editor_id);
363
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
364
        $request->set_param('status', 'draft');
365
        $response = $this->server->dispatch($request);
366
        $this->assertEquals(200, $response->get_status());
367
        $this->assertEquals(1, count($response->get_data()));
368
    }
369
370 View Code Duplication
    public function test_get_items_multiple_statuses_string_query() 
371
    {
372
        wp_set_current_user(self::$editor_id);
373
374
        $this->factory->post->create(array( 'post_status' => 'draft' ));
375
        $this->factory->post->create(array( 'post_status' => 'private' ));
376
        $this->factory->post->create(array( 'post_status' => 'publish' ));
377
378
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
379
        $request->set_param('context', 'edit');
380
        $request->set_param('status', 'draft,private');
381
382
        $response = $this->server->dispatch($request);
383
        $this->assertEquals(200, $response->get_status());
384
        $data = $response->get_data();
385
        $this->assertEquals(2, count($data));
386
        $statuses = array(
387
         $data[0]['status'],
388
         $data[1]['status'],
389
        );
390
        sort($statuses);
391
        $this->assertEquals(array( 'draft', 'private' ), $statuses);
392
    }
393
394 View Code Duplication
    public function test_get_items_multiple_statuses_array_query() 
395
    {
396
        wp_set_current_user(self::$editor_id);
397
398
        $this->factory->post->create(array( 'post_status' => 'draft' ));
399
        $this->factory->post->create(array( 'post_status' => 'pending' ));
400
        $this->factory->post->create(array( 'post_status' => 'publish' ));
401
402
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
403
        $request->set_param('context', 'edit');
404
        $request->set_param('status', array( 'draft', 'pending' ));
405
406
        $response = $this->server->dispatch($request);
407
        $this->assertEquals(200, $response->get_status());
408
        $data = $response->get_data();
409
        $this->assertEquals(2, count($data));
410
        $statuses = array(
411
         $data[0]['status'],
412
         $data[1]['status'],
413
        );
414
        sort($statuses);
415
        $this->assertEquals(array( 'draft', 'pending' ), $statuses);
416
    }
417
418
    public function test_get_items_multiple_statuses_one_invalid_query() 
419
    {
420
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
421
        $request->set_param('context', 'edit');
422
        $request->set_param('status', array( 'draft', 'nonsense' ));
423
        $response = $this->server->dispatch($request);
424
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
425
    }
426
427 View Code Duplication
    public function test_get_items_invalid_status_query() 
428
    {
429
        wp_set_current_user(0);
430
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
431
        $request->set_param('status', 'invalid');
432
        $response = $this->server->dispatch($request);
433
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
434
    }
435
436 View Code Duplication
    public function test_get_items_status_without_permissions() 
437
    {
438
        $draft_id = $this->factory->post->create(
439
            array(
440
            'post_status' => 'draft',
441
            ) 
442
        );
443
        wp_set_current_user(0);
444
445
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
446
        $response = $this->server->dispatch($request);
447
448
        $this->assertEquals(200, $response->get_status());
449
450
        $all_data = $response->get_data();
451
        foreach ( $all_data as $post ) {
452
            $this->assertNotEquals($draft_id, $post['id']);
453
        }
454
    }
455
456
    public function test_get_items_order_and_orderby() 
457
    {
458
        $this->factory->post->create(array( 'post_title' => 'Apple Pie', 'post_status' => 'publish' ));
459
        $this->factory->post->create(array( 'post_title' => 'Apple Sauce', 'post_status' => 'publish' ));
460
        $this->factory->post->create(array( 'post_title' => 'Apple Cobbler', 'post_status' => 'publish' ));
461
        $this->factory->post->create(array( 'post_title' => 'Apple Coffee Cake', 'post_status' => 'publish' ));
462
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
463
        $request->set_param('search', 'Apple');
464
        // order defaults to 'desc'
465
        $request->set_param('orderby', 'title');
466
        $response = $this->server->dispatch($request);
467
        $data = $response->get_data();
468
        $this->assertEquals('Apple Sauce', $data[0]['title']['rendered']);
469
        // order=>asc
470
        $request->set_param('order', 'asc');
471
        $response = $this->server->dispatch($request);
472
        $data = $response->get_data();
473
        $this->assertEquals('Apple Cobbler', $data[0]['title']['rendered']);
474
        // order=>asc,id should fail
475
        $request->set_param('order', 'asc,id');
476
        $response = $this->server->dispatch($request);
477
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
478
        // orderby=>content should fail (invalid param test)
479
        $request->set_param('order', 'asc');
480
        $request->set_param('orderby', 'content');
481
        $response = $this->server->dispatch($request);
482
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
483
    }
484
485
    public function test_get_items_with_orderby_include_without_include_param() 
486
    {
487
        $this->factory->post->create(array( 'post_status' => 'publish' ));
488
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
489
        $request->set_param('orderby', 'include');
490
491
        $response = $this->server->dispatch($request);
492
493
        $this->assertErrorResponse('rest_orderby_include_missing_include', $response, 400);
494
    }
495
496
    public function test_get_items_with_orderby_id() 
497
    {
498
        $id1 = $this->factory->post->create(array( 'post_status' => 'publish', 'post_date' => '2016-01-13 02:26:48' ));
499
        $id2 = $this->factory->post->create(array( 'post_status' => 'publish', 'post_date' => '2016-01-12 02:26:48' ));
500
        $id3 = $this->factory->post->create(array( 'post_status' => 'publish', 'post_date' => '2016-01-11 02:26:48' ));
501
502
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
503
        $request->set_param('orderby', 'id');
504
        $request->set_param('include', array( $id1, $id2, $id3 ));
505
506
        $response = $this->server->dispatch($request);
507
        $data = $response->get_data();
508
509
        // Default ORDER is DESC.
510
        $this->assertEquals($id3, $data[0]['id']);
511
        $this->assertEquals($id2, $data[1]['id']);
512
        $this->assertEquals($id1, $data[2]['id']);
513
    }
514
515
    public function test_get_items_with_orderby_slug() 
516
    {
517
        $id1 = $this->factory->post->create(array( 'post_title' => 'ABC', 'post_name' => 'xyz', 'post_status' => 'publish' ));
518
        $id2 = $this->factory->post->create(array( 'post_title' => 'XYZ', 'post_name' => 'abc', 'post_status' => 'publish' ));
519
520
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
521
        $request->set_param('orderby', 'slug');
522
        $request->set_param('include', array( $id1, $id2 ));
523
524
        $response = $this->server->dispatch($request);
525
        $data = $response->get_data();
526
527
        // Default ORDER is DESC.
528
        $this->assertEquals('xyz', $data[0]['slug']);
529
        $this->assertEquals('abc', $data[1]['slug']);
530
    }
531
532
    public function test_get_items_with_orderby_relevance() 
533
    {
534
        $this->factory->post->create(array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ));
535
        $this->factory->post->create(array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ));
536
537
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
538
        $request->set_param('orderby', 'relevance');
539
        $response = $this->server->dispatch($request);
540
        $this->assertErrorResponse('rest_no_search_term_defined', $response, 400);
541
    }
542
543
    public function test_get_items_ignore_sticky_posts_by_default() 
544
    {
545
        $this->markTestSkipped('Broken, see https://github.com/WP-API/WP-API/issues/2210');
546
        $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' ));
547
        $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' ));
548
        $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' ));
549
        stick_post($post_id2);
550
551
        // No stickies by default
552
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
553
        $response = $this->server->dispatch($request);
554
        $data = $response->get_data();
555
        $this->assertEquals(array( self::$post_id, $post_id3, $post_id2, $post_id1 ), wp_list_pluck($data, 'id'));
556
557
        // Permit stickies
558
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
559
        $request->set_param('ignore_sticky_posts', false);
560
        $response = $this->server->dispatch($request);
561
        $data = $response->get_data();
562
        $this->assertEquals(array( $post_id2, self::$post_id, $post_id3, $post_id1 ), wp_list_pluck($data, 'id'));
563
    }
564
565
    public function test_get_items_offset_query() 
566
    {
567
        $id1 = self::$post_id;
568
        $id2 = $this->factory->post->create(array( 'post_status' => 'publish' ));
569
        $id3 = $this->factory->post->create(array( 'post_status' => 'publish' ));
570
        $id4 = $this->factory->post->create(array( 'post_status' => 'publish' ));
571
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
572
        $request->set_param('offset', 1);
573
        $response = $this->server->dispatch($request);
574
        $this->assertCount(3, $response->get_data());
575
        // 'offset' works with 'per_page'
576
        $request->set_param('per_page', 2);
577
        $response = $this->server->dispatch($request);
578
        $this->assertCount(2, $response->get_data());
579
        // 'offset' takes priority over 'page'
580
        $request->set_param('page', 3);
581
        $response = $this->server->dispatch($request);
582
        $this->assertCount(2, $response->get_data());
583
        // Invalid 'offset' should error
584
        $request->set_param('offset', 'moreplease');
585
        $response = $this->server->dispatch($request);
586
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
587
    }
588
589
    public function test_get_items_tags_query() 
590
    {
591
        $id1 = self::$post_id;
592
        $id2 = $this->factory->post->create(array( 'post_status' => 'publish' ));
593
        $id3 = $this->factory->post->create(array( 'post_status' => 'publish' ));
594
        $id4 = $this->factory->post->create(array( 'post_status' => 'publish' ));
595
        $tag = wp_insert_term('My Tag', 'post_tag');
596
597
        wp_set_object_terms($id1, array( $tag['term_id'] ), 'post_tag');
598
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
599
        $request->set_param('tags', array( $tag['term_id'] ));
600
601
        $response = $this->server->dispatch($request);
602
        $data = $response->get_data();
603
        $this->assertCount(1, $data);
604
        $this->assertEquals($id1, $data[0]['id']);
605
    }
606
607
    public function test_get_items_tags_exclude_query() 
608
    {
609
        $id1 = self::$post_id;
610
        $id2 = $this->factory->post->create(array( 'post_status' => 'publish' ));
611
        $id3 = $this->factory->post->create(array( 'post_status' => 'publish' ));
612
        $id4 = $this->factory->post->create(array( 'post_status' => 'publish' ));
613
        $tag = wp_insert_term('My Tag', 'post_tag');
614
615
        wp_set_object_terms($id1, array( $tag['term_id'] ), 'post_tag');
616
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
617
        $request->set_param('tags_exclude', array( $tag['term_id'] ));
618
619
        $response = $this->server->dispatch($request);
620
        $data = $response->get_data();
621
        $this->assertCount(3, $data);
622
        $this->assertEquals($id4, $data[0]['id']);
623
        $this->assertEquals($id3, $data[1]['id']);
624
        $this->assertEquals($id2, $data[2]['id']);
625
    }
626
627
    public function test_get_items_tags_and_categories_query() 
628
    {
629
        $id1 = self::$post_id;
630
        $id2 = $this->factory->post->create(array( 'post_status' => 'publish' ));
631
        $id3 = $this->factory->post->create(array( 'post_status' => 'publish' ));
632
        $id4 = $this->factory->post->create(array( 'post_status' => 'publish' ));
633
        $tag = wp_insert_term('My Tag', 'post_tag');
634
        $category = wp_insert_term('My Category', 'category');
635
636
        wp_set_object_terms($id1, array( $tag['term_id'] ), 'post_tag');
637
        wp_set_object_terms($id2, array( $tag['term_id'] ), 'post_tag');
638
        wp_set_object_terms($id1, array( $category['term_id'] ), 'category');
639
640
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
641
        $request->set_param('tags', array( $tag['term_id'] ));
642
        $request->set_param('categories', array( $category['term_id'] ));
643
644
        $response = $this->server->dispatch($request);
645
        $this->assertCount(1, $response->get_data());
646
647
        $request->set_param('tags', array( 'my-tag' ));
648
        $response = $this->server->dispatch($request);
649
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
650
    }
651
652
    public function test_get_items_tags_and_categories_exclude_query() 
653
    {
654
        $id1 = self::$post_id;
655
        $id2 = $this->factory->post->create(array( 'post_status' => 'publish' ));
656
        $id3 = $this->factory->post->create(array( 'post_status' => 'publish' ));
657
        $id4 = $this->factory->post->create(array( 'post_status' => 'publish' ));
658
        $tag = wp_insert_term('My Tag', 'post_tag');
659
        $category = wp_insert_term('My Category', 'category');
660
661
        wp_set_object_terms($id1, array( $tag['term_id'] ), 'post_tag');
662
        wp_set_object_terms($id2, array( $tag['term_id'] ), 'post_tag');
663
        wp_set_object_terms($id1, array( $category['term_id'] ), 'category');
664
665
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
666
        $request->set_param('tags', array( $tag['term_id'] ));
667
        $request->set_param('categories_exclude', array( $category['term_id'] ));
668
669
        $response = $this->server->dispatch($request);
670
        $data = $response->get_data();
671
        $this->assertCount(1, $data);
672
        $this->assertEquals($id2, $data[0]['id']);
673
674
        $request->set_param('tags_exclude', array( 'my-tag' ));
675
        $response = $this->server->dispatch($request);
676
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
677
    }
678
679
    public function test_get_items_sticky_query() 
680
    {
681
        $id1 = self::$post_id;
682
        $id2 = $this->factory->post->create(array( 'post_status' => 'publish' ));
683
684
        update_option('sticky_posts', array( $id2 ));
685
686
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
687
        $request->set_param('sticky', true);
688
689
        $response = $this->server->dispatch($request);
690
        $this->assertCount(1, $response->get_data());
691
692
        $posts = $response->get_data();
693
        $post = $posts[0];
694
        $this->assertEquals($id2, $post['id']);
695
696
        $request->set_param('sticky', 'nothanks');
697
        $response = $this->server->dispatch($request);
698
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
699
    }
700
701
    public function test_get_items_sticky_with_post__in_query() 
702
    {
703
        $id1 = self::$post_id;
704
        $id2 = $this->factory->post->create(array( 'post_status' => 'publish' ));
705
        $id3 = $this->factory->post->create(array( 'post_status' => 'publish' ));
706
707
        update_option('sticky_posts', array( $id2 ));
708
709
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
710
        $request->set_param('sticky', true);
711
        $request->set_param('include', array( $id1 ));
712
713
        $response = $this->server->dispatch($request);
714
        $this->assertCount(0, $response->get_data());
715
716
        update_option('sticky_posts', array( $id1, $id2 ));
717
718
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
719
        $request->set_param('sticky', true);
720
        $request->set_param('include', array( $id1 ));
721
722
        $response = $this->server->dispatch($request);
723
724
        $this->assertCount(1, $response->get_data());
725
726
        $posts = $response->get_data();
727
        $post = $posts[0];
728
        $this->assertEquals($id1, $post['id']);
729
    }
730
731
    public function test_get_items_not_sticky_query() 
732
    {
733
        $id1 = self::$post_id;
734
        $id2 = $this->factory->post->create(array( 'post_status' => 'publish' ));
735
736
        update_option('sticky_posts', array( $id2 ));
737
738
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
739
        $request->set_param('sticky', false);
740
741
        $response = $this->server->dispatch($request);
742
        $this->assertCount(1, $response->get_data());
743
744
        $posts = $response->get_data();
745
        $post = $posts[0];
746
        $this->assertEquals($id1, $post['id']);
747
    }
748
749
    public function test_get_items_sticky_with_post__not_in_query() 
750
    {
751
        $id1 = self::$post_id;
752
        $id2 = $this->factory->post->create(array( 'post_status' => 'publish' ));
753
        $id3 = $this->factory->post->create(array( 'post_status' => 'publish' ));
754
755
        update_option('sticky_posts', array( $id2 ));
756
757
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
758
        $request->set_param('sticky', false);
759
        $request->set_param('exclude', array( $id3 ));
760
761
        $response = $this->server->dispatch($request);
762
        $this->assertCount(1, $response->get_data());
763
764
        $posts = $response->get_data();
765
        $post = $posts[0];
766
        $this->assertEquals($id1, $post['id']);
767
    }
768
769
    /**
770
     * @group test
771
     */
772
    public function test_get_items_pagination_headers() 
773
    {
774
        // Start of the index
775
        for ( $i = 0; $i < 49; $i++ ) {
776
            $this->factory->post->create(
777
                array(
778
                'post_title'   => "Post {$i}",
779
                ) 
780
            );
781
        }
782
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
783
        $response = $this->server->dispatch($request);
784
        $headers = $response->get_headers();
785
        $this->assertEquals(50, $headers['X-WP-Total']);
786
        $this->assertEquals(5, $headers['X-WP-TotalPages']);
787
        $next_link = add_query_arg(
788
            array(
789
            'page'    => 2,
790
            ), rest_url('/wp/v2/posts') 
791
        );
792
        $this->assertFalse(stripos($headers['Link'], 'rel="prev"'));
793
        $this->assertContains('<' . $next_link . '>; rel="next"', $headers['Link']);
794
        // 3rd page
795
        $this->factory->post->create(
796
            array(
797
            'post_title'   => 'Post 51',
798
            ) 
799
        );
800
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
801
        $request->set_param('page', 3);
802
        $response = $this->server->dispatch($request);
803
        $headers = $response->get_headers();
804
        $this->assertEquals(51, $headers['X-WP-Total']);
805
        $this->assertEquals(6, $headers['X-WP-TotalPages']);
806
        $prev_link = add_query_arg(
807
            array(
808
            'page'    => 2,
809
            ), rest_url('/wp/v2/posts') 
810
        );
811
        $this->assertContains('<' . $prev_link . '>; rel="prev"', $headers['Link']);
812
        $next_link = add_query_arg(
813
            array(
814
            'page'    => 4,
815
            ), rest_url('/wp/v2/posts') 
816
        );
817
        $this->assertContains('<' . $next_link . '>; rel="next"', $headers['Link']);
818
        // Last page
819
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
820
        $request->set_param('page', 6);
821
        $response = $this->server->dispatch($request);
822
        $headers = $response->get_headers();
823
        $this->assertEquals(51, $headers['X-WP-Total']);
824
        $this->assertEquals(6, $headers['X-WP-TotalPages']);
825
        $prev_link = add_query_arg(
826
            array(
827
            'page'    => 5,
828
            ), rest_url('/wp/v2/posts') 
829
        );
830
        $this->assertContains('<' . $prev_link . '>; rel="prev"', $headers['Link']);
831
        $this->assertFalse(stripos($headers['Link'], 'rel="next"'));
832
        // Out of bounds
833
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
834
        $request->set_param('page', 8);
835
        $response = $this->server->dispatch($request);
836
        $headers = $response->get_headers();
837
        $this->assertEquals(51, $headers['X-WP-Total']);
838
        $this->assertEquals(6, $headers['X-WP-TotalPages']);
839
        $prev_link = add_query_arg(
840
            array(
841
            'page'    => 6,
842
            ), rest_url('/wp/v2/posts') 
843
        );
844
        $this->assertContains('<' . $prev_link . '>; rel="prev"', $headers['Link']);
845
        $this->assertFalse(stripos($headers['Link'], 'rel="next"'));
846
847
        // With query params.
848
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
849
        $request->set_query_params(array( 'per_page' => 5, 'page' => 2 ));
850
        $response = $this->server->dispatch($request);
851
        $headers = $response->get_headers();
852
        $this->assertEquals(51, $headers['X-WP-Total']);
853
        $this->assertEquals(11, $headers['X-WP-TotalPages']);
854
        $prev_link = add_query_arg(
855
            array(
856
            'per_page' => 5,
857
            'page'     => 1,
858
            ), rest_url('/wp/v2/posts') 
859
        );
860
        $this->assertContains('<' . $prev_link . '>; rel="prev"', $headers['Link']);
861
        $next_link = add_query_arg(
862
            array(
863
            'per_page' => 5,
864
            'page'     => 3,
865
            ), rest_url('/wp/v2/posts') 
866
        );
867
        $this->assertContains('<' . $next_link . '>; rel="next"', $headers['Link']);
868
    }
869
870 View Code Duplication
    public function test_get_items_private_status_query_var() 
871
    {
872
        // Private query vars inaccessible to unauthorized users
873
        wp_set_current_user(0);
874
        $draft_id = $this->factory->post->create(array( 'post_status' => 'draft' ));
875
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
876
        $request->set_param('status', 'draft');
877
        $response = $this->server->dispatch($request);
878
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
879
880
        // But they are accessible to authorized users
881
        wp_set_current_user(self::$editor_id);
882
        $response = $this->server->dispatch($request);
883
        $data = $response->get_data();
884
        $this->assertCount(1, $data);
885
        $this->assertEquals($draft_id, $data[0]['id']);
886
    }
887
888
    public function test_get_items_invalid_per_page() 
889
    {
890
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
891
        $request->set_query_params(array( 'per_page' => -1 ));
892
        $response = $this->server->dispatch($request);
893
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
894
    }
895
896 View Code Duplication
    public function test_get_items_invalid_context() 
897
    {
898
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
899
        $request->set_param('context', 'banana');
900
        $response = $this->server->dispatch($request);
901
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
902
    }
903
904
    public function test_get_items_invalid_date() 
905
    {
906
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
907
        $request->set_param('after', rand_str());
908
        $request->set_param('before', rand_str());
909
        $response = $this->server->dispatch($request);
910
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
911
    }
912
913
    public function test_get_items_valid_date() 
914
    {
915
        $post1 = $this->factory->post->create(array( 'post_date' => '2016-01-15T00:00:00Z' ));
916
        $post2 = $this->factory->post->create(array( 'post_date' => '2016-01-16T00:00:00Z' ));
917
        $post3 = $this->factory->post->create(array( 'post_date' => '2016-01-17T00:00:00Z' ));
918
919
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
920
        $request->set_param('after', '2016-01-15T00:00:00Z');
921
        $request->set_param('before', '2016-01-17T00:00:00Z');
922
        $response = $this->server->dispatch($request);
923
        $data = $response->get_data();
924
        $this->assertCount(1, $data);
925
        $this->assertEquals($post2, $data[0]['id']);
926
    }
927
928
    public function test_get_item() 
929
    {
930
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', self::$post_id));
931
        $response = $this->server->dispatch($request);
932
933
        $this->check_get_post_response($response, 'view');
934
    }
935
936
    public function test_get_item_links() 
937
    {
938
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', self::$post_id));
939
        $response = $this->server->dispatch($request);
940
941
        $links = $response->get_links();
942
943
        $this->assertEquals(rest_url('/wp/v2/posts/' . self::$post_id), $links['self'][0]['href']);
944
        $this->assertEquals(rest_url('/wp/v2/posts'), $links['collection'][0]['href']);
945
946
        $this->assertEquals(rest_url('/wp/v2/types/' . get_post_type(self::$post_id)), $links['about'][0]['href']);
947
948
        $replies_url = rest_url('/wp/v2/comments');
949
        $replies_url = add_query_arg('post', self::$post_id, $replies_url);
950
        $this->assertEquals($replies_url, $links['replies'][0]['href']);
951
952
        $this->assertEquals(rest_url('/wp/v2/posts/' . self::$post_id . '/revisions'), $links['version-history'][0]['href']);
953
954
        $attachments_url = rest_url('/wp/v2/media');
955
        $attachments_url = add_query_arg('parent', self::$post_id, $attachments_url);
956
        $this->assertEquals($attachments_url, $links['https://api.w.org/attachment'][0]['href']);
957
958
        $term_links = $links['https://api.w.org/term'];
959
        $tag_link = $cat_link = $format_link = null;
960
        foreach ( $term_links as $link ) {
961
            if ('post_tag' === $link['attributes']['taxonomy'] ) {
962
                $tag_link = $link;
963
            } elseif ('category' === $link['attributes']['taxonomy'] ) {
964
                $cat_link = $link;
965
            } elseif ('post_format' === $link['attributes']['taxonomy'] ) {
966
                $format_link = $link;
967
            }
968
        }
969
        $this->assertNotEmpty($tag_link);
970
        $this->assertNotEmpty($cat_link);
971
        $this->assertNull($format_link);
972
973
        $tags_url = add_query_arg('post', self::$post_id, rest_url('/wp/v2/tags'));
974
        $this->assertEquals($tags_url, $tag_link['href']);
975
976
        $category_url = add_query_arg('post', self::$post_id, rest_url('/wp/v2/categories'));
977
        $this->assertEquals($category_url, $cat_link['href']);
978
    }
979
980
    public function test_get_item_links_no_author() 
981
    {
982
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', self::$post_id));
983
        $response = $this->server->dispatch($request);
984
        $links = $response->get_links();
985
        $this->assertFalse(isset($links['author']));
986
        wp_update_post(array( 'ID' => self::$post_id, 'post_author' => self::$author_id ));
987
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', self::$post_id));
988
        $response = $this->server->dispatch($request);
989
        $links = $response->get_links();
990
        $this->assertEquals(rest_url('/wp/v2/users/' . self::$author_id), $links['author'][0]['href']);
991
    }
992
993
    public function test_get_post_without_permission() 
994
    {
995
        $draft_id = $this->factory->post->create(
996
            array(
997
            'post_status' => 'draft',
998
            ) 
999
        );
1000
        wp_set_current_user(0);
1001
1002
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', $draft_id));
1003
        $response = $this->server->dispatch($request);
1004
1005
        $this->assertErrorResponse('rest_forbidden', $response, 403);
1006
    }
1007
1008 View Code Duplication
    public function test_get_post_invalid_id() 
1009
    {
1010
        $request = new WP_REST_Request('GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER);
1011
        $response = $this->server->dispatch($request);
1012
1013
        $this->assertErrorResponse('rest_post_invalid_id', $response, 404);
1014
    }
1015
1016
    public function test_get_post_list_context_with_permission() 
1017
    {
1018
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
1019
        $request->set_query_params(
1020
            array(
1021
            'context' => 'edit',
1022
            ) 
1023
        );
1024
1025
        wp_set_current_user(self::$editor_id);
1026
1027
        $response = $this->server->dispatch($request);
1028
1029
        $this->check_get_posts_response($response, 'edit');
1030
    }
1031
1032 View Code Duplication
    public function test_get_post_list_context_without_permission() 
1033
    {
1034
        wp_set_current_user(0);
1035
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
1036
        $request->set_query_params(
1037
            array(
1038
            'context' => 'edit',
1039
            ) 
1040
        );
1041
        $response = $this->server->dispatch($request);
1042
1043
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
1044
    }
1045
1046 View Code Duplication
    public function test_get_post_context_without_permission() 
1047
    {
1048
        wp_set_current_user(0);
1049
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', self::$post_id));
1050
        $request->set_query_params(
1051
            array(
1052
            'context' => 'edit',
1053
            ) 
1054
        );
1055
        $response = $this->server->dispatch($request);
1056
1057
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
1058
    }
1059
1060 View Code Duplication
    public function test_get_post_with_password() 
1061
    {
1062
        $post_id = $this->factory->post->create(
1063
            array(
1064
            'post_password' => '$inthebananastand',
1065
            ) 
1066
        );
1067
1068
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', $post_id));
1069
        $response = $this->server->dispatch($request);
1070
1071
        $this->check_get_post_response($response, 'view');
1072
1073
        $data = $response->get_data();
1074
        $this->assertEquals('', $data['content']['rendered']);
1075
        $this->assertTrue($data['content']['protected']);
1076
        $this->assertEquals('', $data['excerpt']['rendered']);
1077
        $this->assertTrue($data['excerpt']['protected']);
1078
    }
1079
1080 View Code Duplication
    public function test_get_post_with_password_using_password() 
1081
    {
1082
        $post_id = $this->factory->post->create(
1083
            array(
1084
            'post_password' => '$inthebananastand',
1085
            'post_content'  => 'Some secret content.',
1086
            'post_excerpt'  => 'Some secret excerpt.',
1087
            ) 
1088
        );
1089
1090
        $post = get_post($post_id);
1091
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', $post_id));
1092
        $request->set_param('password', '$inthebananastand');
1093
        $response = $this->server->dispatch($request);
1094
1095
        $this->check_get_post_response($response, 'view');
1096
1097
        $data = $response->get_data();
1098
        $this->assertEquals(wpautop($post->post_content), $data['content']['rendered']);
1099
        $this->assertTrue($data['content']['protected']);
1100
        $this->assertEquals(wpautop($post->post_excerpt), $data['excerpt']['rendered']);
1101
        $this->assertTrue($data['excerpt']['protected']);
1102
    }
1103
1104 View Code Duplication
    public function test_get_post_with_password_using_incorrect_password() 
1105
    {
1106
        $post_id = $this->factory->post->create(
1107
            array(
1108
            'post_password' => '$inthebananastand',
1109
            ) 
1110
        );
1111
1112
        $post = get_post($post_id);
1113
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', $post_id));
1114
        $request->set_param('password', 'wrongpassword');
1115
        $response = $this->server->dispatch($request);
1116
1117
        $this->assertErrorResponse('rest_post_incorrect_password', $response, 403);
1118
    }
1119
1120 View Code Duplication
    public function test_get_post_with_password_without_permission() 
1121
    {
1122
        $post_id = $this->factory->post->create(
1123
            array(
1124
            'post_password' => '$inthebananastand',
1125
            'post_content'  => 'Some secret content.',
1126
            'post_excerpt'  => 'Some secret excerpt.',
1127
            ) 
1128
        );
1129
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', $post_id));
1130
        $response = $this->server->dispatch($request);
1131
        $data = $response->get_data();
1132
        $this->check_get_post_response($response, 'view');
1133
        $this->assertEquals('', $data['content']['rendered']);
1134
        $this->assertTrue($data['content']['protected']);
1135
        $this->assertEquals('', $data['excerpt']['rendered']);
1136
        $this->assertTrue($data['excerpt']['protected']);
1137
    }
1138
1139
    public function test_get_item_read_permission_custom_post_status() 
1140
    {
1141
        register_post_status('testpubstatus', array( 'public' => true ));
1142
        register_post_status('testprivtatus', array( 'public' => false ));
1143
        // Public status
1144
        wp_update_post(array( 'ID' => self::$post_id, 'post_status' => 'testpubstatus' ));
1145
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', self::$post_id));
1146
        $response = $this->server->dispatch($request);
1147
        $this->assertEquals(200, $response->get_status());
1148
        // Private status
1149
        wp_update_post(array( 'ID' => self::$post_id, 'post_status' => 'testprivtatus' ));
1150
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', self::$post_id));
1151
        $response = $this->server->dispatch($request);
1152
        $this->assertEquals(403, $response->get_status());
1153
    }
1154
1155
    public function test_prepare_item() 
1156
    {
1157
        wp_set_current_user(self::$editor_id);
1158
1159
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', self::$post_id));
1160
        $request->set_query_params(array( 'context' => 'edit' ));
1161
        $response = $this->server->dispatch($request);
1162
1163
        $this->check_get_post_response($response, 'edit');
1164
    }
1165
1166
    public function test_create_item() 
1167
    {
1168
        wp_set_current_user(self::$editor_id);
1169
1170
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1171
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1172
        $params = $this->set_post_data();
1173
        $request->set_body_params($params);
1174
        $response = $this->server->dispatch($request);
1175
1176
        $this->check_create_post_response($response);
1177
    }
1178
1179
    /**
1180
     * @ticket 38698
1181
     */
1182
    public function test_create_item_with_template() 
1183
    {
1184
        wp_set_current_user(self::$editor_id);
1185
        add_filter('theme_post_templates', array( $this, 'filter_theme_post_templates' ));
1186
1187
        // reregister the route as we now have a template available.
1188
        $GLOBALS['wp_rest_server']->override_by_default = true;
1189
        $controller = new WP_REST_Posts_Controller('post');
1190
        $controller->register_routes();
1191
        $GLOBALS['wp_rest_server']->override_by_default = false;
1192
1193
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1194
        $params = $this->set_post_data(
1195
            array(
1196
            'template' => 'post-my-test-template.php',
1197
            ) 
1198
        );
1199
        $request->set_body_params($params);
1200
        $response = $this->server->dispatch($request);
1201
1202
        $data = $response->get_data();
1203
        $post_template = get_page_template_slug(get_post($data['id']));
1204
1205
        remove_filter('theme_post_templates', array( $this, 'filter_theme_post_templates' ));
1206
1207
        $this->assertEquals('post-my-test-template.php', $data['template']);
1208
        $this->assertEquals('post-my-test-template.php', $post_template);
1209
    }
1210
1211
    /**
1212
     * @ticket 38698
1213
     */
1214 View Code Duplication
    public function test_create_item_with_template_none_available() 
1215
    {
1216
        wp_set_current_user(self::$editor_id);
1217
1218
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1219
        $params = $this->set_post_data(
1220
            array(
1221
            'template' => 'post-my-test-template.php',
1222
            ) 
1223
        );
1224
        $request->set_body_params($params);
1225
        $response = $this->server->dispatch($request);
1226
1227
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1228
    }
1229
1230
    /**
1231
     * @ticket 38877
1232
     */
1233
    public function test_create_item_with_template_none() 
1234
    {
1235
        wp_set_current_user(self::$editor_id);
1236
        add_filter('theme_post_templates', array( $this, 'filter_theme_post_templates' ));
1237
        update_post_meta(self::$post_id, '_wp_page_template', 'post-my-test-template.php');
1238
1239
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
1240
        $params = $this->set_post_data(
1241
            array(
1242
            'template' => '',
1243
            ) 
1244
        );
1245
        $request->set_body_params($params);
1246
        $response = $this->server->dispatch($request);
1247
1248
        $data = $response->get_data();
1249
        $post_template = get_page_template_slug(get_post($data['id']));
1250
1251
        $this->assertEquals('', $data['template']);
1252
        $this->assertEquals('', $post_template);
1253
    }
1254
1255
    public function test_rest_create_item() 
1256
    {
1257
        wp_set_current_user(self::$editor_id);
1258
1259
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1260
        $request->add_header('content-type', 'application/json');
1261
        $params = $this->set_post_data();
1262
        $request->set_body(wp_json_encode($params));
1263
        $response = $this->server->dispatch($request);
1264
1265
        $this->check_create_post_response($response);
1266
    }
1267
1268 View Code Duplication
    public function test_create_post_invalid_id() 
1269
    {
1270
        wp_set_current_user(self::$editor_id);
1271
1272
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1273
        $params = $this->set_post_data(
1274
            array(
1275
            'id' => '3',
1276
            ) 
1277
        );
1278
        $request->set_body_params($params);
1279
        $response = $this->server->dispatch($request);
1280
1281
        $this->assertErrorResponse('rest_post_exists', $response, 400);
1282
    }
1283
1284
    public function test_create_post_as_contributor() 
1285
    {
1286
        wp_set_current_user(self::$contributor_id);
1287
1288
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1289
        $params = $this->set_post_data(
1290
            array(
1291
            'status' => 'pending',
1292
            )
1293
        );
1294
1295
        $request->set_body_params($params);
1296
        $response = $this->server->dispatch($request);
1297
        $this->check_create_post_response($response);
1298
    }
1299
1300
    public function test_create_post_sticky() 
1301
    {
1302
        wp_set_current_user(self::$editor_id);
1303
1304
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1305
        $params = $this->set_post_data(
1306
            array(
1307
            'sticky' => true,
1308
            ) 
1309
        );
1310
        $request->set_body_params($params);
1311
        $response = $this->server->dispatch($request);
1312
1313
        $new_data = $response->get_data();
1314
        $this->assertEquals(true, $new_data['sticky']);
1315
        $post = get_post($new_data['id']);
1316
        $this->assertEquals(true, is_sticky($post->ID));
1317
    }
1318
1319
    public function test_create_post_sticky_as_contributor() 
1320
    {
1321
        wp_set_current_user(self::$contributor_id);
1322
1323
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1324
        $params = $this->set_post_data(
1325
            array(
1326
            'sticky' => true,
1327
            'status' => 'pending',
1328
            ) 
1329
        );
1330
        $request->set_body_params($params);
1331
        $response = $this->server->dispatch($request);
1332
1333
        $this->assertErrorResponse('rest_cannot_assign_sticky', $response, 403);
1334
    }
1335
1336 View Code Duplication
    public function test_create_post_other_author_without_permission() 
1337
    {
1338
        wp_set_current_user(self::$author_id);
1339
1340
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1341
        $params = $this->set_post_data(
1342
            array(
1343
            'author' => self::$editor_id,
1344
            )
1345
        );
1346
        $request->set_body_params($params);
1347
        $response = $this->server->dispatch($request);
1348
1349
        $this->assertErrorResponse('rest_cannot_edit_others', $response, 403);
1350
    }
1351
1352
    public function test_create_post_without_permission() 
1353
    {
1354
        wp_set_current_user(0);
1355
1356
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1357
        $params = $this->set_post_data(
1358
            array(
1359
            'status' => 'draft',
1360
            ) 
1361
        );
1362
        $request->set_body_params($params);
1363
        $response = $this->server->dispatch($request);
1364
1365
        $this->assertErrorResponse('rest_cannot_create', $response, 401);
1366
    }
1367
1368 View Code Duplication
    public function test_create_post_draft() 
1369
    {
1370
        wp_set_current_user(self::$editor_id);
1371
1372
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1373
        $params = $this->set_post_data(
1374
            array(
1375
            'status' => 'draft',
1376
            ) 
1377
        );
1378
        $request->set_body_params($params);
1379
        $response = $this->server->dispatch($request);
1380
1381
        $data = $response->get_data();
1382
        $new_post = get_post($data['id']);
1383
        $this->assertEquals('draft', $data['status']);
1384
        $this->assertEquals('draft', $new_post->post_status);
1385
        // Confirm dates are null
1386
        $this->assertNull($data['date_gmt']);
1387
        $this->assertNull($data['modified_gmt']);
1388
    }
1389
1390
    public function test_create_post_private() 
1391
    {
1392
        wp_set_current_user(self::$editor_id);
1393
1394
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1395
        $params = $this->set_post_data(
1396
            array(
1397
            'status' => 'private',
1398
            ) 
1399
        );
1400
        $request->set_body_params($params);
1401
        $response = $this->server->dispatch($request);
1402
1403
        $data = $response->get_data();
1404
        $new_post = get_post($data['id']);
1405
        $this->assertEquals('private', $data['status']);
1406
        $this->assertEquals('private', $new_post->post_status);
1407
    }
1408
1409 View Code Duplication
    public function test_create_post_private_without_permission() 
1410
    {
1411
        wp_set_current_user(self::$author_id);
1412
        $user = wp_get_current_user();
1413
        $user->add_cap('publish_posts', false);
1414
        // Flush capabilities, https://core.trac.wordpress.org/ticket/28374
1415
        $user->get_role_caps();
1416
        $user->update_user_level_from_caps();
1417
1418
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1419
        $params = $this->set_post_data(
1420
            array(
1421
            'status' => 'private',
1422
            'author' => self::$author_id,
1423
            ) 
1424
        );
1425
        $request->set_body_params($params);
1426
        $response = $this->server->dispatch($request);
1427
1428
        $this->assertErrorResponse('rest_cannot_publish', $response, 403);
1429
    }
1430
1431 View Code Duplication
    public function test_create_post_publish_without_permission() 
1432
    {
1433
        wp_set_current_user(self::$author_id);
1434
        $user = wp_get_current_user();
1435
        $user->add_cap('publish_posts', false);
1436
        // Flush capabilities, https://core.trac.wordpress.org/ticket/28374
1437
        $user->get_role_caps();
1438
        $user->update_user_level_from_caps();
1439
1440
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1441
        $params = $this->set_post_data(
1442
            array(
1443
            'status' => 'publish',
1444
            ) 
1445
        );
1446
        $request->set_body_params($params);
1447
        $response = $this->server->dispatch($request);
1448
1449
        $this->assertErrorResponse('rest_cannot_publish', $response, 403);
1450
    }
1451
1452 View Code Duplication
    public function test_create_post_invalid_status() 
1453
    {
1454
        wp_set_current_user(self::$editor_id);
1455
1456
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1457
        $params = $this->set_post_data(
1458
            array(
1459
            'status' => 'teststatus',
1460
            ) 
1461
        );
1462
        $request->set_body_params($params);
1463
        $response = $this->server->dispatch($request);
1464
1465
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1466
    }
1467
1468 View Code Duplication
    public function test_create_post_with_format() 
1469
    {
1470
        wp_set_current_user(self::$editor_id);
1471
1472
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1473
        $params = $this->set_post_data(
1474
            array(
1475
            'format' => 'gallery',
1476
            ) 
1477
        );
1478
        $request->set_body_params($params);
1479
        $response = $this->server->dispatch($request);
1480
1481
        $data = $response->get_data();
1482
        $new_post = get_post($data['id']);
1483
        $this->assertEquals('gallery', $data['format']);
1484
        $this->assertEquals('gallery', get_post_format($new_post->ID));
1485
    }
1486
1487 View Code Duplication
    public function test_create_post_with_standard_format() 
1488
    {
1489
        wp_set_current_user(self::$editor_id);
1490
1491
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1492
        $params = $this->set_post_data(
1493
            array(
1494
            'format' => 'standard',
1495
            ) 
1496
        );
1497
        $request->set_body_params($params);
1498
        $response = $this->server->dispatch($request);
1499
1500
        $data = $response->get_data();
1501
        $new_post = get_post($data['id']);
1502
        $this->assertEquals('standard', $data['format']);
1503
        $this->assertFalse(get_post_format($new_post->ID));
1504
    }
1505
1506 View Code Duplication
    public function test_create_post_with_invalid_format() 
1507
    {
1508
        wp_set_current_user(self::$editor_id);
1509
1510
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1511
        $params = $this->set_post_data(
1512
            array(
1513
            'format' => 'testformat',
1514
            ) 
1515
        );
1516
        $request->set_body_params($params);
1517
        $response = $this->server->dispatch($request);
1518
1519
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1520
    }
1521
1522
    /**
1523
     * Test with a valid format, but one unsupported by the theme.
1524
     *
1525
     * https://core.trac.wordpress.org/ticket/38610
1526
     */
1527 View Code Duplication
    public function test_create_post_with_unsupported_format() 
1528
    {
1529
        wp_set_current_user(self::$editor_id);
1530
1531
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1532
        $params = $this->set_post_data(
1533
            array(
1534
            'format' => 'link',
1535
            ) 
1536
        );
1537
        $request->set_body_params($params);
1538
        $response = $this->server->dispatch($request);
1539
1540
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1541
    }
1542
1543
    public function test_create_update_post_with_featured_media() 
1544
    {
1545
1546
        $file = DIR_TESTDATA . '/images/canola.jpg';
1547
        $this->attachment_id = $this->factory->attachment->create_object(
1548
            $file, 0, array(
1549
            'post_mime_type' => 'image/jpeg',
1550
            'menu_order' => rand(1, 100),
1551
            ) 
1552
        );
1553
1554
        wp_set_current_user(self::$editor_id);
1555
1556
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1557
        $params = $this->set_post_data(
1558
            array(
1559
            'featured_media' => $this->attachment_id,
1560
            ) 
1561
        );
1562
        $request->set_body_params($params);
1563
        $response = $this->server->dispatch($request);
1564
        $data = $response->get_data();
1565
        $new_post = get_post($data['id']);
1566
        $this->assertEquals($this->attachment_id, $data['featured_media']);
1567
        $this->assertEquals($this->attachment_id, (int) get_post_thumbnail_id($new_post->ID));
1568
1569
        $request = new WP_REST_Request('POST', '/wp/v2/posts/' . $new_post->ID);
1570
        $params = $this->set_post_data(
1571
            array(
1572
            'featured_media' => 0,
1573
            ) 
1574
        );
1575
        $request->set_body_params($params);
1576
        $response = $this->server->dispatch($request);
1577
        $data = $response->get_data();
1578
        $this->assertEquals(0, $data['featured_media']);
1579
        $this->assertEquals(0, (int) get_post_thumbnail_id($new_post->ID));
1580
    }
1581
1582 View Code Duplication
    public function test_create_post_invalid_author() 
1583
    {
1584
        wp_set_current_user(self::$editor_id);
1585
1586
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1587
        $params = $this->set_post_data(
1588
            array(
1589
            'author' => -1,
1590
            ) 
1591
        );
1592
        $request->set_body_params($params);
1593
        $response = $this->server->dispatch($request);
1594
1595
        $this->assertErrorResponse('rest_invalid_author', $response, 400);
1596
    }
1597
1598 View Code Duplication
    public function test_create_post_invalid_author_without_permission() 
1599
    {
1600
        wp_set_current_user(self::$author_id);
1601
1602
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1603
        $params = $this->set_post_data(
1604
            array(
1605
            'author' => self::$editor_id,
1606
            ) 
1607
        );
1608
        $request->set_body_params($params);
1609
        $response = $this->server->dispatch($request);
1610
1611
        $this->assertErrorResponse('rest_cannot_edit_others', $response, 403);
1612
    }
1613
1614 View Code Duplication
    public function test_create_post_with_password() 
1615
    {
1616
        wp_set_current_user(self::$editor_id);
1617
1618
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1619
        $params = $this->set_post_data(
1620
            array(
1621
            'password' => 'testing',
1622
            ) 
1623
        );
1624
        $request->set_body_params($params);
1625
        $response = $this->server->dispatch($request);
1626
1627
        $data = $response->get_data();
1628
        $this->assertEquals('testing', $data['password']);
1629
    }
1630
1631 View Code Duplication
    public function test_create_post_with_falsy_password() 
1632
    {
1633
        wp_set_current_user(self::$editor_id);
1634
1635
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1636
        $params = $this->set_post_data(
1637
            array(
1638
            'password' => '0',
1639
            ) 
1640
        );
1641
        $request->set_body_params($params);
1642
        $response = $this->server->dispatch($request);
1643
1644
        $data = $response->get_data();
1645
1646
        $this->assertEquals('0', $data['password']);
1647
    }
1648
1649 View Code Duplication
    public function test_create_post_with_empty_string_password_and_sticky() 
1650
    {
1651
        wp_set_current_user(self::$editor_id);
1652
1653
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1654
        $params = $this->set_post_data(
1655
            array(
1656
            'password' => '',
1657
            'sticky'   => true,
1658
            ) 
1659
        );
1660
        $request->set_body_params($params);
1661
        $response = $this->server->dispatch($request);
1662
1663
        $this->assertEquals(201, $response->get_status());
1664
        $data = $response->get_data();
1665
        $this->assertEquals('', $data['password']);
1666
    }
1667
1668
    public function test_create_post_with_password_and_sticky_fails() 
1669
    {
1670
        wp_set_current_user(self::$editor_id);
1671
1672
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1673
        $params = $this->set_post_data(
1674
            array(
1675
            'password' => '123',
1676
            'sticky'   => true,
1677
            ) 
1678
        );
1679
        $request->set_body_params($params);
1680
        $response = $this->server->dispatch($request);
1681
1682
        $this->assertErrorResponse('rest_invalid_field', $response, 400);
1683
    }
1684
1685
    public function test_create_post_custom_date() 
1686
    {
1687
        wp_set_current_user(self::$editor_id);
1688
1689
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1690
        $params = $this->set_post_data(
1691
            array(
1692
            'date' => '2010-01-01T02:00:00Z',
1693
            ) 
1694
        );
1695
        $request->set_body_params($params);
1696
        $response = $this->server->dispatch($request);
1697
1698
        $data = $response->get_data();
1699
        $new_post = get_post($data['id']);
1700
        $time = gmmktime(2, 0, 0, 1, 1, 2010);
1701
        $this->assertEquals('2010-01-01T02:00:00', $data['date']);
1702
        $this->assertEquals($time, strtotime($new_post->post_date));
1703
    }
1704
1705
    public function test_create_post_custom_date_with_timezone() 
1706
    {
1707
        wp_set_current_user(self::$editor_id);
1708
1709
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1710
        $params = $this->set_post_data(
1711
            array(
1712
            'date' => '2010-01-01T02:00:00-10:00',
1713
            ) 
1714
        );
1715
        $request->set_body_params($params);
1716
        $response = $this->server->dispatch($request);
1717
1718
        $data = $response->get_data();
1719
        $new_post = get_post($data['id']);
1720
        $time = gmmktime(12, 0, 0, 1, 1, 2010);
1721
1722
        $this->assertEquals('2010-01-01T12:00:00', $data['date']);
1723
        $this->assertEquals('2010-01-01T12:00:00', $data['modified']);
1724
1725
        $this->assertEquals($time, strtotime($new_post->post_date));
1726
        $this->assertEquals($time, strtotime($new_post->post_modified));
1727
    }
1728
1729 View Code Duplication
    public function test_create_post_with_db_error() 
1730
    {
1731
        wp_set_current_user(self::$editor_id);
1732
1733
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1734
        $params  = $this->set_post_data(array());
1735
        $request->set_body_params($params);
1736
1737
        /**
1738
         * Disable showing error as the below is going to intentionally
1739
         * trigger a DB error.
1740
         */
1741
        global $wpdb;
1742
        $wpdb->suppress_errors = true;
1743
        add_filter('query', array( $this, 'error_insert_query' ));
1744
1745
        $response = $this->server->dispatch($request);
1746
        remove_filter('query', array( $this, 'error_insert_query' ));
1747
        $wpdb->show_errors = true;
1748
1749
        $this->assertErrorResponse('db_insert_error', $response, 500);
1750
    }
1751
1752 View Code Duplication
    public function test_create_post_with_invalid_date() 
1753
    {
1754
        wp_set_current_user(self::$editor_id);
1755
1756
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1757
        $params = $this->set_post_data(
1758
            array(
1759
            'date' => '2010-60-01T02:00:00Z',
1760
            ) 
1761
        );
1762
        $request->set_body_params($params);
1763
        $response = $this->server->dispatch($request);
1764
1765
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1766
    }
1767
1768 View Code Duplication
    public function test_create_post_with_invalid_date_gmt() 
1769
    {
1770
        wp_set_current_user(self::$editor_id);
1771
1772
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1773
        $params = $this->set_post_data(
1774
            array(
1775
            'date_gmt' => '2010-60-01T02:00:00',
1776
            ) 
1777
        );
1778
        $request->set_body_params($params);
1779
        $response = $this->server->dispatch($request);
1780
1781
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1782
    }
1783
1784 View Code Duplication
    public function test_create_post_with_quotes_in_title() 
1785
    {
1786
        wp_set_current_user(self::$editor_id);
1787
1788
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1789
        $params = $this->set_post_data(
1790
            array(
1791
            'title' => "Rob O'Rourke's Diary",
1792
            ) 
1793
        );
1794
        $request->set_body_params($params);
1795
        $response = $this->server->dispatch($request);
1796
        $new_data = $response->get_data();
1797
        $this->assertEquals("Rob O'Rourke's Diary", $new_data['title']['raw']);
1798
    }
1799
1800
    public function test_create_post_with_categories() 
1801
    {
1802
        wp_set_current_user(self::$editor_id);
1803
        $category = wp_insert_term('Test Category', 'category');
1804
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1805
        $params = $this->set_post_data(
1806
            array(
1807
            'password'   => 'testing',
1808
            'categories' => array(
1809
            $category['term_id']
1810
            ),
1811
            ) 
1812
        );
1813
        $request->set_body_params($params);
1814
        $response = $this->server->dispatch($request);
1815
1816
        $data = $response->get_data();
1817
        $this->assertEquals(array( $category['term_id'] ), $data['categories']);
1818
    }
1819
1820
    public function test_create_post_with_categories_as_csv() 
1821
    {
1822
        wp_set_current_user(self::$editor_id);
1823
        $category = wp_insert_term('Chicken', 'category');
1824
        $category2 = wp_insert_term('Ribs', 'category');
1825
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1826
        $params = $this->set_post_data(
1827
            array(
1828
            'categories' => $category['term_id'] . ',' . $category2['term_id'],
1829
            ) 
1830
        );
1831
        $request->set_body_params($params);
1832
        $response = $this->server->dispatch($request);
1833
1834
        $data = $response->get_data();
1835
        $this->assertEquals(array( $category['term_id'], $category2['term_id'] ), $data['categories']);
1836
    }
1837
1838
    public function test_create_post_with_invalid_categories() 
1839
    {
1840
        wp_set_current_user(self::$editor_id);
1841
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1842
        $params = $this->set_post_data(
1843
            array(
1844
            'password'   => 'testing',
1845
            'categories' => array(
1846
            REST_TESTS_IMPOSSIBLY_HIGH_NUMBER
1847
            ),
1848
            ) 
1849
        );
1850
        $request->set_body_params($params);
1851
        $response = $this->server->dispatch($request);
1852
1853
        $data = $response->get_data();
1854
        $this->assertEquals(array(), $data['categories']);
1855
    }
1856
1857
    /**
1858
     * @ticket 38505
1859
     */
1860 View Code Duplication
    public function test_create_post_with_categories_that_cannot_be_assigned_by_current_user() 
1861
    {
1862
        $cats = self::factory()->category->create_many(2);
1863
        $this->forbidden_cat = $cats[1];
1864
1865
        wp_set_current_user(self::$editor_id);
1866
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
1867
        $params = $this->set_post_data(
1868
            array(
1869
            'password'   => 'testing',
1870
            'categories' => $cats,
1871
            ) 
1872
        );
1873
        $request->set_body_params($params);
1874
1875
        add_filter('map_meta_cap', array( $this, 'revoke_assign_term' ), 10, 4);
1876
        $response = $this->server->dispatch($request);
1877
        remove_filter('map_meta_cap', array( $this, 'revoke_assign_term' ), 10, 4);
1878
1879
        $this->assertErrorResponse('rest_cannot_assign_term', $response, 403);
1880
    }
1881
1882
    public function revoke_assign_term( $caps, $cap, $user_id, $args ) 
1883
    {
1884
        if ('assign_term' === $cap && isset($args[0]) && $this->forbidden_cat == $args[0] ) {
1885
            $caps = array( 'do_not_allow' );
1886
        }
1887
        return $caps;
1888
    }
1889
1890 View Code Duplication
    public function test_update_item() 
1891
    {
1892
        wp_set_current_user(self::$editor_id);
1893
1894
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
1895
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1896
        $params = $this->set_post_data();
1897
        $request->set_body_params($params);
1898
        $response = $this->server->dispatch($request);
1899
1900
        $this->check_update_post_response($response);
1901
        $new_data = $response->get_data();
1902
        $this->assertEquals(self::$post_id, $new_data['id']);
1903
        $this->assertEquals($params['title'], $new_data['title']['raw']);
1904
        $this->assertEquals($params['content'], $new_data['content']['raw']);
1905
        $this->assertEquals($params['excerpt'], $new_data['excerpt']['raw']);
1906
        $post = get_post(self::$post_id);
1907
        $this->assertEquals($params['title'], $post->post_title);
1908
        $this->assertEquals($params['content'], $post->post_content);
1909
        $this->assertEquals($params['excerpt'], $post->post_excerpt);
1910
    }
1911
1912
    public function test_update_item_no_change() 
1913
    {
1914
        wp_set_current_user(self::$editor_id);
1915
        $post = get_post(self::$post_id);
1916
1917
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
1918
        $request->set_param('author', $post->post_author);
1919
1920
        // Run twice to make sure that the update still succeeds even if no DB
1921
        // rows are updated.
1922
        $response = $this->server->dispatch($request);
1923
        $this->check_update_post_response($response);
1924
1925
        $response = $this->server->dispatch($request);
1926
        $this->check_update_post_response($response);
1927
    }
1928
1929 View Code Duplication
    public function test_rest_update_post() 
1930
    {
1931
        wp_set_current_user(self::$editor_id);
1932
1933
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
1934
        $request->add_header('content-type', 'application/json');
1935
        $params = $this->set_post_data();
1936
        $request->set_body(wp_json_encode($params));
1937
        $response = $this->server->dispatch($request);
1938
1939
        $this->check_update_post_response($response);
1940
        $new_data = $response->get_data();
1941
        $this->assertEquals(self::$post_id, $new_data['id']);
1942
        $this->assertEquals($params['title'], $new_data['title']['raw']);
1943
        $this->assertEquals($params['content'], $new_data['content']['raw']);
1944
        $this->assertEquals($params['excerpt'], $new_data['excerpt']['raw']);
1945
        $post = get_post(self::$post_id);
1946
        $this->assertEquals($params['title'], $post->post_title);
1947
        $this->assertEquals($params['content'], $post->post_content);
1948
        $this->assertEquals($params['excerpt'], $post->post_excerpt);
1949
    }
1950
1951
    public function test_rest_update_post_raw() 
1952
    {
1953
        wp_set_current_user(self::$editor_id);
1954
1955
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
1956
        $request->add_header('content-type', 'application/json');
1957
        $params = $this->set_raw_post_data();
1958
        $request->set_body(wp_json_encode($params));
1959
        $response = $this->server->dispatch($request);
1960
1961
        $this->check_update_post_response($response);
1962
        $new_data = $response->get_data();
1963
        $this->assertEquals(self::$post_id, $new_data['id']);
1964
        $this->assertEquals($params['title']['raw'], $new_data['title']['raw']);
1965
        $this->assertEquals($params['content']['raw'], $new_data['content']['raw']);
1966
        $this->assertEquals($params['excerpt']['raw'], $new_data['excerpt']['raw']);
1967
        $post = get_post(self::$post_id);
1968
        $this->assertEquals($params['title']['raw'], $post->post_title);
1969
        $this->assertEquals($params['content']['raw'], $post->post_content);
1970
        $this->assertEquals($params['excerpt']['raw'], $post->post_excerpt);
1971
    }
1972
1973
    public function test_update_post_without_extra_params() 
1974
    {
1975
        wp_set_current_user(self::$editor_id);
1976
1977
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
1978
        $params = $this->set_post_data();
1979
        unset($params['type']);
1980
        unset($params['name']);
1981
        unset($params['author']);
1982
        unset($params['status']);
1983
        $request->set_body_params($params);
1984
        $response = $this->server->dispatch($request);
1985
1986
        $this->check_update_post_response($response);
1987
    }
1988
1989
    public function test_update_post_without_permission() 
1990
    {
1991
        wp_set_current_user(self::$editor_id);
1992
        $user = wp_get_current_user();
1993
        $user->add_cap('edit_published_posts', false);
1994
        // Flush capabilities, https://core.trac.wordpress.org/ticket/28374
1995
        $user->get_role_caps();
1996
        $user->update_user_level_from_caps();
1997
1998
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
1999
        $params = $this->set_post_data();
2000
        $request->set_body_params($params);
2001
        $response = $this->server->dispatch($request);
2002
2003
        $this->assertErrorResponse('rest_cannot_edit', $response, 403);
2004
    }
2005
2006 View Code Duplication
    public function test_update_post_sticky_as_contributor() 
2007
    {
2008
        wp_set_current_user(self::$contributor_id);
2009
2010
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2011
        $params = $this->set_post_data(
2012
            array(
2013
            'sticky' => true,
2014
            'status' => 'pending',
2015
            ) 
2016
        );
2017
        $request->set_body_params($params);
2018
        $response = $this->server->dispatch($request);
2019
2020
        $this->assertErrorResponse('rest_cannot_edit', $response, 403);
2021
    }
2022
2023
    public function test_update_post_invalid_id() 
2024
    {
2025
        wp_set_current_user(self::$editor_id);
2026
2027
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER));
2028
        $response = $this->server->dispatch($request);
2029
2030
        $this->assertErrorResponse('rest_post_invalid_id', $response, 404);
2031
    }
2032
2033
    public function test_update_post_invalid_route() 
2034
    {
2035
        wp_set_current_user(self::$editor_id);
2036
2037
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/pages/%d', self::$post_id));
2038
        $response = $this->server->dispatch($request);
2039
2040
        $this->assertErrorResponse('rest_post_invalid_id', $response, 404);
2041
    }
2042
2043 View Code Duplication
    public function test_update_post_with_format() 
2044
    {
2045
        wp_set_current_user(self::$editor_id);
2046
2047
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2048
        $params = $this->set_post_data(
2049
            array(
2050
            'format' => 'gallery',
2051
            ) 
2052
        );
2053
        $request->set_body_params($params);
2054
        $response = $this->server->dispatch($request);
2055
2056
        $data = $response->get_data();
2057
        $new_post = get_post($data['id']);
2058
        $this->assertEquals('gallery', $data['format']);
2059
        $this->assertEquals('gallery', get_post_format($new_post->ID));
2060
    }
2061
2062 View Code Duplication
    public function test_update_post_with_standard_format() 
2063
    {
2064
        wp_set_current_user(self::$editor_id);
2065
2066
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2067
        $params = $this->set_post_data(
2068
            array(
2069
            'format' => 'standard',
2070
            ) 
2071
        );
2072
        $request->set_body_params($params);
2073
        $response = $this->server->dispatch($request);
2074
2075
        $data = $response->get_data();
2076
        $new_post = get_post($data['id']);
2077
        $this->assertEquals('standard', $data['format']);
2078
        $this->assertFalse(get_post_format($new_post->ID));
2079
    }
2080
2081 View Code Duplication
    public function test_update_post_with_invalid_format() 
2082
    {
2083
        wp_set_current_user(self::$editor_id);
2084
2085
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2086
        $params = $this->set_post_data(
2087
            array(
2088
            'format' => 'testformat',
2089
            ) 
2090
        );
2091
        $request->set_body_params($params);
2092
        $response = $this->server->dispatch($request);
2093
2094
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
2095
    }
2096
2097
    /**
2098
     * Test with a valid format, but one unsupported by the theme.
2099
     *
2100
     * https://core.trac.wordpress.org/ticket/38610
2101
     */
2102 View Code Duplication
    public function test_update_post_with_unsupported_format() 
2103
    {
2104
        wp_set_current_user(self::$editor_id);
2105
2106
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2107
        $params = $this->set_post_data(
2108
            array(
2109
            'format' => 'link',
2110
            ) 
2111
        );
2112
        $request->set_body_params($params);
2113
        $response = $this->server->dispatch($request);
2114
2115
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
2116
    }
2117
2118
    public function test_update_post_ignore_readonly() 
2119
    {
2120
        wp_set_current_user(self::$editor_id);
2121
2122
        $new_content = rand_str();
2123
        $expected_modified = current_time('mysql');
2124
2125
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2126
        $params = $this->set_post_data(
2127
            array(
2128
            'modified' => '2010-06-01T02:00:00Z',
2129
            'content'  => $new_content,
2130
            ) 
2131
        );
2132
        $request->set_body_params($params);
2133
        $response = $this->server->dispatch($request);
2134
2135
        // The readonly modified param should be ignored, request should be a success.
2136
        $data = $response->get_data();
2137
        $new_post = get_post($data['id']);
2138
2139
        $this->assertEquals($new_content, $data['content']['raw']);
2140
        $this->assertEquals($new_content, $new_post->post_content);
2141
2142
        // The modified date should equal the current time.
2143
        $this->assertEquals(date('Y-m-d', strtotime(mysql_to_rfc3339($expected_modified))), date('Y-m-d', strtotime($data['modified'])));
2144
        $this->assertEquals(date('Y-m-d', strtotime($expected_modified)), date('Y-m-d', strtotime($new_post->post_modified)));
2145
    }
2146
2147 View Code Duplication
    public function test_update_post_with_invalid_date() 
2148
    {
2149
        wp_set_current_user(self::$editor_id);
2150
2151
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2152
        $params = $this->set_post_data(
2153
            array(
2154
            'date' => rand_str(),
2155
            ) 
2156
        );
2157
        $request->set_body_params($params);
2158
        $response = $this->server->dispatch($request);
2159
2160
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
2161
    }
2162
2163 View Code Duplication
    public function test_update_post_with_invalid_date_gmt() 
2164
    {
2165
        wp_set_current_user(self::$editor_id);
2166
2167
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2168
        $params = $this->set_post_data(
2169
            array(
2170
            'date_gmt' => rand_str(),
2171
            ) 
2172
        );
2173
        $request->set_body_params($params);
2174
        $response = $this->server->dispatch($request);
2175
2176
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
2177
    }
2178
2179 View Code Duplication
    public function test_update_post_slug() 
2180
    {
2181
        wp_set_current_user(self::$editor_id);
2182
2183
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2184
        $params = $this->set_post_data(
2185
            array(
2186
            'slug' => 'sample-slug',
2187
            ) 
2188
        );
2189
        $request->set_body_params($params);
2190
        $response = $this->server->dispatch($request);
2191
2192
        $new_data = $response->get_data();
2193
        $this->assertEquals('sample-slug', $new_data['slug']);
2194
        $post = get_post($new_data['id']);
2195
        $this->assertEquals('sample-slug', $post->post_name);
2196
    }
2197
2198 View Code Duplication
    public function test_update_post_slug_accented_chars() 
2199
    {
2200
        wp_set_current_user(self::$editor_id);
2201
2202
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2203
        $params = $this->set_post_data(
2204
            array(
2205
            'slug' => 'tęst-acceńted-chäræcters',
2206
            ) 
2207
        );
2208
        $request->set_body_params($params);
2209
        $response = $this->server->dispatch($request);
2210
2211
        $new_data = $response->get_data();
2212
        $this->assertEquals('test-accented-charaecters', $new_data['slug']);
2213
        $post = get_post($new_data['id']);
2214
        $this->assertEquals('test-accented-charaecters', $post->post_name);
2215
    }
2216
2217
    public function test_update_post_sticky() 
2218
    {
2219
        wp_set_current_user(self::$editor_id);
2220
2221
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2222
        $params = $this->set_post_data(
2223
            array(
2224
            'sticky' => true,
2225
            ) 
2226
        );
2227
        $request->set_body_params($params);
2228
        $response = $this->server->dispatch($request);
2229
2230
        $new_data = $response->get_data();
2231
        $this->assertEquals(true, $new_data['sticky']);
2232
        $post = get_post($new_data['id']);
2233
        $this->assertEquals(true, is_sticky($post->ID));
2234
2235
        // Updating another field shouldn't change sticky status
2236
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2237
        $params = $this->set_post_data(
2238
            array(
2239
            'title'       => 'This should not reset sticky',
2240
            ) 
2241
        );
2242
        $request->set_body_params($params);
2243
        $response = $this->server->dispatch($request);
2244
2245
        $new_data = $response->get_data();
2246
        $this->assertEquals(true, $new_data['sticky']);
2247
        $post = get_post($new_data['id']);
2248
        $this->assertEquals(true, is_sticky($post->ID));
2249
    }
2250
2251
    public function test_update_post_excerpt() 
2252
    {
2253
        wp_set_current_user(self::$editor_id);
2254
2255
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2256
        $request->set_body_params(
2257
            array(
2258
            'excerpt' => 'An Excerpt',
2259
            ) 
2260
        );
2261
2262
        $response = $this->server->dispatch($request);
2263
        $new_data = $response->get_data();
2264
        $this->assertEquals('An Excerpt', $new_data['excerpt']['raw']);
2265
    }
2266
2267
    public function test_update_post_empty_excerpt() 
2268
    {
2269
        wp_set_current_user(self::$editor_id);
2270
2271
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2272
        $request->set_body_params(
2273
            array(
2274
            'excerpt' => '',
2275
            ) 
2276
        );
2277
2278
        $response = $this->server->dispatch($request);
2279
        $new_data = $response->get_data();
2280
        $this->assertEquals('', $new_data['excerpt']['raw']);
2281
    }
2282
2283
    public function test_update_post_content() 
2284
    {
2285
        wp_set_current_user(self::$editor_id);
2286
2287
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2288
        $request->set_body_params(
2289
            array(
2290
            'content' => 'Some Content',
2291
            ) 
2292
        );
2293
2294
        $response = $this->server->dispatch($request);
2295
        $new_data = $response->get_data();
2296
        $this->assertEquals('Some Content', $new_data['content']['raw']);
2297
    }
2298
2299
    public function test_update_post_empty_content() 
2300
    {
2301
        wp_set_current_user(self::$editor_id);
2302
2303
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2304
        $request->set_body_params(
2305
            array(
2306
            'content' => '',
2307
            ) 
2308
        );
2309
2310
        $response = $this->server->dispatch($request);
2311
        $new_data = $response->get_data();
2312
        $this->assertEquals('', $new_data['content']['raw']);
2313
    }
2314
2315
    public function test_update_post_with_empty_password() 
2316
    {
2317
        wp_set_current_user(self::$editor_id);
2318
        wp_update_post(
2319
            array(
2320
            'ID'            => self::$post_id,
2321
            'post_password' => 'foo',
2322
            ) 
2323
        );
2324
2325
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2326
        $params = $this->set_post_data(
2327
            array(
2328
            'password' => '',
2329
            ) 
2330
        );
2331
        $request->set_body_params($params);
2332
        $response = $this->server->dispatch($request);
2333
        $data = $response->get_data();
2334
        $this->assertEquals('', $data['password']);
2335
    }
2336
2337 View Code Duplication
    public function test_update_post_with_password_and_sticky_fails() 
2338
    {
2339
        wp_set_current_user(self::$editor_id);
2340
2341
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2342
        $params = $this->set_post_data(
2343
            array(
2344
            'password' => '123',
2345
            'sticky'   => true,
2346
            ) 
2347
        );
2348
        $request->set_body_params($params);
2349
        $response = $this->server->dispatch($request);
2350
2351
        $this->assertErrorResponse('rest_invalid_field', $response, 400);
2352
    }
2353
2354 View Code Duplication
    public function test_update_stick_post_with_password_fails() 
2355
    {
2356
        wp_set_current_user(self::$editor_id);
2357
2358
        stick_post(self::$post_id);
2359
2360
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2361
        $params = $this->set_post_data(
2362
            array(
2363
            'password' => '123',
2364
            ) 
2365
        );
2366
        $request->set_body_params($params);
2367
        $response = $this->server->dispatch($request);
2368
2369
        $this->assertErrorResponse('rest_invalid_field', $response, 400);
2370
    }
2371
2372 View Code Duplication
    public function test_update_password_protected_post_with_sticky_fails() 
2373
    {
2374
        wp_set_current_user(self::$editor_id);
2375
2376
        wp_update_post(array( 'ID' => self::$post_id, 'post_password' => '123' ));
2377
2378
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2379
        $params = $this->set_post_data(
2380
            array(
2381
            'sticky' => true,
2382
            ) 
2383
        );
2384
        $request->set_body_params($params);
2385
        $response = $this->server->dispatch($request);
2386
2387
        $this->assertErrorResponse('rest_invalid_field', $response, 400);
2388
    }
2389
2390 View Code Duplication
    public function test_update_post_with_quotes_in_title() 
2391
    {
2392
        wp_set_current_user(self::$editor_id);
2393
2394
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2395
        $params = $this->set_post_data(
2396
            array(
2397
            'title' => "Rob O'Rourke's Diary",
2398
            ) 
2399
        );
2400
        $request->set_body_params($params);
2401
        $response = $this->server->dispatch($request);
2402
        $new_data = $response->get_data();
2403
        $this->assertEquals("Rob O'Rourke's Diary", $new_data['title']['raw']);
2404
    }
2405
2406
    public function test_update_post_with_categories() 
2407
    {
2408
2409
        wp_set_current_user(self::$editor_id);
2410
        $category = wp_insert_term('Test Category', 'category');
2411
2412
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2413
        $params = $this->set_post_data(
2414
            array(
2415
            'title' => 'Tester',
2416
            'categories' => array(
2417
            $category['term_id'],
2418
            ),
2419
            ) 
2420
        );
2421
        $request->set_body_params($params);
2422
        $response = $this->server->dispatch($request);
2423
        $new_data = $response->get_data();
2424
        $this->assertEquals(array( $category['term_id'] ), $new_data['categories']);
2425
        $categories_path = '';
2426
        $links = $response->get_links();
2427
        foreach ( $links['https://api.w.org/term'] as $link ) {
2428
            if ('category' === $link['attributes']['taxonomy'] ) {
2429
                $categories_path = $link['href'];
2430
            }
2431
        }
2432
        $query = parse_url($categories_path, PHP_URL_QUERY);
2433
        parse_str($query, $args);
2434
        $request = new WP_REST_Request('GET', $args['rest_route']);
2435
        unset($args['rest_route']);
2436
        $request->set_query_params($args);
2437
        $response = $this->server->dispatch($request);
2438
        $data = $response->get_data();
2439
        $this->assertCount(1, $data);
2440
        $this->assertEquals('Test Category', $data[0]['name']);
2441
    }
2442
2443
    public function test_update_post_with_empty_categories() 
2444
    {
2445
2446
        wp_set_current_user(self::$editor_id);
2447
        $category = wp_insert_term('Test Category', 'category');
2448
        wp_set_object_terms(self::$post_id, $category['term_id'], 'category');
2449
2450
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2451
        $params = $this->set_post_data(
2452
            array(
2453
            'title' => 'Tester',
2454
            'categories' => array(),
2455
            ) 
2456
        );
2457
        $request->set_body_params($params);
2458
        $response = $this->server->dispatch($request);
2459
        $new_data = $response->get_data();
2460
        $this->assertEquals(array(), $new_data['categories']);
2461
    }
2462
2463
    /**
2464
     * @ticket 38505
2465
     */
2466 View Code Duplication
    public function test_update_post_with_categories_that_cannot_be_assigned_by_current_user() 
2467
    {
2468
        $cats = self::factory()->category->create_many(2);
2469
        $this->forbidden_cat = $cats[1];
2470
2471
        wp_set_current_user(self::$editor_id);
2472
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2473
        $params = $this->set_post_data(
2474
            array(
2475
            'password'   => 'testing',
2476
            'categories' => $cats,
2477
            ) 
2478
        );
2479
        $request->set_body_params($params);
2480
2481
        add_filter('map_meta_cap', array( $this, 'revoke_assign_term' ), 10, 4);
2482
        $response = $this->server->dispatch($request);
2483
        remove_filter('map_meta_cap', array( $this, 'revoke_assign_term' ), 10, 4);
2484
2485
        $this->assertErrorResponse('rest_cannot_assign_term', $response, 403);
2486
    }
2487
2488
    /**
2489
     * @ticket 38698
2490
     */
2491
    public function test_update_item_with_template() 
2492
    {
2493
        wp_set_current_user(self::$editor_id);
2494
        add_filter('theme_post_templates', array( $this, 'filter_theme_post_templates' ));
2495
2496
        // reregister the route as we now have a template available.
2497
        $GLOBALS['wp_rest_server']->override_by_default = true;
2498
        $controller = new WP_REST_Posts_Controller('post');
2499
        $controller->register_routes();
2500
        $GLOBALS['wp_rest_server']->override_by_default = false;
2501
2502
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2503
        $params = $this->set_post_data(
2504
            array(
2505
            'template' => 'post-my-test-template.php',
2506
            ) 
2507
        );
2508
        $request->set_body_params($params);
2509
        $response = $this->server->dispatch($request);
2510
2511
        $data = $response->get_data();
2512
        $post_template = get_page_template_slug(get_post($data['id']));
2513
2514
        $this->assertEquals('post-my-test-template.php', $data['template']);
2515
        $this->assertEquals('post-my-test-template.php', $post_template);
2516
    }
2517
2518
    /**
2519
     * @ticket 38877
2520
     */
2521
    public function test_update_item_with_template_none() 
2522
    {
2523
        wp_set_current_user(self::$editor_id);
2524
        add_filter('theme_post_templates', array( $this, 'filter_theme_post_templates' ));
2525
        update_post_meta(self::$post_id, '_wp_page_template', 'post-my-test-template.php');
2526
2527
        // reregister the route as we now have a template available.
2528
        $GLOBALS['wp_rest_server']->override_by_default = true;
2529
        $controller = new WP_REST_Posts_Controller('post');
2530
        $controller->register_routes();
2531
        $GLOBALS['wp_rest_server']->override_by_default = false;
2532
2533
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
2534
        $params = $this->set_post_data(
2535
            array(
2536
            'template' => '',
2537
            ) 
2538
        );
2539
        $request->set_body_params($params);
2540
        $response = $this->server->dispatch($request);
2541
2542
        $data = $response->get_data();
2543
        $post_template = get_page_template_slug(get_post($data['id']));
2544
2545
        $this->assertEquals('', $data['template']);
2546
        $this->assertEquals('', $post_template);
2547
    }
2548
2549
2550
    public function verify_post_roundtrip( $input = array(), $expected_output = array() ) 
2551
    {
2552
        // Create the post
2553
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
2554
        foreach ( $input as $name => $value ) {
2555
            $request->set_param($name, $value);
2556
        }
2557
        $response = $this->server->dispatch($request);
2558
        $this->assertEquals(201, $response->get_status());
2559
        $actual_output = $response->get_data();
2560
2561
        // Compare expected API output to actual API output
2562
        $this->assertEquals($expected_output['title']['raw'], $actual_output['title']['raw']);
2563
        $this->assertEquals($expected_output['title']['rendered'], trim($actual_output['title']['rendered']));
2564
        $this->assertEquals($expected_output['content']['raw'], $actual_output['content']['raw']);
2565
        $this->assertEquals($expected_output['content']['rendered'], trim($actual_output['content']['rendered']));
2566
        $this->assertEquals($expected_output['excerpt']['raw'], $actual_output['excerpt']['raw']);
2567
        $this->assertEquals($expected_output['excerpt']['rendered'], trim($actual_output['excerpt']['rendered']));
2568
2569
        // Compare expected API output to WP internal values
2570
        $post = get_post($actual_output['id']);
2571
        $this->assertEquals($expected_output['title']['raw'], $post->post_title);
2572
        $this->assertEquals($expected_output['content']['raw'], $post->post_content);
2573
        $this->assertEquals($expected_output['excerpt']['raw'], $post->post_excerpt);
2574
2575
        // Update the post
2576
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', $actual_output['id']));
2577
        foreach ( $input as $name => $value ) {
2578
            $request->set_param($name, $value);
2579
        }
2580
        $response = $this->server->dispatch($request);
2581
        $this->assertEquals(200, $response->get_status());
2582
        $actual_output = $response->get_data();
2583
2584
        // Compare expected API output to actual API output
2585
        $this->assertEquals($expected_output['title']['raw'], $actual_output['title']['raw']);
2586
        $this->assertEquals($expected_output['title']['rendered'], trim($actual_output['title']['rendered']));
2587
        $this->assertEquals($expected_output['content']['raw'], $actual_output['content']['raw']);
2588
        $this->assertEquals($expected_output['content']['rendered'], trim($actual_output['content']['rendered']));
2589
        $this->assertEquals($expected_output['excerpt']['raw'], $actual_output['excerpt']['raw']);
2590
        $this->assertEquals($expected_output['excerpt']['rendered'], trim($actual_output['excerpt']['rendered']));
2591
2592
        // Compare expected API output to WP internal values
2593
        $post = get_post($actual_output['id']);
2594
        $this->assertEquals($expected_output['title']['raw'], $post->post_title);
2595
        $this->assertEquals($expected_output['content']['raw'], $post->post_content);
2596
        $this->assertEquals($expected_output['excerpt']['raw'], $post->post_excerpt);
2597
    }
2598
2599 View Code Duplication
    public static function post_roundtrip_provider() 
2600
    {
2601
        return array(
2602
         array(
2603
          // Raw values.
2604
          array(
2605
        'title'   => '\o/ ¯\_(ツ)_/¯',
2606
        'content' => '\o/ ¯\_(ツ)_/¯',
2607
        'excerpt' => '\o/ ¯\_(ツ)_/¯',
2608
          ),
2609
          // Expected returned values.
2610
          array(
2611
           'title' => array(
2612
         'raw'      => '\o/ ¯\_(ツ)_/¯',
2613
         'rendered' => '\o/ ¯\_(ツ)_/¯',
2614
           ),
2615
           'content' => array(
2616
            'raw'      => '\o/ ¯\_(ツ)_/¯',
2617
            'rendered' => '<p>\o/ ¯\_(ツ)_/¯</p>',
2618
           ),
2619
           'excerpt' => array(
2620
            'raw'      => '\o/ ¯\_(ツ)_/¯',
2621
            'rendered' => '<p>\o/ ¯\_(ツ)_/¯</p>',
2622
           ),
2623
          )
2624
         ),
2625
         array(
2626
          // Raw values.
2627
          array(
2628
        'title'   => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
2629
        'content' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
2630
        'excerpt' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
2631
          ),
2632
          // Expected returned values.
2633
          array(
2634
           'title' => array(
2635
         'raw'      => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
2636
         'rendered' => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
2637
           ),
2638
           'content' => array(
2639
            'raw'      => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
2640
            'rendered' => '<p>\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;</p>',
2641
           ),
2642
           'excerpt' => array(
2643
            'raw'      => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
2644
            'rendered' => '<p>\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;</p>',
2645
           ),
2646
          ),
2647
         ),
2648
         array(
2649
          // Raw values.
2650
          array(
2651
        'title'   => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2652
        'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2653
        'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2654
          ),
2655
          // Expected returned values.
2656
          array(
2657
           'title' => array(
2658
         'raw'      => 'div <strong>strong</strong> oh noes',
2659
         'rendered' => 'div <strong>strong</strong> oh noes',
2660
           ),
2661
           'content' => array(
2662
            'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
2663
            'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
2664
           ),
2665
           'excerpt' => array(
2666
            'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
2667
            'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
2668
           ),
2669
          )
2670
         ),
2671
         array(
2672
          // Raw values.
2673
          array(
2674
        'title'   => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
2675
        'content' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
2676
        'excerpt' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
2677
          ),
2678
          // Expected returned values.
2679
          array(
2680
           'title' => array(
2681
         'raw'      => '<a href="#">link</a>',
2682
         'rendered' => '<a href="#">link</a>',
2683
           ),
2684
           'content' => array(
2685
            'raw'      => '<a href="#" target="_blank">link</a>',
2686
            'rendered' => '<p><a href="#" target="_blank">link</a></p>',
2687
           ),
2688
           'excerpt' => array(
2689
            'raw'      => '<a href="#" target="_blank">link</a>',
2690
            'rendered' => '<p><a href="#" target="_blank">link</a></p>',
2691
           ),
2692
          )
2693
         ),
2694
        );
2695
    }
2696
2697
    /**
2698
     * @dataProvider post_roundtrip_provider
2699
     */
2700
    public function test_post_roundtrip_as_author( $raw, $expected ) 
2701
    {
2702
        wp_set_current_user(self::$author_id);
2703
        $this->assertFalse(current_user_can('unfiltered_html'));
2704
        $this->verify_post_roundtrip($raw, $expected);
2705
    }
2706
2707 View Code Duplication
    public function test_post_roundtrip_as_editor_unfiltered_html() 
2708
    {
2709
        wp_set_current_user(self::$editor_id);
2710
        if (is_multisite() ) {
2711
            $this->assertFalse(current_user_can('unfiltered_html'));
2712
            $this->verify_post_roundtrip(
2713
                array(
2714
                'title'   => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2715
                'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2716
                'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2717
                ), array(
2718
                'title' => array(
2719
                'raw'      => 'div <strong>strong</strong> oh noes',
2720
                'rendered' => 'div <strong>strong</strong> oh noes',
2721
                ),
2722
                'content' => array(
2723
                'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
2724
                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
2725
                ),
2726
                'excerpt' => array(
2727
                'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
2728
                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
2729
                ),
2730
                ) 
2731
            );
2732
        } else {
2733
            $this->assertTrue(current_user_can('unfiltered_html'));
2734
            $this->verify_post_roundtrip(
2735
                array(
2736
                'title'   => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2737
                'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2738
                'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2739
                ), array(
2740
                'title' => array(
2741
                'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2742
                'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2743
                ),
2744
                'content' => array(
2745
                'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2746
                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
2747
                ),
2748
                'excerpt' => array(
2749
                'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2750
                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
2751
                ),
2752
                ) 
2753
            );
2754
        }
2755
    }
2756
2757 View Code Duplication
    public function test_post_roundtrip_as_superadmin_unfiltered_html() 
2758
    {
2759
        wp_set_current_user(self::$superadmin_id);
2760
        $this->assertTrue(current_user_can('unfiltered_html'));
2761
        $this->verify_post_roundtrip(
2762
            array(
2763
            'title'   => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2764
            'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2765
            'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2766
            ), array(
2767
            'title' => array(
2768
            'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2769
            'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2770
            ),
2771
            'content' => array(
2772
            'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2773
            'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
2774
            ),
2775
            'excerpt' => array(
2776
            'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
2777
            'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
2778
            ),
2779
            ) 
2780
        );
2781
    }
2782
2783 View Code Duplication
    public function test_delete_item() 
2784
    {
2785
        $post_id = $this->factory->post->create(array( 'post_title' => 'Deleted post' ));
2786
        wp_set_current_user(self::$editor_id);
2787
2788
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/posts/%d', $post_id));
2789
        $request->set_param('force', 'false');
2790
        $response = $this->server->dispatch($request);
2791
2792
        $this->assertNotInstanceOf('WP_Error', $response);
2793
        $this->assertEquals(200, $response->get_status());
2794
        $data = $response->get_data();
2795
        $this->assertEquals('Deleted post', $data['title']['raw']);
2796
        $this->assertEquals('trash', $data['status']);
2797
    }
2798
2799 View Code Duplication
    public function test_delete_item_skip_trash() 
2800
    {
2801
        $post_id = $this->factory->post->create(array( 'post_title' => 'Deleted post' ));
2802
        wp_set_current_user(self::$editor_id);
2803
2804
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/posts/%d', $post_id));
2805
        $request['force'] = true;
2806
        $response = $this->server->dispatch($request);
2807
2808
        $this->assertNotInstanceOf('WP_Error', $response);
2809
        $this->assertEquals(200, $response->get_status());
2810
        $data = $response->get_data();
2811
        $this->assertTrue($data['deleted']);
2812
        $this->assertNotEmpty($data['previous']);
2813
    }
2814
2815
    public function test_delete_item_already_trashed() 
2816
    {
2817
        $post_id = $this->factory->post->create(array( 'post_title' => 'Deleted post' ));
2818
        wp_set_current_user(self::$editor_id);
2819
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/posts/%d', $post_id));
2820
        $response = $this->server->dispatch($request);
2821
        $this->assertEquals(200, $response->get_status());
2822
        $response = $this->server->dispatch($request);
2823
        $this->assertErrorResponse('rest_already_trashed', $response, 410);
2824
    }
2825
2826
    public function test_delete_post_invalid_id() 
2827
    {
2828
        wp_set_current_user(self::$editor_id);
2829
2830
        $request = new WP_REST_Request('DELETE', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER);
2831
        $response = $this->server->dispatch($request);
2832
2833
        $this->assertErrorResponse('rest_post_invalid_id', $response, 404);
2834
    }
2835
2836
    public function test_delete_post_invalid_post_type() 
2837
    {
2838
        $page_id = $this->factory->post->create(array( 'post_type' => 'page' ));
2839
        wp_set_current_user(self::$editor_id);
2840
2841
        $request = new WP_REST_Request('DELETE', '/wp/v2/posts/' . $page_id);
2842
        $response = $this->server->dispatch($request);
2843
2844
        $this->assertErrorResponse('rest_post_invalid_id', $response, 404);
2845
    }
2846
2847
    public function test_delete_post_without_permission() 
2848
    {
2849
        wp_set_current_user(self::$author_id);
2850
2851
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/posts/%d', self::$post_id));
2852
        $response = $this->server->dispatch($request);
2853
2854
        $this->assertErrorResponse('rest_cannot_delete', $response, 403);
2855
    }
2856
2857
    public function test_register_post_type_invalid_controller() 
2858
    {
2859
2860
        register_post_type('invalid-controller', array( 'show_in_rest' => true, 'rest_controller_class' => 'Fake_Class_Baba' ));
2861
        create_initial_rest_routes();
2862
        $routes = $this->server->get_routes();
2863
        $this->assertFalse(isset($routes['/wp/v2/invalid-controller']));
2864
        _unregister_post_type('invalid-controller');
2865
2866
    }
2867
2868
    public function test_get_item_schema() 
2869
    {
2870
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/posts');
2871
        $response = $this->server->dispatch($request);
2872
        $data = $response->get_data();
2873
        $properties = $data['schema']['properties'];
2874
        $this->assertEquals(24, count($properties));
2875
        $this->assertArrayHasKey('author', $properties);
2876
        $this->assertArrayHasKey('comment_status', $properties);
2877
        $this->assertArrayHasKey('content', $properties);
2878
        $this->assertArrayHasKey('date', $properties);
2879
        $this->assertArrayHasKey('date_gmt', $properties);
2880
        $this->assertArrayHasKey('excerpt', $properties);
2881
        $this->assertArrayHasKey('featured_media', $properties);
2882
        $this->assertArrayHasKey('guid', $properties);
2883
        $this->assertArrayHasKey('format', $properties);
2884
        $this->assertArrayHasKey('id', $properties);
2885
        $this->assertArrayHasKey('link', $properties);
2886
        $this->assertArrayHasKey('meta', $properties);
2887
        $this->assertArrayHasKey('modified', $properties);
2888
        $this->assertArrayHasKey('modified_gmt', $properties);
2889
        $this->assertArrayHasKey('password', $properties);
2890
        $this->assertArrayHasKey('ping_status', $properties);
2891
        $this->assertArrayHasKey('slug', $properties);
2892
        $this->assertArrayHasKey('status', $properties);
2893
        $this->assertArrayHasKey('sticky', $properties);
2894
        $this->assertArrayHasKey('template', $properties);
2895
        $this->assertArrayHasKey('title', $properties);
2896
        $this->assertArrayHasKey('type', $properties);
2897
        $this->assertArrayHasKey('tags', $properties);
2898
        $this->assertArrayHasKey('categories', $properties);
2899
    }
2900
2901
    public function test_status_array_enum_args() 
2902
    {
2903
        $request = new WP_REST_Request('GET', '/wp/v2');
2904
        $response = $this->server->dispatch($request);
2905
        $data = $response->get_data();
2906
        $list_posts_args = $data['routes']['/wp/v2/posts']['endpoints'][0]['args'];
2907
        $status_arg = $list_posts_args['status'];
2908
        $this->assertEquals('array', $status_arg['type']);
2909
        $this->assertEquals(
2910
            array(
2911
            'type' => 'string',
2912
            'enum' => array( 'publish', 'future', 'draft', 'pending', 'private', 'trash', 'auto-draft', 'inherit', 'any' ),
2913
            ), $status_arg['items'] 
2914
        );
2915
    }
2916
2917
    public function test_get_additional_field_registration() 
2918
    {
2919
2920
        $schema = array(
2921
         'type'        => 'integer',
2922
         'description' => 'Some integer of mine',
2923
         'enum'        => array( 1, 2, 3, 4 ),
2924
         'context'     => array( 'view', 'edit' ),
2925
        );
2926
2927
        register_rest_field(
2928
            'post', 'my_custom_int', array(
2929
            'schema'          => $schema,
2930
            'get_callback'    => array( $this, 'additional_field_get_callback' ),
2931
            'update_callback' => array( $this, 'additional_field_update_callback' ),
2932
            ) 
2933
        );
2934
2935
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/posts');
2936
2937
        $response = $this->server->dispatch($request);
2938
        $data = $response->get_data();
2939
2940
        $this->assertArrayHasKey('my_custom_int', $data['schema']['properties']);
2941
        $this->assertEquals($schema, $data['schema']['properties']['my_custom_int']);
2942
2943
        wp_set_current_user(1);
2944
2945
        $post_id = $this->factory->post->create();
2946
2947
        $request = new WP_REST_Request('GET', '/wp/v2/posts/' . $post_id);
2948
2949
        $response = $this->server->dispatch($request);
2950
        $this->assertArrayHasKey('my_custom_int', $response->data);
2951
2952
        $request = new WP_REST_Request('POST', '/wp/v2/posts/' . $post_id);
2953
        $request->set_body_params(
2954
            array(
2955
            'my_custom_int' => 123,
2956
            )
2957
        );
2958
2959
        $response = $this->server->dispatch($request);
2960
        $this->assertEquals(123, get_post_meta($post_id, 'my_custom_int', true));
2961
2962
        $request = new WP_REST_Request('POST', '/wp/v2/posts');
2963
        $request->set_body_params(
2964
            array(
2965
            'my_custom_int' => 123,
2966
            'title' => 'hello',
2967
            )
2968
        );
2969
2970
        $response = $this->server->dispatch($request);
2971
2972
        $this->assertEquals(123, $response->data['my_custom_int']);
2973
2974
        global $wp_rest_additional_fields;
2975
        $wp_rest_additional_fields = array();
2976
    }
2977
2978 View Code Duplication
    public function test_additional_field_update_errors() 
2979
    {
2980
        $schema = array(
2981
         'type'        => 'integer',
2982
         'description' => 'Some integer of mine',
2983
         'enum'        => array( 1, 2, 3, 4 ),
2984
         'context'     => array( 'view', 'edit' ),
2985
        );
2986
2987
        register_rest_field(
2988
            'post', 'my_custom_int', array(
2989
            'schema'          => $schema,
2990
            'get_callback'    => array( $this, 'additional_field_get_callback' ),
2991
            'update_callback' => array( $this, 'additional_field_update_callback' ),
2992
            ) 
2993
        );
2994
2995
        wp_set_current_user(self::$editor_id);
2996
        // Check for error on update.
2997
        $request = new WP_REST_Request('POST', sprintf('/wp/v2/posts/%d', self::$post_id));
2998
        $request->set_body_params(
2999
            array(
3000
            'my_custom_int' => 'returnError',
3001
            ) 
3002
        );
3003
3004
        $response = $this->server->dispatch($request);
3005
3006
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
3007
3008
        global $wp_rest_additional_fields;
3009
        $wp_rest_additional_fields = array();
3010
    }
3011
3012
    public function additional_field_get_callback( $object ) 
3013
    {
3014
        return get_post_meta($object['id'], 'my_custom_int', true);
3015
    }
3016
3017 View Code Duplication
    public function additional_field_update_callback( $value, $post ) 
3018
    {
3019
        if ('returnError' === $value ) {
3020
            return new WP_Error('rest_invalid_param', 'Testing an error.', array( 'status' => 400 ));
3021
        }
3022
        update_post_meta($post->ID, 'my_custom_int', $value);
3023
    }
3024
3025
    public function tearDown() 
0 ignored issues
show
Coding Style introduced by
The function name tearDown is in camel caps, but expected tear_down instead as per the coding standard.
Loading history...
3026
    {
3027
        _unregister_post_type('youseeeme');
3028
        if (isset($this->attachment_id) ) {
3029
            $this->remove_added_uploads();
3030
        }
3031
        parent::tearDown();
3032
    }
3033
3034
    /**
3035
     * Internal function used to disable an insert query which
3036
     * will trigger a wpdb error for testing purposes.
3037
     */
3038
    public function error_insert_query( $query ) 
3039
    {
3040
        if (strpos($query, 'INSERT') === 0 ) {
0 ignored issues
show
introduced by
Found "=== 0". Use Yoda Condition checks, you must
Loading history...
3041
            $query = '],';
3042
        }
3043
        return $query;
3044
    }
3045
3046
    public function filter_theme_post_templates( $post_templates ) 
3047
    {
3048
        return array(
3049
         'post-my-test-template.php' => 'My Test Template',
3050
        );
3051
    }
3052
}
3053