Make WordPress Core

Changeset 56454


Ignore:
Timestamp:
08/24/2023 07:12:33 PM (14 months ago)
Author:
jorbin
Message:

Login and Registration: Improve test coverage for sign on related functions

Adds new tests to ensure that wp_authenticate_email_password, wp_authenticate_username_password, and wp_authenticate_cookie are better tested. This also unsets cookies properly between tests.

Props JordanPak, johnregan3.
Fixes #36476.

File:
1 edited

Legend:

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

    r55301 r56454  
    66 */
    77class Tests_Auth extends WP_UnitTestCase {
     8    // Class User values assigned to constants.
     9    const USER_EMAIL = 'test@password.com';
     10    const USER_LOGIN = 'password-user';
     11    const USER_PASS  = 'password';
     12
    813    protected $user;
    914
     
    2328        self::$_user = $factory->user->create_and_get(
    2429            array(
    25                 'user_login' => 'password-tests',
     30                'user_login' => self::USER_LOGIN,
     31                'user_email' => self::USER_EMAIL,
     32                'user_pass'  => self::USER_PASS,
    2633            )
    2734        );
     
    4653        // Cleanup all the global state.
    4754        unset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'], $GLOBALS['wp_rest_application_password_status'], $GLOBALS['wp_rest_application_password_uuid'] );
     55
     56        // Cleanup manual auth cookie test.
     57        unset( $_COOKIE[ AUTH_COOKIE ] );
     58        unset( $_COOKIE[ SECURE_AUTH_COOKIE ] );
    4859
    4960        parent::tear_down();
     
    428439        $this->assertInstanceOf( 'WP_User', wp_authenticate( $user_args['user_email'], $user_args['user_pass'] ) );
    429440        $this->assertInstanceOf( 'WP_User', wp_authenticate( $user_args['user_login'], $user_args['user_pass'] ) );
     441    }
     442
     443    /**
     444     * @ticket 36476
     445     */
     446    public function test_wp_authenticate_username_password_with_wp_user_object() {
     447        $result = wp_authenticate_username_password( self::$_user, '', '' );
     448        $this->assertSame( $result->ID, self::$user_id );
     449    }
     450
     451    /**
     452     * @ticket 36476
     453     */
     454    public function test_wp_authenticate_username_password_with_login_and_password() {
     455        $result = wp_authenticate_username_password( null, self::USER_LOGIN, self::USER_PASS );
     456        $this->assertSame( self::$user_id, $result->ID );
     457    }
     458
     459    /**
     460     * @ticket 36476
     461     */
     462    public function test_wp_authenticate_username_password_with_null_password() {
     463        $result = wp_authenticate_username_password( null, self::USER_LOGIN, null );
     464        $this->assertInstanceOf( 'WP_Error', $result );
     465    }
     466
     467    /**
     468     * @ticket 36476
     469     */
     470    public function test_wp_authenticate_username_password_with_null_login() {
     471        $result = wp_authenticate_username_password( null, null, self::USER_PASS );
     472        $this->assertInstanceOf( 'WP_Error', $result );
     473    }
     474
     475    /**
     476     * @ticket 36476
     477     */
     478    public function test_wp_authenticate_username_password_with_invalid_login() {
     479        $result = wp_authenticate_username_password( null, 'invalidlogin', self::USER_PASS );
     480        $this->assertInstanceOf( 'WP_Error', $result );
     481    }
     482
     483    /**
     484     * @ticket 36476
     485     */
     486    public function test_wp_authenticate_username_password_with_invalid_password() {
     487        $result = wp_authenticate_username_password( null, self::USER_LOGIN, 'invalidpassword' );
     488        $this->assertInstanceOf( 'WP_Error', $result );
     489    }
     490
     491    /**
     492     * @ticket 36476
     493     */
     494    public function test_wp_authenticate_email_password_with_wp_user_object() {
     495        $result = wp_authenticate_email_password( self::$_user, '', '' );
     496        $this->assertSame( self::$user_id, $result->ID );
     497    }
     498
     499    /**
     500     * @ticket 36476
     501     */
     502    public function test_wp_authenticate_email_password_with_login_and_password() {
     503        $result = wp_authenticate_email_password( null, self::USER_EMAIL, self::USER_PASS );
     504        $this->assertSame( self::$user_id, $result->ID );
     505    }
     506
     507    /**
     508     * @ticket 36476
     509     */
     510    public function test_wp_authenticate_email_password_with_null_password() {
     511        $result = wp_authenticate_email_password( null, self::USER_EMAIL, null );
     512        $this->assertInstanceOf( 'WP_Error', $result );
     513    }
     514
     515    /**
     516     * @ticket 36476
     517     */
     518    public function test_wp_authenticate_email_password_with_null_email() {
     519        $result = wp_authenticate_email_password( null, null, self::USER_PASS );
     520        $this->assertInstanceOf( 'WP_Error', $result );
     521    }
     522
     523    /**
     524     * @ticket 36476
     525     */
     526    public function test_wp_authenticate_email_password_with_invalid_email() {
     527        $result = wp_authenticate_email_password( null, 'invalid@example.com', self::USER_PASS );
     528        $this->assertInstanceOf( 'WP_Error', $result );
     529    }
     530
     531    /**
     532     * @ticket 36476
     533     */
     534    public function test_wp_authenticate_email_password_with_invalid_password() {
     535        $result = wp_authenticate_email_password( null, self::USER_EMAIL, 'invalidpassword' );
     536        $this->assertInstanceOf( 'WP_Error', $result );
     537    }
     538
     539    /**
     540     * @ticket 36476
     541     */
     542    public function test_wp_authenticate_cookie_with_wp_user_object() {
     543        $result = wp_authenticate_cookie( $this->user, null, null );
     544        $this->assertSame( self::$user_id, $result->ID );
     545    }
     546
     547    /**
     548     * @ticket 36476
     549     */
     550    public function test_wp_authenticate_cookie_with_null_params() {
     551        $result = wp_authenticate_cookie( null, null, null );
     552        $this->assertNull( $result );
     553    }
     554
     555    /**
     556     * @ticket 36476
     557     */
     558    public function test_wp_authenticate_cookie_with_invalid_cookie() {
     559        $_COOKIE[ AUTH_COOKIE ]        = 'invalid_cookie';
     560        $_COOKIE[ SECURE_AUTH_COOKIE ] = 'secure_invalid_cookie';
     561
     562        $result = wp_authenticate_cookie( null, null, null );
     563        $this->assertInstanceOf( 'WP_Error', $result );
    430564    }
    431565
Note: See TracChangeset for help on using the changeset viewer.