Skip to:
Content

BuddyPress.org

Ticket #5572: 5572.patch

File 5572.patch, 3.1 KB (added by boonebgorges, 11 years ago)
  • bp-members/bp-members-functions.php

    diff --git bp-members/bp-members-functions.php bp-members/bp-members-functions.php
    index 6dbe403..4f8df49 100644
    function bp_last_activity_migrate() { 
    10081008
    10091009        $bp = buddypress();
    10101010
    1011         // The "NOT IN" clause prevents duplicates
     1011        // Wipe out existing last_activity data in the activity table -
     1012        // this helps to prevent duplicates when pulling from the usermeta
     1013        // table
     1014        $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->members->table_name_last_activity} WHERE component = %s AND action = 'last_activity'", $bp->members->id ) );
     1015
    10121016        $sql = "INSERT INTO {$bp->members->table_name_last_activity} (`user_id`, `component`, `type`, `action`, `content`, `primary_link`, `item_id`, `date_recorded` ) (
    10131017                  SELECT user_id, '{$bp->members->id}' as component, 'last_activity' as type, '' as action, '' as content, '' as primary_link, 0 as item_id, meta_value AS date_recorded
    10141018                  FROM {$wpdb->usermeta}
    10151019                  WHERE
    10161020                    meta_key = 'last_activity'
    1017                     AND
    1018                     user_id NOT IN (
    1019                       SELECT user_id
    1020                       FROM {$bp->members->table_name_last_activity}
    1021                       WHERE component = '{$bp->members->id}' AND type = 'last_activity'
    1022                     )
    10231021        );";
    10241022
    10251023        return $wpdb->query( $sql );
  • tests/testcases/members/functions.php

    diff --git tests/testcases/members/functions.php tests/testcases/members/functions.php
    index 4444ccc..f7b6af9 100644
    class BP_Tests_Members_Functions extends BP_UnitTestCase { 
    279279
    280280                $this->assertSame( $expected, bp_core_get_user_displaynames( array( $u1, $u2, ) ) );
    281281        }
     282
     283        /**
     284         * @group bp_last_activity_migrate
     285         * @expectedIncorrectUsage update_user_meta( $user_id, 'last_activity' )
     286         * @expectedIncorrectUsage get_user_meta( $user_id, 'last_activity' )
     287         */
     288        public function test_bp_last_activity_migrate() {
     289                // We explicitly do not want last_activity created, so use the
     290                // WP factory methods
     291                $u1 = $this->factory->user->create();
     292                $u2 = $this->factory->user->create();
     293                $u3 = $this->factory->user->create();
     294
     295                $time = time();
     296                $t1 = date( 'Y-m-d H:i:s', $time - 50 );
     297                $t2 = date( 'Y-m-d H:i:s', $time - 500 );
     298                $t3 = date( 'Y-m-d H:i:s', $time - 5000 );
     299
     300                update_user_meta( $u1, 'last_activity', $t1 );
     301                update_user_meta( $u2, 'last_activity', $t2 );
     302                update_user_meta( $u3, 'last_activity', $t3 );
     303
     304                // Create an existing entry in last_activity to test no dupes
     305                global $wpdb, $bp;
     306                $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->members->table_name_last_activity} (`user_id`, `component`, `type`, `action`, `content`, `primary_link`, `item_id`, `date_recorded` ) VALUES ( %d, %s, %s, %s, %s, %s, %d, %s )", $u2, $bp->members->id, 'last_activity', '', '', 0, $t1 ) );
     307
     308                bp_last_activity_migrate();
     309
     310                $expected = array(
     311                        $u1 => $t1,
     312                        $u2 => $t2,
     313                        $u3 => $t3,
     314                );
     315
     316                $found = array(
     317                        $u1 => '',
     318                        $u2 => '',
     319                        $u3 => '',
     320                );
     321
     322                foreach ( $found as $uid => $v ) {
     323                        $found[ $uid ] = bp_get_user_last_activity( $uid );
     324                }
     325
     326                $this->assertSame( $expected, $found );
     327        }
    282328}