[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/tests/phpunit/testcases/topics/functions/ -> counts.php (source)

   1  <?php
   2  
   3  /**
   4   * Tests for the topic component count functions.
   5   *
   6   * @group topics
   7   * @group functions
   8   * @group counts
   9   */
  10  class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase {
  11  
  12      /**
  13       * Generic function to test the topics counts with a new reply
  14       */
  15  	public function test_bbp_topic_new_reply_counts() {
  16          remove_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10 );
  17  
  18          $u  = $this->factory->user->create();
  19          $u2 = $this->factory->user->create();
  20          $f  = $this->factory->forum->create();
  21          $t  = $this->factory->topic->create( array(
  22              'post_parent' => $f,
  23              'post_author' => $u,
  24              'topic_meta' => array(
  25                  'forum_id' => $f,
  26              ),
  27          ) );
  28          $r1 = $this->factory->reply->create( array(
  29              'post_parent' => $t,
  30              'post_author' => $u,
  31              'reply_meta' => array(
  32                  'forum_id' => $f,
  33                  'topic_id' => $t,
  34              ),
  35          ) );
  36  
  37          // Don't attempt to send an email. This is for speed and PHP errors.
  38          remove_action( 'bbp_new_reply', 'bbp_notify_topic_subscribers', 11, 5 );
  39  
  40          // Simulate the 'bbp_new_reply' action.
  41          do_action( 'bbp_new_reply', $r1, $t, $f, false, bbp_get_current_user_id() );
  42  
  43          $count = bbp_get_topic_reply_count( $t, true );
  44          $this->assertSame( 1, $count );
  45  
  46          $count = bbp_get_topic_reply_count_hidden( $t, true );
  47          $this->assertSame( 0, $count );
  48  
  49          $count = bbp_get_topic_voice_count( $t, true );
  50          $this->assertSame( 1, $count );
  51  
  52          $r2 = $this->factory->reply->create( array(
  53              'post_parent' => $t,
  54              'post_author' => $u2,
  55              'reply_meta' => array(
  56                  'forum_id' => $f,
  57                  'topic_id' => $t,
  58              ),
  59          ) );
  60  
  61          // Simulate the 'bbp_new_topic' action.
  62          do_action( 'bbp_new_reply', $r2, $t, $f, false, $u2 );
  63  
  64          $count = bbp_get_topic_reply_count( $t, true );
  65          $this->assertSame( 2, $count );
  66  
  67          $count = bbp_get_topic_reply_count_hidden( $t, true );
  68          $this->assertSame( 0, $count );
  69  
  70          $count = bbp_get_topic_voice_count( $t, true );
  71          $this->assertSame( 2, $count );
  72  
  73          // Re-add removed actions.
  74          add_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10, 2 );
  75          add_action( 'bbp_new_reply',    'bbp_notify_topic_subscribers',   11, 5 );
  76      }
  77  
  78      /**
  79       * Generic function to test the topic counts on a deleted reply
  80       */
  81  	public function test_bbp_topic_deleted_reply_counts() {
  82          $u  = $this->factory->user->create();
  83          $u2 = $this->factory->user->create();
  84          $f  = $this->factory->forum->create();
  85          $t  = $this->factory->topic->create( array(
  86              'post_parent' => $f,
  87              'post_author' => $u,
  88              'topic_meta' => array(
  89                  'forum_id' => $f,
  90              ),
  91          ) );
  92          $r1 = $this->factory->reply->create( array(
  93              'post_parent' => $t,
  94              'post_author' => $u,
  95              'reply_meta' => array(
  96                  'forum_id' => $f,
  97                  'topic_id' => $t,
  98              ),
  99          ) );
 100  
 101          $count = bbp_update_topic_reply_count( $t );
 102          $this->assertSame( 1, $count );
 103  
 104          $count = bbp_update_topic_reply_count_hidden( $t );
 105          $this->assertSame( 0, $count );
 106  
 107          $count = bbp_update_topic_voice_count( $t );
 108          $this->assertSame( 1, $count );
 109  
 110          $r2 = $this->factory->reply->create( array(
 111              'post_parent' => $t,
 112              'post_author' => $u2,
 113              'reply_meta' => array(
 114                  'forum_id' => $f,
 115                  'topic_id' => $t,
 116              ),
 117          ) );
 118  
 119          $count = bbp_update_topic_reply_count( $t );
 120          $this->assertSame( 2, $count );
 121  
 122          $count = bbp_update_topic_reply_count_hidden( $t );
 123          $this->assertSame( 0, $count );
 124  
 125          $count = bbp_update_topic_voice_count( $t );
 126          $this->assertSame( 2, $count );
 127  
 128          // ToDo: Update this to use bbp_delete_reply().
 129          clean_post_cache( $t );
 130          wp_delete_post( $r2, true );
 131  
 132          $count = bbp_get_topic_reply_count( $t, true );
 133          $this->assertSame( 1, $count );
 134  
 135          $count = bbp_get_topic_reply_count_hidden( $t, true );
 136          $this->assertSame( 0, $count );
 137  
 138          $count = bbp_get_topic_voice_count( $t, true );
 139          $this->assertSame( 1, $count );
 140      }
 141  
 142      /**
 143       * Generic function to test the topic counts on a trashed/untrashed reply
 144       */
 145  	public function test_bbp_topic_trashed_untrashed_reply_counts() {
 146          $u  = $this->factory->user->create();
 147          $u2 = $this->factory->user->create();
 148          $f  = $this->factory->forum->create();
 149          $t  = $this->factory->topic->create( array(
 150              'post_parent' => $f,
 151              'post_author' => $u,
 152              'topic_meta' => array(
 153                  'forum_id' => $f,
 154              ),
 155          ) );
 156          $r = $this->factory->reply->create_many( 2, array(
 157              'post_parent' => $t,
 158              'post_author' => $u,
 159              'reply_meta' => array(
 160                  'forum_id' => $f,
 161                  'topic_id' => $t,
 162              ),
 163          ) );
 164          $r3 = $this->factory->reply->create( array(
 165              'post_parent' => $t,
 166              'post_author' => $u2,
 167              'reply_meta' => array(
 168                  'forum_id' => $f,
 169                  'topic_id' => $t,
 170              ),
 171          ) );
 172  
 173          $count = bbp_update_topic_reply_count( $t );
 174          $this->assertSame( 3, $count );
 175  
 176          $count = bbp_update_topic_reply_count_hidden( $t );
 177          $this->assertSame( 0, $count );
 178  
 179          $count = bbp_update_topic_voice_count( $t );
 180          $this->assertSame( 2, $count );
 181  
 182          // ToDo: Update this to use bbp_trash_reply().
 183          wp_trash_post( $r3 );
 184  
 185          $count = bbp_get_topic_reply_count( $t, true );
 186          $this->assertSame( 2, $count );
 187  
 188          $count = bbp_get_topic_reply_count_hidden( $t, true );
 189          $this->assertSame( 1, $count );
 190  
 191          $count = bbp_get_topic_voice_count( $t, true );
 192          $this->assertSame( 1, $count );
 193  
 194          // ToDo: Update this to use bbp_untrash_reply().
 195          wp_untrash_post( $r3 );
 196  
 197          $count = bbp_get_topic_reply_count( $t, true );
 198          $this->assertSame( 3, $count );
 199  
 200          $count = bbp_get_topic_reply_count_hidden( $t, true );
 201          $this->assertSame( 0, $count );
 202  
 203          $count = bbp_get_topic_voice_count( $t, true );
 204          $this->assertSame( 2, $count );
 205      }
 206  
 207      /**
 208       * Generic function to test the topic counts on a spammed/unspammed reply
 209       */
 210  	public function test_bbp_topic_spammed_unspammed_reply_counts() {
 211          $u  = $this->factory->user->create();
 212          $u2 = $this->factory->user->create();
 213          $f  = $this->factory->forum->create();
 214          $t  = $this->factory->topic->create( array(
 215              'post_parent' => $f,
 216              'post_author' => $u,
 217              'topic_meta' => array(
 218                  'forum_id' => $f,
 219              ),
 220          ) );
 221          $r = $this->factory->reply->create_many( 2, array(
 222              'post_parent' => $t,
 223              'post_author' => $u,
 224              'reply_meta' => array(
 225                  'forum_id' => $f,
 226                  'topic_id' => $t,
 227              ),
 228          ) );
 229          $r3 = $this->factory->reply->create( array(
 230              'post_parent' => $t,
 231              'post_author' => $u2,
 232              'reply_meta' => array(
 233                  'forum_id' => $f,
 234                  'topic_id' => $t,
 235              ),
 236          ) );
 237  
 238          $count = bbp_update_topic_reply_count( $t );
 239          $this->assertSame( 3, $count );
 240  
 241          $count = bbp_update_topic_reply_count_hidden( $t );
 242          $this->assertSame( 0, $count );
 243  
 244          $count = bbp_update_topic_voice_count( $t );
 245          $this->assertSame( 2, $count );
 246  
 247          bbp_spam_reply( $r3 );
 248  
 249          $count = bbp_get_topic_reply_count( $t, true );
 250          $this->assertSame( 2, $count );
 251  
 252          $count = bbp_get_topic_reply_count_hidden( $t, true );
 253          $this->assertSame( 1, $count );
 254  
 255          $count = bbp_get_topic_voice_count( $t, true );
 256          $this->assertSame( 1, $count );
 257  
 258          bbp_unspam_reply( $r3 );
 259  
 260          $count = bbp_get_topic_reply_count( $t, true );
 261          $this->assertSame( 3, $count );
 262  
 263          $count = bbp_get_topic_reply_count_hidden( $t, true );
 264          $this->assertSame( 0, $count );
 265  
 266          $count = bbp_get_topic_voice_count( $t, true );
 267          $this->assertSame( 2, $count );
 268      }
 269  
 270      /**
 271       * Generic function to test the topic counts on a approved/unapproved reply
 272       */
 273      public function test_bbp_topic_approved_unapproved_reply_counts() {
 274          $u  = $this->factory->user->create();
 275          $u2 = $this->factory->user->create();
 276          $f  = $this->factory->forum->create();
 277          $t  = $this->factory->topic->create( array(
 278              'post_parent' => $f,
 279              'post_author' => $u,
 280              'topic_meta' => array(
 281                  'forum_id' => $f,
 282              ),
 283          ) );
 284          $r = $this->factory->reply->create_many( 2, array(
 285              'post_parent' => $t,
 286              'post_author' => $u,
 287              'reply_meta' => array(
 288                  'forum_id' => $f,
 289                  'topic_id' => $t,
 290              ),
 291          ) );
 292          $r3 = $this->factory->reply->create( array(
 293              'post_parent' => $t,
 294              'post_author' => $u2,
 295              'reply_meta' => array(
 296                  'forum_id' => $f,
 297                  'topic_id' => $t,
 298              ),
 299          ) );
 300  
 301          $count = bbp_update_topic_reply_count( $t );
 302          $this->assertSame( 3, $count );
 303  
 304          $count = bbp_update_topic_reply_count_hidden( $t );
 305          $this->assertSame( 0, $count );
 306  
 307          $count = bbp_update_topic_voice_count( $t );
 308          $this->assertSame( 2, $count );
 309  
 310          bbp_unapprove_reply( $r3 );
 311  
 312          $count = bbp_get_topic_reply_count( $t, true );
 313          $this->assertSame( 2, $count );
 314  
 315          $count = bbp_get_topic_reply_count_hidden( $t, true );
 316          $this->assertSame( 1, $count );
 317  
 318          $count = bbp_get_topic_voice_count( $t, true );
 319          $this->assertSame( 1, $count );
 320  
 321          bbp_approve_reply( $r3 );
 322  
 323          $count = bbp_get_topic_reply_count( $t, true );
 324          $this->assertSame( 3, $count );
 325  
 326          $count = bbp_get_topic_reply_count_hidden( $t, true );
 327          $this->assertSame( 0, $count );
 328  
 329          $count = bbp_get_topic_voice_count( $t, true );
 330          $this->assertSame( 2, $count );
 331      }
 332  
 333      /**
 334       * @covers ::bbp_bump_topic_reply_count
 335       */
 336  	public function test_bbp_bump_topic_reply_count() {
 337          $t = $this->factory->topic->create();
 338  
 339          $count = bbp_get_topic_reply_count( $t );
 340          $this->assertSame( '0', $count );
 341  
 342          $count = bbp_bump_topic_reply_count( $t );
 343          $this->assertSame( 1, $count );
 344  
 345          $count = bbp_bump_topic_reply_count( $t, 3 );
 346  
 347          $count = bbp_get_topic_reply_count( $t );
 348          $this->assertSame( '4', $count );
 349      }
 350  
 351      /**
 352       * @covers ::bbp_increase_topic_reply_count
 353       */
 354  	public function test_bbp_increase_topic_reply_count() {
 355          $t = $this->factory->topic->create();
 356  
 357          $count = bbp_get_topic_reply_count( $t );
 358          $this->assertSame( '0', $count );
 359  
 360          bbp_increase_topic_reply_count( $t );
 361  
 362          $count = bbp_get_topic_reply_count( $t );
 363          $this->assertSame( '1', $count );
 364      }
 365  
 366      /**
 367       * @covers ::bbp_decrease_topic_reply_count
 368       */
 369  	public function test_bbp_decrease_topic_reply_count() {
 370          $t = $this->factory->topic->create();
 371  
 372          $count = bbp_get_topic_reply_count( $t );
 373          $this->assertSame( '0', $count );
 374  
 375          // Set the count manually to 2
 376          bbp_update_topic_reply_count( $t, 2 );
 377  
 378          $count = bbp_get_topic_reply_count( $t );
 379          $this->assertSame( '2', $count );
 380  
 381          bbp_decrease_topic_reply_count( $t );
 382  
 383          $count = bbp_get_topic_reply_count( $t );
 384          $this->assertSame( '1', $count );
 385      }
 386  
 387      /**
 388       * @covers ::bbp_bump_topic_reply_count_hidden
 389       */
 390  	public function test_bbp_bump_topic_reply_count_hidden() {
 391          $t = $this->factory->topic->create();
 392  
 393          $count = bbp_get_topic_reply_count_hidden( $t );
 394          $this->assertSame( '0', $count );
 395  
 396          $count = bbp_bump_topic_reply_count_hidden( $t );
 397          $this->assertSame( 1, $count );
 398  
 399          bbp_bump_topic_reply_count_hidden( $t, 3 );
 400  
 401          $count = bbp_get_topic_reply_count_hidden( $t );
 402          $this->assertSame( '4', $count );
 403      }
 404  
 405      /**
 406       * @covers ::bbp_increase_topic_reply_count_hidden
 407       */
 408  	public function test_bbp_increase_topic_reply_count_hidden() {
 409          $t = $this->factory->topic->create();
 410  
 411          $count = bbp_get_topic_reply_count_hidden( $t );
 412          $this->assertSame( '0', $count );
 413  
 414          bbp_increase_topic_reply_count_hidden( $t );
 415  
 416          $count = bbp_get_topic_reply_count_hidden( $t );
 417          $this->assertSame( '1', $count );
 418      }
 419  
 420      /**
 421       * @covers ::bbp_decrease_topic_reply_count_hidden
 422       */
 423  	public function test_bbp_decrease_topic_reply_count_hidden() {
 424          $t = $this->factory->topic->create();
 425  
 426          $count = bbp_get_topic_reply_count_hidden( $t );
 427          $this->assertSame( '0', $count );
 428  
 429          // Set the count manually to 2
 430          bbp_update_topic_reply_count_hidden( $t, 2 );
 431  
 432          $count = bbp_get_topic_reply_count_hidden( $t );
 433          $this->assertSame( '2', $count );
 434  
 435          bbp_decrease_topic_reply_count_hidden( $t );
 436  
 437          $count = bbp_get_topic_reply_count_hidden( $t );
 438          $this->assertSame( '1', $count );
 439      }
 440  
 441      /**
 442       * @covers ::bbp_update_topic_reply_count
 443       */
 444  	public function test_bbp_update_topic_reply_count() {
 445          // Create a forum
 446          $f = $this->factory->forum->create();
 447  
 448          // Create a topic
 449          $t = $this->factory->topic->create( array(
 450              'post_parent' => $f,
 451              'topic_meta' => array(
 452                  'forum_id' => $f,
 453              )
 454          ) );
 455  
 456          // Start with zero
 457          $count = bbp_get_topic_reply_count( $t );
 458          $this->assertSame( '0', $count );
 459  
 460          // Create 3 replies
 461          $r1 = $this->factory->reply->create_many( 3, array(
 462              'post_parent' => $t,
 463              'reply_meta' => array(
 464                  'forum_id' => $f,
 465                  'topic_id' => $t,
 466              )
 467          ) );
 468  
 469          $count = bbp_get_topic_reply_count( $t );
 470          $this->assertSame( '3', $count );
 471  
 472          bbp_update_topic_reply_count( $t );
 473  
 474          $count = bbp_get_topic_reply_count( $t );
 475          $this->assertSame( '3', $count );
 476  
 477          // Create another reply
 478          $r2 = $this->factory->reply->create( array(
 479              'post_parent' => $t,
 480              'reply_meta' => array(
 481                  'forum_id' => $f,
 482                  'topic_id' => $t,
 483              )
 484          ) );
 485  
 486          // Test update using reply id
 487          bbp_update_topic_reply_count( $r2 );
 488          $count = bbp_get_topic_reply_count( $t );
 489          $this->assertSame( '4', $count );
 490  
 491          // Spam a reply
 492          bbp_spam_reply( $r2 );
 493  
 494          bbp_update_topic_reply_count( $t );
 495          $count = bbp_get_topic_reply_count( $t );
 496          $this->assertSame( '3', $count );
 497  
 498          // Set the reply count manually
 499          bbp_update_topic_reply_count( $t, 7 );
 500          $count = bbp_get_topic_reply_count( $t );
 501          $this->assertSame( '7', $count );
 502      }
 503  
 504      /**
 505       * @covers ::bbp_update_topic_reply_count_hidden
 506       */
 507  	public function test_bbp_update_topic_reply_count_hidden() {
 508          // Create a forum
 509          $f = $this->factory->forum->create();
 510  
 511          // Create a topic
 512          $t = $this->factory->topic->create( array(
 513              'post_parent' => $f,
 514              'topic_meta' => array(
 515                  'forum_id' => $f,
 516              )
 517          ) );
 518  
 519          // Start with zero
 520          $count = bbp_get_topic_reply_count_hidden( $t );
 521          $this->assertSame( '0', $count );
 522  
 523          $r = $this->factory->reply->create_many( 3, array(
 524              'post_parent' => $t,
 525              'reply_meta' => array(
 526                  'forum_id' => $f,
 527                  'topic_id' => $t,
 528              )
 529          ) );
 530  
 531          bbp_update_topic_reply_count_hidden( $t );
 532          $count = bbp_get_topic_reply_count_hidden( $t );
 533          $this->assertSame( '0', $count );
 534  
 535          bbp_spam_reply( $r[2] );
 536  
 537          bbp_update_topic_reply_count_hidden( $t );
 538          $count = bbp_get_topic_reply_count_hidden( $t );
 539          $this->assertSame( '1', $count );
 540  
 541          bbp_unapprove_reply( $r[0] );
 542  
 543          bbp_update_topic_reply_count_hidden( $t );
 544          $count = bbp_get_topic_reply_count_hidden( $t );
 545          $this->assertSame( '2', $count );
 546      }
 547  
 548      /**
 549       * @covers ::bbp_update_topic_voice_count
 550       */
 551  	public function test_bbp_update_topic_voice_count() {
 552          $u = $this->factory->user->create_many( 2 );
 553          $t = $this->factory->topic->create();
 554  
 555          bbp_update_topic_voice_count( $t );
 556          $count = bbp_get_topic_voice_count( $t );
 557          $this->assertSame( '0', $count );
 558  
 559          $r = $this->factory->reply->create( array(
 560              'post_author' => $u[0],
 561              'post_parent' => $t,
 562          ) );
 563  
 564          bbp_update_topic_voice_count( $t );
 565          $count = bbp_get_topic_voice_count( $t );
 566          $this->assertSame( '1', $count );
 567  
 568          $r = $this->factory->reply->create( array(
 569              'post_author' => $u[1],
 570              'post_parent' => $t,
 571          ) );
 572  
 573          bbp_update_topic_voice_count( $t );
 574          $count = bbp_get_topic_voice_count( $t );
 575          $this->assertSame( '2', $count );
 576      }
 577  
 578      /**
 579       * @covers ::bbp_update_topic_anonymous_reply_count
 580       * @todo   Implement test_bbp_update_topic_anonymous_reply_count().
 581       */
 582  	public function test_bbp_update_topic_anonymous_reply_count() {
 583          // Remove the following lines when you implement this test.
 584          $this->markTestIncomplete(
 585              'This test has not been implemented yet.'
 586          );
 587      }
 588  }


Generated: Tue Mar 19 01:01:02 2024 Cross-referenced by PHPXref 0.7.1