| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group blogs |
| 5 | * @group functions |
| 6 | */ |
| 7 | class BP_Tests_Blogs_Functions extends BP_UnitTestCase { |
| 8 | /** |
| 9 | * @group bp_blogs_catch_transition_post_status |
| 10 | */ |
| 11 | public function test_transition_post_status_publish_to_publish() { |
| 12 | $post_id = $this->factory->post->create( array( |
| 13 | 'post_status' => 'publish', |
| 14 | 'post_type' => 'post', |
| 15 | ) ); |
| 16 | $post = get_post( $post_id ); |
| 17 | |
| 18 | // 'publish' => 'publish' |
| 19 | $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' ); |
| 20 | |
| 21 | $post->post_status = 'publish'; |
| 22 | $post->post_content .= ' foo'; |
| 23 | |
| 24 | wp_update_post( $post ); |
| 25 | |
| 26 | $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity (no change)' ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @group bp_blogs_catch_transition_post_status |
| 31 | */ |
| 32 | public function test_transition_post_status_draft_to_draft() { |
| 33 | $post_id = $this->factory->post->create( array( |
| 34 | 'post_status' => 'draft', |
| 35 | 'post_type' => 'post', |
| 36 | ) ); |
| 37 | $post = get_post( $post_id ); |
| 38 | |
| 39 | $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' ); |
| 40 | |
| 41 | $post->post_status = 'draft'; |
| 42 | $post->post_content .= ' foo'; |
| 43 | |
| 44 | wp_update_post( $post ); |
| 45 | |
| 46 | $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity (no change)' ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @group bp_blogs_catch_transition_post_status |
| 51 | */ |
| 52 | public function test_transition_post_status_draft_to_publish() { |
| 53 | $post_id = $this->factory->post->create( array( |
| 54 | 'post_status' => 'draft', |
| 55 | 'post_type' => 'post', |
| 56 | ) ); |
| 57 | $post = get_post( $post_id ); |
| 58 | |
| 59 | $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' ); |
| 60 | |
| 61 | $post->post_status = 'publish'; |
| 62 | $post->post_content .= ' foo'; |
| 63 | |
| 64 | wp_update_post( $post ); |
| 65 | |
| 66 | $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @group bp_blogs_catch_transition_post_status |
| 71 | */ |
| 72 | public function test_transition_post_status_publish_to_draft() { |
| 73 | $post_id = $this->factory->post->create( array( |
| 74 | 'post_status' => 'publish', |
| 75 | 'post_type' => 'post', |
| 76 | ) ); |
| 77 | $post = get_post( $post_id ); |
| 78 | |
| 79 | $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' ); |
| 80 | |
| 81 | $post->post_status = 'draft'; |
| 82 | $post->post_content .= ' foo'; |
| 83 | |
| 84 | wp_update_post( $post ); |
| 85 | |
| 86 | $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @group bp_blogs_catch_transition_post_status |
| 91 | */ |
| 92 | public function test_transition_post_status_wp_delete_post() { |
| 93 | $post_id = $this->factory->post->create( array( |
| 94 | 'post_status' => 'publish', |
| 95 | 'post_type' => 'post', |
| 96 | ) ); |
| 97 | $post = get_post( $post_id ); |
| 98 | |
| 99 | $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' ); |
| 100 | |
| 101 | wp_delete_post( $post->ID ); |
| 102 | |
| 103 | $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @group bp_blogs_catch_transition_post_status |
| 108 | */ |
| 109 | public function test_transition_post_status_wp_trash_post() { |
| 110 | $post_id = $this->factory->post->create( array( |
| 111 | 'post_status' => 'publish', |
| 112 | 'post_type' => 'post', |
| 113 | ) ); |
| 114 | $post = get_post( $post_id ); |
| 115 | |
| 116 | $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' ); |
| 117 | |
| 118 | wp_trash_post( $post->ID ); |
| 119 | |
| 120 | $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' ); |
| 121 | } |
| 122 | |
| 123 | protected function activity_exists_for_post( $post_id ) { |
| 124 | $a = bp_activity_get( array( |
| 125 | 'component' => buddypress()->blogs->id, |
| 126 | 'action' => 'new_blog_post', |
| 127 | 'item_id' => get_current_blog_id(), |
| 128 | 'secondary_item_id' => $post_id, |
| 129 | ) ); |
| 130 | |
| 131 | return ! empty( $a['total'] ); |
| 132 | } |
| 133 | } |