Make WordPress Core

Changeset 60895


Ignore:
Timestamp:
10/03/2025 11:03:25 AM (10 months ago)
Author:
jorbin
Message:

Tests: Use a dataprovider for some auth tests.

Refactor some auth tests to use dataproviders rather than looping through an array or doing multiple assertions in one test.

See #63167.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/auth.php

    r60298 r60895  
    133133        /**
    134134         * @ticket 23494
    135          */
    136         public function test_password_trimming() {
    137                 $passwords_to_test = array(
    138                         'a password with no trailing or leading spaces',
    139                         'a password with trailing spaces ',
    140                         ' a password with leading spaces',
    141                         ' a password with trailing and leading spaces ',
    142                 );
    143 
    144                 foreach ( $passwords_to_test as $password_to_test ) {
    145                         wp_set_password( $password_to_test, $this->user->ID );
    146                         $authed_user = wp_authenticate( $this->user->user_login, $password_to_test );
    147 
    148                         $this->assertNotWPError( $authed_user );
    149                         $this->assertInstanceOf( 'WP_User', $authed_user );
    150                         $this->assertSame( $this->user->ID, $authed_user->ID );
    151                 }
     135         * @dataProvider data_passwords_for_trimming
     136         */
     137        public function test_password_trimming( $password_to_test ) {
     138                wp_set_password( $password_to_test, $this->user->ID );
     139                $authed_user = wp_authenticate( $this->user->user_login, $password_to_test );
     140
     141                $this->assertNotWPError( $authed_user );
     142                $this->assertInstanceOf( 'WP_User', $authed_user );
     143                $this->assertSame( $this->user->ID, $authed_user->ID );
     144        }
     145
     146        public function data_passwords_for_trimming() {
     147                return array(
     148                        'no spaces'                => array( 'a password with no trailing or leading spaces' ),
     149                        'trailing space'           => array( 'a password with trailing spaces ' ),
     150                        'leading space'            => array( ' a password with leading spaces' ),
     151                        'leading and trailing'     => array( ' a password with trailing and leading spaces ' ),
     152                        'multiple leading spaces'  => array( '    a password with multiple leading spaces' ),
     153                        'multiple trailing spaces' => array( 'a password with multiple trailing spaces    ' ),
     154                );
    152155        }
    153156
     
    181184         *
    182185         * @ticket 24973
    183          */
    184         public function test_wp_hash_password_trimming() {
    185 
    186                 $password = ' pass with leading whitespace';
    187                 $this->assertTrue( wp_check_password( 'pass with leading whitespace', wp_hash_password( $password ) ) );
    188 
    189                 $password = 'pass with trailing whitespace ';
    190                 $this->assertTrue( wp_check_password( 'pass with trailing whitespace', wp_hash_password( $password ) ) );
    191 
    192                 $password = ' pass with whitespace ';
    193                 $this->assertTrue( wp_check_password( 'pass with whitespace', wp_hash_password( $password ) ) );
    194 
    195                 $password = "pass with new line \n";
    196                 $this->assertTrue( wp_check_password( 'pass with new line', wp_hash_password( $password ) ) );
    197 
    198                 $password = "pass with vertical tab o_O\x0B";
    199                 $this->assertTrue( wp_check_password( 'pass with vertical tab o_O', wp_hash_password( $password ) ) );
     186         * @dataProvider data_passwords_with_whitespace
     187         */
     188        public function test_wp_hash_password_trimming( $password_with_whitespace, $expected_password ) {
     189                $this->assertTrue( wp_check_password( $expected_password, wp_hash_password( $password_with_whitespace ) ) );
     190        }
     191
     192        public function data_passwords_with_whitespace() {
     193                return array(
     194                        'leading whitespace'  => array( ' pass with leading whitespace', 'pass with leading whitespace' ),
     195                        'trailing whitespace' => array( 'pass with trailing whitespace ', 'pass with trailing whitespace' ),
     196                        'both whitespace'     => array( ' pass with whitespace ', 'pass with whitespace' ),
     197                        'new line'            => array( "pass with new line \n", 'pass with new line' ),
     198                        'vertical tab'        => array( "pass with vertical tab o_O\x0B", 'pass with vertical tab o_O' ),
     199                );
    200200        }
    201201
Note: See TracChangeset for help on using the changeset viewer.