Skip to:
Content

BuddyPress.org

Ticket #5192: 5192.2.diff

File 5192.2.diff, 32.9 KB (added by boonebgorges, 10 years ago)
  • src/bp-members/bp-members-functions.php

    diff --git src/bp-members/bp-members-functions.php src/bp-members/bp-members-functions.php
    index fcd8307..5d40cc4 100644
    function bp_register_member_type( $member_type, $args = array() ) { 
    25232523
    25242524        $member_type = sanitize_key( $member_type );
    25252525
     2526        /**
     2527         * Filters the list of illegal member type names.
     2528         *
     2529         * - 'any' is a special pseudo-type, used to retrieve items unrestricted by member type.
     2530         * - 'null' is a special pseudo-type, representing users without any type.
     2531         * - '_none' is used internally to denote an item that should not apply to any membe types.
     2532         *
     2533         * @since BuddyPress (2.4.0)
     2534         *
     2535         * @param array $illegal_names Array of illegal names.
     2536         */
     2537        $illegal_names = apply_filters( 'bp_member_type_illegal_names', array( 'any', 'null', '_none' ));
     2538        if ( in_array( $member_type, $illegal_names, true ) ) {
     2539                return new WP_Error( 'bp_member_type_illegal_name', __( 'You may not register a member type with this name.', 'buddypress' ), $member_type );
     2540        }
     2541
    25262542        // Store the post type name as data in the object (not just as the array key).
    25272543        $r['name'] = $member_type;
    25282544
  • src/bp-xprofile/bp-xprofile-admin.php

    diff --git src/bp-xprofile/bp-xprofile-admin.php src/bp-xprofile/bp-xprofile-admin.php
    index 55c4b6d..e023836 100644
    function xprofile_admin_manage_field( $group_id, $field_id = null ) { 
    361361                                        bp_update_option( 'bp-xprofile-fullname-field-name', $field->name );
    362362                                }
    363363
     364                                // Set member types.
     365                                if ( isset( $_POST['has-member-types'] ) ) {
     366                                        $member_types = array();
     367                                        if ( isset( $_POST['member-types'] ) ) {
     368                                                $member_types = stripslashes_deep( $_POST['member-types'] );
     369                                        }
     370
     371                                        $field->set_member_types( $member_types );
     372                                }
     373
    364374                                // Validate default visibility
    365375                                if ( ! empty( $_POST['default-visibility'] ) && in_array( $_POST['default-visibility'], wp_list_pluck( bp_xprofile_get_visibility_levels(), 'id' ) ) ) {
    366376                                        bp_xprofile_update_field_meta( $field_id, 'default_visibility', $_POST['default-visibility'] );
    function xprofile_admin_field( $admin_field, $admin_group, $class = '' ) { 
    499509                                <?php if ( empty( $field->can_delete )                                    ) : ?><?php esc_html_e( '(Primary)',  'buddypress' ); endif; ?>
    500510                                <?php if ( bp_get_the_profile_field_is_required()                         ) : ?><?php esc_html_e( '(Required)', 'buddypress' ); endif; ?>
    501511                                <?php if ( bp_xprofile_get_meta( $field->id, 'field', 'signup_position' ) ) : ?><?php esc_html_e( '(Sign-up)',  'buddypress' ); endif; ?>
     512                                <?php echo $field->get_member_type_label(); ?>
    502513
    503514                                <?php
    504515
  • src/bp-xprofile/bp-xprofile-template.php

    diff --git src/bp-xprofile/bp-xprofile-template.php src/bp-xprofile/bp-xprofile-template.php
    index 298b69b..c66cda4 100644
    class BP_XProfile_Data_Template { 
    153153                $r = wp_parse_args( $args, array(
    154154                        'profile_group_id'       => false,
    155155                        'user_id'                => false,
     156                        'member_type'            => 'any',
    156157                        'hide_empty_groups'      => false,
    157158                        'hide_empty_fields'      => false,
    158159                        'fetch_fields'           => false,
    function bp_has_profile( $args = '' ) { 
    333334        // Parse arguments
    334335        $r = bp_parse_args( $args, array(
    335336                'user_id'                => bp_displayed_user_id(),
     337                'member_type'            => 'any',
    336338                'profile_group_id'       => false,
    337339                'hide_empty_groups'      => true,
    338340                'hide_empty_fields'      => $hide_empty_fields_default,
  • src/bp-xprofile/classes/class-bp-xprofile-field.php

    diff --git src/bp-xprofile/classes/class-bp-xprofile-field.php src/bp-xprofile/classes/class-bp-xprofile-field.php
    index 4d08bca..26c2e6c 100644
    class BP_XProfile_Field { 
    124124        public $data;
    125125
    126126        /**
     127         * Member types to which the profile field should be applied.
     128         *
     129         * @since BuddyPress (2.4.0)
     130         * @access protected
     131         * @var array Array of member types.
     132         */
     133        protected $member_types;
     134
     135        /**
    127136         * Initialize and/or populate profile field
    128137         *
    129138         * @since BuddyPress (1.1.0)
    class BP_XProfile_Field { 
    457466                $wpdb->query( $sql );
    458467        }
    459468
     469        /**
     470         * Get the member types to which this field should be available.
     471         *
     472         * @since BuddyPress (2.4.0)
     473         */
     474        public function get_member_types() {
     475                if ( ! is_null( $this->member_types ) ) {
     476                        return $this->member_types;
     477                }
     478
     479                $raw_types = bp_xprofile_get_meta( $this->id, 'field', 'member_type', false );
     480
     481                // If `$raw_types` is not an array, it probably means this is a new field (id=0).
     482                if ( ! is_array( $raw_types ) ) {
     483                        $raw_types = array();
     484                }
     485
     486                // If '_none' is found in the array, it overrides all types.
     487                $types = array();
     488                if ( ! in_array( '_none', $raw_types ) ) {
     489                        $registered_types = bp_get_member_types();
     490
     491                        // Eliminate invalid member types saved in the database.
     492                        foreach ( $raw_types as $raw_type ) {
     493                                // 'null' is a special case - it represents users without a type.
     494                                if ( 'null' === $raw_type || isset( $registered_types[ $raw_type ] ) ) {
     495                                        $types[] = $raw_type;
     496                                }
     497                        }
     498
     499                        // If no member types have been saved, intepret as *all* member types.
     500                        if ( empty( $types ) ) {
     501                                $types = array_values( $registered_types );
     502
     503                                // + the "null" type, ie users without a type.
     504                                $types[] = 'null';
     505                        }
     506                }
     507
     508                /**
     509                 * Filters the member types to which an XProfile object should be applied.
     510                 *
     511                 * @since BuddyPress (2.4.0)
     512                 *
     513                 * @param array             $types Member types.
     514                 * @param BP_XProfile_Field $field Field object.
     515                 */
     516                $this->member_types = apply_filters( 'bp_xprofile_field_member_types', $types, $this );
     517
     518                return $this->member_types;
     519        }
     520
     521        /**
     522         * Set the member types for this field.
     523         *
     524         * @since BuddyPress (2.4.0)
     525         *
     526         * @param array $member_types Array of member types.
     527         * @param bool  $append       Whether to append to existing member types. If false, all existing member type
     528         *                            associations will be deleted before adding your `$member_types`. Default false.
     529         * @return array Member types for the current field, after being saved.
     530         */
     531        public function set_member_types( $member_types, $append = false ) {
     532                // Unset invalid member types.
     533                $types = array();
     534                foreach ( $member_types as $member_type ) {
     535                        // 'null' is a special case - it represents users without a type.
     536                        if ( 'null' === $member_type || bp_get_member_type_object( $member_type ) ) {
     537                                $types[] = $member_type;
     538                        }
     539                }
     540
     541                // When `$append` is false, delete all existing types before adding new ones.
     542                if ( ! $append ) {
     543                        bp_xprofile_delete_meta( $this->id, 'field', 'member_type' );
     544
     545                        /*
     546                         * We interpret an empty array as disassociating the field from all types. This is
     547                         * represented internally with the '_none' flag.
     548                         */
     549                        if ( empty( $types ) ) {
     550                                bp_xprofile_add_meta( $this->id, 'field', 'member_type', '_none' );
     551                        }
     552                }
     553
     554                // "All types" is represented in the database with no meta entries.
     555                $registered_types = bp_get_member_types();
     556                $rtypes = array_values( $registered_types );
     557                $rtypes[] = 'null';
     558
     559                sort( $types );
     560                sort( $rtypes );
     561
     562                if ( $types !== $rtypes ) {
     563                        // Save new types.
     564                        foreach ( $types as $type ) {
     565                                bp_xprofile_add_meta( $this->id, 'field', 'member_type', $type );
     566                        }
     567                }
     568
     569                // Reset internal cache of member types.
     570                $this->member_types = null;
     571
     572                // Refetch fresh items from the database.
     573                return $this->get_member_types();
     574        }
     575
     576        public function get_member_type_label() {
     577                $member_types = $this->get_member_types();
     578
     579                $label = '';
     580                if ( ! empty( $member_types ) ) {
     581                        $member_type_labels = array();
     582                        foreach ( $member_types as $member_type ) {
     583                                if ( 'null' === $member_type ) {
     584                                        $member_type_labels[] = __( 'Users with no member type', 'buddypress' );
     585                                } else {
     586                                        $mt_obj = bp_get_member_type_object( $member_type );
     587                                        $member_type_labels[] = $mt_obj->labels['name'];
     588                                }
     589                        }
     590
     591                        $label = sprintf( '(Member Types: %s)', implode( ', ', array_map( 'esc_html', $member_type_labels ) ) );
     592                }
     593
     594                return $label;
     595        }
     596
    460597        /** Static Methods ********************************************************/
    461598
    462599        public static function get_type( $field_id = 0 ) {
    class BP_XProfile_Field { 
    574711        }
    575712
    576713        /**
     714         * Get the IDs of fields applicable for a given member type or array of member types.
     715         *
     716         * @todo Improved cache support.
     717         *
     718         * @since BuddyPress (2.4.0)
     719         *
     720         * @param string|array $member_types Member type or array of member types.
     721         */
     722        public static function get_fields_for_member_type( $member_types ) {
     723                global $wpdb;
     724
     725                $bp = buddypress();
     726
     727                if ( ! is_array( $member_types ) ) {
     728                        $member_types = array( $member_types );
     729                }
     730
     731                $fields = array();
     732
     733                // Pull up all recorded field member type data.
     734                $mt_meta = $wpdb->get_results( "SELECT object_id, meta_value FROM {$bp->profile->table_name_meta} WHERE meta_key = 'member_type' AND object_type = 'field'" );
     735
     736                // Keep track of all fields with recorded member_type metadata.
     737                $all_recorded_field_ids = wp_list_pluck( $mt_meta, 'object_id' );
     738
     739                // Sort member_type matches in arrays, keyed by field_id.
     740                foreach ( $mt_meta as $_mt_meta ) {
     741                        if ( ! isset( $fields[ $_mt_meta->object_id ] ) ) {
     742                                $fields[ $_mt_meta->object_id ] = array();
     743                        }
     744
     745                        $fields[ $_mt_meta->object_id ][] = $_mt_meta->meta_value;
     746                }
     747
     748                // Filter out fields that don't match any passed types, or those marked _none.
     749                foreach ( $fields as $field_id => $field_types ) {
     750                        if ( ! array_intersect( $field_types, $member_types ) || ( in_array( '_none', $field_types ) ) && ! in_array( '_none', $member_types ) ) {
     751                                unset( $fields[ $field_id ] );
     752                        }
     753                }
     754
     755                // Any fields with no member_type metadata are available to all member types.
     756                if ( ! in_array( '_none', $member_types ) ) {
     757                        if ( ! empty( $all_recorded_field_ids ) ) {
     758                                $all_recorded_field_ids_sql = implode( ',', array_map( 'absint', $all_recorded_field_ids ) );
     759                                $unrestricted_field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields} WHERE id NOT IN ({$all_recorded_field_ids_sql})" );
     760                        } else {
     761                                $unrestricted_field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields}" );
     762                        }
     763
     764                        $all_member_types   = bp_get_member_types();
     765                        $all_member_types   = array_values( $all_member_types );
     766                        $all_member_types[] = 'null';
     767
     768                        foreach ( $unrestricted_field_ids as $unrestricted_field_id ) {
     769                                $fields[ $unrestricted_field_id ] = $all_member_types;
     770                        }
     771                }
     772
     773                return $fields;
     774        }
     775
     776        /**
    577777         * Validate form field data on sumbission
    578778         *
    579779         * @since BuddyPress (2.2.0)
    class BP_XProfile_Field { 
    718918                                                        // Output the required metabox
    719919                                                        $this->required_metabox();
    720920
     921                                                        // Output the Member Types metabox.
     922                                                        $this->member_type_metabox();
     923
    721924                                                        // Output the field visibility metaboxes
    722925                                                        $this->visibility_metabox();
    723926
    class BP_XProfile_Field { 
    8641067        }
    8651068
    8661069        /**
     1070         * Private method used to output field Member Type metabox.
     1071         *
     1072         * @since BuddyPress (2.4.0)
     1073         */
     1074        private function member_type_metabox() {
     1075
     1076                // The primary field is for all, so bail.
     1077                if ( 1 === (int) $this->id ) {
     1078                        return;
     1079                }
     1080
     1081                // Bail when no member types are registered.
     1082                if ( ! $member_types = bp_get_member_types( array(), 'objects' ) ) {
     1083                        return;
     1084                }
     1085
     1086                $field_member_types = $this->get_member_types();
     1087
     1088                ?>
     1089
     1090                <div id="for_member_types" class="postbox">
     1091                        <h3><?php _e( 'Member Types', 'buddypress' ); ?></h3>
     1092                        <div class="inside">
     1093                                <p class="description"><?php _e( 'This field should be available to:', 'buddypress' ); ?></p>
     1094
     1095                                <ul>
     1096                                        <?php foreach ( $member_types as $member_type ) : ?>
     1097                                        <li>
     1098                                                <label>
     1099                                                        <input name="member-types[]" type="checkbox" value="<?php echo $member_type->name; ?>" <?php checked( in_array( $member_type->name, $field_member_types ) ); ?>/>
     1100                                                        <?php echo $member_type->labels['name']; ?>
     1101                                                </label>
     1102                                        </li>
     1103                                        <?php endforeach; ?>
     1104
     1105                                        <li>
     1106                                                <label>
     1107                                                        <input name="member-types[]" type="checkbox" value="null" <?php checked( in_array( 'null', $field_member_types ) ); ?>/>
     1108                                                        <?php _e( 'Users with no member type', 'buddypress' ); ?>
     1109                                                </label>
     1110                                        </li>
     1111
     1112                                </ul>
     1113                        </div>
     1114
     1115                        <input type="hidden" name="has-member-types" value="1" />
     1116                </div>
     1117
     1118                <?php
     1119        }
     1120
     1121        /**
    8671122         * Private method used to output field visibility metaboxes
    8681123         *
    8691124         * @since BuddyPress (2.3.0)
  • src/bp-xprofile/classes/class-bp-xprofile-group.php

    diff --git src/bp-xprofile/classes/class-bp-xprofile-group.php src/bp-xprofile/classes/class-bp-xprofile-group.php
    index 7d85a14..baefc35 100644
    class BP_XProfile_Group { 
    232232         *      @type int   $profile_group_id  Limit results to a single profile group.
    233233         *      @type int   $user_id           Required if you want to load a specific user's data.
    234234         *                                     Default: displayed user's ID.
     235         *      @type array|string $member_type Limit fields by those restricted to a given member type, or array of
     236         *                                      member types. If `$user_id` is provided, the value of `$member_type`
     237         *                                      will be overridden by the member types of the provided user. The
     238         *                                      special value of 'any' will return only those fields that are
     239         *                                      unrestricted by member type - i.e., those applicable to any type.
    235240         *      @type bool  $hide_empty_groups True to hide groups that don't have any fields. Default: false.
    236241         *      @type bool  $hide_empty_fields True to hide fields where the user has not provided data. Default: false.
    237242         *      @type bool  $fetch_fields      Whether to fetch each group's fields. Default: false.
    class BP_XProfile_Group { 
    251256                $r = wp_parse_args( $args, array(
    252257                        'profile_group_id'       => false,
    253258                        'user_id'                => bp_displayed_user_id(),
     259                        'member_type'            => false,
    254260                        'hide_empty_groups'      => false,
    255261                        'hide_empty_fields'      => false,
    256262                        'fetch_fields'           => false,
    class BP_XProfile_Group { 
    318324                $exclude_fields_cs  = array_merge( $exclude_fields_cs, $hidden_user_fields );
    319325                $exclude_fields_cs  = implode( ',', $exclude_fields_cs );
    320326
    321                 // Setup IN query for field IDs
     327                // Set up NOT IN query for excluded field IDs.
    322328                if ( ! empty( $exclude_fields_cs ) ) {
    323329                        $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})";
    324330                } else {
    325331                        $exclude_fields_sql = '';
    326332                }
    327333
     334                // Set up IN query for included field IDs.
     335                $include_field_ids = array();
     336
     337                // Member-type restrictions.
     338                if ( $r['user_id'] || false !== $r['member_type'] ) {
     339                        $member_types = $r['member_type'];
     340                        if ( $r['user_id'] ) {
     341                                $member_types = bp_get_member_type( $r['user_id'], false );
     342                                if ( empty( $member_types ) ) {
     343                                        $member_types = array( 'null' );
     344                                }
     345                        }
     346
     347                        $member_types_fields = BP_XProfile_Field::get_fields_for_member_type( $member_types );
     348                        $include_field_ids += array_keys( $member_types_fields );
     349                }
     350
     351                $in_sql = '';
     352                if ( ! empty( $include_field_ids ) ) {
     353                        $include_field_ids_cs = implode( ',', array_map( 'intval', $include_field_ids ) );
     354                        $in_sql = " AND id IN ({$include_field_ids_cs}) ";
     355                }
     356
    328357                // Fetch the fields
    329                 $fields    = $wpdb->get_results( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} ORDER BY field_order" );
     358                $fields = $wpdb->get_results( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order" );
     359
    330360                $field_ids = wp_list_pluck( $fields, 'id' );
    331361
    332362                // Store field IDs for meta cache priming
  • tests/phpunit/testcases/members/types.php

    diff --git tests/phpunit/testcases/members/types.php tests/phpunit/testcases/members/types.php
    index 249049b..91dccd0 100644
    class BP_Tests_Members_Types extends BP_UnitTestCase { 
    2121        }
    2222
    2323        /**
     24         * @dataProvider illegal_names
     25         * @ticket BP5192
     26         */
     27        public function test_illegal_names( $name ) {
     28                $this->assertWPError( bp_register_member_type( $name ) );
     29        }
     30
     31        public function illegal_names() {
     32                return array(
     33                        array( 'any' ),
     34                        array( 'null' ),
     35                        array( '_none' ),
     36                );
     37        }
     38
     39        /**
    2440         * @ticket BP6139
    2541         */
    2642        public function test_bp_register_member_type_should_sanitize_member_type_key() {
  • new file tests/phpunit/testcases/xprofile/BP_XProfile_Field/member_types.php

    diff --git tests/phpunit/testcases/xprofile/BP_XProfile_Field/member_types.php tests/phpunit/testcases/xprofile/BP_XProfile_Field/member_types.php
    new file mode 100644
    index 0000000..2b1559b
    - +  
     1<?php
     2
     3/**
     4 * @group member_types
     5 * @group xprofile
     6 * @ticket BP5192
     7 */
     8class BP_Tests_XProfile_BpXprofileField_MemberTypes extends BP_UnitTestCase {
     9        protected $field_group_id;
     10        protected $field_id;
     11        protected $field;
     12
     13        public function setUp() {
     14                parent::setUp();
     15                bp_register_member_type( 'foo' );
     16                bp_register_member_type( 'bar' );
     17
     18                $this->field_group_id = $this->factory->xprofile_group->create();
     19                $this->field_id = $this->factory->xprofile_field->create( array( 'field_group_id' => $this->field_group_id ) );
     20                $this->field = new BP_XProfile_Field( $this->field_id );
     21        }
     22
     23        public function tearDown() {
     24                buddypress()->members->types = array();
     25                parent::tearDown();
     26        }
     27
     28        public function test_get_single_member_type() {
     29                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' );
     30                $this->assertEqualSets( array( 'foo' ), $this->field->get_member_types() );
     31        }
     32
     33        public function test_get_multiple_member_types() {
     34                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' );
     35                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'bar' );
     36                $this->assertEqualSets( array( 'foo', 'bar' ), $this->field->get_member_types() );
     37        }
     38
     39        public function test_invalid_member_types_should_not_be_returned() {
     40                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' );
     41                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'phony' );
     42                $this->assertEquals( array( 'foo' ), $this->field->get_member_types() );
     43        }
     44
     45        public function test_when_no_stored_types_are_found_all_registered_member_types_as_well_as_null_type_should_be_returned() {
     46                $this->assertEqualSets( array( 'null', 'foo', 'bar' ), $this->field->get_member_types() );
     47        }
     48
     49        public function test__none_meta_should_result_in_empty_array() {
     50                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', '_none' );
     51                $this->assertEquals( array(), $this->field->get_member_types() );
     52        }
     53
     54        public function test__none_meta_should_override_other_values() {
     55                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', '_none' );
     56                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' );
     57                $this->assertEquals( array(), $this->field->get_member_types() );
     58        }
     59
     60        public function test_set_should_not_append_by_default() {
     61                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' );
     62                $this->assertEquals( array( 'bar' ), $this->field->set_member_types( array( 'bar' ) ) );
     63        }
     64
     65        public function test_set_should_not_append_when_append_is_set_to_false() {
     66                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' );
     67                $this->assertEquals( array( 'bar' ), $this->field->set_member_types( array( 'bar', false ) ) );
     68        }
     69
     70        public function test_set_should_append_when_append_is_set_to_true() {
     71                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' );
     72                $this->assertEqualSets( array( 'foo', 'bar' ), $this->field->set_member_types( array( 'bar' ), true ) );
     73        }
     74
     75        public function test_set_empty_array_with_append_true_should_have_no_effect_on_saved_types() {
     76                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' );
     77                $this->assertEqualSets( array( 'foo' ), $this->field->set_member_types( array(), true ) );
     78        }
     79
     80        public function test_set_empty_array_with_append_false_should_result_in_field_being_associated_with_no_member_types() {
     81                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' );
     82                $this->assertEqualSets( array(), $this->field->set_member_types( array() ) );
     83        }
     84
     85        public function test_set_should_interpret_null_flag_properly() {
     86                $this->assertEqualSets( array( 'null' ), $this->field->set_member_types( array( 'null' ) ) );
     87        }
     88
     89        public function test_set_all_types_plus_null_should_result_in_nothing_stored_in_db() {
     90                $types = array( 'null', 'foo', 'bar' );
     91                $this->assertEqualSets( $types, $this->field->set_member_types( $types ) );
     92
     93                $types_db = bp_xprofile_get_meta( $this->field_id, 'field', 'member_type', false );
     94                $this->assertEqualSets( array(), $types_db );
     95        }
     96
     97        public function test_get_fields_for_member_type_should_get_field_with_explicit_member_type() {
     98                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' );
     99                $found = BP_XProfile_Field::get_fields_for_member_type( 'foo' );
     100                $this->assertEqualSets( array( 1, $this->field_id ), array_keys( $found ) );
     101        }
     102
     103        public function test_get_fields_for_member_type_should_ignore_field_that_applies_to_no_member_types() {
     104                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', '_none' );
     105                $found = BP_XProfile_Field::get_fields_for_member_type( 'foo' );
     106                $this->assertEqualSets( array( 1 ), array_keys( $found ) );
     107        }
     108
     109        public function test_get_fields_for_member_type_should_ignore_field_that_applies_to_different_member_types() {
     110                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'bar' );
     111                $found = BP_XProfile_Field::get_fields_for_member_type( 'foo' );
     112                $this->assertEqualSets( array( 1 ), array_keys( $found ) );
     113        }
     114
     115        public function test_get_fields_for_member_type_should_include_fields_with_no_member_type_restrictions() {
     116                // A field with no member_type metadata applies to all member types.
     117                $found = BP_XProfile_Field::get_fields_for_member_type( 'foo' );
     118                $this->assertEqualSets( array( 1, $this->field_id ), array_keys( $found ) );
     119        }
     120
     121        public function test_passing_member_type_any_to_get_fields_for_member_type_should_return_unrestricted_fields() {
     122                $found = BP_XProfile_Field::get_fields_for_member_type( '' );
     123                $this->assertEqualSets( array( 1, $this->field_id ), array_keys( $found ) );
     124        }
     125
     126        public function test_passing_empty_member_type_to_get_fields_for_member_type_should_return_unrestricted_fields() {
     127                $found = BP_XProfile_Field::get_fields_for_member_type( 'any' );
     128                $this->assertEqualSets( array( 1, $this->field_id ), array_keys( $found ) );
     129        }
     130
     131        public function test_passing_member_type_none_to_get_fields_for_member_type_should_return_fields_unavailable_to_all_member_types() {
     132                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', '_none' );
     133                $found = BP_XProfile_Field::get_fields_for_member_type( '_none' );
     134                $this->assertEqualSets( array( $this->field_id ), array_keys( $found ) );
     135        }
     136
     137        public function test_get_fields_for_member_type_should_accept_an_array_of_member_types() {
     138                $f2 = $this->factory->xprofile_field->create( array( 'field_group_id' => $this->field_group_id ) );
     139                bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' );
     140                bp_xprofile_add_meta( $f2, 'field', 'member_type', 'bar' );
     141
     142                $found = BP_XProfile_Field::get_fields_for_member_type( array( 'foo', 'bar' ) );
     143                $this->assertEqualSets( array( 1, $this->field_id, $f2 ), array_keys( $found ) );
     144        }
     145}
  • tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php

    diff --git tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php
    index 6a0492b..7fddb47 100644
    class BP_Tests_BP_XProfile_Group extends BP_UnitTestCase { 
    113113        }
    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( $fg->fields, 'id' ) );
     214                $this->assertContains( $f3, wp_list_pluck( $fg->fields, 'id' ) );
     215                $this->assertNotContains( $f2, wp_list_pluck( $fg->fields, 'id' ) );
     216                $this->assertNotContains( $f4, wp_list_pluck( $fg->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( $fg->fields, 'id' ) );
     257                $this->assertContains( $f2, wp_list_pluck( $fg->fields, 'id' ) );
     258                $this->assertContains( $f3, wp_list_pluck( $fg->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( $fg->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( $fg->fields, 'id' ) );
     325                $this->assertContains( $f2, wp_list_pluck( $fg->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( $fg->fields, 'id' ) );
     357                $this->assertContains( $f2, wp_list_pluck( $fg->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( $fg->fields, 'id' ) );
     387                $this->assertContains( $f2, wp_list_pluck( $fg->fields, 'id' ) );
     388        }
     389
     390        /**
    116391         * @group save_xprofile_group_name
    117392         */
    118393        public function test_save_xprofile_group_name() {