Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/04/2020 01:29:50 PM (6 years ago)
Author:
imath
Message:

BP Members: improve our password validation process

We're introducing a new function to validate the member's chosen password: bp_members_validate_user_password().

This function is primarly used to check the password is not empty, and to make sure the password confirmation matches the password. If it's the case, the function will return a WP_Error object with no error message. Otherwise this object will contain an error message.

Plugins can now use the bp_members_validate_user_password filter to add their own error messages according to a custom validation process. See the last unit tests of this commit for an example of use.

Props devnik, tharsheblows

Fixes #8066

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/members/functions.php

    r12665 r12679  
    579579        public function test_wp_registration_url_should_return_bp_register_page_when_register_page_is_configured_properly() {
    580580                $this->assertSame( bp_get_signup_page(), wp_registration_url() );
     581        }
     582
     583        /**
     584         * @group bp_members_validate_user_password
     585         */
     586        public function test_bp_members_validate_user_password() {
     587                $validate = bp_members_validate_user_password( 'foobar', 'foobar' );
     588
     589                $this->assertEmpty( $validate->get_error_message() );
     590        }
     591
     592        /**
     593         * @group bp_members_validate_user_password
     594         */
     595        public function test_bp_members_validate_user_password_missing() {
     596                $validate = bp_members_validate_user_password( '', '' );
     597
     598                $this->assertEquals( 'missing_user_password', $validate->get_error_code() );
     599
     600                $validate = bp_members_validate_user_password( 'foobar', '' );
     601
     602                $this->assertEquals( 'missing_user_password', $validate->get_error_code() );
     603
     604                $validate = bp_members_validate_user_password( '', 'foobar' );
     605
     606                $this->assertEquals( 'missing_user_password', $validate->get_error_code() );
     607        }
     608
     609        /**
     610         * @group bp_members_validate_user_password
     611         */
     612        public function test_bp_members_validate_user_password_mismatching() {
     613                $validate = bp_members_validate_user_password( 'foobar', 'barfoo' );
     614
     615                $this->assertEquals( 'mismatching_user_password', $validate->get_error_code() );
     616        }
     617
     618        /**
     619         * @group bp_members_validate_user_password
     620         */
     621        public function test_bp_members_validate_user_password_too_short() {
     622                add_filter( 'bp_members_validate_user_password', array( $this, 'filter_bp_members_validate_user_password' ), 10, 2 );
     623
     624                $validate = bp_members_validate_user_password( 'one', 'one' );
     625
     626                remove_filter( 'bp_members_validate_user_password', array( $this, 'filter_bp_members_validate_user_password' ), 10, 2 );
     627
     628                $this->assertEquals( 'too_short_user_password', $validate->get_error_code() );
     629        }
     630
     631        function filter_bp_members_validate_user_password( $errors, $pass ) {
     632                if ( 4 > strlen( $pass ) ) {
     633                        $errors->add( 'too_short_user_password', __( 'Your password is too short.', 'buddypress' ) );
     634                }
     635
     636                return $errors;
    581637        }
    582638
Note: See TracChangeset for help on using the changeset viewer.