| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group post |
| 5 | * @group query |
| 6 | */ |
| 7 | class Tests_Post_GetPosts extends WP_UnitTestCase { |
| 8 | public function test_offset_should_be_null_by_default() { |
| 9 | $p1 = self::factory()->post->create( array( |
| 10 | 'post_date' => '2015-04-04 04:04:04', |
| 11 | ) ); |
| 12 | $p2 = self::factory()->post->create( array( |
| 13 | 'post_date' => '2014-04-04 04:04:04', |
| 14 | ) ); |
| 15 | |
| 16 | $found = get_posts( array( |
| 17 | 'numberposts' => 1, |
| 18 | 'orderby' => 'date', |
| 19 | 'order' => 'DESC', |
| 20 | 'fields' => 'ids', |
| 21 | ) ); |
| 22 | |
| 23 | $this->assertSame( array( $p1 ), $found ); |
| 24 | } |
| 25 | |
| 26 | public function test_offset_0_should_be_respected() { |
| 27 | $p1 = self::factory()->post->create( array( |
| 28 | 'post_date' => '2015-04-04 04:04:04', |
| 29 | ) ); |
| 30 | $p2 = self::factory()->post->create( array( |
| 31 | 'post_date' => '2014-04-04 04:04:04', |
| 32 | ) ); |
| 33 | |
| 34 | $found = get_posts( array( |
| 35 | 'numberposts' => 1, |
| 36 | 'orderby' => 'date', |
| 37 | 'order' => 'DESC', |
| 38 | 'fields' => 'ids', |
| 39 | 'offset' => 0, |
| 40 | ) ); |
| 41 | |
| 42 | $this->assertSame( array( $p1 ), $found ); |
| 43 | } |
| 44 | |
| 45 | public function test_offset_non_0_should_be_respected() { |
| 46 | $p1 = self::factory()->post->create( array( |
| 47 | 'post_date' => '2015-04-04 04:04:04', |
| 48 | ) ); |
| 49 | $p2 = self::factory()->post->create( array( |
| 50 | 'post_date' => '2014-04-04 04:04:04', |
| 51 | ) ); |
| 52 | |
| 53 | $found = get_posts( array( |
| 54 | 'numberposts' => 1, |
| 55 | 'orderby' => 'date', |
| 56 | 'order' => 'DESC', |
| 57 | 'fields' => 'ids', |
| 58 | 'offset' => 1, |
| 59 | ) ); |
| 60 | |
| 61 | $this->assertSame( array( $p2 ), $found ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @ticket 34060 |
| 66 | */ |
| 67 | public function test_paged_should_not_be_overridden_by_default_offset() { |
| 68 | $p1 = self::factory()->post->create( array( |
| 69 | 'post_date' => '2015-04-04 04:04:04', |
| 70 | ) ); |
| 71 | $p2 = self::factory()->post->create( array( |
| 72 | 'post_date' => '2014-04-04 04:04:04', |
| 73 | ) ); |
| 74 | |
| 75 | $found = get_posts( array( |
| 76 | 'orderby' => 'date', |
| 77 | 'order' => 'DESC', |
| 78 | 'fields' => 'ids', |
| 79 | 'paged' => 2, |
| 80 | 'posts_per_page' => 1, |
| 81 | ) ); |
| 82 | |
| 83 | $this->assertSame( array( $p2 ), $found ); |
| 84 | } |
| 85 | |
| 86 | public function test_explicit_offset_0_should_override_paged() { |
| 87 | $p1 = self::factory()->post->create( array( |
| 88 | 'post_date' => '2015-04-04 04:04:04', |
| 89 | ) ); |
| 90 | $p2 = self::factory()->post->create( array( |
| 91 | 'post_date' => '2014-04-04 04:04:04', |
| 92 | ) ); |
| 93 | |
| 94 | $found = get_posts( array( |
| 95 | 'orderby' => 'date', |
| 96 | 'order' => 'DESC', |
| 97 | 'fields' => 'ids', |
| 98 | 'paged' => 2, |
| 99 | 'posts_per_page' => 1, |
| 100 | 'offset' => 0, |
| 101 | ) ); |
| 102 | |
| 103 | $this->assertSame( array( $p1 ), $found ); |
| 104 | } |
| 105 | |
| 106 | public function test_explicit_offset_non_0_should_override_paged() { |
| 107 | $p1 = self::factory()->post->create( array( |
| 108 | 'post_date' => '2015-04-04 04:04:04', |
| 109 | ) ); |
| 110 | $p2 = self::factory()->post->create( array( |
| 111 | 'post_date' => '2014-04-04 04:04:04', |
| 112 | ) ); |
| 113 | $p3 = self::factory()->post->create( array( |
| 114 | 'post_date' => '2013-04-04 04:04:04', |
| 115 | ) ); |
| 116 | |
| 117 | $found = get_posts( array( |
| 118 | 'orderby' => 'date', |
| 119 | 'order' => 'DESC', |
| 120 | 'fields' => 'ids', |
| 121 | 'paged' => 2, |
| 122 | 'posts_per_page' => 1, |
| 123 | 'offset' => 2, |
| 124 | ) ); |
| 125 | |
| 126 | $this->assertSame( array( $p3 ), $found ); |
| 127 | } |
| 128 | } |