| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * |
|---|
| 4 | * @group posts |
|---|
| 5 | */ |
|---|
| 6 | class Tests_Query_commentsQuery extends WP_UnitTestCase |
|---|
| 7 | { |
|---|
| 8 | |
|---|
| 9 | static $postIDs = array(); |
|---|
| 10 | public $q; |
|---|
| 11 | static $post_type = 'page'; // can be anything |
|---|
| 12 | |
|---|
| 13 | public function setUp() { |
|---|
| 14 | parent::setUp(); |
|---|
| 15 | unset( $this->q ); |
|---|
| 16 | $this->q = new WP_Query(); |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | public function tearDown() { |
|---|
| 20 | parent::tearDown(); |
|---|
| 21 | unset( $this->q ); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | public static function wpSetUpBeforeClass( $factory ) { |
|---|
| 25 | $post_id = self::factory()->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => self::$post_type ) ); |
|---|
| 26 | self::$postIDs[1][] = $post_id; |
|---|
| 27 | self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); |
|---|
| 28 | |
|---|
| 29 | $post_id = self::factory()->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => self::$post_type ) ); |
|---|
| 30 | self::$postIDs[4][] = $post_id; |
|---|
| 31 | for( $i = 0; $i < 4; $i++ ){ |
|---|
| 32 | self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | $post_id = self::factory()->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => self::$post_type ) ); |
|---|
| 36 | self::$postIDs[5][] = $post_id; |
|---|
| 37 | for( $i = 0; $i < 5; $i++ ){ |
|---|
| 38 | self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | $post_id = self::factory()->post->create( array( 'post_content' => 1 . rand_str() . ' about', 'post_type' => self::$post_type ) ); |
|---|
| 42 | self::$postIDs[5][] = $post_id; |
|---|
| 43 | for( $i = 0; $i < 5; $i++ ){ |
|---|
| 44 | self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | public static function wpTearDownAfterClass() { |
|---|
| 49 | foreach ( self::$postIDs as $postID ) { |
|---|
| 50 | // Delete hard, so skip trash and remove comments as well |
|---|
| 51 | wp_delete_post( $postID, true ); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | private function helper_get_found_post_ids() { |
|---|
| 56 | $found_post_ids = array(); |
|---|
| 57 | if ( $this->q->posts ) { |
|---|
| 58 | foreach ( $this->q->posts as $post ) { |
|---|
| 59 | $found_post_ids[] = $post->ID; |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | // Sort to make sure we are comparing the post IDs |
|---|
| 63 | sort($found_post_ids); |
|---|
| 64 | return $found_post_ids; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public function test_operator_equals() { |
|---|
| 68 | |
|---|
| 69 | $args = http_build_query( array( |
|---|
| 70 | 'post_type' => self::$post_type, |
|---|
| 71 | 'posts_per_page' => -1, |
|---|
| 72 | 'comment_count' => array( |
|---|
| 73 | 'value' => 4, |
|---|
| 74 | 'compare' => '=' |
|---|
| 75 | ) |
|---|
| 76 | ) ); |
|---|
| 77 | $this->q->query( $args ); |
|---|
| 78 | |
|---|
| 79 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 80 | |
|---|
| 81 | $expected = array(); |
|---|
| 82 | foreach( self::$postIDs[4] as $expectedID ) { |
|---|
| 83 | $expected[] = $expectedID; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | // Sort to make sure we are comparing the post IDs |
|---|
| 87 | sort($expected); |
|---|
| 88 | |
|---|
| 89 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | public function test_operator_greater_than() { |
|---|
| 93 | |
|---|
| 94 | $args = http_build_query( array( |
|---|
| 95 | 'post_type' => self::$post_type, |
|---|
| 96 | 'posts_per_page' => -1, |
|---|
| 97 | 'comment_count' => array( |
|---|
| 98 | 'value' => 4, |
|---|
| 99 | 'compare' => '>' |
|---|
| 100 | ) |
|---|
| 101 | ) ); |
|---|
| 102 | $this->q->query( $args ); |
|---|
| 103 | |
|---|
| 104 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 105 | |
|---|
| 106 | $expected = array(); |
|---|
| 107 | foreach( self::$postIDs[5] as $expectedID ) { |
|---|
| 108 | $expected[] = $expectedID; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | // Sort to make sure we are comparing the post IDs |
|---|
| 112 | sort($expected); |
|---|
| 113 | |
|---|
| 114 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | public function test_operator_greater_than_no_results() { |
|---|
| 118 | |
|---|
| 119 | $args = http_build_query( array( |
|---|
| 120 | 'post_type' => self::$post_type, |
|---|
| 121 | 'posts_per_page' => -1, |
|---|
| 122 | 'comment_count' => array ( |
|---|
| 123 | 'value' => 6, |
|---|
| 124 | 'compare' => '>' |
|---|
| 125 | ) |
|---|
| 126 | ) ); |
|---|
| 127 | $this->q->query( $args ); |
|---|
| 128 | |
|---|
| 129 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 130 | |
|---|
| 131 | $expected = array(); |
|---|
| 132 | |
|---|
| 133 | // Sort to make sure we are comparing the post IDs |
|---|
| 134 | sort($found_post_ids); |
|---|
| 135 | |
|---|
| 136 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 137 | } |
|---|
| 138 | public function test_operator_lower_than() { |
|---|
| 139 | |
|---|
| 140 | $args = http_build_query( array( |
|---|
| 141 | 'post_type' => self::$post_type, |
|---|
| 142 | 'posts_per_page' => -1, |
|---|
| 143 | 'comment_count' => array( |
|---|
| 144 | 'value' => 6, |
|---|
| 145 | 'compare' => '<' |
|---|
| 146 | ) |
|---|
| 147 | ) ); |
|---|
| 148 | $this->q->query( $args ); |
|---|
| 149 | |
|---|
| 150 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 151 | |
|---|
| 152 | $expected = array(); |
|---|
| 153 | foreach( self::$postIDs[1] as $expectedID ) { |
|---|
| 154 | $expected[] = $expectedID; |
|---|
| 155 | } |
|---|
| 156 | foreach( self::$postIDs[4] as $expectedID ) { |
|---|
| 157 | $expected[] = $expectedID; |
|---|
| 158 | } |
|---|
| 159 | foreach( self::$postIDs[5] as $expectedID ) { |
|---|
| 160 | $expected[] = $expectedID; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | // Sort to make sure we are comparing the post IDs |
|---|
| 164 | sort($expected); |
|---|
| 165 | |
|---|
| 166 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 167 | } |
|---|
| 168 | public function test_operator_lower_than_no_results() { |
|---|
| 169 | |
|---|
| 170 | $args = http_build_query( array( |
|---|
| 171 | 'post_type' => self::$post_type, |
|---|
| 172 | 'posts_per_page' => -1, |
|---|
| 173 | 'comment_count' => array( |
|---|
| 174 | 'value' => 1, |
|---|
| 175 | 'compare' => '<' |
|---|
| 176 | ) |
|---|
| 177 | ) ); |
|---|
| 178 | $this->q->query( $args ); |
|---|
| 179 | |
|---|
| 180 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 181 | |
|---|
| 182 | $expected = array(); |
|---|
| 183 | |
|---|
| 184 | // Sort to make sure we are comparing the post IDs |
|---|
| 185 | sort($found_post_ids); |
|---|
| 186 | |
|---|
| 187 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | public function test_operator_not_equal() { |
|---|
| 192 | |
|---|
| 193 | $args = http_build_query( array( |
|---|
| 194 | 'post_type' => self::$post_type, |
|---|
| 195 | 'posts_per_page' => -1, |
|---|
| 196 | 'comment_count' => array( |
|---|
| 197 | 'value' => 15, |
|---|
| 198 | 'compare' => '!=' |
|---|
| 199 | ) |
|---|
| 200 | ) ); |
|---|
| 201 | $this->q->query( $args ); |
|---|
| 202 | |
|---|
| 203 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 204 | |
|---|
| 205 | $expected = array(); |
|---|
| 206 | foreach( self::$postIDs[1] as $expectedID ) { |
|---|
| 207 | $expected[] = $expectedID; |
|---|
| 208 | } |
|---|
| 209 | foreach( self::$postIDs[4] as $expectedID ) { |
|---|
| 210 | $expected[] = $expectedID; |
|---|
| 211 | } |
|---|
| 212 | foreach( self::$postIDs[5] as $expectedID ) { |
|---|
| 213 | $expected[] = $expectedID; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | // Sort to make sure we are comparing the post IDs |
|---|
| 217 | sort($expected); |
|---|
| 218 | |
|---|
| 219 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 220 | |
|---|
| 221 | } |
|---|
| 222 | public function test_operator_equal_or_greater_than() { |
|---|
| 223 | |
|---|
| 224 | $args = http_build_query( array( |
|---|
| 225 | 'post_type' => self::$post_type, |
|---|
| 226 | 'posts_per_page' => -1, |
|---|
| 227 | 'comment_count' => array( |
|---|
| 228 | 'value' => 4, |
|---|
| 229 | 'compare' => '>=' |
|---|
| 230 | ) |
|---|
| 231 | ) ); |
|---|
| 232 | $this->q->query( $args ); |
|---|
| 233 | |
|---|
| 234 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 235 | |
|---|
| 236 | $expected = array(); |
|---|
| 237 | foreach( self::$postIDs[4] as $expectedID ) { |
|---|
| 238 | $expected[] = $expectedID; |
|---|
| 239 | } |
|---|
| 240 | foreach( self::$postIDs[5] as $expectedID ) { |
|---|
| 241 | $expected[] = $expectedID; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | // Sort to make sure we are comparing the post IDs |
|---|
| 245 | sort($expected); |
|---|
| 246 | |
|---|
| 247 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | public function test_operator_equal_or_greater_than_no_results() { |
|---|
| 251 | |
|---|
| 252 | $args = http_build_query( array( |
|---|
| 253 | 'post_type' => self::$post_type, |
|---|
| 254 | 'posts_per_page' => -1, |
|---|
| 255 | 'comment_count' => array( |
|---|
| 256 | 'value' => 7, |
|---|
| 257 | 'compare' => '>=' |
|---|
| 258 | ) |
|---|
| 259 | ) ); |
|---|
| 260 | $this->q->query( $args ); |
|---|
| 261 | |
|---|
| 262 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 263 | |
|---|
| 264 | $expected = array(); |
|---|
| 265 | |
|---|
| 266 | // Sort to make sure we are comparing the post IDs |
|---|
| 267 | sort($found_post_ids); |
|---|
| 268 | |
|---|
| 269 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | public function test_operator_equal_or_lower_than() { |
|---|
| 273 | |
|---|
| 274 | $args = http_build_query( array( |
|---|
| 275 | 'post_type' => self::$post_type, |
|---|
| 276 | 'posts_per_page' => -1, |
|---|
| 277 | 'comment_count' => array( |
|---|
| 278 | 'value' => 4, |
|---|
| 279 | 'compare' => '<=' |
|---|
| 280 | ) |
|---|
| 281 | ) ); |
|---|
| 282 | $this->q->query( $args ); |
|---|
| 283 | |
|---|
| 284 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 285 | |
|---|
| 286 | $expected = array(); |
|---|
| 287 | foreach( self::$postIDs[1] as $expectedID ) { |
|---|
| 288 | $expected[] = $expectedID; |
|---|
| 289 | } |
|---|
| 290 | foreach( self::$postIDs[4] as $expectedID ) { |
|---|
| 291 | $expected[] = $expectedID; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | // Sort to make sure we are comparing the post IDs |
|---|
| 295 | sort($expected); |
|---|
| 296 | |
|---|
| 297 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | public function test_operator_equal_or_lower_than_no_results() { |
|---|
| 301 | |
|---|
| 302 | $args = http_build_query( array( |
|---|
| 303 | 'post_type' => self::$post_type, |
|---|
| 304 | 'posts_per_page' => -1, |
|---|
| 305 | 'comment_count' => array( |
|---|
| 306 | 'value' => 0, |
|---|
| 307 | 'compare' => '<=' |
|---|
| 308 | ) |
|---|
| 309 | ) ); |
|---|
| 310 | $this->q->query( $args ); |
|---|
| 311 | |
|---|
| 312 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 313 | |
|---|
| 314 | $expected = array(); |
|---|
| 315 | |
|---|
| 316 | // Sort to make sure we are comparing the post IDs |
|---|
| 317 | sort($expected); |
|---|
| 318 | |
|---|
| 319 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | public function test_wrong_operator() { |
|---|
| 323 | |
|---|
| 324 | $args = http_build_query( array( |
|---|
| 325 | 'post_type' => self::$post_type, |
|---|
| 326 | 'posts_per_page' => -1, |
|---|
| 327 | 'comment_count' => array( |
|---|
| 328 | 'value' => 5, |
|---|
| 329 | 'compare' => '@' |
|---|
| 330 | ) |
|---|
| 331 | ) ); |
|---|
| 332 | $this->q->query( $args ); |
|---|
| 333 | |
|---|
| 334 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 335 | |
|---|
| 336 | $expected = array(); |
|---|
| 337 | foreach( self::$postIDs[5] as $expectedID ) { |
|---|
| 338 | $expected[] = $expectedID; |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | // Sort to make sure we are comparing the post IDs |
|---|
| 342 | sort($expected); |
|---|
| 343 | |
|---|
| 344 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | public function test_wrong_count_no_results() { |
|---|
| 348 | |
|---|
| 349 | $args = http_build_query( array( |
|---|
| 350 | 'post_type' => self::$post_type, |
|---|
| 351 | 'posts_per_page' => -1, |
|---|
| 352 | 'comment_count' => array( |
|---|
| 353 | 'value' => 'abc', |
|---|
| 354 | 'compare' => '=' |
|---|
| 355 | ) |
|---|
| 356 | ) ); |
|---|
| 357 | $this->q->query( $args ); |
|---|
| 358 | |
|---|
| 359 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 360 | |
|---|
| 361 | $expected = array(); |
|---|
| 362 | |
|---|
| 363 | // Sort to make sure we are comparing the post IDs |
|---|
| 364 | sort($expected); |
|---|
| 365 | |
|---|
| 366 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | public function test_no_operator_no_results() { |
|---|
| 370 | |
|---|
| 371 | $args = http_build_query( array( |
|---|
| 372 | 'post_type' => self::$post_type, |
|---|
| 373 | 'posts_per_page' => -1, |
|---|
| 374 | 'comment_count' => array( |
|---|
| 375 | 'value' => 5 |
|---|
| 376 | ) |
|---|
| 377 | ) ); |
|---|
| 378 | $this->q->query( $args ); |
|---|
| 379 | |
|---|
| 380 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 381 | |
|---|
| 382 | $expected = array(); |
|---|
| 383 | foreach( self::$postIDs[5] as $expectedID ) { |
|---|
| 384 | $expected[] = $expectedID; |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | // Sort to make sure we are comparing the post IDs |
|---|
| 388 | sort($expected); |
|---|
| 389 | |
|---|
| 390 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | public function test_blanco() { |
|---|
| 394 | |
|---|
| 395 | $args = http_build_query( array( |
|---|
| 396 | 'post_type' => self::$post_type, |
|---|
| 397 | 'posts_per_page' => -1, |
|---|
| 398 | 'comment_count' => '', |
|---|
| 399 | ) ); |
|---|
| 400 | $this->q->query( $args ); |
|---|
| 401 | |
|---|
| 402 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 403 | |
|---|
| 404 | $expected = array(); |
|---|
| 405 | foreach( self::$postIDs[1] as $expectedID ) { |
|---|
| 406 | $expected[] = $expectedID; |
|---|
| 407 | } |
|---|
| 408 | foreach( self::$postIDs[4] as $expectedID ) { |
|---|
| 409 | $expected[] = $expectedID; |
|---|
| 410 | } |
|---|
| 411 | foreach( self::$postIDs[5] as $expectedID ) { |
|---|
| 412 | $expected[] = $expectedID; |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | // Sort to make sure we are comparing the post IDs |
|---|
| 416 | sort($expected); |
|---|
| 417 | |
|---|
| 418 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | public function test_simple_count() { |
|---|
| 423 | |
|---|
| 424 | $args = http_build_query( array( |
|---|
| 425 | 'post_type' => self::$post_type, |
|---|
| 426 | 'posts_per_page' => -1, |
|---|
| 427 | 'comment_count' => 5 |
|---|
| 428 | ) ); |
|---|
| 429 | $this->q->query( $args ); |
|---|
| 430 | |
|---|
| 431 | $found_post_ids = $this->helper_get_found_post_ids(); |
|---|
| 432 | |
|---|
| 433 | $expected = array(); |
|---|
| 434 | foreach( self::$postIDs[5] as $expectedID ) { |
|---|
| 435 | $expected[] = $expectedID; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | // Sort to make sure we are comparing the post IDs |
|---|
| 439 | sort($expected); |
|---|
| 440 | |
|---|
| 441 | $this->assertEquals( $found_post_ids, $expected ); |
|---|
| 442 | } |
|---|
| 443 | } |
|---|