Skip to:
Content

BuddyPress.org

Changeset 10592


Ignore:
Timestamp:
02/14/2016 01:48:59 PM (9 years ago)
Author:
dcavins
Message:

Profile: Improve validation in is_valid().

When checking a submitted array of values,
ensure that the foreach breaks when an
invalid value is encountered. Previously,
the foreach returned the validity of the
last element in the array.

Also update unit tests so that invalid values
appear in various positions in the array of
values to check.

Fixes #6884.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type.php

    r10434 r10592  
    207207        }
    208208
    209         // If there's a whitelist set, also check the $value.
     209        // If there's a whitelist set, make sure that each value is a whitelisted value.
    210210        if ( ( true === $validated ) && ! empty( $values ) && ! empty( $this->validation_whitelist ) ) {
    211211            foreach ( (array) $values as $value ) {
    212                 $validated = in_array( $value, $this->validation_whitelist, true );
     212                if ( ! in_array( $value, $this->validation_whitelist, true ) ) {
     213                    $validated = false;
     214                    break;
     215                }
    213216            }
    214217        }
  • trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php

    r9819 r10592  
    3939        $this->assertTrue( $field->is_valid( array( 'cheese', 'pepporoni' ) ) );
    4040        $this->assertTrue( $field->is_valid( array( 'cheese' ) ) );
    41         $this->assertFalse( $field->is_valid( array( 'cheese', 'pepporoni', 'pinapple' ) ) );
     41        $this->assertFalse( $field->is_valid( array( 'cheese', 'pinapple', 'pepporoni' ) ) );
    4242        $this->assertFalse( $field->is_valid( array( 'pinapple' ) ) );
    4343    }
     
    105105        $this->assertTrue( $field->is_valid( array( 123 ) ) );
    106106        $this->assertTrue( $field->is_valid( array( 456 ) ) );
    107         $this->assertFalse( $field->is_valid( array( 123, 456, 789 ) ) );
     107        $this->assertFalse( $field->is_valid( array( 789, 456, 123 ) ) );
    108108        $this->assertFalse( $field->is_valid( array( 789 ) ) );
    109109    }
     
    115115        $this->assertTrue( $field->is_valid( array( 'cheese', 'pepporoni' ) ) );
    116116        $this->assertTrue( $field->is_valid( array( 'cheese' ) ) );
    117         $this->assertFalse( $field->is_valid( array( 'cheese', 'pepporoni', 'pinapple' ) ) );
     117        $this->assertFalse( $field->is_valid( array(  'pinapple', 'cheese', 'pepporoni' ) ) );
    118118        $this->assertFalse( $field->is_valid( array( 'pinapple' ) ) );
    119119        $this->assertFalse( $field->is_valid( '' ) );
     
    133133        $this->assertTrue( $field->is_valid( array( 'cheese', 'pepporoni' ) ) );
    134134        $this->assertTrue( $field->is_valid( array( 'cheese' ) ) );
    135         $this->assertFalse( $field->is_valid( array( 'cheese', 'pepporoni', 'pinapple' ) ) );
     135        $this->assertFalse( $field->is_valid( array( 'pepporoni', 'cheese', 'pinapple' ) ) );
    136136        $this->assertFalse( $field->is_valid( array( 'pinapple' ) ) );
    137137        $this->assertFalse( $field->is_valid( '' ) );
Note: See TracChangeset for help on using the changeset viewer.