Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/21/2015 02:00:38 PM (11 years ago)
Author:
boonebgorges
Message:

Allow xProfile fields to be restricted to users belonging to one or more member types.

A new metabox on the profile field edit panel allows the administrator to
select the member types to which the field is applicable. Admins can also
choose to have a field apply to users who do not belong to any member type.
Information about a field's member type associations is displayed on the
Users > Profile Fields page, alongside each field name.

During registration, the only fields that are displayed are those that are
unrestricted - that is, those available to all users, regardless of member type
(or lack thereof).

This changeset introduces a number of new methods on BP_XProfile_Field that
developers should use to manipulate member type information:
get_member_types(), set_member_types(), and the static
get_fields_for_member_type(). In addition to member types that have been
explicitly registered, 'null' is a pseudo-type representing users who do not
belong to a member type.

This changeset introduces a blacklist of illegal member type names. By default,
the blacklist includes 'any', 'null', and '_none'. Use the
'bp_member_type_illegal_names' filter to add names to the blacklist.

Props Offereins, boonebgorges, tanner m, imath.
See #5192.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php

    r10004 r10022  
    114114
    115115    /**
     116     * @group member_types
     117     * @ticket BP5192
     118     */
     119    public function test_member_type_restrictions_should_be_ignored_when_user_id_is_null_and_member_type_is_not_explicitly_provided() {
     120        $g = $this->factory->xprofile_group->create();
     121        $f = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     122        bp_register_member_type( 'foo' );
     123
     124        $field = new BP_XProfile_Field( $f );
     125        $field->set_member_types( array( 'foo' ) );
     126
     127        $found_groups = BP_XProfile_Group::get( array(
     128            'user_id' => false,
     129            'fetch_fields' => true,
     130        ) );
     131
     132        // The groups aren't indexed, so we have to go looking for it.
     133        foreach ( $found_groups as $fg ) {
     134            if ( $g == $fg->id ) {
     135                $the_group = $fg;
     136            }
     137        }
     138
     139        $this->assertContains( $f, wp_list_pluck( $the_group->fields, 'id' ) );
     140    }
     141
     142    /**
     143     * @group member_types
     144     * @ticket BP5192
     145     */
     146    public function test_member_type_restrictions_should_be_ignored_when_user_id_is_0_and_member_type_is_false() {
     147        $g = $this->factory->xprofile_group->create();
     148        $f = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     149        bp_register_member_type( 'foo' );
     150
     151        $field = new BP_XProfile_Field( $f );
     152        $field->set_member_types( array( 'foo' ) );
     153
     154        $found_groups = BP_XProfile_Group::get( array(
     155            'user_id' => 0,
     156            'member_type' => false,
     157            'fetch_fields' => true,
     158        ) );
     159
     160        // The groups aren't indexed, so we have to go looking for it.
     161        foreach ( $found_groups as $fg ) {
     162            if ( $g == $fg->id ) {
     163                $the_group = $fg;
     164            }
     165        }
     166
     167        $this->assertContains( $f, wp_list_pluck( $the_group->fields, 'id' ) );
     168    }
     169
     170    /**
     171     * @group member_types
     172     * @ticket BP5192
     173     */
     174    public function test_member_type_restrictions_should_be_obeyed_for_nonzero_user_id() {
     175        $g = $this->factory->xprofile_group->create();
     176        $f1 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     177        $f2 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     178        $f3 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     179        $f4 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     180        bp_register_member_type( 'foo' );
     181        bp_register_member_type( 'bar' );
     182
     183        // Field 1 is visible only to 'foo' users.
     184        $field1 = new BP_XProfile_Field( $f1 );
     185        $field1->set_member_types( array( 'foo' ) );
     186
     187        // Field 2 is visible only to 'bar' users.
     188        $field2 = new BP_XProfile_Field( $f2 );
     189        $field2->set_member_types( array( 'bar' ) );
     190
     191        // Field 3 is visible to all users (no member type set).
     192
     193        // Field 4 is visible to no one.
     194        $field4 = new BP_XProfile_Field( $f4 );
     195        $field4->set_member_types( array() );
     196
     197        // User is in 'foo', so should have f1 and f3 only.
     198        $u = $this->factory->user->create();
     199        bp_set_member_type( $u, 'foo' );
     200
     201        $found_groups = BP_XProfile_Group::get( array(
     202            'user_id' => $u,
     203            'fetch_fields' => true,
     204        ) );
     205
     206        // The groups aren't indexed, so we have to go looking for it.
     207        foreach ( $found_groups as $fg ) {
     208            if ( $g == $fg->id ) {
     209                $the_group = $fg;
     210            }
     211        }
     212
     213        $this->assertContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) );
     214        $this->assertContains( $f3, wp_list_pluck( $the_group->fields, 'id' ) );
     215        $this->assertNotContains( $f2, wp_list_pluck( $the_group->fields, 'id' ) );
     216        $this->assertNotContains( $f4, wp_list_pluck( $the_group->fields, 'id' ) );
     217    }
     218
     219    /**
     220     * @group member_types
     221     * @ticket BP5192
     222     */
     223    public function test_member_type_restrictions_should_be_obeyed_for_nonzero_user_id_with_no_member_types() {
     224        $g = $this->factory->xprofile_group->create();
     225        $f1 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     226        $f2 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     227        $f3 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     228        bp_register_member_type( 'foo' );
     229        bp_register_member_type( 'bar' );
     230
     231        // Field 1 is visible only to 'foo' users.
     232        $field1 = new BP_XProfile_Field( $f1 );
     233        $field1->set_member_types( array( 'foo' ) );
     234
     235        // Field 2 is visible only to 'null' users.
     236        $field2 = new BP_XProfile_Field( $f2 );
     237        $field2->set_member_types( array( 'null' ) );
     238
     239        // Field 3 is visible to all users (no member type set).
     240
     241        // User has no member types, so should see f2 and f3 .
     242        $u = $this->factory->user->create();
     243
     244        $found_groups = BP_XProfile_Group::get( array(
     245            'user_id' => $u,
     246            'fetch_fields' => true,
     247        ) );
     248
     249        // The groups aren't indexed, so we have to go looking for it.
     250        foreach ( $found_groups as $fg ) {
     251            if ( $g == $fg->id ) {
     252                $the_group = $fg;
     253            }
     254        }
     255
     256        $this->assertNotContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) );
     257        $this->assertContains( $f2, wp_list_pluck( $the_group->fields, 'id' ) );
     258        $this->assertContains( $f3, wp_list_pluck( $the_group->fields, 'id' ) );
     259    }
     260
     261    /**
     262     * @group member_types
     263     * @ticket BP5192
     264     */
     265    public function test_member_types_of_provided_user_id_should_take_precedence_over_provided_member_type() {
     266        $g = $this->factory->xprofile_group->create();
     267        $f1 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     268        $f2 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     269        bp_register_member_type( 'foo' );
     270        bp_register_member_type( 'bar' );
     271
     272        $field1 = new BP_XProfile_Field( $f1 );
     273        $field1->set_member_types( array( 'foo' ) );
     274        $field2 = new BP_XProfile_Field( $f2 );
     275        $field2->set_member_types( array( 'bar' ) );
     276
     277        $u = $this->factory->user->create();
     278        bp_set_member_type( $u, 'foo' );
     279
     280        $found_groups = BP_XProfile_Group::get( array(
     281            'user_id' => $u,
     282            'member_type' => 'bar',
     283            'fetch_fields' => true,
     284        ) );
     285
     286        // The groups aren't indexed, so we have to go looking for it.
     287        foreach ( $found_groups as $fg ) {
     288            if ( $g == $fg->id ) {
     289                $the_group = $fg;
     290            }
     291        }
     292
     293        $this->assertContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) );
     294    }
     295
     296    /**
     297     * @group member_types
     298     * @ticket BP5192
     299     */
     300    public function test_member_type_single_value_should_be_respected() {
     301        $g = $this->factory->xprofile_group->create();
     302        $f1 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     303        $f2 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     304        bp_register_member_type( 'foo' );
     305        bp_register_member_type( 'bar' );
     306
     307        $field1 = new BP_XProfile_Field( $f1 );
     308        $field1->set_member_types( array( 'foo' ) );
     309        $field2 = new BP_XProfile_Field( $f2 );
     310        $field2->set_member_types( array( 'bar' ) );
     311
     312        $found_groups = BP_XProfile_Group::get( array(
     313            'member_type' => 'bar',
     314            'fetch_fields' => true,
     315        ) );
     316
     317        // The groups aren't indexed, so we have to go looking for it.
     318        foreach ( $found_groups as $fg ) {
     319            if ( $g == $fg->id ) {
     320                $the_group = $fg;
     321            }
     322        }
     323
     324        $this->assertNotContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) );
     325        $this->assertContains( $f2, wp_list_pluck( $the_group->fields, 'id' ) );
     326    }
     327
     328    /**
     329     * @group member_types
     330     * @ticket BP5192
     331     */
     332    public function test_member_type_array_value_should_be_respected() {
     333        $g = $this->factory->xprofile_group->create();
     334        $f1 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     335        $f2 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     336        bp_register_member_type( 'foo' );
     337        bp_register_member_type( 'bar' );
     338
     339        $field1 = new BP_XProfile_Field( $f1 );
     340        $field1->set_member_types( array( 'foo' ) );
     341        $field2 = new BP_XProfile_Field( $f2 );
     342        $field2->set_member_types( array( 'bar' ) );
     343
     344        $found_groups = BP_XProfile_Group::get( array(
     345            'member_type' => array( 'bar' ),
     346            'fetch_fields' => true,
     347        ) );
     348
     349        // The groups aren't indexed, so we have to go looking for it.
     350        foreach ( $found_groups as $fg ) {
     351            if ( $g == $fg->id ) {
     352                $the_group = $fg;
     353            }
     354        }
     355
     356        $this->assertNotContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) );
     357        $this->assertContains( $f2, wp_list_pluck( $the_group->fields, 'id' ) );
     358    }
     359
     360    /**
     361     * @group member_types
     362     * @ticket BP5192
     363     */
     364    public function test_member_type_null_should_be_respected() {
     365        $g = $this->factory->xprofile_group->create();
     366        $f1 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     367        $f2 = $this->factory->xprofile_field->create( array( 'field_group_id' => $g ) );
     368        bp_register_member_type( 'foo' );
     369        bp_register_member_type( 'bar' );
     370
     371        $field1 = new BP_XProfile_Field( $f1 );
     372        $field1->set_member_types( array( 'foo' ) );
     373
     374        $found_groups = BP_XProfile_Group::get( array(
     375            'member_type' => array( 'null' ),
     376            'fetch_fields' => true,
     377        ) );
     378
     379        // The groups aren't indexed, so we have to go looking for it.
     380        foreach ( $found_groups as $fg ) {
     381            if ( $g == $fg->id ) {
     382                $the_group = $fg;
     383            }
     384        }
     385
     386        $this->assertNotContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) );
     387        $this->assertContains( $f2, wp_list_pluck( $the_group->fields, 'id' ) );
     388    }
     389
     390    /**
    116391     * @group save_xprofile_group_name
    117392     */
Note: See TracChangeset for help on using the changeset viewer.