[ 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          if ( function_exists( 'wp_initialize_site' ) ) {
 291              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 292          }
 293  
 294          // Create a regular member
 295          $u = self::factory()->user->create();
 296  
 297          // Create blogs
 298          $b1 = self::factory()->blog->create( array( 'user_id' => $u ) );
 299          $b2 = self::factory()->blog->create( array( 'user_id' => $u ) );
 300  
 301          $expected = array(
 302              $b1 => $b1,
 303              $b2 => $b2
 304          );
 305  
 306          // Mark the user as spam
 307          bp_core_process_spammer_status( $u, 'spam' );
 308  
 309          // get all blogs for user
 310          $blogs = bp_blogs_get_blogs_for_user( $u, true );
 311          $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' );
 312  
 313          $this->assertNotEquals( $expected, array_map( 'intval', $blog_ids ), 'User marked as spam should not have any blog registered' );
 314  
 315          // Ham the user
 316          bp_core_process_spammer_status( $u, 'ham' );
 317  
 318          // get all blogs for user
 319          $blogs = bp_blogs_get_blogs_for_user( $u, true );
 320          $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' );
 321  
 322          $this->assertEquals( $expected, array_map( 'intval', $blog_ids ) );
 323      }
 324  
 325      /**
 326       * @group bp_blogs_catch_transition_post_status
 327       */
 328      public function test_transition_post_status_publish_to_publish() {
 329          $post_id = self::factory()->post->create( array(
 330              'post_status' => 'publish',
 331              'post_type' => 'post',
 332          ) );
 333          $post = get_post( $post_id );
 334  
 335          // 'publish' => 'publish'
 336          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 337  
 338          $post->post_status = 'publish';
 339          $post->post_content .= ' foo';
 340  
 341          wp_update_post( $post );
 342  
 343          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity (no change)' );
 344      }
 345  
 346      /**
 347       * @group bp_blogs_catch_transition_post_status
 348       */
 349  	public function test_transition_post_status_password_publish() {
 350          $post_id = self::factory()->post->create( array(
 351              'post_status'   => 'publish',
 352              'post_type'     => 'post',
 353              'post_password' => 'pass',
 354          ) );
 355          $post = get_post( $post_id );
 356  
 357          // 'new' => 'publish with password'
 358          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Published with password post should not have activity' );
 359      }
 360  
 361      /**
 362       * @group bp_blogs_catch_transition_post_status
 363       */
 364      public function test_transition_post_status_publish_update_password() {
 365          $post_id = self::factory()->post->create( array(
 366              'post_status'   => 'publish',
 367              'post_type'     => 'post',
 368          ) );
 369          $post = get_post( $post_id );
 370  
 371          // 'publish' => 'publish'
 372          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 373  
 374          $post->post_content .= ' foo';
 375          $post->post_password = 'pass';
 376  
 377          wp_update_post( $post );
 378  
 379          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Updated with password post should not have activity' );
 380      }
 381  
 382      /**
 383       * @group bp_blogs_catch_transition_post_status
 384       */
 385  	public function test_transition_post_status_private_publish() {
 386          $post_id = self::factory()->post->create( array(
 387              'post_status'   => 'private',
 388              'post_type'     => 'post',
 389          ) );
 390          $post = get_post( $post_id );
 391  
 392          // 'new' => 'private'
 393          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Private post should not have activity' );
 394  
 395          $post->post_status = 'publish';
 396  
 397          wp_update_post( $post );
 398  
 399          // 'private' => 'publish'
 400          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 401      }
 402  
 403      /**
 404       * @group bp_blogs_catch_transition_post_status
 405       */
 406  	public function test_transition_post_status_publish_private() {
 407          $post_id = self::factory()->post->create( array(
 408              'post_status'   => 'publish',
 409              'post_type'     => 'post',
 410          ) );
 411          $post = get_post( $post_id );
 412  
 413          // 'new' => 'publish'
 414          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 415  
 416          $post->post_status = 'private';
 417  
 418          wp_update_post( $post );
 419  
 420          // 'publish' => 'private'
 421          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Private post should not have activity' );
 422      }
 423  
 424      /**
 425       * @group bp_blogs_catch_transition_post_status
 426       */
 427  	public function test_transition_post_status_draft_to_draft() {
 428          $post_id = self::factory()->post->create( array(
 429              'post_status' => 'draft',
 430              'post_type' => 'post',
 431          ) );
 432          $post = get_post( $post_id );
 433  
 434          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
 435  
 436          $post->post_status = 'draft';
 437          $post->post_content .= ' foo';
 438  
 439          wp_update_post( $post );
 440  
 441          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity (no change)' );
 442      }
 443  
 444      /**
 445       * @group bp_blogs_catch_transition_post_status
 446       */
 447  	public function test_transition_post_status_draft_to_publish() {
 448          $post_id = self::factory()->post->create( array(
 449              'post_status' => 'draft',
 450              'post_type' => 'post',
 451          ) );
 452          $post = get_post( $post_id );
 453  
 454          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
 455  
 456          $post->post_status = 'publish';
 457          $post->post_content .= ' foo';
 458  
 459          wp_update_post( $post );
 460  
 461          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 462      }
 463  
 464      /**
 465       * @group bp_blogs_catch_transition_post_status
 466       */
 467  	public function test_transition_post_status_publish_to_draft() {
 468          $post_id = self::factory()->post->create( array(
 469              'post_status' => 'publish',
 470              'post_type' => 'post',
 471          ) );
 472          $post = get_post( $post_id );
 473  
 474          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 475  
 476          $post->post_status = 'draft';
 477          $post->post_content .= ' foo';
 478  
 479          wp_update_post( $post );
 480  
 481          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
 482      }
 483  
 484      /**
 485       * @group bp_blogs_catch_transition_post_status
 486       */
 487  	public function test_transition_post_status_wp_delete_post() {
 488          $post_id = self::factory()->post->create( array(
 489              'post_status' => 'publish',
 490              'post_type' => 'post',
 491          ) );
 492          $post = get_post( $post_id );
 493  
 494          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 495  
 496          wp_delete_post( $post->ID );
 497  
 498          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
 499      }
 500  
 501      /**
 502       * @group bp_blogs_catch_transition_post_status
 503       */
 504  	public function test_transition_post_status_wp_trash_post() {
 505          $post_id = self::factory()->post->create( array(
 506              'post_status' => 'publish',
 507              'post_type' => 'post',
 508          ) );
 509          $post = get_post( $post_id );
 510  
 511          $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
 512  
 513          wp_trash_post( $post->ID );
 514  
 515          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
 516      }
 517  
 518      /**
 519       * @group bp_blogs_catch_transition_post_status
 520       * @group post_type_comment_activities
 521       */
 522      public function test_update_blog_post_and_new_blog_comment_and_activity_comment_meta() {
 523          // save the current user and override logged-in user
 524          $old_user = get_current_user_id();
 525          $u = self::factory()->user->create();
 526          $this->set_current_user( $u );
 527          $userdata = get_userdata( $u );
 528  
 529          // create the blog post
 530          $post_id = self::factory()->post->create( array(
 531              'post_status' => 'publish',
 532              'post_type' => 'post',
 533              'post_title' => 'First title',
 534          ) );
 535  
 536          // remove comment flood protection temporarily
 537          add_filter( 'comment_flood_filter', '__return_false' );
 538  
 539          // let's use activity comments instead of single "new_blog_comment" activity items
 540          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 541          $c1 = wp_new_comment( array(
 542              'comment_post_ID'      => $post_id,
 543              'comment_author'       => $userdata->user_nicename,
 544              'comment_author_url'   => 'http://buddypress.org',
 545              'comment_author_email' => $userdata->user_email,
 546              'comment_content'      => 'this is a blog comment',
 547              'comment_type'         => '',
 548              'comment_parent'       => 0,
 549              'user_id'              => $u,
 550          ) );
 551          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 552  
 553          // let's also add a "new_blog_comment" activity entry
 554          $c2 = wp_new_comment( array(
 555              'comment_post_ID'      => $post_id,
 556              'comment_author'       => $userdata->user_nicename,
 557              'comment_author_url'   => 'http://buddypress.org',
 558              'comment_author_email' => $userdata->user_email,
 559              'comment_content'      => 'this is another blog comment',
 560              'comment_type'         => '',
 561              'comment_parent'       => 0,
 562              'user_id'              => $u,
 563          ) );
 564  
 565          // bring back flood protection
 566          remove_filter( 'comment_flood_filter', '__return_false' );
 567  
 568          // update the initial blog post
 569          wp_update_post( array(
 570              'ID'        => $post_id,
 571              'post_title' => 'Second title',
 572          ) );
 573  
 574          // grab the activity ID for the activity comment
 575          $a1 = bp_activity_get_activity_id( array(
 576              'type'              => 'activity_comment',
 577              'display_comments'  => 'stream',
 578              'meta_query'        => array( array(
 579                  'key'     => 'bp_blogs_post_comment_id',
 580                  'value'   => $c1,
 581              ) )
 582          ) );
 583  
 584          // grab the activity ID for the blog comment
 585          $a2 = bp_activity_get_activity_id( array(
 586              'component'         => buddypress()->blogs->id,
 587              'type'              => 'new_blog_comment',
 588              'secondary_item_id' => $c2,
 589          ) );
 590  
 591          // see if blog comment activity meta matches the post items
 592          $this->assertEquals( 'Second title', bp_activity_get_meta( $a1, 'post_title' ) );
 593          $this->assertEquals( add_query_arg( 'p', $post_id, home_url( '/' ) ), bp_activity_get_meta( $a1, 'post_url' ) );
 594  
 595          $this->assertEquals( 'Second title', bp_activity_get_meta( $a2, 'post_title' ) );
 596          $this->assertEquals( add_query_arg( 'p', $post_id, home_url( '/' ) ), bp_activity_get_meta( $a2, 'post_url' ) );
 597  
 598          // reset
 599          $this->set_current_user( $old_user );
 600      }
 601  
 602      /**
 603       * @group bp_blogs_transition_activity_status
 604       * @group bp_blogs_post_type_remove_comment
 605       * @group post_type_comment_activities
 606       */
 607      public function test_bp_blogs_remove_comment_should_remove_spammed_activity_comment() {
 608          // save the current user and override logged-in user
 609          $old_user = get_current_user_id();
 610          $u = self::factory()->user->create();
 611          $this->set_current_user( $u );
 612          $userdata = get_userdata( $u );
 613  
 614          // create the blog post
 615          $post_id = self::factory()->post->create( array(
 616              'post_status' => 'publish',
 617              'post_type' => 'post',
 618              'post_title' => 'First title',
 619          ) );
 620  
 621          // let's use activity comments instead of single "new_blog_comment" activity items
 622          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 623          $c1 = wp_new_comment( array(
 624              'comment_post_ID'      => $post_id,
 625              'comment_author'       => $userdata->user_nicename,
 626              'comment_author_url'   => 'http://buddypress.org',
 627              'comment_author_email' => $userdata->user_email,
 628              'comment_content'      => 'this is a blog comment',
 629              'comment_type'         => '',
 630              'comment_parent'       => 0,
 631              'user_id'              => $u,
 632          ) );
 633  
 634          // save the corresponding activity comment ID
 635          $a1 = bp_activity_get_activity_id( array(
 636              'type'              => 'activity_comment',
 637              'display_comments'  => 'stream',
 638              'meta_query'        => array( array(
 639                  'key'     => 'bp_blogs_post_comment_id',
 640                  'value'   => $c1,
 641              ) )
 642          ) );
 643  
 644          // trash the parent comment.
 645          // corresponding activity comment should now be marked as spam
 646          // @see bp_blogs_transition_activity_status()
 647          wp_trash_comment( $c1 );
 648  
 649          // now permanently delete the comment
 650          wp_delete_comment( $c1, true );
 651  
 652          // activity comment should no longer exist
 653          $a = bp_activity_get( array(
 654              'in'               => $a1,
 655              'display_comments' => 'stream',
 656              'spam'             => 'all'
 657          ) );
 658          // this is a convoluted way of testing if the activity comment still exists
 659          $this->assertTrue( empty( $a['activities'][0] ) );
 660  
 661          // reset
 662          $this->set_current_user( $old_user );
 663          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 664      }
 665  
 666      /**
 667       * @group bp_blogs_post_type_remove_comment
 668       * @group post_type_comment_activities
 669       */
 670  	public function test_bp_blogs_post_type_remove_comment() {
 671          $old_user = get_current_user_id();
 672          $u = self::factory()->user->create();
 673          $this->set_current_user( $u );
 674          $userdata = get_userdata( $u );
 675  
 676          // create the blog post
 677          $p = self::factory()->post->create( array(
 678              'post_status' => 'publish',
 679              'post_type' => 'post',
 680              'post_title' => 'First title',
 681          ) );
 682  
 683          $c = wp_new_comment( array(
 684              'comment_post_ID'      => $p,
 685              'comment_author'       => $userdata->user_nicename,
 686              'comment_author_url'   => 'http://buddypress.org',
 687              'comment_author_email' => $userdata->user_email,
 688              'comment_content'      => 'this comment will be removed',
 689              'comment_type'         => '',
 690              'comment_parent'       => 0,
 691              'user_id'              => $u,
 692          ) );
 693  
 694          // An activity should exist
 695          $a = bp_activity_get_activity_id( array(
 696              'user_id' => $u,
 697              'type'    => 'new_blog_comment'
 698          ) );
 699  
 700          // now permanently delete the comment
 701          wp_delete_comment( $c, true );
 702  
 703          // The activity comment should no longer exist
 704          $ac = bp_activity_get( array( 'in' => $a ) );
 705          $this->assertTrue( empty( $ac['activities'] ) );
 706      }
 707  
 708      /**
 709       * @group bp_blogs_catch_transition_post_status
 710       */
 711      public function test_bp_blogs_is_blog_trackable_false_publish_post() {
 712          add_filter( 'bp_blogs_is_blog_trackable', '__return_false' );
 713  
 714          $post_id = self::factory()->post->create( array(
 715              'post_status'   => 'publish',
 716              'post_type'     => 'post',
 717          ) );
 718          $post = get_post( $post_id );
 719  
 720          // 'new' => 'publish'
 721          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not trackable blog post should not have activity' );
 722  
 723          $post->post_content .= ' foo';
 724  
 725          wp_update_post( $post );
 726  
 727          // 'publish' => 'publish'
 728          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not trackable blog post should not have activity' );
 729  
 730          remove_filter( 'bp_blogs_is_blog_trackable', '__return_false' );
 731      }
 732  
 733      /**
 734       * @group bp_blogs_catch_transition_post_status
 735       */
 736  	public function test_bp_is_blog_public_zero_publish_post() {
 737          if ( ! is_multisite() ) {
 738              $this->markTestSkipped();
 739          }
 740  
 741          add_filter( 'bp_is_blog_public', '__return_zero' );
 742  
 743          $post_id = self::factory()->post->create( array(
 744              'post_status'   => 'publish',
 745              'post_type'     => 'post',
 746          ) );
 747          $post = get_post( $post_id );
 748  
 749          // 'new' => 'publish'
 750          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not public blog post should not have activity' );
 751  
 752          $post->post_content .= ' foo';
 753  
 754          wp_update_post( $post );
 755  
 756          // 'publish' => 'publish'
 757          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not public blog post should not have activity' );
 758  
 759          remove_filter( 'bp_is_blog_public', '__return_zero' );
 760      }
 761  
 762      /**
 763       * @group bp_blogs_record_comment
 764       * @group unique
 765       * @group post_type_comment_activities
 766       */
 767      public function test_bp_blogs_record_comment_no_duplicate_activity_comments() {
 768          // save the current user and override logged-in user
 769          $old_user = get_current_user_id();
 770          $u = self::factory()->user->create();
 771          $this->set_current_user( $u );
 772          $userdata = get_userdata( $u );
 773          $this->activity_saved_comment_count = 0;
 774          $this->comment_saved_count = 0;
 775  
 776          // let's use activity comments instead of single "new_blog_comment" activity items
 777          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 778          add_action( 'bp_activity_add', array( $this, 'count_activity_comment_saved' ) );
 779          add_action( 'wp_insert_comment', array( $this, 'count_post_comment_saved' ) );
 780          add_action( 'edit_comment', array( $this, 'count_post_comment_saved' ) );
 781  
 782          // create the blog post
 783          $post_id = self::factory()->post->create( array(
 784              'post_status' => 'publish',
 785              'post_type'   => 'post',
 786              'post_title'  => 'Test Duplicate activity comments',
 787          ) );
 788  
 789          // grab the activity ID for the activity comment
 790          $a1 = bp_activity_get_activity_id( array(
 791              'type'      => 'new_blog_post',
 792              'component' => buddypress()->blogs->id,
 793              'filter'    => array(
 794                  'item_id' => get_current_blog_id(),
 795                  'secondary_item_id' => $post_id
 796              ),
 797          ) );
 798  
 799          $a2 = bp_activity_new_comment( array(
 800              'content'     => 'activity comment should be unique',
 801              'user_id'     => $u,
 802              'activity_id' => $a1,
 803          ) );
 804  
 805          $activities = bp_activity_get( array(
 806              'type'             => 'activity_comment',
 807              'display_comments' => 'stream',
 808              'search_terms'     => 'activity comment should be unique',
 809          ) );
 810  
 811          $this->assertTrue( count( $activities['activities'] ) === 1, 'An activity comment should be unique' );
 812  
 813          $this->assertTrue( 2 === $this->activity_saved_comment_count, 'An activity comment should be saved only twice' );
 814          $this->assertTrue( 1 === $this->comment_saved_count, 'A comment should be saved only once' );
 815  
 816          // reset
 817          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 818          remove_action( 'bp_activity_add', array( $this, 'count_activity_comment_saved' ) );
 819          remove_action( 'wp_insert_comment', array( $this, 'count_post_comment_saved' ) );
 820          remove_action( 'edit_comment', array( $this, 'count_post_comment_saved' ) );
 821  
 822          $this->activity_saved_comment_count = 0;
 823          $this->comment_saved_count = 0;
 824          $this->set_current_user( $old_user );
 825      }
 826  
 827      /**
 828       * @group bp_blogs_record_comment
 829       * @group post_type_comment_activities
 830       */
 831      public function test_bp_blogs_record_comment_should_record_parent_blog_post_activity_if_not_found() {
 832          // Save the current user and override logged-in user
 833          $old_user = get_current_user_id();
 834          $u = self::factory()->user->create();
 835          $this->set_current_user( $u );
 836  
 837          // Get user details
 838          $user = get_userdata( $u );
 839  
 840          // Let's use activity comments instead of single "new_blog_comment" activity items
 841          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 842  
 843          // Create the blog post
 844          $post_id = self::factory()->post->create( array(
 845              'post_status' => 'publish',
 846              'post_type'   => 'post',
 847          ) );
 848  
 849          // Now, delete the activity item for the blog post
 850          bp_activity_delete( array(
 851              'component'         => buddypress()->blogs->id,
 852              'type'              => 'new_blog_post',
 853              'item_id'           => get_current_blog_id(),
 854              'secondary_item_id' => $post_id,
 855          ) );
 856  
 857          // Add a comment to blog post
 858          wp_new_comment( array(
 859              'comment_post_ID' => $post_id,
 860              'user_id' => $u,
 861              'comment_content' => 'Dummy comment',
 862              'comment_author' => 'Dumbo',
 863              'comment_author_url' => 'http://buddypress.org',
 864  
 865              // Important to pass check in bp_blogs_record_comment()
 866              'comment_author_email' => $user->user_email
 867          ) );
 868  
 869          // Fetch the activity ID for the blog post to see if it exists
 870          $a1 = bp_activity_get_activity_id( array(
 871              'type'      => 'new_blog_post',
 872              'component' => buddypress()->blogs->id,
 873              'filter'    => array(
 874                  'item_id' => get_current_blog_id(),
 875                  'secondary_item_id' => $post_id
 876              ),
 877          ) );
 878  
 879          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 880  
 881          // Assert that activity item for blog post was created after adding a comment
 882          $this->assertNotNull( $a1, 'Activity item was not created for existing blog post when recording post comment.' );
 883  
 884          $this->set_current_user( $old_user );
 885      }
 886  
 887      /**
 888       * @group bp_blogs_comment_sync_activity_comment
 889       * @group post_type_comment_activities
 890       */
 891      public function test_bp_blogs_comment_sync_activity_comment_for_custom_post_type() {
 892          if ( is_multisite() ) {
 893              if ( function_exists( 'wp_initialize_site' ) ) {
 894                  $this->setExpectedDeprecated( 'wpmu_new_blog' );
 895              }
 896  
 897              $b = self::factory()->blog->create();
 898              switch_to_blog( $b );
 899              add_filter( 'comment_flood_filter', '__return_false' );
 900          } else {
 901              $b = get_current_blog_id();
 902          }
 903  
 904          $u = self::factory()->user->create();
 905          $userdata = get_userdata( $u );
 906  
 907          $labels = array(
 908              'name'                       => 'bars',
 909              'singular_name'              => 'bar',
 910          );
 911  
 912          register_post_type( 'foo', array(
 913              'labels'   => $labels,
 914              'public'   => true,
 915              'supports' => array( 'comments' ),
 916          ) );
 917  
 918          add_post_type_support( 'foo', 'buddypress-activity' );
 919  
 920          bp_activity_set_post_type_tracking_args( 'foo', array(
 921              'comment_action_id' => 'new_foo_comment',
 922          ) );
 923  
 924          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 925  
 926          $p = self::factory()->post->create( array(
 927              'post_author' => $u,
 928              'post_type'   => 'foo',
 929          ) );
 930  
 931          $a1 = bp_activity_get_activity_id( array(
 932              'type'      => 'new_foo',
 933              'filter'    => array(
 934                  'item_id' => $b,
 935                  'secondary_item_id' => $p
 936              ),
 937          ) );
 938  
 939          $c = wp_new_comment( array(
 940              'comment_post_ID'      => $p,
 941              'comment_author'       => $userdata->user_nicename,
 942              'comment_author_url'   => 'http://buddypress.org',
 943              'comment_author_email' => $userdata->user_email,
 944              'comment_content'      => 'this is a foo comment',
 945              'comment_type'         => '',
 946              'comment_parent'       => 0,
 947              'user_id'              => $u,
 948          ) );
 949  
 950          $a2 = bp_activity_new_comment( array(
 951              'content'     => 'this should generate a new foo comment',
 952              'user_id'     => $u,
 953              'activity_id' => $a1,
 954          ) );
 955  
 956          $activity_args = array(
 957              'type'              => 'activity_comment',
 958              'display_comments'  => 'stream',
 959              'meta_query'        => array( array(
 960                  'key'       => 'bp_blogs_foo_comment_id',
 961                  'compare'   => 'exists',
 962              ) )
 963          );
 964  
 965          $a = bp_activity_get( $activity_args );
 966          $aids = wp_list_pluck( $a['activities'], 'id' );
 967          $cids = wp_list_pluck( get_approved_comments( $p ), 'comment_ID' );
 968  
 969          foreach ( $aids as $aid ) {
 970              $this->assertTrue( in_array( bp_activity_get_meta( $aid, 'bp_blogs_foo_comment_id' ), $cids ), 'The comment ID should be in the activity meta' );
 971          }
 972  
 973          foreach ( $cids as $cid ) {
 974              $this->assertTrue( in_array( get_comment_meta( $cid, 'bp_activity_comment_id', true ), $aids ), 'The activity ID should be in the comment meta' );
 975          }
 976  
 977          _unregister_post_type( 'foo' );
 978  
 979          if ( is_multisite() ) {
 980              restore_current_blog();
 981              remove_filter( 'comment_flood_filter', '__return_false' );
 982          }
 983  
 984          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 985      }
 986  
 987  	public function count_activity_comment_saved() {
 988          $this->activity_saved_comment_count += 1;
 989      }
 990  
 991  	public function count_post_comment_saved() {
 992          $this->comment_saved_count += 1;
 993      }
 994  
 995      /**
 996       * @group bp_blogs_record_existing_blogs
 997       */
 998  	public function test_bp_blogs_record_existing_blogs_limit() {
 999          if ( ! is_multisite() ) {
1000              $this->markTestSkipped();
1001          }
1002  
1003          if ( function_exists( 'wp_initialize_site' ) ) {
1004              $this->setExpectedDeprecated( 'wpmu_new_blog' );
1005          }
1006  
1007          $old_user = get_current_user_id();
1008  
1009          $u = self::factory()->user->create();
1010          $this->set_current_user( $u );
1011  
1012          // Create three sites.
1013          self::factory()->blog->create_many( 3, array(
1014              'user_id' => $u
1015          ) );
1016  
1017          // Record each site one at a time
1018          bp_blogs_record_existing_blogs( array(
1019              'limit' => 1
1020          ) );
1021  
1022          // Assert!
1023          $blogs = bp_blogs_get_blogs( array(
1024              'user_id' => $u
1025          ) );
1026          $this->assertSame( 3, (int) $blogs['total'] );
1027  
1028          $this->set_current_user( $old_user );
1029      }
1030  
1031      /**
1032       * @group bp_blogs_remove_blog
1033       */
1034  	public function test_bp_blogs_remove_blog() {
1035          if ( ! is_multisite() ) {
1036              $this->markTestSkipped();
1037          }
1038  
1039          if ( function_exists( 'wp_initialize_site' ) ) {
1040              $this->setExpectedDeprecated( 'wpmu_new_blog' );
1041              $this->setExpectedDeprecated( 'delete_blog' );
1042          }
1043  
1044          $reset_post = $_POST;
1045          $old_user = get_current_user_id();
1046  
1047          // Simulate a new "BuddyPress generated" blog
1048          $_POST['blog_public'] = 1;
1049  
1050          $u = self::factory()->user->create();
1051          $this->set_current_user( $u );
1052  
1053          // Create three sites.
1054          $b = self::factory()->blog->create( array(
1055              'user_id' => $u
1056          ) );
1057  
1058          $activity = bp_activity_get( array(
1059              'filter' => array(
1060                  'object'     => 'blogs',
1061                  'action'     => 'new_blog',
1062                  'primary_id' => $b,
1063              ),
1064          ) );
1065  
1066          $new_blog = array_map( 'intval', wp_list_pluck( $activity['activities'], 'item_id', 'id' ) );
1067          $this->assertSame( $b, reset( $new_blog ) );
1068  
1069          // Removing the blog should delete the activity and the blog association.
1070          wpmu_delete_blog( $b );
1071  
1072          $deleted = bp_activity_get( array(
1073              'in' => array_keys( $new_blog ),
1074          ) );
1075  
1076          $this->assertEmpty( $deleted['activities'] );
1077          $this->assertEmpty( BP_Blogs_Blog::is_recorded( $b ) );
1078  
1079          $_POST = $reset_post;
1080          $this->set_current_user( $old_user );
1081      }
1082  
1083      /**
1084       * @group bp_blogs_remove_blog_for_user
1085       */
1086      public function test_bp_blogs_remove_blog_for_user_is_contributor() {
1087          if ( ! is_multisite() ) {
1088              $this->markTestSkipped();
1089          }
1090  
1091          if ( function_exists( 'wp_initialize_site' ) ) {
1092              $this->setExpectedDeprecated( 'wpmu_new_blog' );
1093          }
1094  
1095          $reset_post = $_POST;
1096          $old_user = get_current_user_id();
1097  
1098          // Simulate a new "BuddyPress generated" blog
1099          $_POST['blog_public'] = 1;
1100  
1101          $u = self::factory()->user->create();
1102          $this->set_current_user( $u );
1103  
1104          // Create three sites.
1105          $b = self::factory()->blog->create( array(
1106              'user_id' => $u
1107          ) );
1108  
1109          $u2 = self::factory()->user->create();
1110          add_user_to_blog( $b, $u2, 'contributor' );
1111  
1112          $u2_blogs = BP_Blogs_Blog::get_blog_ids_for_user( $u2 );
1113          $this->assertContains( $b, $u2_blogs, 'The user should be associated to the blog as he is a contributor' );
1114  
1115          remove_user_from_blog( $u2, $b );
1116          $u2_blogs = BP_Blogs_Blog::get_blog_ids_for_user( $u2 );
1117          $this->assertNotContains( $b, $u2_blogs, 'The user should not be associated anymore to the blog' );
1118  
1119          $activity = bp_activity_get( array(
1120              'filter' => array(
1121                  'object'     => 'blogs',
1122                  'action'     => 'new_blog',
1123                  'primary_id' => $b,
1124              ),
1125          ) );
1126  
1127          $new_blog = array_map( 'intval', wp_list_pluck( $activity['activities'], 'item_id', 'id' ) );
1128          $this->assertSame( $b, reset( $new_blog ), 'The new_blog activity should not be deleted when a contributor is removed from the blog.' );
1129  
1130          $_POST = $reset_post;
1131          $this->set_current_user( $old_user );
1132      }
1133  
1134  	protected function activity_exists_for_post( $post_id ) {
1135          $a = bp_activity_get( array(
1136              'component' => buddypress()->blogs->id,
1137              'action' => 'new_blog_post',
1138              'item_id' => get_current_blog_id(),
1139              'secondary_item_id' => $post_id,
1140          ) );
1141  
1142          return ! empty( $a['activities'] );
1143      }
1144  }


Generated: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1