| | 1 | <?php |
| | 2 | /** |
| | 3 | * @group query |
| | 4 | */ |
| | 5 | class Tests_Query_CommentCount extends WP_UnitTestCase { |
| | 6 | static $post_ids = array(); |
| | 7 | public $q; |
| | 8 | static $post_type = 'page'; // can be anything |
| | 9 | |
| | 10 | public function setUp() { |
| | 11 | parent::setUp(); |
| | 12 | unset( $this->q ); |
| | 13 | $this->q = new WP_Query(); |
| | 14 | } |
| | 15 | |
| | 16 | public function tearDown() { |
| | 17 | parent::tearDown(); |
| | 18 | unset( $this->q ); |
| | 19 | } |
| | 20 | |
| | 21 | public static function wpSetUpBeforeClass( $factory ) { |
| | 22 | $post_id = self::factory()->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => self::$post_type ) ); |
| | 23 | self::$post_ids[1][] = $post_id; |
| | 24 | self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); |
| | 25 | |
| | 26 | $post_id = self::factory()->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => self::$post_type ) ); |
| | 27 | self::$post_ids[4][] = $post_id; |
| | 28 | for ( $i = 0; $i < 4; $i++ ) { |
| | 29 | self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); |
| | 30 | } |
| | 31 | |
| | 32 | $post_id = self::factory()->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => self::$post_type ) ); |
| | 33 | self::$post_ids[5][] = $post_id; |
| | 34 | for ( $i = 0; $i < 5; $i++ ) { |
| | 35 | self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); |
| | 36 | } |
| | 37 | |
| | 38 | $post_id = self::factory()->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => self::$post_type ) ); |
| | 39 | self::$post_ids[5][] = $post_id; |
| | 40 | for ( $i = 0; $i < 5; $i++ ) { |
| | 41 | self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); |
| | 42 | } |
| | 43 | } |
| | 44 | |
| | 45 | private function helper_get_found_post_ids() { |
| | 46 | return wp_list_pluck( $this->q->posts, 'ID' ); |
| | 47 | } |
| | 48 | |
| | 49 | public function test_operator_equals() { |
| | 50 | $args = array( |
| | 51 | 'post_type' => self::$post_type, |
| | 52 | 'posts_per_page' => -1, |
| | 53 | 'comment_count' => array( |
| | 54 | 'value' => 4, |
| | 55 | 'compare' => '=', |
| | 56 | ), |
| | 57 | ); |
| | 58 | $this->q->query( $args ); |
| | 59 | |
| | 60 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 61 | |
| | 62 | $expected = self::$post_ids[4]; |
| | 63 | |
| | 64 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 65 | } |
| | 66 | |
| | 67 | public function test_operator_greater_than() { |
| | 68 | $args = array( |
| | 69 | 'post_type' => self::$post_type, |
| | 70 | 'posts_per_page' => -1, |
| | 71 | 'comment_count' => array( |
| | 72 | 'value' => 4, |
| | 73 | 'compare' => '>', |
| | 74 | ), |
| | 75 | ); |
| | 76 | $this->q->query( $args ); |
| | 77 | |
| | 78 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 79 | |
| | 80 | $expected = self::$post_ids[5]; |
| | 81 | |
| | 82 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 83 | } |
| | 84 | |
| | 85 | public function test_operator_greater_than_no_results() { |
| | 86 | $args = array( |
| | 87 | 'post_type' => self::$post_type, |
| | 88 | 'posts_per_page' => -1, |
| | 89 | 'comment_count' => array( |
| | 90 | 'value' => 6, |
| | 91 | 'compare' => '>', |
| | 92 | ), |
| | 93 | ); |
| | 94 | $this->q->query( $args ); |
| | 95 | |
| | 96 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 97 | |
| | 98 | $expected = array(); |
| | 99 | |
| | 100 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 101 | } |
| | 102 | public function test_operator_less_than() { |
| | 103 | $args = array( |
| | 104 | 'post_type' => self::$post_type, |
| | 105 | 'posts_per_page' => -1, |
| | 106 | 'comment_count' => array( |
| | 107 | 'value' => 6, |
| | 108 | 'compare' => '<', |
| | 109 | ), |
| | 110 | ); |
| | 111 | $this->q->query( $args ); |
| | 112 | |
| | 113 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 114 | |
| | 115 | $expected = array(); |
| | 116 | foreach ( self::$post_ids[1] as $expected_id ) { |
| | 117 | $expected[] = $expected_id; |
| | 118 | } |
| | 119 | foreach ( self::$post_ids[4] as $expected_id ) { |
| | 120 | $expected[] = $expected_id; |
| | 121 | } |
| | 122 | foreach ( self::$post_ids[5] as $expected_id ) { |
| | 123 | $expected[] = $expected_id; |
| | 124 | } |
| | 125 | |
| | 126 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 127 | } |
| | 128 | |
| | 129 | public function test_operator_less_than_no_results() { |
| | 130 | $args = array( |
| | 131 | 'post_type' => self::$post_type, |
| | 132 | 'posts_per_page' => -1, |
| | 133 | 'comment_count' => array( |
| | 134 | 'value' => 1, |
| | 135 | 'compare' => '<', |
| | 136 | ), |
| | 137 | ); |
| | 138 | $this->q->query( $args ); |
| | 139 | |
| | 140 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 141 | |
| | 142 | $expected = array(); |
| | 143 | |
| | 144 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 145 | } |
| | 146 | |
| | 147 | |
| | 148 | public function test_operator_not_equal() { |
| | 149 | $args = array( |
| | 150 | 'post_type' => self::$post_type, |
| | 151 | 'posts_per_page' => -1, |
| | 152 | 'comment_count' => array( |
| | 153 | 'value' => 15, |
| | 154 | 'compare' => '!=', |
| | 155 | ), |
| | 156 | ); |
| | 157 | $this->q->query( $args ); |
| | 158 | |
| | 159 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 160 | |
| | 161 | $expected = array(); |
| | 162 | foreach ( self::$post_ids[1] as $expected_id ) { |
| | 163 | $expected[] = $expected_id; |
| | 164 | } |
| | 165 | foreach ( self::$post_ids[4] as $expected_id ) { |
| | 166 | $expected[] = $expected_id; |
| | 167 | } |
| | 168 | foreach ( self::$post_ids[5] as $expected_id ) { |
| | 169 | $expected[] = $expected_id; |
| | 170 | } |
| | 171 | |
| | 172 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 173 | |
| | 174 | } |
| | 175 | public function test_operator_equal_or_greater_than() { |
| | 176 | $args = array( |
| | 177 | 'post_type' => self::$post_type, |
| | 178 | 'posts_per_page' => -1, |
| | 179 | 'comment_count' => array( |
| | 180 | 'value' => 4, |
| | 181 | 'compare' => '>=', |
| | 182 | ), |
| | 183 | ); |
| | 184 | $this->q->query( $args ); |
| | 185 | |
| | 186 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 187 | |
| | 188 | $expected = array(); |
| | 189 | foreach ( self::$post_ids[4] as $expected_id ) { |
| | 190 | $expected[] = $expected_id; |
| | 191 | } |
| | 192 | foreach ( self::$post_ids[5] as $expected_id ) { |
| | 193 | $expected[] = $expected_id; |
| | 194 | } |
| | 195 | |
| | 196 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 197 | } |
| | 198 | |
| | 199 | public function test_operator_equal_or_greater_than_no_results() { |
| | 200 | $args = array( |
| | 201 | 'post_type' => self::$post_type, |
| | 202 | 'posts_per_page' => -1, |
| | 203 | 'comment_count' => array( |
| | 204 | 'value' => 7, |
| | 205 | 'compare' => '>=', |
| | 206 | ), |
| | 207 | ); |
| | 208 | $this->q->query( $args ); |
| | 209 | |
| | 210 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 211 | |
| | 212 | $expected = array(); |
| | 213 | |
| | 214 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 215 | } |
| | 216 | |
| | 217 | public function test_operator_equal_or_less_than() { |
| | 218 | $args = array( |
| | 219 | 'post_type' => self::$post_type, |
| | 220 | 'posts_per_page' => -1, |
| | 221 | 'comment_count' => array( |
| | 222 | 'value' => 4, |
| | 223 | 'compare' => '<=', |
| | 224 | ), |
| | 225 | ); |
| | 226 | $this->q->query( $args ); |
| | 227 | |
| | 228 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 229 | |
| | 230 | $expected = array(); |
| | 231 | foreach ( self::$post_ids[1] as $expected_id ) { |
| | 232 | $expected[] = $expected_id; |
| | 233 | } |
| | 234 | foreach ( self::$post_ids[4] as $expected_id ) { |
| | 235 | $expected[] = $expected_id; |
| | 236 | } |
| | 237 | |
| | 238 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 239 | } |
| | 240 | |
| | 241 | public function test_operator_equal_or_less_than_no_results() { |
| | 242 | $args = array( |
| | 243 | 'post_type' => self::$post_type, |
| | 244 | 'posts_per_page' => -1, |
| | 245 | 'comment_count' => array( |
| | 246 | 'value' => 0, |
| | 247 | 'compare' => '<=', |
| | 248 | ), |
| | 249 | ); |
| | 250 | $this->q->query( $args ); |
| | 251 | |
| | 252 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 253 | |
| | 254 | $expected = array(); |
| | 255 | |
| | 256 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 257 | } |
| | 258 | |
| | 259 | public function test_invalid_operator_should_fall_back_on_equals() { |
| | 260 | $args = array( |
| | 261 | 'post_type' => self::$post_type, |
| | 262 | 'posts_per_page' => -1, |
| | 263 | 'comment_count' => array( |
| | 264 | 'value' => 5, |
| | 265 | 'compare' => '@', |
| | 266 | ), |
| | 267 | ); |
| | 268 | $this->q->query( $args ); |
| | 269 | |
| | 270 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 271 | |
| | 272 | $expected = array(); |
| | 273 | foreach ( self::$post_ids[5] as $expected_id ) { |
| | 274 | $expected[] = $expected_id; |
| | 275 | } |
| | 276 | |
| | 277 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 278 | } |
| | 279 | |
| | 280 | public function test_wrong_count_no_results() { |
| | 281 | $args = array( |
| | 282 | 'post_type' => self::$post_type, |
| | 283 | 'posts_per_page' => -1, |
| | 284 | 'comment_count' => array( |
| | 285 | 'value' => 'abc', |
| | 286 | 'compare' => '=', |
| | 287 | ), |
| | 288 | ); |
| | 289 | $this->q->query( $args ); |
| | 290 | |
| | 291 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 292 | |
| | 293 | $expected = array(); |
| | 294 | |
| | 295 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 296 | } |
| | 297 | |
| | 298 | public function test_no_operator_no_results() { |
| | 299 | $args = array( |
| | 300 | 'post_type' => self::$post_type, |
| | 301 | 'posts_per_page' => -1, |
| | 302 | 'comment_count' => array( |
| | 303 | 'value' => 5, |
| | 304 | ), |
| | 305 | ); |
| | 306 | $this->q->query( $args ); |
| | 307 | |
| | 308 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 309 | |
| | 310 | $expected = self::$post_ids[5]; |
| | 311 | |
| | 312 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 313 | } |
| | 314 | |
| | 315 | public function test_empty_non_numeric_string_should_be_ignored() { |
| | 316 | $args = array( |
| | 317 | 'post_type' => self::$post_type, |
| | 318 | 'posts_per_page' => -1, |
| | 319 | 'comment_count' => '', |
| | 320 | ); |
| | 321 | $this->q->query( $args ); |
| | 322 | |
| | 323 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 324 | |
| | 325 | $expected = array(); |
| | 326 | foreach ( self::$post_ids[1] as $expected_id ) { |
| | 327 | $expected[] = $expected_id; |
| | 328 | } |
| | 329 | foreach ( self::$post_ids[4] as $expected_id ) { |
| | 330 | $expected[] = $expected_id; |
| | 331 | } |
| | 332 | foreach ( self::$post_ids[5] as $expected_id ) { |
| | 333 | $expected[] = $expected_id; |
| | 334 | } |
| | 335 | |
| | 336 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 337 | } |
| | 338 | |
| | 339 | public function test_simple_count() { |
| | 340 | $args = array( |
| | 341 | 'post_type' => self::$post_type, |
| | 342 | 'posts_per_page' => -1, |
| | 343 | 'comment_count' => 5, |
| | 344 | ); |
| | 345 | $this->q->query( $args ); |
| | 346 | |
| | 347 | $found_post_ids = $this->helper_get_found_post_ids(); |
| | 348 | |
| | 349 | $expected = self::$post_ids[5]; |
| | 350 | |
| | 351 | $this->assertEqualSets( $found_post_ids, $expected ); |
| | 352 | } |
| | 353 | } |
| | 354 | |