@@ -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
0 commit comments