| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group query |
| | 5 | */ |
| | 6 | class Tests_Query_PostsPerPage extends WP_UnitTestCase { |
| | 7 | public $q; |
| | 8 | |
| | 9 | public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| | 10 | // More than posts_per_page default of 10 |
| | 11 | // This number is verified in the 'test_posts_per_page_all' method. |
| | 12 | self::factory()->post->create_many( 11 ); |
| | 13 | } |
| | 14 | |
| | 15 | public function set_up() { |
| | 16 | parent::set_up(); |
| | 17 | unset( $this->q ); |
| | 18 | $this->q = new WP_Query(); |
| | 19 | } |
| | 20 | |
| | 21 | public function _get_post_count( $args = array() ) { |
| | 22 | $args = wp_parse_args( |
| | 23 | $args, |
| | 24 | array( |
| | 25 | 'fields' => 'ids', |
| | 26 | ) |
| | 27 | ); |
| | 28 | |
| | 29 | return count( $this->q->query( $args ) ); |
| | 30 | } |
| | 31 | |
| | 32 | public function test_posts_per_page_integer_positive() { |
| | 33 | $count = $this->_get_post_count( array( |
| | 34 | 'posts_per_page' => 2, |
| | 35 | ) ); |
| | 36 | |
| | 37 | $this->assertSame( 2, $count ); |
| | 38 | } |
| | 39 | |
| | 40 | /** |
| | 41 | * @ticket 24142 |
| | 42 | */ |
| | 43 | public function test_posts_per_page_integer_zero() { |
| | 44 | $count = $this->_get_post_count( array( |
| | 45 | 'posts_per_page' => 0, |
| | 46 | ) ); |
| | 47 | |
| | 48 | $this->assertSame( 0, $count ); |
| | 49 | } |
| | 50 | |
| | 51 | public function test_posts_per_page_string_numeric() { |
| | 52 | $count = $this->_get_post_count( array( |
| | 53 | 'posts_per_page' => '2', |
| | 54 | ) ); |
| | 55 | |
| | 56 | $this->assertSame( 2, $count ); |
| | 57 | } |
| | 58 | |
| | 59 | public function test_posts_per_page_string_non_numeric() { |
| | 60 | $count = $this->_get_post_count( array( |
| | 61 | 'posts_per_page' => 'foo', |
| | 62 | ) ); |
| | 63 | |
| | 64 | $this->assertSame( 10, $count ); |
| | 65 | } |
| | 66 | |
| | 67 | public function test_posts_per_page_boolean_true() { |
| | 68 | $count = $this->_get_post_count( array( |
| | 69 | 'posts_per_page' => true, |
| | 70 | ) ); |
| | 71 | |
| | 72 | $this->assertSame( 1, $count ); |
| | 73 | } |
| | 74 | |
| | 75 | /** |
| | 76 | * @ticket 24142 |
| | 77 | */ |
| | 78 | public function test_posts_per_page_boolean_false() { |
| | 79 | $count = $this->_get_post_count( array( |
| | 80 | 'posts_per_page' => false, |
| | 81 | ) ); |
| | 82 | |
| | 83 | $this->assertSame( 0, $count ); |
| | 84 | } |
| | 85 | |
| | 86 | public function test_posts_per_page_null() { |
| | 87 | $count = $this->_get_post_count( array( |
| | 88 | 'posts_per_page' => null, |
| | 89 | ) ); |
| | 90 | |
| | 91 | $this->assertSame( 10, $count ); |
| | 92 | } |
| | 93 | |
| | 94 | public function test_posts_per_page_empty_string() { |
| | 95 | $count = $this->_get_post_count( array( |
| | 96 | 'posts_per_page' => null, |
| | 97 | ) ); |
| | 98 | |
| | 99 | $this->assertSame( 10, $count ); |
| | 100 | } |
| | 101 | |
| | 102 | public function test_posts_per_page_array() { |
| | 103 | $count = $this->_get_post_count( array( |
| | 104 | 'posts_per_page' => array(), |
| | 105 | ) ); |
| | 106 | |
| | 107 | $this->assertSame( 10, $count ); |
| | 108 | } |
| | 109 | |
| | 110 | public function test_posts_per_page_negative() { |
| | 111 | $count = $this->_get_post_count( array( |
| | 112 | 'posts_per_page' => -2, |
| | 113 | ) ); |
| | 114 | |
| | 115 | $this->assertSame( 2, $count ); |
| | 116 | } |
| | 117 | |
| | 118 | public function test_posts_per_page_all() { |
| | 119 | $count = $this->_get_post_count( array( |
| | 120 | 'posts_per_page' => -1, |
| | 121 | ) ); |
| | 122 | |
| | 123 | $this->assertSame( 11, $count ); |
| | 124 | } |
| | 125 | } |
| | 126 | No newline at end of file |