@@ -94,6 +94,70 @@ public function test_get_items_for_post() {
9494 $ this ->assertCount ( 2 , $ comments );
9595 }
9696
97+ public function test_get_comments_pagination_headers () {
98+ wp_set_current_user ( $ this ->admin_id );
99+ // Start of the index
100+ for ( $ i = 0 ; $ i < 49 ; $ i ++ ) {
101+ $ this ->factory ->comment ->create ( array (
102+ 'comment_content ' => "Comment {$ i }" ,
103+ 'comment_post_ID ' => $ this ->post_id ,
104+ ) );
105+ }
106+ $ request = new WP_REST_Request ( 'GET ' , '/wp/v2/comments ' );
107+ $ response = $ this ->server ->dispatch ( $ request );
108+ $ headers = $ response ->get_headers ();
109+ $ this ->assertEquals ( 50 , $ headers ['X-WP-Total ' ] );
110+ $ this ->assertEquals ( 5 , $ headers ['X-WP-TotalPages ' ] );
111+ $ next_link = add_query_arg ( array (
112+ 'page ' => 2 ,
113+ ), rest_url ( '/wp/v2/comments ' ) );
114+ $ this ->assertFalse ( stripos ( $ headers ['Link ' ], 'rel="prev" ' ) );
115+ $ this ->assertContains ( '< ' . $ next_link . '>; rel="next" ' , $ headers ['Link ' ] );
116+ // 3rd page
117+ $ this ->factory ->comment ->create ( array (
118+ 'comment_content ' => 'Comment 51 ' ,
119+ 'comment_post_ID ' => $ this ->post_id ,
120+ ) );
121+ $ request = new WP_REST_Request ( 'GET ' , '/wp/v2/comments ' );
122+ $ request ->set_param ( 'page ' , 3 );
123+ $ response = $ this ->server ->dispatch ( $ request );
124+ $ headers = $ response ->get_headers ();
125+ $ this ->assertEquals ( 51 , $ headers ['X-WP-Total ' ] );
126+ $ this ->assertEquals ( 6 , $ headers ['X-WP-TotalPages ' ] );
127+ $ prev_link = add_query_arg ( array (
128+ 'page ' => 2 ,
129+ ), rest_url ( '/wp/v2/comments ' ) );
130+ $ this ->assertContains ( '< ' . $ prev_link . '>; rel="prev" ' , $ headers ['Link ' ] );
131+ $ next_link = add_query_arg ( array (
132+ 'page ' => 4 ,
133+ ), rest_url ( '/wp/v2/comments ' ) );
134+ $ this ->assertContains ( '< ' . $ next_link . '>; rel="next" ' , $ headers ['Link ' ] );
135+ // Last page
136+ $ request = new WP_REST_Request ( 'GET ' , '/wp/v2/comments ' );
137+ $ request ->set_param ( 'page ' , 6 );
138+ $ response = $ this ->server ->dispatch ( $ request );
139+ $ headers = $ response ->get_headers ();
140+ $ this ->assertEquals ( 51 , $ headers ['X-WP-Total ' ] );
141+ $ this ->assertEquals ( 6 , $ headers ['X-WP-TotalPages ' ] );
142+ $ prev_link = add_query_arg ( array (
143+ 'page ' => 5 ,
144+ ), rest_url ( '/wp/v2/comments ' ) );
145+ $ this ->assertContains ( '< ' . $ prev_link . '>; rel="prev" ' , $ headers ['Link ' ] );
146+ $ this ->assertFalse ( stripos ( $ headers ['Link ' ], 'rel="next" ' ) );
147+ // Out of bounds
148+ $ request = new WP_REST_Request ( 'GET ' , '/wp/v2/comments ' );
149+ $ request ->set_param ( 'page ' , 8 );
150+ $ response = $ this ->server ->dispatch ( $ request );
151+ $ headers = $ response ->get_headers ();
152+ $ this ->assertEquals ( 51 , $ headers ['X-WP-Total ' ] );
153+ $ this ->assertEquals ( 6 , $ headers ['X-WP-TotalPages ' ] );
154+ $ prev_link = add_query_arg ( array (
155+ 'page ' => 6 ,
156+ ), rest_url ( '/wp/v2/comments ' ) );
157+ $ this ->assertContains ( '< ' . $ prev_link . '>; rel="prev" ' , $ headers ['Link ' ] );
158+ $ this ->assertFalse ( stripos ( $ headers ['Link ' ], 'rel="next" ' ) );
159+ }
160+
97161 public function test_get_item () {
98162 $ request = new WP_REST_Request ( 'GET ' , sprintf ( '/wp/v2/comments/%d ' , $ this ->approved_id ) );
99163
@@ -119,7 +183,7 @@ public function test_prepare_item() {
119183 }
120184
121185 public function test_get_comment_invalid_id () {
122- $ request = new WP_REST_Request ( 'GET ' , '/wp/v2/comments/ ' . 100 );
186+ $ request = new WP_REST_Request ( 'GET ' , '/wp/v2/comments/ ' . 99999 );
123187
124188 $ response = $ this ->server ->dispatch ( $ request );
125189 $ this ->assertErrorResponse ( 'rest_comment_invalid_id ' , $ response , 404 );
@@ -135,7 +199,7 @@ public function test_get_comment_invalid_context() {
135199 public function test_get_comment_invalid_post_id () {
136200 $ comment_id = $ this ->factory ->comment ->create ( array (
137201 'comment_approved ' => 1 ,
138- 'comment_post_ID ' => 100 ,
202+ 'comment_post_ID ' => 99999 ,
139203 ));
140204 $ request = new WP_REST_Request ( 'GET ' , '/wp/v2/comments/ ' . $ comment_id );
141205
@@ -338,7 +402,7 @@ public function test_update_comment_invalid_id() {
338402 $ params = array (
339403 'content ' => 'Oh, they have the internet on computers now! ' ,
340404 );
341- $ request = new WP_REST_Request ( 'PUT ' , '/wp/v2/comments/ ' . 100 );
405+ $ request = new WP_REST_Request ( 'PUT ' , '/wp/v2/comments/ ' . 99999 );
342406 $ request ->add_header ( 'content-type ' , 'application/json ' );
343407 $ request ->set_body ( json_encode ( $ params ) );
344408
@@ -378,7 +442,7 @@ public function test_delete_item() {
378442 public function test_delete_comment_invalid_id () {
379443 wp_set_current_user ( $ this ->admin_id );
380444
381- $ request = new WP_REST_Request ( 'DELETE ' , sprintf ( '/wp/v2/comments/%d ' , 100 ) );
445+ $ request = new WP_REST_Request ( 'DELETE ' , sprintf ( '/wp/v2/comments/%d ' , 99999 ) );
382446
383447 $ response = $ this ->server ->dispatch ( $ request );
384448 $ response = rest_ensure_response ( $ response );
0 commit comments