Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/14/2014 02:01:18 PM (10 years ago)
Author:
boonebgorges
Message:

In automated tests, move user creation to a proper factory method.

BP user creation requires that a couple of extra pieces of data be set up
(last activity, display name, etc). So we previously had a wrapper in
BP_UnitTestCase called create_user() that performed the extra setup.
However, the wrapper made it impossible to use create_user() statically,
because the user_login and user_email iterator was not persistent from call to
call. (The create_user() syntax is also a break with the rest of our unit
tests, which is not ideal.)

This changeset introduces BP_UnitTest_Factory_For_User, which reproduces the
customizations of create_user(), but in a proper factory method. All
instances of create_user() throughout the test suite have also been replaced.

See #6009.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/factory.php

    r9120 r9139  
    66        parent::__construct();
    77
     8        $this->user = new BP_UnitTest_Factory_For_User( $this );
    89        $this->activity = new BP_UnitTest_Factory_For_Activity( $this );
    910        $this->group = new BP_UnitTest_Factory_For_Group( $this );
     
    1617}
    1718
     19class BP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_User {
     20    /**
     21     * When creating a new user, it's almost always necessary to have the
     22     * last_activity usermeta set right away, so that the user shows up in
     23     * directory queries. This is a shorthand wrapper for the user factory
     24     * create() method.
     25     *
     26     * Also set a display name
     27     */
     28    public function create_object( $args ) {
     29        $r = wp_parse_args( $args, array(
     30            'role' => 'subscriber',
     31            'last_activity' => date( 'Y-m-d H:i:s', strtotime( bp_core_current_time() ) - 60*60*24*365 ),
     32        ) );
     33
     34        $last_activity = $r['last_activity'];
     35        unset( $r['last_activity'] );
     36
     37        $user_id = wp_insert_user( $r );
     38
     39        bp_update_user_last_activity( $user_id, $last_activity );
     40
     41        if ( bp_is_active( 'xprofile' ) ) {
     42            $user = new WP_User( $user_id );
     43            xprofile_set_field_data( 1, $user_id, $user->display_name );
     44        }
     45
     46        return $user_id;
     47    }
     48}
     49
    1850class BP_UnitTest_Factory_For_Activity extends WP_UnitTest_Factory_For_Thing {
    1951
Note: See TracChangeset for help on using the changeset viewer.