Skip to:
Content

BuddyPress.org

Changeset 9611


Ignore:
Timestamp:
03/10/2015 03:43:27 PM (5 years ago)
Author:
boonebgorges
Message:

Introduce bp_has_member_type().

This function allows developers to test whether a given user has a given member
type. Thus the clever function name.

Props Offereins, Mamaduka.
Fixes #6138.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/bp-members-functions.php

    r9589 r9611  
    26822682
    26832683/**
     2684 * Check whether the given user has a certain member type.
     2685 *
     2686 * @since BuddyPress (2.3.0)
     2687 *
     2688 * @param  int    $user_id     $user_id ID of the user.
     2689 * @param  string $member_type Member Type.
     2690 * @return bool Whether the user has the given member type.
     2691 */
     2692function bp_has_member_type( $user_id, $member_type ) {
     2693    // Bail if no valid member type was passed.
     2694    if ( empty( $member_type ) || ! bp_get_member_type_object( $member_type ) ) {
     2695        return false;
     2696    }
     2697
     2698    // Get all user's member types.
     2699    $types = bp_get_member_type( $user_id, false );
     2700
     2701    if ( ! is_array( $types ) ) {
     2702        return false;
     2703    }
     2704
     2705    return in_array( $member_type, $types );
     2706}
     2707
     2708/**
    26842709 * Delete a user's member type when the user when the user is deleted.
    26852710 *
  • trunk/tests/phpunit/testcases/members/types.php

    r9610 r9611  
    279279        $this->assertEquals( array( 'bar' ), $types );
    280280    }
     281
     282    /**
     283     * @group BP6138
     284     */
     285    function test_bp_has_member_type_should_return_false_when_member_type_is_empty() {
     286        $this->assertFalse( bp_has_member_type( 5, '' ) );
     287    }
     288
     289    /**
     290     * @group BP6138
     291     */
     292    function test_bp_has_member_type_should_return_false_when_member_type_is_invalid() {
     293        $this->assertFalse( bp_has_member_type( 5, 'foo' ) );
     294    }
     295
     296    /**
     297     * @group BP6138
     298     */
     299    public function test_bp_has_member_type_should_return_false_when_member_id_is_empty() {
     300        bp_register_member_type( 'foo' );
     301
     302        $this->assertFalse( bp_has_member_type( '', 'foo' ) );
     303    }
     304
     305    /**
     306     * @group BP6138
     307     */
     308    public function test_bp_has_member_type_should_return_false_when_member_is_not_of_provided_type() {
     309        $u1 = $this->factory->user->create();
     310        bp_register_member_type( 'foo' );
     311        bp_register_member_type( 'bar' );
     312        bp_set_member_type( $u1, 'bar' );
     313
     314        $this->assertFalse( bp_has_member_type( $u1, 'foo' ) );
     315    }
     316
     317    /**
     318     * @group BP6138
     319     */
     320    public function test_bp_has_member_type_should_return_true_on_success() {
     321        $u1 = $this->factory->user->create();
     322        bp_register_member_type( 'foo' );
     323        bp_register_member_type( 'bar' );
     324        bp_set_member_type( $u1, 'foo' );
     325        bp_set_member_type( $u1, 'bar', true );
     326
     327        $this->assertTrue( bp_has_member_type( $u1, 'foo' ) );
     328        $types = bp_get_member_type( $u1, false );
     329        $this->assertEqualSets( array( 'bar', 'foo' ), $types );
     330    }
    281331}
Note: See TracChangeset for help on using the changeset viewer.