forked from WP-API/WP-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-json-comments-controller.php
More file actions
412 lines (326 loc) · 13.9 KB
/
Copy pathtest-json-comments-controller.php
File metadata and controls
412 lines (326 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/**
* Unit tests covering WP_JSON_Comments_Controller functionality.
*
* @package WordPress
* @subpackage JSON API
*/
class WP_Test_JSON_Comments_Controller extends WP_Test_JSON_Controller_Testcase {
protected $admin_id;
protected $subscriber_id;
protected $post_id;
protected $approved_id;
protected $hold_id;
protected $endpoint;
public function setUp() {
parent::setUp();
$this->admin_id = $this->factory->user->create( array(
'role' => 'administrator',
));
$this->subscriber_id = $this->factory->user->create( array(
'role' => 'subscriber',
));
$this->post_id = $this->factory->post->create();
$this->approved_id = $this->factory->comment->create( array(
'comment_approved' => 1,
'comment_post_ID' => $this->post_id,
'user_id' => 0,
));
$this->hold_id = $this->factory->comment->create( array(
'comment_approved' => 0,
'comment_post_ID' => $this->post_id,
'user_id' => $this->subscriber_id,
));
$this->endpoint = new WP_JSON_Comments_Controller;
}
public function tearDown() {
parent::tearDown();
}
public function test_register_routes() {
$routes = $this->server->get_routes();
$this->assertArrayHasKey( '/wp/comments', $routes );
$this->assertCount( 2, $routes['/wp/comments'] );
$this->assertArrayHasKey( '/wp/comments/(?P<id>[\d]+)', $routes );
$this->assertCount( 3, $routes['/wp/comments/(?P<id>[\d]+)'] );
}
public function test_get_items() {
$this->factory->comment->create_post_comments( $this->post_id, 6 );
$request = new WP_JSON_Request( 'GET', '/wp/comments' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$comments = $response->get_data();
// We created 6 comments in this method, plus $this->approved_id.
$this->assertCount( 7, $comments );
}
public function test_get_items_for_post() {
$second_post_id = $this->factory->post->create();
$this->factory->comment->create_post_comments( $second_post_id, 2 );
$request = new WP_JSON_Request( 'GET', '/wp/comments' );
$request->set_query_params( array(
'post' => $second_post_id,
) );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$comments = $response->get_data();
$this->assertCount( 2, $comments );
}
public function test_get_item() {
$request = new WP_JSON_Request( 'GET', sprintf( '/wp/comments/%d', $this->approved_id ) );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->check_comment_data( $data, 'view' );
}
public function test_prepare_item() {
$request = new WP_JSON_Request( 'GET', sprintf( '/wp/comments/%d', $this->approved_id ) );
$request->set_query_params( array(
'context' => 'edit',
) );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->check_comment_data( $data, 'edit' );
}
public function test_get_comment_invalid_id() {
$request = new WP_JSON_Request( 'GET', '/wp/comments/' . 100 );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'json_comment_invalid_id', $response, 404 );
}
public function test_get_comment_invalid_post_id() {
$comment_id = $this->factory->comment->create( array(
'comment_approved' => 1,
'comment_post_ID' => 100,
));
$request = new WP_JSON_Request( 'GET', '/wp/comments/' . $comment_id );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'json_post_invalid_id', $response, 404 );
}
public function test_get_comment_not_approved() {
wp_set_current_user( 0 );
$request = new WP_JSON_Request( 'GET', sprintf( '/wp/comments/%d', $this->hold_id ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'json_forbidden', $response, 403 );
}
public function test_get_comment_not_approved_same_user() {
wp_set_current_user( $this->subscriber_id );
$request = new WP_JSON_Request( 'GET', sprintf( '/wp/comments/%d', $this->hold_id ) );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
}
public function test_create_item() {
wp_set_current_user( 0 );
$params = array(
'post' => $this->post_id,
'author' => 'Comic Book Guy',
'author_email' => 'cbg@androidsdungeon.com',
'author_url' => 'http://androidsdungeon.com',
'content' => 'Worst Comment Ever!',
'date' => '2014-11-07T10:14:25',
);
$request = new WP_JSON_Request( 'POST', '/wp/comments' );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( json_encode( $params ) );
$response = $this->server->dispatch( $request );
$response = json_ensure_response( $response );
$this->assertEquals( 201, $response->get_status() );
$data = $response->get_data();
$this->check_comment_data( $data, 'edit' );
$this->assertEquals( 'hold', $data['status'] );
$this->assertEquals( '2014-11-07T10:14:25', $data['date'] );
}
public function test_create_comment_other_user() {
wp_set_current_user( $this->admin_id );
$params = array(
'post' => $this->post_id,
'author' => 'Homer Jay Simpson',
'author_email' => 'chunkylover53@aol.com',
'author_url' => 'http://compuglobalhypermeganet.com',
'content' => 'Here’s to alcohol: the cause of, and solution to, all of life’s problems.',
'user' => 0,
);
$request = new WP_JSON_Request( 'POST', '/wp/comments' );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( json_encode( $params ) );
$response = $this->server->dispatch( $request );
$response = json_ensure_response( $response );
$this->assertEquals( 201, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 0, $data['user'] );
}
public function test_create_item_duplicate() {
$this->markTestSkipped( 'Needs to be revisited after wp_die handling is added' );
$original_id = $this->factory->comment->create(
array(
'comment_post_ID' => $this->post_id,
'comment_author' => 'Guy N. Cognito',
'comment_author_email' => 'chunkylover53@aol.co.uk',
'comment_content' => 'Homer? Who is Homer? My name is Guy N. Cognito.',
)
);
wp_set_current_user( 0 );
$params = array(
'post' => $this->post_id,
'author' => 'Guy N. Cognito',
'author_email' => 'chunkylover53@aol.co.uk',
'content' => 'Homer? Who is Homer? My name is Guy N. Cognito.',
);
$request = new WP_JSON_Request( 'POST', '/wp/comments' );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( json_encode( $params ) );
$response = $this->server->dispatch( $request );
$response = json_ensure_response( $response );
$this->assertEquals( 409, $response->get_status() );
}
public function test_create_comment_closed() {
$post_id = $this->factory->post->create( array(
'comment_status' => 'closed',
));
wp_set_current_user( 0 );
$params = array(
'post' => $post_id,
);
$request = new WP_JSON_Request( 'POST', '/wp/comments' );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( json_encode( $params ) );
$response = $this->server->dispatch( $request );
$response = json_ensure_response( $response );
$this->assertEquals( 403, $response->get_status() );
}
public function test_update_item() {
wp_set_current_user( $this->admin_id );
$params = array(
'content' => "Disco Stu doesn't advertise.",
'author' => 'Disco Stu',
'author_url' => 'http://stusdisco.com',
'author_email' => 'stu@stusdisco.com',
'date' => '2014-11-07T10:14:25',
);
$request = new WP_JSON_Request( 'PUT', sprintf( '/wp/comments/%d', $this->approved_id ) );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( json_encode( $params ) );
$response = $this->server->dispatch( $request );
$response = json_ensure_response( $response );
$this->assertEquals( 201, $response->get_status() );
$comment = $response->get_data();
$updated = get_comment( $this->approved_id );
$this->assertEquals( $params['content'], $comment['content']['raw'] );
$this->assertEquals( $params['author'], $comment['author'] );
$this->assertEquals( $params['author_url'], $comment['author_url'] );
$this->assertEquals( $params['author_email'], $comment['author_email'] );
$this->assertEquals( json_mysql_to_rfc3339( $updated->comment_date ), $comment['date'] );
$this->assertEquals( '2014-11-07T10:14:25', $comment['date'] );
}
public function test_update_comment_status() {
wp_set_current_user( $this->admin_id );
$comment_id = $this->factory->comment->create( array(
'comment_approved' => 0,
'comment_post_ID' => $this->post_id,
));
$params = array(
'status' => 'approve',
);
$request = new WP_JSON_Request( 'PUT', sprintf( '/wp/comments/%d', $comment_id ) );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( json_encode( $params ) );
$response = $this->server->dispatch( $request );
$response = json_ensure_response( $response );
$this->assertEquals( 201, $response->get_status() );
$comment = $response->get_data();
$updated = get_comment( $comment_id );
$this->assertEquals( 'approved', $comment['status'] );
$this->assertEquals( 1, $updated->comment_approved );
}
public function test_update_comment_invalid_id() {
wp_set_current_user( 0 );
$params = array(
'content' => 'Oh, they have the internet on computers now!',
);
$request = new WP_JSON_Request( 'PUT', '/wp/comments/' . 100 );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( json_encode( $params ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'json_comment_invalid_id', $response, 404 );
}
public function test_update_comment_invalid_permission() {
wp_set_current_user( 0 );
$params = array(
'content' => 'Disco Stu likes disco music.',
);
$request = new WP_JSON_Request( 'PUT', sprintf( '/wp/comments/%d', $this->hold_id ) );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( json_encode( $params ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'json_forbidden', $response, 403 );
}
public function test_delete_item() {
wp_set_current_user( $this->admin_id );
$comment_id = $this->factory->comment->create( array(
'comment_approved' => 1,
'comment_post_ID' => $this->post_id,
'user_id' => $this->subscriber_id,
));
$request = new WP_JSON_Request( 'DELETE', sprintf( '/wp/comments/%d', $comment_id ) );
$response = $this->server->dispatch( $request );
$response = json_ensure_response( $response );
$this->assertEquals( 200, $response->get_status() );
}
public function test_delete_comment_invalid_id() {
wp_set_current_user( $this->admin_id );
$request = new WP_JSON_Request( 'DELETE', sprintf( '/wp/comments/%d', 100 ) );
$response = $this->server->dispatch( $request );
$response = json_ensure_response( $response );
$this->assertErrorResponse( 'json_comment_invalid_id', $response, 404 );
}
public function test_delete_comment_without_permission() {
wp_set_current_user( $this->subscriber_id );
$request = new WP_JSON_Request( 'DELETE', sprintf( '/wp/comments/%d', $this->approved_id ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'json_forbidden', $response, 403 );
}
public function test_get_item_schema() {
$request = new WP_JSON_Request( 'GET', '/wp/comments/schema' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['properties'];
$this->assertEquals( 12, count( $properties ) );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'author_email', $properties );
$this->assertArrayHasKey( 'author_url', $properties );
$this->assertArrayHasKey( 'content', $properties );
$this->assertArrayHasKey( 'date', $properties );
$this->assertArrayHasKey( 'link', $properties );
$this->assertArrayHasKey( 'parent', $properties );
$this->assertArrayHasKey( 'post', $properties );
$this->assertArrayHasKey( 'status', $properties );
$this->assertArrayHasKey( 'type', $properties );
$this->assertArrayHasKey( 'user', $properties );
}
protected function check_comment_data( $data, $context ) {
$comment = get_comment( $data['id'] );
$this->assertEquals( $comment->comment_ID, $data['id'] );
$this->assertEquals( $comment->comment_post_ID, $data['post'] );
$this->assertEquals( $comment->comment_parent, $data['parent'] );
$this->assertEquals( $comment->user_id, $data['user' ] );
$this->assertEquals( $comment->comment_author, $data['author'] );
$this->assertEquals( $comment->comment_author_email, $data['author_email'] );
$this->assertEquals( $comment->comment_author_url, $data['author_url'] );
$this->assertEquals( wpautop( $comment->comment_content ), $data['content']['rendered'] );
$this->assertEquals( json_mysql_to_rfc3339( $comment->comment_date ), $data['date'] );
$this->assertEquals( get_comment_link( $comment ), $data['link'] );
if ( 'edit' === $context ) {
$this->assertEquals( $comment->comment_author_IP, $data['author_ip'] );
$this->assertEquals( $comment->comment_agent, $data['author_user_agent'] );
$this->assertEquals( json_mysql_to_rfc3339( $comment->comment_date_gmt ), $data['date_gmt'] );
$this->assertEquals( $comment->comment_content, $data['content']['raw'] );
$this->assertEquals( $comment->comment_karma, $data['karma'] );
}
if ( 'edit' !== $context ) {
$this->assertArrayNotHasKey( 'author_ip', $data );
$this->assertArrayNotHasKey( 'author_user_agent', $data );
$this->assertArrayNotHasKey( 'date_gmt', $data );
$this->assertArrayNotHasKey( 'raw', $data['content'] );
$this->assertArrayNotHasKey( 'karma', $data );
}
}
}