From f1ca28c2ab071bd6128c657778269e8204424b3f Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 28 Oct 2015 18:17:27 +0000 Subject: [PATCH] Don't specify an `offset` default in `get_posts()`. The default value should be a null offset. A `0` default overrides any value of `paged` passed to `get_posts()`. See [34697]. Fixes #34060. git-svn-id: https://develop.svn.wordpress.org/trunk@35417 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post-functions.php | 2 +- tests/phpunit/tests/post/getPosts.php | 128 ++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/post/getPosts.php diff --git a/src/wp-includes/post-functions.php b/src/wp-includes/post-functions.php index 58961fe025..34452c462e 100644 --- a/src/wp-includes/post-functions.php +++ b/src/wp-includes/post-functions.php @@ -1610,7 +1610,7 @@ function is_post_type_viewable( $post_type_object ) { */ function get_posts( $args = null ) { $defaults = array( - 'numberposts' => 5, 'offset' => 0, + 'numberposts' => 5, 'category' => 0, 'orderby' => 'date', 'order' => 'DESC', 'include' => array(), 'exclude' => array(), 'meta_key' => '', diff --git a/tests/phpunit/tests/post/getPosts.php b/tests/phpunit/tests/post/getPosts.php new file mode 100644 index 0000000000..496a0327e8 --- /dev/null +++ b/tests/phpunit/tests/post/getPosts.php @@ -0,0 +1,128 @@ +post->create( array( + 'post_date' => '2015-04-04 04:04:04', + ) ); + $p2 = self::factory()->post->create( array( + 'post_date' => '2014-04-04 04:04:04', + ) ); + + $found = get_posts( array( + 'numberposts' => 1, + 'orderby' => 'date', + 'order' => 'DESC', + 'fields' => 'ids', + ) ); + + $this->assertSame( array( $p1 ), $found ); + } + + public function test_offset_0_should_be_respected() { + $p1 = self::factory()->post->create( array( + 'post_date' => '2015-04-04 04:04:04', + ) ); + $p2 = self::factory()->post->create( array( + 'post_date' => '2014-04-04 04:04:04', + ) ); + + $found = get_posts( array( + 'numberposts' => 1, + 'orderby' => 'date', + 'order' => 'DESC', + 'fields' => 'ids', + 'offset' => 0, + ) ); + + $this->assertSame( array( $p1 ), $found ); + } + + public function test_offset_non_0_should_be_respected() { + $p1 = self::factory()->post->create( array( + 'post_date' => '2015-04-04 04:04:04', + ) ); + $p2 = self::factory()->post->create( array( + 'post_date' => '2014-04-04 04:04:04', + ) ); + + $found = get_posts( array( + 'numberposts' => 1, + 'orderby' => 'date', + 'order' => 'DESC', + 'fields' => 'ids', + 'offset' => 1, + ) ); + + $this->assertSame( array( $p2 ), $found ); + } + + /** + * @ticket 34060 + */ + public function test_paged_should_not_be_overridden_by_default_offset() { + $p1 = self::factory()->post->create( array( + 'post_date' => '2015-04-04 04:04:04', + ) ); + $p2 = self::factory()->post->create( array( + 'post_date' => '2014-04-04 04:04:04', + ) ); + + $found = get_posts( array( + 'orderby' => 'date', + 'order' => 'DESC', + 'fields' => 'ids', + 'paged' => 2, + 'posts_per_page' => 1, + ) ); + + $this->assertSame( array( $p2 ), $found ); + } + + public function test_explicit_offset_0_should_override_paged() { + $p1 = self::factory()->post->create( array( + 'post_date' => '2015-04-04 04:04:04', + ) ); + $p2 = self::factory()->post->create( array( + 'post_date' => '2014-04-04 04:04:04', + ) ); + + $found = get_posts( array( + 'orderby' => 'date', + 'order' => 'DESC', + 'fields' => 'ids', + 'paged' => 2, + 'posts_per_page' => 1, + 'offset' => 0, + ) ); + + $this->assertSame( array( $p1 ), $found ); + } + + public function test_explicit_offset_non_0_should_override_paged() { + $p1 = self::factory()->post->create( array( + 'post_date' => '2015-04-04 04:04:04', + ) ); + $p2 = self::factory()->post->create( array( + 'post_date' => '2014-04-04 04:04:04', + ) ); + $p3 = self::factory()->post->create( array( + 'post_date' => '2013-04-04 04:04:04', + ) ); + + $found = get_posts( array( + 'orderby' => 'date', + 'order' => 'DESC', + 'fields' => 'ids', + 'paged' => 2, + 'posts_per_page' => 1, + 'offset' => 2, + ) ); + + $this->assertSame( array( $p3 ), $found ); + } +}