[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/activity/ -> actions.php (source)

   1  <?php
   2  /**
   3   * @group activity
   4   */
   5  class BP_Tests_Activity_Actions extends BP_UnitTestCase {
   6  
   7      /**
   8       * @group bp_activity_catch_transition_post_type_status
   9       * @group activity_tracking
  10       */
  11      public function test_bp_activity_catch_transition_post_type_status_publish() {
  12          register_post_type( 'foo', array(
  13              'label'   => 'foo',
  14              'public'   => true,
  15              'supports' => array( 'buddypress-activity' ),
  16          ) );
  17  
  18          $post_id = self::factory()->post->create( array(
  19              'post_status' => 'publish',
  20              'post_type'   => 'foo',
  21          ) );
  22  
  23          $post = get_post( $post_id );
  24  
  25          // 'new' => 'publish'
  26          $this->assertTrue( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Published post type should have activity' );
  27  
  28          _unregister_post_type( 'foo' );
  29      }
  30  
  31      /**
  32       * @group bp_activity_catch_transition_post_type_status
  33       * @group activity_tracking
  34       */
  35      public function test_bp_activity_catch_transition_post_type_status_publish_to_publish() {
  36          register_post_type( 'foo', array(
  37              'label'   => 'foo',
  38              'public'   => true,
  39              'supports' => array( 'buddypress-activity' ),
  40          ) );
  41  
  42          $post_id = self::factory()->post->create( array(
  43              'post_status' => 'publish',
  44              'post_type'   => 'foo',
  45          ) );
  46  
  47          $post = get_post( $post_id );
  48  
  49          // 'new' => 'publish'
  50          $this->assertTrue( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Published post type should have activity' );
  51  
  52          _unregister_post_type( 'foo' );
  53      }
  54  
  55      /**
  56       * @group bp_activity_catch_transition_post_type_status
  57       * @group activity_tracking
  58       */
  59      public function test_bp_activity_catch_transition_post_type_status_publish_existing_post() {
  60          $u = self::factory()->user->create();
  61  
  62          $labels = array(
  63              'bp_activity_admin_filter' => 'New Foo',
  64              'bp_activity_front_filter' => 'Foos',
  65                  'bp_activity_new_post'    => '%1$s posted a new <a href="%2$s">foo</a>',
  66                  'bp_activity_new_post_ms' => '%1$s posted a new <a href="%2$s">foo</a>, on the site %3$s',
  67          );
  68  
  69          /**
  70           * 'public' must be set to true, otherwise bp_activity_get_post_types_tracking_args() fails.
  71           */
  72          register_post_type( 'foo', array(
  73              'labels'      => $labels,
  74              'public'      => true,
  75              'supports'    => array( 'buddypress-activity' ),
  76              'bp_activity' => array(
  77                  'action_id'    => 'new_foo',
  78                  'contexts'     => array( 'activity' ),
  79                  'position'     => 40,
  80              )
  81          ) );
  82  
  83          // Temporarily remove post type activity hook so activity item isn't created.
  84          remove_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10 );
  85  
  86          // Create the initial post.
  87          $p = self::factory()->post->create( array(
  88              'post_author' => $u,
  89              'post_type'   => 'foo',
  90          ) );
  91  
  92          $this->assertEmpty( bp_activity_get_activity_id( array( 'type' => 'new_foo' ) ) );
  93  
  94          // Add the post type activity hook back.
  95          add_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3 );
  96  
  97          // Emulate updating a post; this should create an activity item.
  98          wp_update_post( array(
  99              'ID'     => $p,
 100              'post_title' => 'This is an edit',
 101          ) );
 102  
 103          // Assert!
 104          $this->assertNotEmpty( bp_activity_get_activity_id( array( 'type' => 'new_foo' ) ), 'Activity item was not created during an edit of an existing WordPress post.' );
 105  
 106          // Clean up.
 107          _unregister_post_type( 'foo' );
 108      }
 109  
 110      /**
 111       * @group bp_activity_catch_transition_post_type_status
 112       * @group activity_tracking
 113       */
 114      public function test_bp_activity_catch_transition_post_type_status_publish_password() {
 115          register_post_type( 'foo', array(
 116              'label'   => 'foo',
 117              'public'   => true,
 118              'supports' => array( 'buddypress-activity' ),
 119          ) );
 120  
 121          $post_id = self::factory()->post->create( array(
 122              'post_status' => 'publish',
 123              'post_type'   => 'foo',
 124          ) );
 125  
 126          $post = get_post( $post_id );
 127  
 128          // 'new' => 'publish'
 129          $this->assertTrue( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Published post type should have activity' );
 130  
 131          $post->post_status   = 'publish';
 132          $post->post_password = 'foo';
 133  
 134          wp_update_post( $post );
 135  
 136          // 'publish' => 'publish' (password protected)
 137          $this->assertFalse( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Password protected post type should not have activity' );
 138  
 139          _unregister_post_type( 'foo' );
 140      }
 141  
 142      /**
 143       * @group bp_activity_catch_transition_post_type_status
 144       * @group activity_tracking
 145       */
 146      public function test_bp_activity_catch_transition_post_type_status_publish_trash() {
 147          register_post_type( 'foo', array(
 148              'label'   => 'foo',
 149              'public'   => true,
 150              'supports' => array( 'buddypress-activity' ),
 151          ) );
 152  
 153          $post_id = self::factory()->post->create( array(
 154              'post_status' => 'publish',
 155              'post_type'   => 'foo',
 156          ) );
 157  
 158          $post = get_post( $post_id );
 159  
 160          // 'new' => 'publish'
 161          $this->assertTrue( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Published post type should have activity' );
 162  
 163          wp_trash_post( $post->ID );
 164  
 165          // 'publish' => 'publish' (password protected)
 166          $this->assertFalse( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Unpublished post type should not have activity' );
 167  
 168          _unregister_post_type( 'foo' );
 169      }
 170  
 171  	protected function activity_exists_for_post( $post_id, $action ) {
 172          $a = bp_activity_get( array(
 173              'action'            => $action,
 174              'item_id'           => get_current_blog_id(),
 175              'secondary_item_id' => $post_id,
 176          ) );
 177  
 178          return ! empty( $a['activities'] );
 179      }
 180  }


Generated: Mon Dec 16 01:01:39 2019 Cross-referenced by PHPXref 0.7.1