[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group blogs
   5   */
   6  class BP_Tests_Blogs_Functions extends BP_UnitTestCase {
   7      /**
   8       * @group blogmeta
   9       * @group bp_blogs_delete_blogmeta
  10       */
  11      public function test_bp_blogs_delete_blogmeta_non_numeric_blog_id() {
  12          $this->assertFalse( bp_blogs_delete_blogmeta( 'foo' ) );
  13      }
  14  
  15      /**
  16       * @group blogmeta
  17       * @group bp_blogs_delete_blogmeta
  18       * @ticket BP5399
  19       */
  20      public function test_bp_blogs_delete_blogmeta_illegal_characters() {
  21          $this->assertNotEmpty( bp_blogs_update_blogmeta( 1, 'foo', 'bar' ) );
  22          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo' ) );
  23          $krazy_key = ' f!@#$%^o *(){}o?+';
  24          $this->assertFalse( bp_blogs_delete_blogmeta( 1, $krazy_key ) );
  25          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo' ) );
  26      }
  27  
  28      /**
  29       * @group blogmeta
  30       * @group bp_blogs_delete_blogmeta
  31       * @ticket BP5399
  32       */
  33  	public function test_bp_blogs_delete_blogmeta_trim_meta_value() {
  34          $this->assertNotEmpty( bp_blogs_update_blogmeta( 1, 'foo', 'bar' ) );
  35          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo' ) );
  36          bp_blogs_delete_blogmeta( 1, 'foo', '   bar  ' );
  37          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo' ) );
  38      }
  39  
  40      /**
  41       * @group blogmeta
  42       * @group bp_blogs_delete_blogmeta
  43       */
  44  	public function test_bp_blogs_delete_blogmeta_no_meta_key() {
  45          bp_blogs_update_blogmeta( 1, 'foo', 'bar' );
  46          bp_blogs_update_blogmeta( 1, 'foo2', 'bar2' );
  47          $this->assertNotEmpty( bp_blogs_get_blogmeta( 1 ) );
  48          $this->assertTrue( bp_blogs_delete_blogmeta( 1 ) );
  49          $this->assertSame( array(), bp_blogs_get_blogmeta( 1 ) );
  50      }
  51  
  52      /**
  53       * @group blogmeta
  54       * @group bp_blogs_delete_blogmeta
  55       */
  56  	public function test_bp_blogs_delete_blogmeta_with_meta_value() {
  57          bp_blogs_update_blogmeta( 1, 'foo', 'bar' );
  58          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo' ) );
  59          bp_blogs_delete_blogmeta( 1, 'foo', 'baz' );
  60          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo' ) );
  61          $this->assertTrue( bp_blogs_delete_blogmeta( 1, 'foo', 'bar' ) );
  62          $this->assertSame( '', bp_blogs_get_blogmeta( 1, 'foo' ) );
  63      }
  64  
  65      /**
  66       * @group blogmeta
  67       * @group bp_blogs_delete_blogmeta
  68       */
  69  	public function test_bp_blogs_delete_blogmeta_with_delete_all_but_no_meta_key() {
  70          // With no meta key, don't delete for all items - just delete
  71          // all for a single item
  72          bp_blogs_add_blogmeta( 1, 'foo', 'bar' );
  73          bp_blogs_add_blogmeta( 1, 'foo1', 'bar1' );
  74          bp_blogs_add_blogmeta( 2, 'foo', 'bar' );
  75          bp_blogs_add_blogmeta( 2, 'foo1', 'bar1' );
  76  
  77          $this->assertTrue( bp_blogs_delete_blogmeta( 1, '', '', true ) );
  78          $this->assertEmpty( bp_blogs_get_blogmeta( 1 ) );
  79          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 2, 'foo' ) );
  80          $this->assertSame( 'bar1', bp_blogs_get_blogmeta( 2, 'foo1' ) );
  81      }
  82  
  83      /**
  84       * @group blogmeta
  85       * @group bp_blogs_delete_blogmeta
  86       */
  87  	public function test_bp_blogs_delete_blogmeta_with_delete_all() {
  88          // With no meta key, don't delete for all items - just delete
  89          // all for a single item
  90          bp_blogs_add_blogmeta( 1, 'foo', 'bar' );
  91          bp_blogs_add_blogmeta( 1, 'foo1', 'bar1' );
  92          bp_blogs_add_blogmeta( 2, 'foo', 'bar' );
  93          bp_blogs_add_blogmeta( 2, 'foo1', 'bar1' );
  94  
  95          $this->assertTrue( bp_blogs_delete_blogmeta( 1, 'foo', '', true ) );
  96          $this->assertSame( '', bp_blogs_get_blogmeta( 1, 'foo' ) );
  97          $this->assertSame( '', bp_blogs_get_blogmeta( 2, 'foo' ) );
  98          $this->assertSame( 'bar1', bp_blogs_get_blogmeta( 1, 'foo1' ) );
  99          $this->assertSame( 'bar1', bp_blogs_get_blogmeta( 2, 'foo1' ) );
 100      }
 101  
 102      /**
 103       * @group blogmeta
 104       * @group bp_blogs_get_blogmeta
 105       */
 106  	public function test_bp_blogs_get_blogmeta_empty_blog_id() {
 107          $this->assertFalse( bp_blogs_get_blogmeta( 0 ) );
 108          $this->assertFalse( bp_blogs_get_blogmeta( '' ) );
 109      }
 110  
 111      /**
 112       * @group blogmeta
 113       * @group bp_blogs_get_blogmeta
 114       * @ticket BP5399
 115       */
 116  	public function test_bp_blogs_get_blogmeta_illegal_characters() {
 117          bp_blogs_update_blogmeta( 1, 'foo', 'bar' );
 118          $krazy_key = ' f!@#$%^o *(){}o?+';
 119          $this->assertSame( '', bp_blogs_get_blogmeta( 1, $krazy_key ) );
 120      }
 121  
 122      /**
 123       * @group blogmeta
 124       * @group bp_blogs_get_blogmeta
 125       */
 126  	public function test_bp_blogs_get_blogmeta_no_meta_key() {
 127          bp_blogs_update_blogmeta( 1, 'foo', 'bar' );
 128          bp_blogs_update_blogmeta( 1, 'foo2', 'bar2' );
 129  
 130          $expected = array(
 131              'foo' => array(
 132                  'bar',
 133              ),
 134              'foo2' => array(
 135                  'bar2',
 136              ),
 137          );
 138  
 139          $this->assertSame( $expected, bp_blogs_get_blogmeta( 1 ) );
 140      }
 141  
 142      /**
 143       * @group blogmeta
 144       * @group bp_blogs_get_blogmeta
 145       */
 146  	public function test_bp_blogs_get_blogmeta_no_meta_key_empty() {
 147          $this->assertSame( array(), bp_blogs_get_blogmeta( 1 ) );
 148      }
 149  
 150      /**
 151       * @group blogmeta
 152       * @group bp_blogs_get_blogmeta
 153       */
 154  	public function test_bp_blogs_get_blogmeta_single_true() {
 155          bp_blogs_add_blogmeta( 1, 'foo', 'bar' );
 156          bp_blogs_add_blogmeta( 1, 'foo', 'baz' );
 157          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo' ) ); // default is true
 158          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo', true ) );
 159      }
 160  
 161      /**
 162       * @group blogmeta
 163       * @group bp_blogs_get_blogmeta
 164       */
 165  	public function test_bp_blogs_get_blogmeta_single_false() {
 166          bp_blogs_add_blogmeta( 1, 'foo', 'bar' );
 167          bp_blogs_add_blogmeta( 1, 'foo', 'baz' );
 168          $this->assertSame( array( 'bar', 'baz' ), bp_blogs_get_blogmeta( 1, 'foo', false ) );
 169      }
 170      /**
 171       * @group blogmeta
 172       * @group bp_blogs_update_blogmeta
 173       */
 174      public function test_bp_blogs_update_blogmeta_non_numeric_blog_id() {
 175          $this->assertFalse( bp_blogs_update_blogmeta( 'foo', 'foo', 'bar' ) );
 176      }
 177  
 178      /**
 179       * @group blogmeta
 180       * @group bp_blogs_update_blogmeta
 181       * @ticket BP5399
 182       */
 183      public function test_bp_blogs_update_blogmeta_illegal_characters() {
 184          $krazy_key = ' f!@#$%^o *(){}o?+';
 185          bp_blogs_update_blogmeta( 1, $krazy_key, 'bar' );
 186          $this->assertSame( '', bp_blogs_get_blogmeta( 1, 'foo' ) );
 187      }
 188  
 189      /**
 190       * @group blogmeta
 191       * @group bp_blogs_update_blogmeta
 192       */
 193  	public function test_bp_blogs_update_blogmeta_stripslashes() {
 194          $slashed = 'This \"string\" is cool';
 195          bp_blogs_update_blogmeta( 1, 'foo', $slashed );
 196          $this->assertSame( 'This "string" is cool', bp_blogs_get_blogmeta( 1, 'foo' ) );
 197      }
 198  
 199      /**
 200       * @group blogmeta
 201       * @group bp_blogs_update_blogmeta
 202       */
 203  	public function test_bp_blogs_update_blogmeta_new() {
 204          $this->assertNotEmpty( bp_blogs_update_blogmeta( 1, 'foo', 'bar' ) );
 205          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo' ) );
 206      }
 207  
 208      /**
 209       * @group blogmeta
 210       * @group bp_blogs_update_blogmeta
 211       */
 212  	public function test_bp_blogs_update_blogmeta_existing() {
 213          bp_blogs_update_blogmeta( 1, 'foo', 'bar' );
 214          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo' ) );
 215          $this->assertTrue( bp_blogs_update_blogmeta( 1, 'foo', 'baz' ) );
 216          $this->assertSame( 'baz', bp_blogs_get_blogmeta( 1, 'foo' ) );
 217      }
 218  
 219      /**
 220       * @group blogmeta
 221       * @group bp_blogs_update_blogmeta
 222       */
 223      public function test_bp_blogs_update_blogmeta_existing_no_change() {
 224          bp_blogs_update_blogmeta( 1, 'foo', 'bar' );
 225          $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo' ) );
 226          $this->assertFalse( bp_blogs_update_blogmeta( 1, 'foo', 'bar' ) );
 227      }
 228  
 229      /**
 230       * @group blogmeta
 231       * @group bp_blogs_update_blogmeta
 232       */
 233  	public function test_bp_blogs_update_meta_prev_value() {
 234          bp_blogs_add_blogmeta( 1, 'foo', 'bar' );
 235  
 236          // In earlier versions of WordPress, bp_activity_update_meta()
 237          // returns true even on failure. However, we know that in these
 238          // cases the update is failing as expected, so we skip this
 239          // assertion just to keep our tests passing
 240          // See https://core.trac.wordpress.org/ticket/24933
 241          if ( version_compare( $GLOBALS['wp_version'], '3.7', '>=' ) ) {
 242              $this->assertFalse( bp_blogs_update_blogmeta( 1, 'foo', 'bar2', 'baz' ) );
 243          }
 244  
 245          $this->assertTrue( bp_blogs_update_blogmeta( 1, 'foo', 'bar2', 'bar' ) );
 246      }
 247  
 248      /**
 249       * @group blogmeta
 250       * @group bp_blogs_add_blogmeta
 251       */
 252  	public function test_bp_blogs_add_blogmeta_no_meta_key() {
 253          $this->assertFalse( bp_blogs_add_blogmeta( 1, '', 'bar' ) );
 254      }
 255  
 256      /**
 257       * @group blogmeta
 258       * @group bp_blogs_add_blogmeta
 259       */
 260  	public function test_bp_blogs_add_blogmeta_empty_object_id() {
 261          $this->assertFalse( bp_blogs_add_blogmeta( 0, 'foo', 'bar' ) );
 262      }
 263  
 264      /**
 265       * @group blogmeta
 266       * @group bp_blogs_add_blogmeta
 267       */
 268  	public function test_bp_blogs_add_blogmeta_existing_unique() {
 269          bp_blogs_add_blogmeta( 1, 'foo', 'bar' );
 270          $this->assertFalse( bp_blogs_add_blogmeta( 1, 'foo', 'baz', true ) );
 271      }
 272  
 273      /**
 274       * @group blogmeta
 275       * @group bp_blogs_add_blogmeta
 276       */
 277      public function test_bp_blogs_add_blogmeta_existing_not_unique() {
 278          bp_blogs_add_blogmeta( 1, 'foo', 'bar' );
 279          $this->assertNotEmpty( bp_blogs_add_blogmeta( 1, 'foo', 'baz' ) );
 280      }
 281  
 282      /**
 283       * @group bp_blogs_restore_data
 284       */
 285  	public function test_bp_blogs_restore_data() {
 286          if ( ! is_multisite() ) {
 287              $this->markTestSkipped();
 288          }
 289  
 290          // Create a regular member
 291          $u = self::factory()->user->create();
 292  
 293          // Create blogs
 294          $b1 = self::factory()->blog->create( array( 'user_id' => $u ) );
 295          $b2 = self::factory()->blog->create( array( 'user_id' => $u ) );
 296  
 297          $expected = array(
 298              $b1 => $b1,
 299              $b2 => $b2
 300          );
 301  
 302          // Mark the user as spam
 303          bp_core_process_spammer_status( $u, 'spam' );
 304  
 305          // get all blogs for user
 306          $blogs = bp_blogs_get_blogs_for_user( $u, true );
 307          $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' );
 308  
 309          $this->assertNotEquals( $expected, array_map( 'intval', $blog_ids ), 'User marked as spam should not have any blog registered' );
 310  
 311          // Ham the user
 312          bp_core_process_spammer_status( $u, 'ham' );
 313  
 314          // get all blogs for user
 315          $blogs = bp_blogs_get_blogs_for_user( $u, true );
 316          $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' );
 317  
 318          $this->assertEquals( $expected, array_map( 'intval', $blog_ids ) );
 319      }
 320  
 321      /**
 322       * @group bp_blogs_catch_transition_post_status
 323       */
 324      public function test_transition_post_status_publish_to_publish() {
 325          $post_id = self::factory()->post->create( array(
 326              'post_status' => 'publish',
 327              'post_type' => 'post',
 328          ) );
 329          $post = get_post( $post_id );
 330  
 331          // 'publish' => 'publish'
 332          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 333  
 334          $post->post_status = 'publish';
 335          $post->post_content .= ' foo';
 336  
 337          wp_update_post( $post );
 338  
 339          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity (no change)' );
 340      }
 341  
 342      /**
 343       * @group bp_blogs_catch_transition_post_status
 344       */
 345  	public function test_transition_post_status_password_publish() {
 346          $post_id = self::factory()->post->create( array(
 347              'post_status'   => 'publish',
 348              'post_type'     => 'post',
 349              'post_password' => 'pass',
 350          ) );
 351          $post = get_post( $post_id );
 352  
 353          // 'new' => 'publish with password'
 354          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Published with password post should not have activity' );
 355      }
 356  
 357      /**
 358       * @group bp_blogs_catch_transition_post_status
 359       */
 360      public function test_transition_post_status_publish_update_password() {
 361          $post_id = self::factory()->post->create( array(
 362              'post_status'   => 'publish',
 363              'post_type'     => 'post',
 364          ) );
 365          $post = get_post( $post_id );
 366  
 367          // 'publish' => 'publish'
 368          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 369  
 370          $post->post_content .= ' foo';
 371          $post->post_password = 'pass';
 372  
 373          wp_update_post( $post );
 374  
 375          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Updated with password post should not have activity' );
 376      }
 377  
 378      /**
 379       * @group bp_blogs_catch_transition_post_status
 380       */
 381  	public function test_transition_post_status_private_publish() {
 382          $post_id = self::factory()->post->create( array(
 383              'post_status'   => 'private',
 384              'post_type'     => 'post',
 385          ) );
 386          $post = get_post( $post_id );
 387  
 388          // 'new' => 'private'
 389          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Private post should not have activity' );
 390  
 391          $post->post_status = 'publish';
 392  
 393          wp_update_post( $post );
 394  
 395          // 'private' => 'publish'
 396          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 397      }
 398  
 399      /**
 400       * @group bp_blogs_catch_transition_post_status
 401       */
 402  	public function test_transition_post_status_publish_private() {
 403          $post_id = self::factory()->post->create( array(
 404              'post_status'   => 'publish',
 405              'post_type'     => 'post',
 406          ) );
 407          $post = get_post( $post_id );
 408  
 409          // 'new' => 'publish'
 410          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 411  
 412          $post->post_status = 'private';
 413  
 414          wp_update_post( $post );
 415  
 416          // 'publish' => 'private'
 417          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Private post should not have activity' );
 418      }
 419  
 420      /**
 421       * @group bp_blogs_catch_transition_post_status
 422       */
 423  	public function test_transition_post_status_draft_to_draft() {
 424          $post_id = self::factory()->post->create( array(
 425              'post_status' => 'draft',
 426              'post_type' => 'post',
 427          ) );
 428          $post = get_post( $post_id );
 429  
 430          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
 431  
 432          $post->post_status = 'draft';
 433          $post->post_content .= ' foo';
 434  
 435          wp_update_post( $post );
 436  
 437          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity (no change)' );
 438      }
 439  
 440      /**
 441       * @group bp_blogs_catch_transition_post_status
 442       */
 443  	public function test_transition_post_status_draft_to_publish() {
 444          $post_id = self::factory()->post->create( array(
 445              'post_status' => 'draft',
 446              'post_type' => 'post',
 447          ) );
 448          $post = get_post( $post_id );
 449  
 450          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
 451  
 452          $post->post_status = 'publish';
 453          $post->post_content .= ' foo';
 454  
 455          wp_update_post( $post );
 456  
 457          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 458      }
 459  
 460      /**
 461       * @group bp_blogs_catch_transition_post_status
 462       */
 463  	public function test_transition_post_status_publish_to_draft() {
 464          $post_id = self::factory()->post->create( array(
 465              'post_status' => 'publish',
 466              'post_type' => 'post',
 467          ) );
 468          $post = get_post( $post_id );
 469  
 470          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 471  
 472          $post->post_status = 'draft';
 473          $post->post_content .= ' foo';
 474  
 475          wp_update_post( $post );
 476  
 477          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
 478      }
 479  
 480      /**
 481       * @group bp_blogs_catch_transition_post_status
 482       */
 483  	public function test_transition_post_status_wp_delete_post() {
 484          $post_id = self::factory()->post->create( array(
 485              'post_status' => 'publish',
 486              'post_type' => 'post',
 487          ) );
 488          $post = get_post( $post_id );
 489  
 490          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 491  
 492          wp_delete_post( $post->ID );
 493  
 494          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
 495      }
 496  
 497      /**
 498       * @group bp_blogs_catch_transition_post_status
 499       */
 500  	public function test_transition_post_status_wp_trash_post() {
 501          $post_id = self::factory()->post->create( array(
 502              'post_status' => 'publish',
 503              'post_type' => 'post',
 504          ) );
 505          $post = get_post( $post_id );
 506  
 507          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 508  
 509          wp_trash_post( $post->ID );
 510  
 511          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
 512      }
 513  
 514      /**
 515       * @group bp_blogs_catch_transition_post_status
 516       * @group post_type_comment_activities
 517       */
 518      public function test_update_blog_post_and_new_blog_comment_and_activity_comment_meta() {
 519          // save the current user and override logged-in user
 520          $old_user = get_current_user_id();
 521          $u = self::factory()->user->create();
 522          $this->set_current_user( $u );
 523          $userdata = get_userdata( $u );
 524  
 525          // create the blog post
 526          $post_id = self::factory()->post->create( array(
 527              'post_status' => 'publish',
 528              'post_type' => 'post',
 529              'post_title' => 'First title',
 530          ) );
 531  
 532          // remove comment flood protection temporarily
 533          add_filter( 'comment_flood_filter', '__return_false' );
 534  
 535          // let's use activity comments instead of single "new_blog_comment" activity items
 536          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 537          $c1 = wp_new_comment( array(
 538              'comment_post_ID'      => $post_id,
 539              'comment_author'       => $userdata->user_nicename,
 540              'comment_author_url'   => 'http://buddypress.org',
 541              'comment_author_email' => $userdata->user_email,
 542              'comment_content'      => 'this is a blog comment',
 543              'comment_type'         => '',
 544              'comment_parent'       => 0,
 545              'user_id'              => $u,
 546          ) );
 547          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 548  
 549          // let's also add a "new_blog_comment" activity entry
 550          $c2 = wp_new_comment( array(
 551              'comment_post_ID'      => $post_id,
 552              'comment_author'       => $userdata->user_nicename,
 553              'comment_author_url'   => 'http://buddypress.org',
 554              'comment_author_email' => $userdata->user_email,
 555              'comment_content'      => 'this is another blog comment',
 556              'comment_type'         => '',
 557              'comment_parent'       => 0,
 558              'user_id'              => $u,
 559          ) );
 560  
 561          // bring back flood protection
 562          remove_filter( 'comment_flood_filter', '__return_false' );
 563  
 564          // update the initial blog post
 565          wp_update_post( array(
 566              'ID'        => $post_id,
 567              'post_title' => 'Second title',
 568          ) );
 569  
 570          // grab the activity ID for the activity comment
 571          $a1 = bp_activity_get_activity_id( array(
 572              'type'              => 'activity_comment',
 573              'display_comments'  => 'stream',
 574              'meta_query'        => array( array(
 575                  'key'     => 'bp_blogs_post_comment_id',
 576                  'value'   => $c1,
 577              ) )
 578          ) );
 579  
 580          // grab the activity ID for the blog comment
 581          $a2 = bp_activity_get_activity_id( array(
 582              'component'         => buddypress()->blogs->id,
 583              'type'              => 'new_blog_comment',
 584              'secondary_item_id' => $c2,
 585          ) );
 586  
 587          // see if blog comment activity meta matches the post items
 588          $this->assertEquals( 'Second title', bp_activity_get_meta( $a1, 'post_title' ) );
 589          $this->assertEquals( add_query_arg( 'p', $post_id, home_url( '/' ) ), bp_activity_get_meta( $a1, 'post_url' ) );
 590  
 591          $this->assertEquals( 'Second title', bp_activity_get_meta( $a2, 'post_title' ) );
 592          $this->assertEquals( add_query_arg( 'p', $post_id, home_url( '/' ) ), bp_activity_get_meta( $a2, 'post_url' ) );
 593  
 594          // reset
 595          $this->set_current_user( $old_user );
 596      }
 597  
 598      /**
 599       * @group bp_blogs_transition_activity_status
 600       * @group bp_blogs_post_type_remove_comment
 601       * @group post_type_comment_activities
 602       */
 603      public function test_bp_blogs_remove_comment_should_remove_spammed_activity_comment() {
 604          // save the current user and override logged-in user
 605          $old_user = get_current_user_id();
 606          $u = self::factory()->user->create();
 607          $this->set_current_user( $u );
 608          $userdata = get_userdata( $u );
 609  
 610          // create the blog post
 611          $post_id = self::factory()->post->create( array(
 612              'post_status' => 'publish',
 613              'post_type' => 'post',
 614              'post_title' => 'First title',
 615          ) );
 616  
 617          // let's use activity comments instead of single "new_blog_comment" activity items
 618          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 619          $c1 = wp_new_comment( array(
 620              'comment_post_ID'      => $post_id,
 621              'comment_author'       => $userdata->user_nicename,
 622              'comment_author_url'   => 'http://buddypress.org',
 623              'comment_author_email' => $userdata->user_email,
 624              'comment_content'      => 'this is a blog comment',
 625              'comment_type'         => '',
 626              'comment_parent'       => 0,
 627              'user_id'              => $u,
 628          ) );
 629  
 630          // save the corresponding activity comment ID
 631          $a1 = bp_activity_get_activity_id( array(
 632              'type'              => 'activity_comment',
 633              'display_comments'  => 'stream',
 634              'meta_query'        => array( array(
 635                  'key'     => 'bp_blogs_post_comment_id',
 636                  'value'   => $c1,
 637              ) )
 638          ) );
 639  
 640          // trash the parent comment.
 641          // corresponding activity comment should now be marked as spam
 642          // @see bp_blogs_transition_activity_status()
 643          wp_trash_comment( $c1 );
 644  
 645          // now permanently delete the comment
 646          wp_delete_comment( $c1, true );
 647  
 648          // activity comment should no longer exist
 649          $a = bp_activity_get( array(
 650              'in'               => $a1,
 651              'display_comments' => 'stream',
 652              'spam'             => 'all'
 653          ) );
 654          // this is a convoluted way of testing if the activity comment still exists
 655          $this->assertTrue( empty( $a['activities'][0] ) );
 656  
 657          // reset
 658          $this->set_current_user( $old_user );
 659          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 660      }
 661  
 662      /**
 663       * @group bp_blogs_post_type_remove_comment
 664       * @group post_type_comment_activities
 665       */
 666  	public function test_bp_blogs_post_type_remove_comment() {
 667          $old_user = get_current_user_id();
 668          $u = self::factory()->user->create();
 669          $this->set_current_user( $u );
 670          $userdata = get_userdata( $u );
 671  
 672          // create the blog post
 673          $p = self::factory()->post->create( array(
 674              'post_status' => 'publish',
 675              'post_type' => 'post',
 676              'post_title' => 'First title',
 677          ) );
 678  
 679          $c = wp_new_comment( array(
 680              'comment_post_ID'      => $p,
 681              'comment_author'       => $userdata->user_nicename,
 682              'comment_author_url'   => 'http://buddypress.org',
 683              'comment_author_email' => $userdata->user_email,
 684              'comment_content'      => 'this comment will be removed',
 685              'comment_type'         => '',
 686              'comment_parent'       => 0,
 687              'user_id'              => $u,
 688          ) );
 689  
 690          // An activity should exist
 691          $a = bp_activity_get_activity_id( array(
 692              'user_id' => $u,
 693              'type'    => 'new_blog_comment'
 694          ) );
 695  
 696          // now permanently delete the comment
 697          wp_delete_comment( $c, true );
 698  
 699          // The activity comment should no longer exist
 700          $ac = bp_activity_get( array( 'in' => $a ) );
 701          $this->assertTrue( empty( $ac['activities'] ) );
 702      }
 703  
 704      /**
 705       * @group bp_blogs_catch_transition_post_status
 706       */
 707      public function test_bp_blogs_is_blog_trackable_false_publish_post() {
 708          add_filter( 'bp_blogs_is_blog_trackable', '__return_false' );
 709  
 710          $post_id = self::factory()->post->create( array(
 711              'post_status'   => 'publish',
 712              'post_type'     => 'post',
 713          ) );
 714          $post = get_post( $post_id );
 715  
 716          // 'new' => 'publish'
 717          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not trackable blog post should not have activity' );
 718  
 719          $post->post_content .= ' foo';
 720  
 721          wp_update_post( $post );
 722  
 723          // 'publish' => 'publish'
 724          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not trackable blog post should not have activity' );
 725  
 726          remove_filter( 'bp_blogs_is_blog_trackable', '__return_false' );
 727      }
 728  
 729      /**
 730       * @group bp_blogs_catch_transition_post_status
 731       */
 732  	public function test_bp_is_blog_public_zero_publish_post() {
 733          if ( ! is_multisite() ) {
 734              $this->markTestSkipped();
 735          }
 736  
 737          add_filter( 'bp_is_blog_public', '__return_zero' );
 738  
 739          $post_id = self::factory()->post->create( array(
 740              'post_status'   => 'publish',
 741              'post_type'     => 'post',
 742          ) );
 743          $post = get_post( $post_id );
 744  
 745          // 'new' => 'publish'
 746          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not public blog post should not have activity' );
 747  
 748          $post->post_content .= ' foo';
 749  
 750          wp_update_post( $post );
 751  
 752          // 'publish' => 'publish'
 753          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not public blog post should not have activity' );
 754  
 755          remove_filter( 'bp_is_blog_public', '__return_zero' );
 756      }
 757  
 758      /**
 759       * @group bp_blogs_record_comment
 760       * @group unique
 761       * @group post_type_comment_activities
 762       */
 763      public function test_bp_blogs_record_comment_no_duplicate_activity_comments() {
 764          // save the current user and override logged-in user
 765          $old_user = get_current_user_id();
 766          $u = self::factory()->user->create();
 767          $this->set_current_user( $u );
 768          $userdata = get_userdata( $u );
 769          $this->activity_saved_comment_count = 0;
 770          $this->comment_saved_count = 0;
 771  
 772          // let's use activity comments instead of single "new_blog_comment" activity items
 773          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 774          add_action( 'bp_activity_add', array( $this, 'count_activity_comment_saved' ) );
 775          add_action( 'wp_insert_comment', array( $this, 'count_post_comment_saved' ) );
 776          add_action( 'edit_comment', array( $this, 'count_post_comment_saved' ) );
 777  
 778          // create the blog post
 779          $post_id = self::factory()->post->create( array(
 780              'post_status' => 'publish',
 781              'post_type'   => 'post',
 782              'post_title'  => 'Test Duplicate activity comments',
 783          ) );
 784  
 785          // grab the activity ID for the activity comment
 786          $a1 = bp_activity_get_activity_id( array(
 787              'type'      => 'new_blog_post',
 788              'component' => buddypress()->blogs->id,
 789              'filter'    => array(
 790                  'item_id' => get_current_blog_id(),
 791                  'secondary_item_id' => $post_id
 792              ),
 793          ) );
 794  
 795          $a2 = bp_activity_new_comment( array(
 796              'content'     => 'activity comment should be unique',
 797              'user_id'     => $u,
 798              'activity_id' => $a1,
 799          ) );
 800  
 801          $activities = bp_activity_get( array(
 802              'type'             => 'activity_comment',
 803              'display_comments' => 'stream',
 804              'search_terms'     => 'activity comment should be unique',
 805          ) );
 806  
 807          $this->assertTrue( count( $activities['activities'] ) === 1, 'An activity comment should be unique' );
 808  
 809          $this->assertTrue( 2 === $this->activity_saved_comment_count, 'An activity comment should be saved only twice' );
 810          $this->assertTrue( 1 === $this->comment_saved_count, 'A comment should be saved only once' );
 811  
 812          // reset
 813          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 814          remove_action( 'bp_activity_add', array( $this, 'count_activity_comment_saved' ) );
 815          remove_action( 'wp_insert_comment', array( $this, 'count_post_comment_saved' ) );
 816          remove_action( 'edit_comment', array( $this, 'count_post_comment_saved' ) );
 817  
 818          $this->activity_saved_comment_count = 0;
 819          $this->comment_saved_count = 0;
 820          $this->set_current_user( $old_user );
 821      }
 822  
 823      /**
 824       * @group bp_blogs_record_comment
 825       * @group post_type_comment_activities
 826       */
 827      public function test_bp_blogs_record_comment_should_record_parent_blog_post_activity_if_not_found() {
 828          // Save the current user and override logged-in user
 829          $old_user = get_current_user_id();
 830          $u = self::factory()->user->create();
 831          $this->set_current_user( $u );
 832  
 833          // Get user details
 834          $user = get_userdata( $u );
 835  
 836          // Let's use activity comments instead of single "new_blog_comment" activity items
 837          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 838  
 839          // Create the blog post
 840          $post_id = self::factory()->post->create( array(
 841              'post_status' => 'publish',
 842              'post_type'   => 'post',
 843          ) );
 844  
 845          // Now, delete the activity item for the blog post
 846          bp_activity_delete( array(
 847              'component'         => buddypress()->blogs->id,
 848              'type'              => 'new_blog_post',
 849              'item_id'           => get_current_blog_id(),
 850              'secondary_item_id' => $post_id,
 851          ) );
 852  
 853          // Add a comment to blog post
 854          wp_new_comment( array(
 855              'comment_post_ID' => $post_id,
 856              'user_id' => $u,
 857              'comment_content' => 'Dummy comment',
 858              'comment_author' => 'Dumbo',
 859              'comment_author_url' => 'http://buddypress.org',
 860  
 861              // Important to pass check in bp_blogs_record_comment()
 862              'comment_author_email' => $user->user_email
 863          ) );
 864  
 865          // Fetch the activity ID for the blog post to see if it exists
 866          $a1 = bp_activity_get_activity_id( array(
 867              'type'      => 'new_blog_post',
 868              'component' => buddypress()->blogs->id,
 869              'filter'    => array(
 870                  'item_id' => get_current_blog_id(),
 871                  'secondary_item_id' => $post_id
 872              ),
 873          ) );
 874  
 875          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 876  
 877          // Assert that activity item for blog post was created after adding a comment
 878          $this->assertNotNull( $a1, 'Activity item was not created for existing blog post when recording post comment.' );
 879  
 880          $this->set_current_user( $old_user );
 881      }
 882  
 883      /**
 884       * @group bp_blogs_comment_sync_activity_comment
 885       * @group post_type_comment_activities
 886       */
 887      public function test_bp_blogs_comment_sync_activity_comment_for_custom_post_type() {
 888          if ( is_multisite() ) {
 889              $b = self::factory()->blog->create();
 890              switch_to_blog( $b );
 891              add_filter( 'comment_flood_filter', '__return_false' );
 892          } else {
 893              $b = get_current_blog_id();
 894          }
 895  
 896          $u = self::factory()->user->create();
 897          $userdata = get_userdata( $u );
 898  
 899          $labels = array(
 900              'name'                       => 'bars',
 901              'singular_name'              => 'bar',
 902          );
 903  
 904          register_post_type( 'foo', array(
 905              'labels'   => $labels,
 906              'public'   => true,
 907              'supports' => array( 'comments' ),
 908          ) );
 909  
 910          add_post_type_support( 'foo', 'buddypress-activity' );
 911  
 912          bp_activity_set_post_type_tracking_args( 'foo', array(
 913              'comment_action_id' => 'new_foo_comment',
 914          ) );
 915  
 916          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 917  
 918          $p = self::factory()->post->create( array(
 919              'post_author' => $u,
 920              'post_type'   => 'foo',
 921          ) );
 922  
 923          $a1 = bp_activity_get_activity_id( array(
 924              'type'      => 'new_foo',
 925              'filter'    => array(
 926                  'item_id' => $b,
 927                  'secondary_item_id' => $p
 928              ),
 929          ) );
 930  
 931          $c = wp_new_comment( array(
 932              'comment_post_ID'      => $p,
 933              'comment_author'       => $userdata->user_nicename,
 934              'comment_author_url'   => 'http://buddypress.org',
 935              'comment_author_email' => $userdata->user_email,
 936              'comment_content'      => 'this is a foo comment',
 937              'comment_type'         => '',
 938              'comment_parent'       => 0,
 939              'user_id'              => $u,
 940          ) );
 941  
 942          $a2 = bp_activity_new_comment( array(
 943              'content'     => 'this should generate a new foo comment',
 944              'user_id'     => $u,
 945              'activity_id' => $a1,
 946          ) );
 947  
 948          $activity_args = array(
 949              'type'              => 'activity_comment',
 950              'display_comments'  => 'stream',
 951              'meta_query'        => array( array(
 952                  'key'       => 'bp_blogs_foo_comment_id',
 953                  'compare'   => 'exists',
 954              ) )
 955          );
 956  
 957          $a = bp_activity_get( $activity_args );
 958          $aids = wp_list_pluck( $a['activities'], 'id' );
 959          $cids = wp_list_pluck( get_approved_comments( $p ), 'comment_ID' );
 960  
 961          foreach ( $aids as $aid ) {
 962              $this->assertTrue( in_array( bp_activity_get_meta( $aid, 'bp_blogs_foo_comment_id' ), $cids ), 'The comment ID should be in the activity meta' );
 963          }
 964  
 965          foreach ( $cids as $cid ) {
 966              $this->assertTrue( in_array( get_comment_meta( $cid, 'bp_activity_comment_id', true ), $aids ), 'The activity ID should be in the comment meta' );
 967          }
 968  
 969          _unregister_post_type( 'foo' );
 970  
 971          if ( is_multisite() ) {
 972              restore_current_blog();
 973              remove_filter( 'comment_flood_filter', '__return_false' );
 974          }
 975  
 976          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 977      }
 978  
 979  	public function count_activity_comment_saved() {
 980          $this->activity_saved_comment_count += 1;
 981      }
 982  
 983  	public function count_post_comment_saved() {
 984          $this->comment_saved_count += 1;
 985      }
 986  
 987      /**
 988       * @group bp_blogs_record_existing_blogs
 989       */
 990  	public function test_bp_blogs_record_existing_blogs_limit() {
 991          if ( ! is_multisite() ) {
 992              $this->markTestSkipped();
 993          }
 994  
 995          $old_user = get_current_user_id();
 996  
 997          $u = self::factory()->user->create();
 998          $this->set_current_user( $u );
 999  
1000          // Create three sites.
1001          self::factory()->blog->create_many( 3, array(
1002              'user_id' => $u
1003          ) );
1004  
1005          // Record each site one at a time
1006          bp_blogs_record_existing_blogs( array(
1007              'limit' => 1
1008          ) );
1009  
1010          // Assert!
1011          $blogs = bp_blogs_get_blogs( array(
1012              'user_id' => $u
1013          ) );
1014          $this->assertSame( 3, (int) $blogs['total'] );
1015  
1016          $this->set_current_user( $old_user );
1017      }
1018  
1019      /**
1020       * @group bp_blogs_remove_blog
1021       */
1022  	public function test_bp_blogs_remove_blog() {
1023          if ( ! is_multisite() ) {
1024              $this->markTestSkipped();
1025          }
1026  
1027          $reset_post = $_POST;
1028          $old_user = get_current_user_id();
1029  
1030          // Simulate a new "BuddyPress generated" blog
1031          $_POST['blog_public'] = 1;
1032  
1033          $u = self::factory()->user->create();
1034          $this->set_current_user( $u );
1035  
1036          // Create three sites.
1037          $b = self::factory()->blog->create( array(
1038              'user_id' => $u
1039          ) );
1040  
1041          $activity = bp_activity_get( array(
1042              'filter' => array(
1043                  'object'     => 'blogs',
1044                  'action'     => 'new_blog',
1045                  'primary_id' => $b,
1046              ),
1047          ) );
1048  
1049          $new_blog = array_map( 'intval', wp_list_pluck( $activity['activities'], 'item_id', 'id' ) );
1050          $this->assertSame( $b, reset( $new_blog ) );
1051  
1052          // Removing the blog should delete the activity and the blog association.
1053          wpmu_delete_blog( $b );
1054  
1055          $deleted = bp_activity_get( array(
1056              'in' => array_keys( $new_blog ),
1057          ) );
1058  
1059          $this->assertEmpty( $deleted['activities'] );
1060          $this->assertEmpty( BP_Blogs_Blog::is_recorded( $b ) );
1061  
1062          $_POST = $reset_post;
1063          $this->set_current_user( $old_user );
1064      }
1065  
1066      /**
1067       * @group bp_blogs_remove_blog_for_user
1068       */
1069      public function test_bp_blogs_remove_blog_for_user_is_contributor() {
1070          if ( ! is_multisite() ) {
1071              $this->markTestSkipped();
1072          }
1073  
1074          $reset_post = $_POST;
1075          $old_user = get_current_user_id();
1076  
1077          // Simulate a new "BuddyPress generated" blog
1078          $_POST['blog_public'] = 1;
1079  
1080          $u = self::factory()->user->create();
1081          $this->set_current_user( $u );
1082  
1083          // Create three sites.
1084          $b = self::factory()->blog->create( array(
1085              'user_id' => $u
1086          ) );
1087  
1088          $u2 = self::factory()->user->create();
1089          add_user_to_blog( $b, $u2, 'contributor' );
1090  
1091          $u2_blogs = BP_Blogs_Blog::get_blog_ids_for_user( $u2 );
1092          $this->assertContains( $b, $u2_blogs, 'The user should be associated to the blog as he is a contributor' );
1093  
1094          remove_user_from_blog( $u2, $b );
1095          $u2_blogs = BP_Blogs_Blog::get_blog_ids_for_user( $u2 );
1096          $this->assertNotContains( $b, $u2_blogs, 'The user should not be associated anymore to the blog' );
1097  
1098          $activity = bp_activity_get( array(
1099              'filter' => array(
1100                  'object'     => 'blogs',
1101                  'action'     => 'new_blog',
1102                  'primary_id' => $b,
1103              ),
1104          ) );
1105  
1106          $new_blog = array_map( 'intval', wp_list_pluck( $activity['activities'], 'item_id', 'id' ) );
1107          $this->assertSame( $b, reset( $new_blog ), 'The new_blog activity should not be deleted when a contributor is removed from the blog.' );
1108  
1109          $_POST = $reset_post;
1110          $this->set_current_user( $old_user );
1111      }
1112  
1113  	protected function activity_exists_for_post( $post_id ) {
1114          $a = bp_activity_get( array(
1115              'component' => buddypress()->blogs->id,
1116              'action' => 'new_blog_post',
1117              'item_id' => get_current_blog_id(),
1118              'secondary_item_id' => $post_id,
1119          ) );
1120  
1121          return ! empty( $a['activities'] );
1122      }
1123  
1124      /**
1125       * @ticket BP8175
1126       */
1127      public function test_blogs_data_should_be_deleted_on_user_delete_multisite() {
1128          if ( ! is_multisite() ) {
1129              $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
1130          }
1131  
1132          $u1 = self::factory()->user->create();
1133          $b1 = get_current_blog_id();
1134          $b2 = self::factory()->blog->create();
1135  
1136          bp_blogs_record_blog( $b1, $u1, true );
1137          bp_blogs_record_blog( $b2, $u1, true );
1138  
1139          $blogs = bp_blogs_get_blogs_for_user( $u1 );
1140  
1141          $this->assertEqualSets( [ $b1, $b2 ], wp_list_pluck( $blogs['blogs'], 'blog_id' ) );
1142  
1143          wpmu_delete_user( $u1 );
1144  
1145          $blogs = bp_blogs_get_blogs_for_user( $u1 );
1146  
1147          $this->assertEmpty( $blogs['blogs'] );
1148      }
1149  
1150      /**
1151       * @ticket BP8175
1152       */
1153      public function test_blogs_data_should_not_be_deleted_on_wp_delete_user_multisite() {
1154          if ( ! is_multisite() ) {
1155              $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
1156          }
1157  
1158          $u1 = self::factory()->user->create();
1159          $b1 = get_current_blog_id();
1160          $b2 = self::factory()->blog->create();
1161  
1162          bp_blogs_record_blog( $b1, $u1, true );
1163          bp_blogs_record_blog( $b2, $u1, true );
1164  
1165          $blogs = bp_blogs_get_blogs_for_user( $u1 );
1166  
1167          $this->assertEqualSets( [ $b1, $b2 ], wp_list_pluck( $blogs['blogs'], 'blog_id' ) );
1168  
1169          wp_delete_user( $u1 );
1170  
1171          $blogs = bp_blogs_get_blogs_for_user( $u1 );
1172  
1173          $this->assertEqualSets( [ $b2 ], wp_list_pluck( $blogs['blogs'], 'blog_id' ) );
1174      }
1175  }


Generated: Fri Dec 6 01:00:58 2024 Cross-referenced by PHPXref 0.7.1