WordPress.org

Make WordPress Core

Ticket #42904: 42904-2.patch

File 42904-2.patch, 4.1 KB (added by Frank Klein, 19 months ago)
  • phpunit.xml.dist

    diff --git phpunit.xml.dist phpunit.xml.dist
    index 6297c52dc3..bf23114722 100644
     
    2323    <groups>
    2424        <exclude>
    2525            <group>ajax</group>
     26            <group>factory</group>
    2627            <group>ms-files</group>
    2728            <group>ms-required</group>
    2829            <group>external-http</group>
  • tests/phpunit/includes/bootstrap.php

    diff --git tests/phpunit/includes/bootstrap.php tests/phpunit/includes/bootstrap.php
    index 5d3e159685..7337ea7efd 100644
    require_once ABSPATH . '/wp-settings.php'; 
    105105// Delete any default posts & related data
    106106_delete_all_posts();
    107107
     108require dirname( __FILE__ ) . '/class-mock-passwordhash.php';
    108109require dirname( __FILE__ ) . '/testcase.php';
    109110require dirname( __FILE__ ) . '/testcase-rest-api.php';
    110111require dirname( __FILE__ ) . '/testcase-rest-controller.php';
  • new file tests/phpunit/includes/class-mock-passwordhash.php

    diff --git tests/phpunit/includes/class-mock-passwordhash.php tests/phpunit/includes/class-mock-passwordhash.php
    new file mode 100644
    index 0000000000..320de7621c
    - +  
     1<?php
     2/**
     3* Mock of the phpass class.
     4*
     5* @since 5.0.0
     6*/
     7
     8/**
     9* Class Mock_PasswordHash
     10*/
     11class Mock_PasswordHash {
     12        // phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid --- the mocked class does not use snake case.
     13        function HashPassword( $password ) {
     14                return $password;
     15        }
     16}
  • tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php

    diff --git tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php
    index e2e2564b8c..6503a1e9e6 100644
    class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing { 
    2222        }
    2323
    2424        function create_object( $args ) {
     25                // Do not hash the user's password, use the mock hasher instead that returns the password in plain text.
     26                if ( isset( $args['skip_password_hash'] ) && true === $args['skip_password_hash'] ) {
     27                        // phpcs:disable WordPress.Variables.GlobalVariables.OverrideProhibited --- overriding on purpose.
     28                        $GLOBALS['wp_hasher'] = new Mock_PasswordHash();
     29
     30                        $user = wp_insert_user( $args );
     31
     32                        // Unset the hasher global to ensure that the next call to `wp_hash_password()` creates a new instance, and
     33                        // everything works correctly.
     34                        unset( $GLOBALS['wp_hasher'] );
     35
     36                        return $user;
     37                }
     38
    2539                return wp_insert_user( $args );
    2640        }
    2741
  • tests/phpunit/multisite.xml

    diff --git tests/phpunit/multisite.xml tests/phpunit/multisite.xml
    index 0d60efbe1c..4ce0937a91 100644
     
    2424    <groups>
    2525        <exclude>
    2626            <group>ajax</group>
     27            <group>factory</group>
    2728            <group>ms-files</group>
    2829            <group>ms-excluded</group>
    2930            <group>external-http</group>
  • new file tests/phpunit/tests/factory/test-user.php

    diff --git tests/phpunit/tests/factory/test-user.php tests/phpunit/tests/factory/test-user.php
    new file mode 100644
    index 0000000000..28c669fa0c
    - +  
     1<?php
     2/**
     3 * Tests for the user fixture factory.
     4 *
     5 * @group factory
     6 */
     7class Tests_WP_UnitTest_Factory_For_User extends WP_UnitTestCase {
     8        /**
     9         * Test that password hashing can be skipped when creating a user fixture, to improve performance.
     10         */
     11        public function test_skip_password_hash() {
     12                global $wpdb;
     13
     14                $user_id = self::factory()->user->create( array(
     15                        'skip_password_hash' => true,
     16                ) );
     17
     18                // Ensure that the password is stored as plain text.
     19                $stored_password = $wpdb->get_var( $wpdb->prepare( "SELECT 'password' FROM $wpdb->users WHERE ID = %d", $user_id ) );
     20                $this->assertSame( 'password', $stored_password );
     21                // Ensure that the global variable containing the password hasher does not exist. This will ensure that
     22                // `wp_hash_password()` creates a new instance, and everything works correctly.
     23                $this->assertFalse( isset( $GLOBALS['wp_hasher'] ) );
     24        }
     25}