[ Index ] |
PHP Cross Reference of BBPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Tests for the forum component count functions. 5 * 6 * @group forums 7 * @group functions 8 * @group counts 9 */ 10 class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 11 12 /** 13 * Generic function to test the forum counts with a new topic 14 */ 15 public function test_bbp_forum_new_topic_counts() { 16 remove_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10 ); 17 18 $f = $this->factory->forum->create(); 19 $t1 = $this->factory->topic->create( array( 20 'post_parent' => $f, 21 'post_author' => bbp_get_current_user_id(), 22 'topic_meta' => array( 23 'forum_id' => $f, 24 ), 25 ) ); 26 $u = $this->factory->user->create(); 27 28 // Don't attempt to send an email. This is for speed and PHP errors. 29 remove_action( 'bbp_new_topic', 'bbp_notify_forum_subscribers', 11, 4 ); 30 31 // Simulate the 'bbp_new_topic' action. 32 do_action( 'bbp_new_topic', $t1, $f, false, bbp_get_current_user_id(), $t1 ); 33 34 $count = bbp_get_forum_topic_count( $f, true, true ); 35 $this->assertSame( 1, $count ); 36 37 $count = bbp_get_forum_topic_count_hidden( $f, true, true ); 38 $this->assertSame( 0, $count ); 39 40 $t2 = $this->factory->topic->create( array( 41 'post_parent' => $f, 42 'post_author' => $u, 43 'topic_meta' => array( 44 'forum_id' => $f, 45 ), 46 ) ); 47 48 // Simulate the 'bbp_new_topic' action. 49 do_action( 'bbp_new_topic', $t2, $f, false, $u , $t2 ); 50 51 $count = bbp_get_forum_topic_count( $f, true, true ); 52 $this->assertSame( 2, $count ); 53 54 $count = bbp_get_forum_topic_count_hidden( $f, true, true ); 55 $this->assertSame( 0, $count ); 56 57 // Re-add removed actions. 58 add_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10, 2 ); 59 add_action( 'bbp_new_topic', 'bbp_notify_forum_subscribers', 11, 4 ); 60 } 61 62 /** 63 * Generic function to test the forum counts on a trashed/untrashed topic 64 */ 65 public function test_bbp_forum_trashed_untrashed_topic_counts() { 66 $f = $this->factory->forum->create(); 67 $t = $this->factory->topic->create_many( 2, array( 68 'post_parent' => $f, 69 'topic_meta' => array( 70 'forum_id' => $f, 71 ), 72 ) ); 73 $r1 = $this->factory->reply->create_many( 1, array( 74 'post_parent' => $t[0], 75 'reply_meta' => array( 76 'forum_id' => $f, 77 'topic_id' => $t[0], 78 ), 79 ) ); 80 $r2 = $this->factory->reply->create_many( 1, array( 81 'post_parent' => $t[1], 82 'reply_meta' => array( 83 'forum_id' => $f, 84 'topic_id' => $t[1], 85 ), 86 ) ); 87 88 $count = bbp_update_forum_topic_count( $f ); 89 $this->assertSame( 2, $count ); 90 91 $count = bbp_update_forum_topic_count_hidden( $f ); 92 $this->assertSame( 0, $count ); 93 94 $count = bbp_update_forum_reply_count( $f ); 95 $this->assertSame( 2, $count ); 96 97 // ToDo: Update this to use bbp_trash_topic(). 98 wp_trash_post( $t[1] ); 99 100 $count = bbp_get_forum_topic_count( $f, true, true ); 101 $this->assertSame( 1, $count ); 102 103 $count = bbp_get_forum_topic_count_hidden( $f, true, true ); 104 $this->assertSame( 1, $count ); 105 106 $count = bbp_get_forum_reply_count( $f, true, true ); 107 $this->assertSame( 1, $count ); 108 109 // ToDo: Update this to use bbp_untrash_topic(). 110 wp_untrash_post( $t[1] ); 111 112 $count = bbp_get_forum_topic_count( $f, true, true ); 113 $this->assertSame( 2, $count ); 114 115 $count = bbp_get_forum_topic_count_hidden( $f, true, true ); 116 $this->assertSame( 0, $count ); 117 118 $count = bbp_get_forum_reply_count( $f, true, true ); 119 $this->assertSame( 2, $count ); 120 } 121 122 /** 123 * Generic function to test the forum counts on a spammed/unspammed topic 124 */ 125 public function test_bbp_forum_spammed_unspammed_topic_counts() { 126 $f = $this->factory->forum->create(); 127 $t = $this->factory->topic->create_many( 2, array( 128 'post_parent' => $f, 129 'topic_meta' => array( 130 'forum_id' => $f, 131 ), 132 ) ); 133 $r1 = $this->factory->reply->create_many( 1, array( 134 'post_parent' => $t[0], 135 'reply_meta' => array( 136 'forum_id' => $f, 137 'topic_id' => $t[0], 138 ), 139 ) ); 140 $r2 = $this->factory->reply->create_many( 1, array( 141 'post_parent' => $t[1], 142 'reply_meta' => array( 143 'forum_id' => $f, 144 'topic_id' => $t[1], 145 ), 146 ) ); 147 148 $count = bbp_update_forum_topic_count( $f ); 149 $this->assertSame( 2, $count ); 150 151 $count = bbp_update_forum_topic_count_hidden( $f ); 152 $this->assertSame( 0, $count ); 153 154 $count = bbp_update_forum_reply_count( $f ); 155 $this->assertSame( 2, $count ); 156 157 bbp_spam_topic( $t[1] ); 158 159 $count = bbp_get_forum_topic_count( $f, true, true ); 160 $this->assertSame( 1, $count ); 161 162 $count = bbp_get_forum_topic_count_hidden( $f, true, true ); 163 $this->assertSame( 1, $count ); 164 165 $count = bbp_get_forum_reply_count( $f, true, true ); 166 $this->assertSame( 1, $count ); 167 168 bbp_unspam_topic( $t[1] ); 169 170 $count = bbp_get_forum_topic_count( $f, true, true ); 171 $this->assertSame( 2, $count ); 172 173 $count = bbp_get_forum_topic_count_hidden( $f, true, true ); 174 $this->assertSame( 0, $count ); 175 176 $count = bbp_get_forum_reply_count( $f, true, true ); 177 $this->assertSame( 2, $count ); 178 } 179 180 /** 181 * Generic function to test the forum counts on a approved/unapproved topic 182 */ 183 public function test_bbp_forum_approved_unapproved_topic_counts() { 184 $f = $this->factory->forum->create(); 185 $t = $this->factory->topic->create_many( 3, array( 186 'post_parent' => $f, 187 'topic_meta' => array( 188 'forum_id' => $f, 189 ), 190 ) ); 191 $r1 = $this->factory->reply->create_many( 2, array( 192 'post_parent' => $t[1], 193 'reply_meta' => array( 194 'forum_id' => $f, 195 'topic_id' => $t[1], 196 ), 197 ) ); 198 $r2 = $this->factory->reply->create_many( 2, array( 199 'post_parent' => $t[2], 200 'reply_meta' => array( 201 'forum_id' => $f, 202 'topic_id' => $t[2], 203 ), 204 ) ); 205 206 $count = bbp_update_forum_topic_count( $f ); 207 $this->assertSame( 3, $count ); 208 209 $count = bbp_update_forum_topic_count_hidden( $f ); 210 $this->assertSame( 0, $count ); 211 212 $count = bbp_update_forum_reply_count( $f ); 213 $this->assertSame( 4, $count ); 214 215 bbp_unapprove_topic( $t[2] ); 216 217 $count = bbp_get_forum_topic_count( $f, true, true ); 218 $this->assertSame( 2, $count ); 219 220 $count = bbp_get_forum_topic_count_hidden( $f, true, true ); 221 $this->assertSame( 1, $count ); 222 223 $count = bbp_get_forum_reply_count( $f, true, true ); 224 $this->assertSame( 2, $count ); 225 226 bbp_approve_topic( $t[2] ); 227 228 $count = bbp_get_forum_topic_count( $f, true, true ); 229 $this->assertSame( 3, $count ); 230 231 $count = bbp_get_forum_topic_count_hidden( $f, true, true ); 232 $this->assertSame( 0, $count ); 233 234 $count = bbp_get_forum_reply_count( $f, true, true ); 235 $this->assertSame( 4, $count ); 236 } 237 238 /** 239 * @covers ::bbp_bump_forum_topic_count 240 */ 241 public function test_bbp_bump_forum_topic_count() { 242 $f = $this->factory->forum->create(); 243 244 $count = bbp_get_forum_topic_count( $f ); 245 $this->assertSame( '0', $count ); 246 247 bbp_bump_forum_topic_count( $f ); 248 249 $count = bbp_get_forum_topic_count( $f ); 250 $this->assertSame( '1', $count ); 251 } 252 253 /** 254 * @covers ::bbp_increase_forum_topic_count 255 */ 256 public function test_bbp_increase_forum_topic_count() { 257 $f = $this->factory->forum->create(); 258 259 $count = bbp_get_forum_topic_count( $f ); 260 $this->assertSame( '0', $count ); 261 262 bbp_increase_forum_topic_count( $f ); 263 264 $count = bbp_get_forum_topic_count( $f ); 265 $this->assertSame( '1', $count ); 266 } 267 268 /** 269 * @covers ::bbp_decrease_forum_topic_count 270 */ 271 public function test_bbp_decrease_forum_topic_count() { 272 $f = $this->factory->forum->create(); 273 274 $count = bbp_get_forum_topic_count( $f ); 275 $this->assertSame( '0', $count ); 276 277 $t = $this->factory->topic->create_many( 2, array( 278 'post_parent' => $f, 279 ) ); 280 281 bbp_update_forum_topic_count( $f ); 282 283 $count = bbp_get_forum_topic_count( $f ); 284 $this->assertSame( '2', $count ); 285 286 bbp_update_forum_topic_count( $f ); 287 288 bbp_decrease_forum_topic_count( $f ); 289 290 $count = bbp_get_forum_topic_count( $f ); 291 $this->assertSame( '1', $count ); 292 } 293 294 /** 295 * @covers ::bbp_bump_forum_topic_count_hidden 296 */ 297 public function test_bbp_bump_forum_topic_count_hidden() { 298 $f = $this->factory->forum->create(); 299 300 $count = bbp_get_forum_topic_count_hidden( $f ); 301 $this->assertSame( '0', $count ); 302 303 bbp_bump_forum_topic_count_hidden( $f ); 304 305 $count = bbp_get_forum_topic_count_hidden( $f ); 306 $this->assertSame( '1', $count ); 307 } 308 309 /** 310 * @covers ::bbp_increase_forum_topic_count_hidden 311 */ 312 public function test_bbp_increase_forum_topic_count_hidden() { 313 $f = $this->factory->forum->create(); 314 315 $count = bbp_get_forum_topic_count_hidden( $f ); 316 $this->assertSame( '0', $count ); 317 318 bbp_increase_forum_topic_count_hidden( $f ); 319 320 $count = bbp_get_forum_topic_count_hidden( $f ); 321 $this->assertSame( '1', $count ); 322 } 323 324 /** 325 * @covers ::bbp_decrease_forum_topic_count_hidden 326 */ 327 public function test_bbp_decrease_forum_topic_count_hidden() { 328 $f = $this->factory->forum->create(); 329 330 $count = bbp_get_forum_topic_count_hidden( $f ); 331 $this->assertSame( '0', $count ); 332 333 $t = $this->factory->topic->create_many( 2, array( 334 'post_parent' => $f, 335 'post_status' => bbp_get_spam_status_id(), 336 'topic_meta' => array( 337 'forum_id' => $f, 338 'spam_meta_status' => 'publish', 339 ) 340 ) ); 341 342 bbp_update_forum_topic_count_hidden( $f ); 343 344 $count = bbp_get_forum_topic_count_hidden( $f ); 345 $this->assertSame( '2', $count ); 346 347 bbp_decrease_forum_topic_count_hidden( $f ); 348 349 $count = bbp_get_forum_topic_count_hidden( $f ); 350 $this->assertSame( '1', $count ); 351 } 352 353 /** 354 * @covers ::bbp_bump_forum_reply_count 355 */ 356 public function test_bbp_bump_forum_reply_count() { 357 $f = $this->factory->forum->create(); 358 359 $count = bbp_get_forum_reply_count( $f ); 360 $this->assertSame( '0', $count ); 361 362 bbp_bump_forum_reply_count( $f ); 363 364 $count = bbp_get_forum_reply_count( $f ); 365 $this->assertSame( '1', $count ); 366 } 367 368 /** 369 * @covers ::bbp_increase_forum_reply_count 370 */ 371 public function test_bbp_increase_forum_reply_count() { 372 $f = $this->factory->forum->create(); 373 374 $count = bbp_get_forum_reply_count( $f ); 375 $this->assertSame( '0', $count ); 376 377 bbp_increase_forum_reply_count( $f ); 378 379 $count = bbp_get_forum_reply_count( $f ); 380 $this->assertSame( '1', $count ); 381 } 382 383 /** 384 * @covers ::bbp_decrease_forum_reply_count 385 */ 386 public function test_bbp_decrease_forum_reply_count() { 387 $f = $this->factory->forum->create(); 388 389 $count = bbp_get_forum_reply_count( $f ); 390 $this->assertSame( '0', $count ); 391 392 $t = $this->factory->topic->create( array( 393 'post_parent' => $f, 394 ) ); 395 396 $r = $this->factory->reply->create_many( 2, array( 397 'post_parent' => $t, 398 ) ); 399 400 bbp_update_forum_reply_count( $f ); 401 402 $count = bbp_get_forum_reply_count( $f ); 403 $this->assertSame( '2', $count ); 404 405 bbp_decrease_forum_reply_count( $f ); 406 407 $count = bbp_get_forum_reply_count( $f ); 408 $this->assertSame( '1', $count ); 409 } 410 411 /** 412 * @covers ::bbp_update_forum_subforum_count 413 */ 414 public function test_bbp_update_forum_subforum_count() { 415 $f1 = $this->factory->forum->create(); 416 417 $count = bbp_get_forum_subforum_count( $f1, true ); 418 $this->assertSame( 0, $count ); 419 420 $f2 = $this->factory->forum->create_many( 3, array( 421 'post_parent' => $f1, 422 ) ); 423 424 $count = bbp_get_forum_subforum_count( $f1, true ); 425 $this->assertSame( 3, $count ); 426 427 bbp_update_forum_subforum_count( $f1, 10 ); 428 429 $count = bbp_get_forum_subforum_count( $f1, true ); 430 $this->assertSame( 10, $count ); 431 432 bbp_update_forum_subforum_count( $f1 ); 433 434 $count = bbp_get_forum_subforum_count( $f1, true ); 435 $this->assertSame( 3, $count ); 436 } 437 438 /** 439 * @covers ::bbp_update_forum_topic_count 440 */ 441 public function test_bbp_update_forum_topic_count() { 442 // Create a top level forum f1 443 $f1 = $this->factory->forum->create(); 444 445 bbp_normalize_forum( $f1 ); 446 447 $count = bbp_get_forum_topic_count( $f1 ); 448 $this->assertSame( '0', $count ); 449 450 // Create 3 topics in f1 451 $t = $this->factory->topic->create_many( 3, array( 452 'post_parent' => $f1, 453 ) ); 454 455 bbp_update_forum_topic_count( $f1 ); 456 457 $count = bbp_get_forum_topic_count( $f1 ); 458 $this->assertSame( '3', $count ); 459 460 // Create a new sub forum of f1 461 $f2 = $this->factory->forum->create( array( 462 'post_parent' => $f1, 463 ) ); 464 465 // Create another sub forum of f1 466 $f3 = $this->factory->forum->create( array( 467 'post_parent' => $f1, 468 ) ); 469 470 bbp_update_forum_topic_count( $f1 ); 471 bbp_update_forum_topic_count( $f2 ); 472 bbp_update_forum_topic_count( $f3 ); 473 474 $count = bbp_get_forum_topic_count( $f1 ); 475 $this->assertSame( '3', $count ); 476 477 $count = bbp_get_forum_topic_count( $f2 ); 478 $this->assertSame( '0', $count ); 479 480 $count = bbp_get_forum_topic_count( $f3 ); 481 $this->assertSame( '0', $count ); 482 483 // Create some topics in forum f2 484 $this->factory->topic->create_many( 4, array( 485 'post_parent' => $f2, 486 ) ); 487 488 bbp_update_forum_topic_count( $f1 ); 489 bbp_update_forum_topic_count( $f2 ); 490 bbp_update_forum_topic_count( $f3 ); 491 492 $count = bbp_get_forum_topic_count( $f1 ); 493 $this->assertSame( '7', $count ); 494 495 $count = bbp_get_forum_topic_count( $f2 ); 496 $this->assertSame( '4', $count ); 497 498 $count = bbp_get_forum_topic_count( $f3 ); 499 $this->assertSame( '0', $count ); 500 501 // Create some topics in forum f3 502 $this->factory->topic->create_many( 5, array( 503 'post_parent' => $f3, 504 ) ); 505 506 bbp_update_forum_topic_count( $f1 ); 507 bbp_update_forum_topic_count( $f2 ); 508 bbp_update_forum_topic_count( $f3 ); 509 510 $count = bbp_get_forum_topic_count( $f1 ); 511 $this->assertSame( '12', $count ); 512 513 $count = bbp_get_forum_topic_count( $f2 ); 514 $this->assertSame( '4', $count ); 515 516 $count = bbp_get_forum_topic_count( $f3 ); 517 $this->assertSame( '5', $count ); 518 } 519 520 /** 521 * @covers ::bbp_update_forum_topic_count_hidden 522 */ 523 public function test_bbp_update_forum_topic_count_hidden() { 524 $f = $this->factory->forum->create(); 525 526 $count = bbp_get_forum_topic_count( $f, false, true ); 527 $this->assertSame( 0, $count ); 528 529 $t = $this->factory->topic->create_many( 3, array( 530 'post_parent' => $f, 531 'topic_meta' => array( 532 'forum_id' => $f, 533 ), 534 ) ); 535 536 bbp_update_forum_topic_count_hidden( $f ); 537 538 $count = bbp_get_forum_topic_count_hidden( $f, true ); 539 $this->assertSame( 0, $count );; 540 541 bbp_spam_topic( $t[2] ); 542 543 bbp_update_forum_topic_count_hidden( $f ); 544 545 $count = bbp_get_forum_topic_count_hidden( $f, true ); 546 $this->assertSame( 1, $count );; 547 548 bbp_unapprove_topic( $t[0] ); 549 550 bbp_update_forum_topic_count_hidden( $f ); 551 552 $count = bbp_get_forum_topic_count_hidden( $f, true ); 553 $this->assertSame( 2, $count ); 554 } 555 556 /** 557 * @covers ::bbp_update_forum_reply_count 558 */ 559 public function test_bbp_update_forum_reply_count() { 560 $f1 = $this->factory->forum->create(); 561 562 $f2 = $this->factory->forum->create( array( 563 'post_parent' => $f1, 564 ) ); 565 566 $t1 = $this->factory->topic->create( array( 567 'post_parent' => $f1, 568 'topic_meta' => array( 569 'forum_id' => $f1, 570 ), 571 ) ); 572 573 $t2 = $this->factory->topic->create( array( 574 'post_parent' => $f2, 575 'topic_meta' => array( 576 'forum_id' => $f2, 577 ), 578 ) ); 579 580 $count = bbp_get_forum_reply_count( $f1, false, true ); 581 $this->assertSame( 0, $count ); 582 583 $count = bbp_update_forum_reply_count( $f1 ); 584 $this->assertSame( 0, $count ); 585 586 $this->factory->reply->create_many( 3, array( 587 'post_parent' => $t1, 588 'reply_meta' => array( 589 'forum_id' => $f1, 590 'topic_id' => $t1, 591 ), 592 ) ); 593 594 $count = bbp_update_forum_reply_count( $f1 ); 595 $this->assertSame( 3, $count ); 596 597 $this->factory->reply->create_many( 3, array( 598 'post_parent' => $t2, 599 'reply_meta' => array( 600 'forum_id' => $f2, 601 'topic_id' => $t2, 602 ), 603 ) ); 604 605 $count = bbp_update_forum_reply_count( $f1 ); 606 $this->assertSame( 6, $count ); 607 608 $count = bbp_update_forum_reply_count( $f2 ); 609 $this->assertSame( 3, $count ); 610 } 611 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Dec 21 01:00:52 2024 | Cross-referenced by PHPXref 0.7.1 |