Make WordPress Core

Ticket #38693: rest_post_orderby.3.diff

File rest_post_orderby.3.diff, 10.4 KB (added by ChopinBach, 8 years ago)

Adds orderby params author, parent, none, and modified to the posts endpoint for the REST API. Unit tests revised to not rely upon secondary sorting by SQL. Line endings should be fixed as well.

Line 
1��Index: src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
2
3===================================================================
4
5--- src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php     (revision 39370)
6
7+++ src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php     (working copy)
8
9@@ -2038,12 +2038,16 @@
10
11                    'type' => 'string',
12
13                    'default' => 'date',
14
15                    'enum' => array(
16
17+                          'author',
18
19                           'date',
20
21-                          'relevance',
22
23                           'id',
24
25                           'include',
26
27+                          'modified',
28
29+                          'none',
30
31+                          'parent',
32
33+                          'relevance',
34
35+                          'slug',
36
37                           'title',
38
39-                          'slug',
40
41                    ),
42
43             );
44
45
46
47Index: tests/phpunit/tests/rest-api/rest-posts-controller.php
48
49===================================================================
50
51--- tests/phpunit/tests/rest-api/rest-posts-controller.php   (revision 39370)
52
53+++ tests/phpunit/tests/rest-api/rest-posts-controller.php   (working copy)
54
55@@ -227,6 +227,81 @@
56
57             $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
58
59      }
60
61
62
63+     public function test_get_items_orderby_author_query() {
64
65+            $id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$editor_id ) );
66
67+            $id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$editor_id ) );
68
69+            $id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$author_id ) );
70
71+
72
73+            $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
74
75+            $request->set_param( 'include', array( $id1, $id2, $id3 ) );
76
77+            $request->set_param( 'orderby', 'author' );
78
79+
80
81+            $response = $this->server->dispatch( $request );
82
83+            $data = $response->get_data();
84
85+
86
87+            $this->assertEquals( 200, $response->get_status() );
88
89+            $this->assertEquals( self::$author_id, $data[0]['author'] );
90
91+            $this->assertEquals( self::$editor_id, $data[1]['author'] );
92
93+            $this->assertEquals( self::$editor_id, $data[2]['author'] );
94
95+     }
96
97+
98
99+     public function test_get_items_orderby_modified_query() {
100
101+            $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
102
103+            $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
104
105+            $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
106
107+
108
109+            $this->update_post_modified( $id1, '2016-04-20 4:26:20' );
110
111+            $this->update_post_modified( $id2, '2016-02-01 20:24:02' );
112
113+            $this->update_post_modified( $id3, '2016-02-21 12:24:02' );
114
115+
116
117+            $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
118
119+            $request->set_param( 'include', array( $id1, $id2, $id3 ) );
120
121+            $request->set_param( 'orderby', 'modified' );
122
123+
124
125+            $response = $this->server->dispatch( $request );
126
127+            $data = $response->get_data();
128
129+
130
131+            $this->assertEquals( 200, $response->get_status() );
132
133+            $this->assertEquals( $id1, $data[0]['id'] );
134
135+            $this->assertEquals( $id3, $data[1]['id'] );
136
137+            $this->assertEquals( $id2, $data[2]['id'] );
138
139+     }
140
141+
142
143+     public function test_get_items_orderby_none_query() {
144
145+            $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
146
147+            $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
148
149+            $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
150
151+
152
153+            $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
154
155+            $request->set_param( 'include', array( $id1, $id2, $id3 ) );
156
157+            $request->set_param( 'orderby', 'none' );
158
159+
160
161+            $response = $this->server->dispatch( $request );
162
163+            $data = $response->get_data();
164
165+
166
167+            $this->assertEquals( 200, $response->get_status() );
168
169+     }
170
171+
172
173+     public function test_get_items_orderby_parent_query() {
174
175+            $id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page' ) );
176
177+            $id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page' ) );
178
179+            $id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page', 'post_parent' => $id1 ) );
180
181+
182
183+            $request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
184
185+            $request->set_param( 'include', array( $id1, $id2, $id3 ) );
186
187+            $request->set_param( 'orderby', 'parent' );
188
189+
190
191+            $response = $this->server->dispatch( $request );
192
193+            $data = $response->get_data();
194
195+
196
197+            $this->assertEquals( 200, $response->get_status() );
198
199+            $this->assertEquals( $id3, $data[0]['id'] );
200
201+            // Check ordering. Default ORDER is DESC.
202
203+            $this->assertEquals( $id1, $data[0]['parent'] );
204
205+            $this->assertEquals( 0, $data[1]['parent'] );
206
207+            $this->assertEquals( 0, $data[2]['parent'] );
208
209+     }
210
211+
212
213      public function test_get_items_exclude_query() {
214
215             $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
216
217             $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
218
219@@ -2647,4 +2722,28 @@
220
221                    'post-my-test-template.php' => 'My Test Template',
222
223             );
224
225      }
226
227+
228
229+     /**
230
231+      * There's no way to change post_modified through the API.
232
233+      */
234
235+     protected function update_post_modified( $post_id, $date ) {
236
237+            global $wpdb;
238
239+            return $wpdb->update(
240
241+                   $wpdb->posts,
242
243+                   array(
244
245+                          'post_modified' => $date,
246
247+                          'post_modified_gmt' => $date,
248
249+                   ),
250
251+                   array(
252
253+                          'ID' => $post_id,
254
255+                   ),
256
257+                   array(
258
259+                          '%s',
260
261+                          '%s',
262
263+                   ),
264
265+                   array(
266
267+                          '%d',
268
269+                   )
270
271+            );
272
273+     }
274
275 }
276
277