activity->create( array( 'content' => $content, 'type' => 'activity_update', ) ); $result = BP_Activity_Activity::check_exists_by_content( $content ); $this->assertSame( $activity, $result ); } /** * @group delete_activity_item_comments */ public function test_delete_activity_item_comments() { $parent_activity = self::factory()->activity->create( array( 'type' => 'activity_update', ) ); $comments = self::factory()->activity->create_many( 3, array( 'item_id' => $parent_activity, 'type' => 'activity_comment', ) ); BP_Activity_Activity::delete_activity_item_comments( $parent_activity ); $result = BP_Activity_Activity::get( array( 'in' => $comments ) ); $this->assertEmpty( $result['activities'] ); } /** * @ticket BP4804 * @group delete_activity_meta_entries * @group activitymeta */ public function test_delete_activity_meta_entries() { $activity = self::factory()->activity->create( array( 'type' => 'activity_update', ) ); bp_activity_update_meta( $activity, 'Paul', 'is cool' ); BP_Activity_Activity::delete_activity_meta_entries( $activity ); $meta = bp_activity_get_meta( $activity, 'Paul' ); $this->assertSame( '', $meta ); } /** * @group get * @group fields * @ticket BP6426 */ public function test_get_with_fields_parameter_by_id() { $a = self::factory()->activity->create_many( 3, array( 'type' => 'activity_update', ) ); $result = BP_Activity_Activity::get( array( 'fields' => 'ids', ) ); $this->assertEqualSets( $a, $result['activities'] ); } /** * @group get */ public function test_hide_all_for_user() { $activity = self::factory()->activity->create( array( 'type' => 'activity_update', ) ); BP_Activity_Activity::hide_all_for_user( get_current_user_id() ); $activity = BP_Activity_Activity::get( array( 'in' => $activity, 'show_hidden' => true, ) ); $this->assertSame( $activity['activities'][0]->hide_sitewide, 1 ); } /** * @group get * @group meta_query */ public function test_get_with_meta_query() { $a1 = self::factory()->activity->create(); $a2 = self::factory()->activity->create(); bp_activity_update_meta( $a1, 'foo', 'bar' ); $activity = BP_Activity_Activity::get( [ 'meta_query' => [ [ 'key' => 'foo', 'compare' => 'EXISTS', ], ], ] ); $this->assertSame( [ $a1 ], wp_list_pluck( $activity['activities'], 'id' ) ); $activity = BP_Activity_Activity::get( [ 'meta_query' => [ [ 'key' => 'foo', 'compare' => 'NOT EXISTS', ], ], ] ); $this->assertSame( [ $a2 ], wp_list_pluck( $activity['activities'], 'id' ) ); } /** * @group get */ public function test_get_with_meta_query_two_clauses_with_or_relation() { $now = time(); $a1 = self::factory()->activity->create( array( 'recorded_time' => date( 'Y-m-d H:i:s', $now ), ) ); $a2 = self::factory()->activity->create( array( 'recorded_time' => date( 'Y-m-d H:i:s', $now - 60 ), ) ); $a3 = self::factory()->activity->create( array( 'recorded_time' => date( 'Y-m-d H:i:s', $now - 120 ), ) ); bp_activity_update_meta( $a1, 'foo', 'bar' ); bp_activity_update_meta( $a2, 'foo', 'bar' ); bp_activity_update_meta( $a1, 'baz', 'barry' ); $activity = BP_Activity_Activity::get( array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'foo', 'value' => 'bar', ), array( 'key' => 'baz', 'value' => 'barry', ), ), 'count_total' => 'count_query', ) ); $ids = wp_list_pluck( $activity['activities'], 'id' ); $this->assertSame( array( $a1, $a2 ), $ids ); $this->assertSame( 2, $activity['total'] ); } /** * @group get * @group date_query */ public function test_get_with_date_query_before() { $a1 = self::factory()->activity->create(); $a2 = self::factory()->activity->create( array( 'recorded_time' => '2001-01-01 12:00' ) ); $a3 = self::factory()->activity->create( array( 'recorded_time' => '2005-01-01 12:00' ) ); // 'date_query' before test $query = BP_Activity_Activity::get( array( 'date_query' => array( array( 'before' => array( 'year' => 2004, 'month' => 1, 'day' => 1, ), ) ) ) ); $this->assertSame( array( $a2 ), wp_list_pluck( $query['activities'], 'id' ) ); } /** * @group get * @group date_query */ public function test_get_with_date_query_range() { $a1 = self::factory()->activity->create(); $a2 = self::factory()->activity->create( array( 'recorded_time' => '2001-01-01 12:00' ) ); $a3 = self::factory()->activity->create( array( 'recorded_time' => '2005-01-01 12:00' ) ); // 'date_query' range test $query = BP_Activity_Activity::get( array( 'date_query' => array( array( 'after' => 'January 2nd, 2001', 'before' => array( 'year' => 2013, 'month' => 1, 'day' => 1, ), 'inclusive' => true, ) ) ) ); $this->assertSame( array( $a3 ), wp_list_pluck( $query['activities'], 'id' ) ); } /** * @group get * @group date_query */ public function test_get_with_date_query_after() { $a1 = self::factory()->activity->create(); $a2 = self::factory()->activity->create( array( 'recorded_time' => '2001-01-01 12:00' ) ); $a3 = self::factory()->activity->create( array( 'recorded_time' => '2005-01-01 12:00' ) ); // 'date_query' after and relative test $query = BP_Activity_Activity::get( array( 'date_query' => array( array( 'after' => '1 day ago' ) ) ) ); $this->assertSame( array( $a1 ), wp_list_pluck( $query['activities'], 'id' ) ); } /** * @group get */ public function test_get_with_search_terms() { $a1 = self::factory()->activity->create( array( 'content' => 'Boone is a cool guy', ) ); $a2 = self::factory()->activity->create( array( 'content' => 'No he isn\'t', ) ); $activity = BP_Activity_Activity::get( array( 'search_terms' => 'cool', ) ); $ids = wp_list_pluck( $activity['activities'], 'id' ); $this->assertSame( $ids, array( $a1 ) ); } /** * @group get */ public function test_get_with_display_comments_threaded() { $u = self::factory()->user->create(); $now = time(); $a1 = self::factory()->activity->create( array( 'user_id' => $u, 'content' => 'Life Rules', 'recorded_time' => date( 'Y-m-d H:i:s', $now ), ) ); $a2 = self::factory()->activity->create( array( 'user_id' => $u, 'content' => 'Life Drools', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ), ) ); $a3 = bp_activity_new_comment( array( 'user_id' => $u, 'activity_id' => $a1, 'content' => 'Candy is good', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 50 ), ) ); $activity = BP_Activity_Activity::get( array( 'display_comments' => 'threaded', ) ); // Kinda crummy, but let's construct a skeleton $expected = array( $a1 => array( $a3 ), $a2 => array(), ); $found = array(); foreach ( $activity['activities'] as $a ) { $found[ $a->id ] = ! empty( $a->children ) ? array_keys( $a->children ) : array(); } $this->assertSame( $expected, $found ); } /** * @group get */ public function test_get_with_display_comments_stream() { $now = time(); $a1 = self::factory()->activity->create( array( 'content' => 'Life Rules', 'recorded_time' => date( 'Y-m-d H:i:s', $now ), ) ); $a2 = self::factory()->activity->create( array( 'content' => 'Life Drools', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ), ) ); // bp_activity_new_comment() doesn't allow date_recorded $a3 = self::factory()->activity->create( array( 'action' => sprintf( __( '%s posted a new activity comment', 'buddypress' ), bp_loggedin_user_url() ) , 'content' => 'Candy is good', 'component' => buddypress()->activity->id, 'type' => 'activity_comment', 'user_id' => bp_loggedin_user_id(), 'item_id' => $a1, 'secondary_item_id' => $a1, 'primary_link' => '', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 50 ), ) ); $activity = BP_Activity_Activity::get( array( 'display_comments' => 'stream', ) ); $ids = wp_list_pluck( $activity['activities'], 'id' ); $this->assertSame( array( $a1, $a3, $a2 ), $ids ); } /** * @group get */ public function test_get_with_display_comments_false() { $now = time(); $a1 = self::factory()->activity->create( array( 'content' => 'Life Rules', 'recorded_time' => date( 'Y-m-d H:i:s', $now ), ) ); $a2 = self::factory()->activity->create( array( 'content' => 'Life Drools', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ), ) ); $a3 = bp_activity_new_comment( array( 'activity_id' => $a1, 'content' => 'Candy is good', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 50 ), ) ); $activity = BP_Activity_Activity::get( array( 'display_comments' => false, ) ); $ids = wp_list_pluck( $activity['activities'], 'id' ); $this->assertSame( array( $a1, $a2 ), $ids ); } /** * @group get */ public function test_get_with_offset() { $now = time(); $a1 = self::factory()->activity->create( array( 'content' => 'Life Rules', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ), ) ); $a2 = self::factory()->activity->create( array( 'content' => 'Life Drools', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 50 ), ) ); $a3 = self::factory()->activity->create( array( 'content' => 'Life Drools', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 10 ), ) ); $activity = BP_Activity_Activity::get( array( 'filter' => array( 'offset' => $a2, ), ) ); $ids = wp_list_pluck( $activity['activities'], 'id' ); $this->assertSame( array( $a3, $a2 ), $ids ); } /** * @group get */ public function test_get_with_since() { $now = time(); $a1 = self::factory()->activity->create( array( 'content' => 'Life Rules', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ), ) ); $a2 = self::factory()->activity->create( array( 'content' => 'Life Drools', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 50 ), ) ); $a3 = self::factory()->activity->create( array( 'content' => 'Life Drools', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 10 ), ) ); $activity = BP_Activity_Activity::get( array( 'filter' => array( 'since' => date( 'Y-m-d H:i:s', $now - 70 ), ), ) ); $ids = wp_list_pluck( $activity['activities'], 'id' ); $this->assertSame( array( $a3, $a2 ), $ids ); } /** * @group get * @ticket BP9094 */ public function test_get_with_offset_lower() { $now = time(); $a1 = self::factory()->activity->create( array( 'content' => 'Happy Valentine’s day', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ), ) ); $a2 = self::factory()->activity->create( array( 'content' => 'Happy new year', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 50 ), ) ); $a3 = self::factory()->activity->create( array( 'content' => 'Happy days', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 10 ), ) ); $activity = BP_Activity_Activity::get( array( 'filter' => array( 'offset_lower' => $a2, ), ) ); $ids = wp_list_pluck( $activity['activities'], 'id' ); $this->assertSame( array( $a2, $a1 ), $ids ); } /** * @group get * @ticket BP9094 */ public function test_get_with_offset_lower_with_pagination() { $a = self::factory()->activity->create_many( 3, array( 'type' => 'activity_update', ) ); $stream = BP_Activity_Activity::get( array( 'page' => 1, 'per_page' => 2, 'filter' => array( 'offset_lower' => end( $a ), ), ) ); $page_one_ids = wp_list_pluck( $stream['activities'], 'id' ); $last_displayed_on_first_page = end( $page_one_ids ); $a1 = self::factory()->activity->create( array( 'content' => 'Ouch this is an Auto Refresh simulation', ) ); $stream = BP_Activity_Activity::get( array( 'page' => 2, 'per_page' => 2, ) ); $page_two_ids = wp_list_pluck( $stream['activities'], 'id' ); $first_displayed_on_page_two = reset( $page_two_ids ); $this->assertSame( $first_displayed_on_page_two, $last_displayed_on_first_page ); $stream = BP_Activity_Activity::get( array( 'page' => 2, 'per_page' => 2, 'filter' => array( 'offset_lower' => end( $a ), ), ) ); $without_auto_refresh = wp_list_pluck( $stream['activities'], 'id' ); $this->assertNotContains( $last_displayed_on_first_page, $without_auto_refresh ); $this->assertSame( array_reverse( $a ), array_merge( $page_one_ids, $without_auto_refresh ) ); } /** * @group get */ public function test_get_with_user_id__in() { $u1 = self::factory()->user->create(); $u2 = self::factory()->user->create(); $u3 = self::factory()->user->create(); $a1 = self::factory()->activity->create( array( 'user_id' => $u1 ) ); $a2 = self::factory()->activity->create( array( 'user_id' => $u2 ) ); $a3 = self::factory()->activity->create( array( 'user_id' => $u3 ) ); $activities = BP_Activity_Activity::get( array( 'user_id__in' => array( $u1, $u2 ), ) ); $ids = wp_list_pluck( $activities['activities'], 'id' ); sort( $ids ); $this->assertSame( array( $a1, $a2 ), $ids ); } /** * @group get */ public function test_get_with_user_id__not_in() { $u1 = self::factory()->user->create(); $u2 = self::factory()->user->create(); $u3 = self::factory()->user->create(); $a1 = self::factory()->activity->create( array( 'user_id' => $u1 ) ); $a2 = self::factory()->activity->create( array( 'user_id' => $u2 ) ); $a3 = self::factory()->activity->create( array( 'user_id' => $u3 ) ); $activities = BP_Activity_Activity::get( array( 'user_id__not_in' => array( $u2, $u3 ), ) ); $ids = wp_list_pluck( $activities['activities'], 'id' ); $this->assertSame( array( $a1 ), $ids ); } /** * @group get */ public function test_get_with_user_id__in_andnot_in() { $u1 = self::factory()->user->create(); $u2 = self::factory()->user->create(); $u3 = self::factory()->user->create(); $u4 = self::factory()->user->create(); $u5 = self::factory()->user->create(); $a1 = self::factory()->activity->create( array( 'user_id' => $u1 ) ); $a2 = self::factory()->activity->create( array( 'user_id' => $u2 ) ); $a3 = self::factory()->activity->create( array( 'user_id' => $u3 ) ); $a4 = self::factory()->activity->create( array( 'user_id' => $u4 ) ); $a5 = self::factory()->activity->create( array( 'user_id' => $u5 ) ); $activities = BP_Activity_Activity::get( array( 'user_id__in' => array( $u1, $u2, $u4, $u5 ), 'user_id__not_in' => array( $u4, $u5 ), ) ); $ids = wp_list_pluck( $activities['activities'], 'id' ); sort( $ids ); $this->assertSame( array( $a1, $a2 ), $ids ); } /** * @group get * @group count_total */ public function test_get_with_count_total() { $a1 = self::factory()->activity->create(); $a2 = self::factory()->activity->create(); $activity = BP_Activity_Activity::get( array( 'count_total' => 'count_query', ) ); $this->assertSame( 2, $activity['total'] ); } /** * @expectedDeprecated bp_activity_get_join_sql */ public function test_bp_activity_get_join_sql_preserves_arguments_and_runs_before_canonical_hook() { $allowed = self::factory()->activity->create(); self::factory()->activity->create(); $arguments = array(); $order = array(); $legacy = static function( ...$args ) use ( $allowed, &$arguments, &$order ) { $arguments = $args; $order[] = 'legacy'; return $args[0] . " INNER JOIN (SELECT {$allowed} AS activity_id) allowed ON allowed.activity_id = a.id"; }; $canonical = static function( $sql, $type ) use ( &$order ) { $order[] = $type; return $sql; }; add_filter( 'bp_activity_get_join_sql', $legacy, 10, 5 ); add_filter( 'bp_activity_get_sql', $canonical, 10, 2 ); try { $found = BP_Activity_Activity::get( array( 'count_total' => true ) ); } finally { remove_filter( 'bp_activity_get_join_sql', $legacy ); remove_filter( 'bp_activity_get_sql', $canonical ); } $this->assertCount( 5, $arguments ); $this->assertSame( array( $allowed ), wp_list_pluck( $found['activities'], 'id' ) ); $this->assertSame( 1, $found['total'] ); $this->assertSame( array( 'legacy', 'paged', 'count' ), $order ); $this->assertIsArray( $arguments[1] ); $this->assertStringContainsString( 'SELECT', $arguments[2] ); $this->assertStringContainsString( 'FROM', $arguments[3] ); $this->assertStringContainsString( 'WHERE', $arguments[4] ); } /** * @expectedDeprecated bp_activity_paged_activities_sql */ public function test_bp_activity_paged_activities_sql_preserves_arguments_and_runs_before_canonical_hook() { $allowed = self::factory()->activity->create(); self::factory()->activity->create(); $arguments = array(); $order = array(); $legacy = static function( ...$args ) use ( $allowed, &$arguments, &$order ) { $arguments = $args; $order[] = 'legacy'; return str_replace( 'WHERE', "WHERE a.id = {$allowed} AND", $args[0] ); }; $canonical = static function( $sql, $type ) use ( &$order ) { $order[] = $type; return $sql; }; add_filter( 'bp_activity_paged_activities_sql', $legacy, 10, 2 ); add_filter( 'bp_activity_get_sql', $canonical, 10, 2 ); try { $found = BP_Activity_Activity::get( array( 'count_total' => true ) ); } finally { remove_filter( 'bp_activity_paged_activities_sql', $legacy ); remove_filter( 'bp_activity_get_sql', $canonical ); } $this->assertCount( 2, $arguments ); $this->assertSame( array( $allowed ), wp_list_pluck( $found['activities'], 'id' ) ); $this->assertSame( 2, $found['total'] ); $this->assertSame( array( 'legacy', 'paged', 'count' ), $order ); $this->assertIsArray( $arguments[1] ); $this->assertTrue( $arguments[1]['count_total'] ); } /** * @expectedDeprecated bp_activity_total_activities_sql */ public function test_bp_activity_total_activities_sql_preserves_arguments_and_runs_before_canonical_hook() { $allowed = self::factory()->activity->create(); self::factory()->activity->create(); $arguments = array(); $order = array(); $legacy = static function( ...$args ) use ( $allowed, &$arguments, &$order ) { $arguments = $args; $order[] = 'legacy'; return 'SELECT 42'; }; $canonical = static function( $sql, $type ) use ( &$order ) { $order[] = $type; return $sql; }; add_filter( 'bp_activity_total_activities_sql', $legacy, 10, 3 ); add_filter( 'bp_activity_get_sql', $canonical, 10, 2 ); try { $found = BP_Activity_Activity::get( array( 'count_total' => true ) ); } finally { remove_filter( 'bp_activity_total_activities_sql', $legacy ); remove_filter( 'bp_activity_get_sql', $canonical ); } $this->assertCount( 3, $arguments ); $this->assertSame( 42, $found['total'] ); $this->assertSame( array( 'paged', 'legacy', 'count' ), $order ); $this->assertStringContainsString( 'WHERE', $arguments[1] ); $this->assertSame( 'DESC', $arguments[2] ); } /** * @expectedDeprecated bp_activity_get_user_join_filter */ public function test_bp_activity_get_user_join_filter_preserves_arguments_and_runs_before_canonical_hook() { $allowed = self::factory()->activity->create(); self::factory()->activity->create(); $arguments = array(); $order = array(); $legacy = static function( ...$args ) use ( $allowed, &$arguments, &$order ) { $arguments = $args; $order[] = 'legacy'; return str_replace( 'WHERE', "WHERE a.id = {$allowed} AND", $args[0] ); }; $canonical = static function( $sql, $type ) use ( &$order ) { $order[] = $type; return $sql; }; $use_legacy = static function() { return true; }; add_filter( 'bp_use_legacy_activity_query', $use_legacy ); add_filter( 'bp_activity_get_user_join_filter', $legacy, 10, 6 ); add_filter( 'bp_activity_get_sql', $canonical, 10, 2 ); try { $found = BP_Activity_Activity::get( array( 'count_total' => true ) ); } finally { remove_filter( 'bp_activity_get_user_join_filter', $legacy ); remove_filter( 'bp_activity_get_sql', $canonical ); remove_filter( 'bp_use_legacy_activity_query', $use_legacy ); } $this->assertCount( 6, $arguments ); $this->assertSame( array( $allowed ), wp_list_pluck( $found['activities'], 'id' ) ); $this->assertSame( 2, $found['total'] ); $this->assertSame( array( 'legacy', 'paged', 'count' ), $order ); $this->assertStringContainsString( 'SELECT', $arguments[1] ); $this->assertStringContainsString( 'FROM', $arguments[2] ); $this->assertStringContainsString( 'WHERE', $arguments[3] ); $this->assertSame( 'DESC', $arguments[4] ); $this->assertStringContainsString( 'LIMIT', $arguments[5] ); } public function test_sql_clauses_from_restricts_results_and_total_in_both_query_paths() { $allowed = self::factory()->activity->create(); self::factory()->activity->create(); $table = buddypress()->activity->table_name; $filter = static function( $clauses ) use ( $allowed, $table ) { $clauses['from'] = "FROM (SELECT * FROM {$table} WHERE id = {$allowed}) a"; return $clauses; }; $use_legacy = static function() { return true; }; add_filter( 'bp_activity_get_sql_clauses', $filter ); try { foreach ( array( false, true ) as $legacy ) { if ( $legacy ) { add_filter( 'bp_use_legacy_activity_query', $use_legacy ); } $found = BP_Activity_Activity::get( array( 'count_total' => true ) ); $this->assertSame( array( $allowed ), wp_list_pluck( $found['activities'], 'id' ) ); $this->assertSame( 1, $found['total'] ); } } finally { remove_filter( 'bp_activity_get_sql_clauses', $filter ); remove_filter( 'bp_use_legacy_activity_query', $use_legacy ); } } public function test_self_removing_sql_clauses_filter_at_priority_zero() { $allowed = self::factory()->activity->create(); self::factory()->activity->create(); $calls = 0; $filter = static function( $clauses ) use ( $allowed, &$calls, &$filter ) { remove_filter( 'bp_activity_get_sql_clauses', $filter, 0 ); ++$calls; $clauses['where_conditions']['allowed'] = "a.id = {$allowed}"; return $clauses; }; add_filter( 'bp_activity_get_sql_clauses', $filter, 0 ); try { $found = BP_Activity_Activity::get( array( 'count_total' => true ) ); } finally { remove_filter( 'bp_activity_get_sql_clauses', $filter, 0 ); } $this->assertSame( array( $allowed ), wp_list_pluck( $found['activities'], 'id' ) ); $this->assertSame( 1, $found['total'] ); $this->assertSame( 1, $calls ); } public function test_get_with_sql_clauses_filter() { global $wpdb; $user_1 = self::factory()->user->create(); $user_2 = self::factory()->user->create(); $activity_1 = self::factory()->activity->create( array( 'user_id' => $user_1 ) ); self::factory()->activity->create( array( 'user_id' => $user_2 ) ); $filter = static function ( $sql_clauses ) use ( $wpdb, $user_1 ) { $sql_clauses['join'] .= " INNER JOIN {$wpdb->users} bp_activity_user ON bp_activity_user.ID = a.user_id"; $sql_clauses['where_conditions']['user'] = $wpdb->prepare( 'bp_activity_user.ID = %d', $user_1 ); return $sql_clauses; }; add_filter( 'bp_activity_get_sql_clauses', $filter ); try { $activity = BP_Activity_Activity::get( array( 'count_total' => true ) ); } finally { remove_filter( 'bp_activity_get_sql_clauses', $filter ); } $this->assertSame( array( $activity_1 ), wp_list_pluck( $activity['activities'], 'id' ) ); $this->assertSame( 1, $activity['total'] ); } /** * @group get * @group count_total */ public function test_get_with_sql_filter() { $query_types = array(); self::factory()->activity->create(); $filter = static function ( $sql, $query_type ) use ( &$query_types ) { $query_types[] = $query_type; return $sql; }; add_filter( 'bp_activity_get_sql', $filter, 10, 2 ); try { BP_Activity_Activity::get( array( 'count_total' => true ) ); } finally { remove_filter( 'bp_activity_get_sql', $filter ); } $this->assertSame( array( 'paged', 'count' ), $query_types ); } /** * @group get * @expectedDeprecated bp_activity_get_where_conditions */ public function test_get_with_legacy_where_conditions_filter() { $activity_1 = self::factory()->activity->create(); self::factory()->activity->create(); $filter = static function ( $where_conditions ) use ( $activity_1 ) { $where_conditions['activity_id'] = 'a.id = ' . (int) $activity_1; return $where_conditions; }; add_filter( 'bp_activity_get_where_conditions', $filter ); try { $activity = BP_Activity_Activity::get(); } finally { remove_filter( 'bp_activity_get_where_conditions', $filter ); } $this->assertSame( array( $activity_1 ), wp_list_pluck( $activity['activities'], 'id' ) ); } /** * @group get * @group count_total */ public function test_get_with_count_total_false() { $a1 = self::factory()->activity->create(); $a2 = self::factory()->activity->create(); $activity = BP_Activity_Activity::get( array( 'count_total' => false, ) ); $this->assertNull( $activity['total'] ); } /** * @group get * @group count_total */ public function test_get_with_count_total_default_to_false() { $a1 = self::factory()->activity->create(); $a2 = self::factory()->activity->create(); $activity = BP_Activity_Activity::get(); $this->assertNull( $activity['total'] ); } /** * @group get_id */ public function test_get_id_with_item_id() { $a1 = self::factory()->activity->create( array( 'item_id' => 523, ) ); $a2 = self::factory()->activity->create( array( 'item_id' => 1888, ) ); $args = array( 'item_id' => 523, ); $activity = BP_Activity_Activity::get_id( $args ); $this->assertSame( $a1, $activity ); } /** * @group get_id * @group BP8596 * @expectedDeprecated BP_Activity_Activity::get_id */ public function test_get_id_with_deprecated_args() { $a1 = self::factory()->activity->create( array( 'item_id' => 573, ) ); $a2 = self::factory()->activity->create( array( 'item_id' => 1098, ) ); $activity = BP_Activity_Activity::get_id( false, false, false, 1098, false, false, false, false ); $this->assertSame( $a2, $activity ); } /** * @group get_id */ public function test_get_id_with_secondary_item_id() { $a1 = self::factory()->activity->create( array( 'secondary_item_id' => 523, ) ); $a2 = self::factory()->activity->create( array( 'secondary_content' => 1888, ) ); $args = array( 'secondary_item_id' => 523, ); $activity = BP_Activity_Activity::get_id( $args ); $this->assertSame( $a1, $activity ); } /** * @group delete */ public function test_delete_with_item_id() { $a1 = self::factory()->activity->create( array( 'item_id' => 523, ) ); $a2 = self::factory()->activity->create( array( 'item_id' => 1888, ) ); $activity = BP_Activity_Activity::delete( array( 'item_id' => 523, ) ); $this->assertSame( array( $a1 ), $activity ); } /** * @group delete */ public function test_delete_with_secondary_item_id() { $a1 = self::factory()->activity->create( array( 'secondary_item_id' => 523, ) ); $a2 = self::factory()->activity->create( array( 'secondary_item_id' => 1888, ) ); $activity = BP_Activity_Activity::delete( array( 'secondary_item_id' => 523, ) ); $this->assertSame( array( $a1 ), $activity ); } /** * @group get_activity_comments * * Verify the format of the activity comments array, for internal * refactoring */ public function test_get_activity_comments_format() { $now = time(); $u1 = self::factory()->user->create(); $u2 = self::factory()->user->create(); $a1 = self::factory()->activity->create( array( 'content' => 'Life Rules', 'recorded_time' => date( 'Y-m-d H:i:s', $now ), 'user_id' => $u1, ) ); $a2 = bp_activity_new_comment( array( 'activity_id' => $a1, 'content' => 'Candy is good', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 50 ), 'user_id' => $u1, ) ); $a3 = bp_activity_new_comment( array( 'activity_id' => $a1, 'content' => 'Bread is good', 'recorded_time' => date( 'Y-m-d H:i:s', $now - 25 ), 'user_id' => $u2, ) ); $keys = array( 'id', 'item_id', 'secondary_item_id', 'user_id', 'primary_link', 'component', 'type', 'action', 'content', 'date_recorded', 'hide_sitewide', 'mptt_left', 'mptt_right', 'is_spam' ); $a2_obj = new BP_Activity_Activity( $a2 ); $e2 = new stdClass; foreach ( $keys as $key ) { $e2->{$key} = $a2_obj->{$key}; } $e2_user = new WP_User( $a2_obj->user_id ); $e2->user_email = $e2_user->user_email; $e2->user_nicename = $e2_user->user_nicename; $e2->user_login = $e2_user->user_login; $e2->display_name = $e2_user->display_name; $e2->user_fullname = bp_core_get_user_displayname( $e2->user_id ); $e2->children = array(); $e2->depth = 1; $a3_obj = new BP_Activity_Activity( $a3 ); $e3 = new stdClass; foreach ( $keys as $key ) { $e3->{$key} = $a3_obj->{$key}; } $e3_user = new WP_User( $e3->user_id ); $e3->user_email = $e3_user->user_email; $e3->user_nicename = $e3_user->user_nicename; $e3->user_login = $e3_user->user_login; $e3->display_name = $e3_user->display_name; $e3->user_fullname = bp_core_get_user_displayname( $e3->user_id ); $e3->children = array(); $e3->depth = 1; $expected = array( $a2 => $e2, $a3 => $e3, ); $a1_obj = new BP_Activity_Activity( $a1 ); $comments = BP_Activity_Activity::get_activity_comments( $a1, $a1_obj->mptt_left, $a1_obj->mptt_right, 'ham_only', $a1 ); foreach ( $comments as $comment ) { $this->assertInstanceOf( 'stdClass', $comment ); } $expected_comments = array_map( 'get_object_vars', $expected ); $actual_comments = array_map( 'get_object_vars', $comments ); foreach ( $expected_comments as &$comment ) { ksort( $comment ); } unset( $comment ); foreach ( $actual_comments as &$comment ) { ksort( $comment ); } unset( $comment ); $this->assertSame( $expected_comments, $actual_comments ); } /** * @group get_last_updated */ public function test_get_last_updated() { $now = time(); $a1 = self::factory()->activity->create( array( 'recorded_time' => date( 'Y-m-d H:i:s', $now - 500 ), ) ); $a2 = self::factory()->activity->create( array( 'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ), ) ); $a3 = self::factory()->activity->create( array( 'recorded_time' => date( 'Y-m-d H:i:s', $now - 300 ), ) ); $this->assertSame( date( 'Y-m-d H:i:s', $now - 100 ), BP_Activity_Activity::get_last_updated() ); } /** * @group get_recorded_components */ public function test_get_recorded_components_skip_last_activity_false() { $a1 = self::factory()->activity->create( array( 'component' => 'members', 'action' => 'last_activity', ) ); $a2 = self::factory()->activity->create( array( 'component' => 'groups', 'action' => 'created_group', ) ); $a3 = self::factory()->activity->create( array( 'component' => 'friends', 'action' => 'friendship_accepted', ) ); $found = BP_Activity_Activity::get_recorded_components( false ); sort( $found ); $this->assertSame( array( 'friends', 'groups', 'members' ), BP_Activity_Activity::get_recorded_components( false ) ); } /** * @group get_recorded_components */ public function test_get_recorded_components_skip_last_activity_true_filter_empty_component() { $a1 = self::factory()->activity->create( array( 'component' => 'members', 'action' => 'last_activity', ) ); $a2 = self::factory()->activity->create( array( 'component' => 'groups', 'action' => 'created_group', ) ); $a3 = self::factory()->activity->create( array( 'component' => 'friends', 'action' => 'friendship_accepted', ) ); $found = BP_Activity_Activity::get_recorded_components( true ); sort( $found ); $this->assertSame( array( 'friends', 'groups' ), BP_Activity_Activity::get_recorded_components() ); } /** * @group get_recorded_components */ public function test_get_recorded_components_skip_last_activity_true_members_component_not_empty() { $a1 = self::factory()->activity->create( array( 'component' => 'members', 'action' => 'last_activity', ) ); $a2 = self::factory()->activity->create( array( 'component' => 'groups', 'action' => 'created_group', ) ); $a3 = self::factory()->activity->create( array( 'component' => 'friends', 'action' => 'friendship_accepted', ) ); $a4 = self::factory()->activity->create( array( 'component' => 'members', 'action' => 'foo', ) ); $found = BP_Activity_Activity::get_recorded_components( true ); sort( $found ); $this->assertSame( array( 'friends', 'groups', 'members' ), BP_Activity_Activity::get_recorded_components() ); } /** * @group get_recorded_components */ public function test_get_recorded_components_skip_last_activity_true_la_in_multiple_components() { $a1 = self::factory()->activity->create( array( 'component' => 'members', 'action' => 'last_activity', ) ); $a2 = self::factory()->activity->create( array( 'component' => 'groups', 'action' => 'created_group', ) ); $a3 = self::factory()->activity->create( array( 'component' => 'friends', 'action' => 'friendship_accepted', ) ); $a4 = self::factory()->activity->create( array( 'component' => 'groups', 'action' => 'last_activity', ) ); $found = BP_Activity_Activity::get_recorded_components( true ); sort( $found ); $this->assertSame( array( 'friends', 'groups', ), BP_Activity_Activity::get_recorded_components() ); } /** * @group activity_action */ public function test_instantiated_action_with_dynamic_callback() { bp_activity_set_action( 'foo', 'bar', 'Foo Bar', array( $this, 'action_cb' ) ); // Create the activity item with a manual activity string $a = self::factory()->activity->create( array( 'component' => 'foo', 'type' => 'bar', 'action' => 'baz', ) ); $a_obj = new BP_Activity_Activity( $a ); $this->assertSame( 'Woo Hoo!', $a_obj->action ); } /** * @group activity_action */ public function test_instantiated_action_without_dynamic_callback_but_with_stored_action() { // No callback is registered - this mimics a legacy plugin // Create the activity item with a manual activity string $a = self::factory()->activity->create( array( 'component' => 'foo', 'type' => 'bar1', 'action' => 'baz', ) ); $a_obj = new BP_Activity_Activity( $a ); $this->assertSame( 'baz', $a_obj->action ); } /** * @group activity_action */ public function test_instantiated_action_without_dynamic_callback_but_with_no_stored_action() { // No callback is registered - this mimics a legacy plugin // Create the activity item with a manual activity string $a = self::factory()->activity->create( array( 'component' => 'foo', 'type' => 'bar2', ) ); $a_obj = new BP_Activity_Activity( $a ); $this->assertSame( '', $a_obj->action ); } /** * @ticket BP7394 */ public function test_nonexistent_activity_should_have_id_0_after_population() { $a = self::factory()->activity->create(); bp_activity_delete( array( 'id' => $a ) ); $a_obj = new BP_Activity_Activity( $a ); $this->assertSame( 0, $a_obj->id ); } public function action_cb( $activity ) { return 'Woo Hoo!'; } /** * @ticket BP8236 */ public function test_save_activity_requires_component() { $a = bp_activity_add( array( 'component' => '', 'content' => 'Activity without component content %s', 'type' => 'activity_update', 'error_type' => 'wp_error', ) ); $this->assertWPError( $a ); $this->assertSame( 'bp_activity_missing_component', $a->get_error_code() ); } /** * @ticket BP8236 */ public function test_save_activity_requires_type() { $a = bp_activity_add( array( 'component' => 'foobar', 'content' => 'Activity without type content %s', 'type' => '', 'error_type' => 'wp_error', ) ); $this->assertWPError( $a ); $this->assertSame( 'bp_activity_missing_type', $a->get_error_code() ); } /** * @ticket BP8236 */ public function test_save_activity_requires_content() { $a = bp_activity_add( array( 'component' => buddypress()->activity->id, 'content' => '', 'type' => 'activity_update', 'error_type' => 'wp_error', ) ); $this->assertWPError( $a ); $this->assertSame( 'bp_activity_missing_content', $a->get_error_code() ); } /** * @ticket BP8236 */ public function test_save_activity_does_not_require_content() { $a = bp_activity_add( array( 'component' => buddypress()->members->id, 'content' => '', 'type' => 'new_member', 'error_type' => 'wp_error', ) ); $this->assertNotWPError( $a ); } /** * @ticket BP8236 */ public function test_save_activity_custom_type_requires_content() { add_filter( 'bp_activity_type_requires_content', '__return_true' ); $a = bp_activity_add( array( 'component' => 'foo', 'content' => '', 'type' => 'bar', 'error_type' => 'wp_error', ) ); remove_filter( 'bp_activity_type_requires_content', '__return_true' ); $this->assertWPError( $a ); $this->assertSame( 'bp_activity_missing_content', $a->get_error_code() ); } }