8000 Merge pull request #2313 from WP-API/2309-fix-paging-result-set · WP-API/WP-API@7ba596f · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Commit 7ba596f

Browse files
committed
Merge pull request #2313 from WP-API/2309-fix-paging-result-set
Ensure `GET /wp/v2/categories` with out of bounds offset doesn't return results
2 parents 67fdadd + 4a0e3e5 commit 7ba596f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/endpoints/class-wp-rest-terms-controller.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ public function get_items( $request ) {
147147
unset( $count_args['offset'] );
148148
$total_terms = wp_count_terms( $this->taxonomy, $count_args );
149149

150+
// Ensure we don't return results when offset is out of bounds
151+
// see https://core.trac.wordpress.org/ticket/35935
152+
if ( $prepared_args['offset'] >= $total_terms ) {
153+
$query_result = array();
154+
}
155+
150156
// wp_count_terms can return a falsy value when the term has no children
151157
if ( ! $total_terms ) {
152158
$total_terms = 0;

tests/test-rest-categories-controller.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ public function test_get_terms_pagination_headers() {
440440
$headers = $response->get_headers();
441441
$this->assertEquals( 50, $headers['X-WP-Total'] );
442442
$this->assertEquals( 5, $headers['X-WP-TotalPages'] );
443+
$this->assertCount( 10, $response->get_data() );
443444
$next_link = add_query_arg( array(
444445
'page' => 2,
445446
), rest_url( '/wp/v2/categories' ) );
@@ -455,6 +456,7 @@ public function test_get_terms_pagination_headers() {
455456
$headers = $response->get_headers();
456457
$this->assertEquals( 51, $headers['X-WP-Total'] );
457458
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
459+
$this->assertCount( 10, $response->get_data() );
458460
$prev_link = add_query_arg( array(
459461
'page' => 2,
460462
), rest_url( '/wp/v2/categories' ) );
@@ -470,6 +472,7 @@ public function test_get_terms_pagination_headers() {
470472
$headers = $response->get_headers();
471473
$this->assertEquals( 51, $headers['X-WP-Total'] );
472474
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
475+
$this->assertCount( 1, $response->get_data() );
473476
$prev_link = add_query_arg( array(
474477
'page' => 5,
475478
), rest_url( '/wp/v2/categories' ) );
@@ -482,13 +485,39 @@ public function test_get_terms_pagination_headers() {
482485
$headers = $response->get_headers();
483486
$this->assertEquals( 51, $headers['X-WP-Total'] );
484487
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
488+
$this->assertCount( 0, $response->get_data() );
485489
$prev_link = add_query_arg( array(
486490
'page' => 6,
487491
), rest_url( '/wp/v2/categories' ) );
488492
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
489493
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
490494
}
491495

496+
public function test_get_items_per_page_exceeds_number_of_items() {
497+
// Start of the index + Uncategorized default term
498+
for ( $i = 0; $i < 17; $i++ ) {
499+
$this->factory->category->create( array(
500+
'name' => "Category {$i}",
501+
) );
502+
}
503+
$request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
504+
$request->set_param( 'page', 1 );
505+
$request->set_param( 'per_page', 100 );
506+
$response = $this->server->dispatch( $request );
507+
$headers = $response->get_headers();
508+
$this->assertEquals( 18, $headers['X-WP-Total'] );
509+
$this- 8DBD >assertEquals( 1, $headers['X-WP-TotalPages'] );
510+
$this->assertCount( 18, $response->get_data() );
511+
$request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
512+
$request->set_param( 'page', 2 );
513+
$request->set_param( 'per_page', 100 );
514+
$response = $this->server->dispatch( $request );
515+
$headers = $response->get_headers();
516+
$this->assertEquals( 18, $headers['X-WP-Total'] );
517+
$this->assertEquals( 1, $headers['X-WP-TotalPages'] );
518+
$this->assertCount( 0, $response->get_data() );
519+
}
520+
492521
public function test_get_item() {
493522
$request = new WP_REST_Request( 'GET', '/wp/v2/categories/1' );
494523
$response = $this->server->dispatch( $request );

0 commit comments

Comments
 (0)
0