diff --git phpunit.xml.dist phpunit.xml.dist
index 6297c52dc3..bf23114722 100644
|
|
|
|
| 23 | 23 | <groups> |
| 24 | 24 | <exclude> |
| 25 | 25 | <group>ajax</group> |
| | 26 | <group>factory</group> |
| 26 | 27 | <group>ms-files</group> |
| 27 | 28 | <group>ms-required</group> |
| 28 | 29 | <group>external-http</group> |
diff --git tests/phpunit/includes/bootstrap.php tests/phpunit/includes/bootstrap.php
index 5d3e159685..7337ea7efd 100644
|
|
|
require_once ABSPATH . '/wp-settings.php'; |
| 105 | 105 | // Delete any default posts & related data |
| 106 | 106 | _delete_all_posts(); |
| 107 | 107 | |
| | 108 | require dirname( __FILE__ ) . '/class-mock-passwordhash.php'; |
| 108 | 109 | require dirname( __FILE__ ) . '/testcase.php'; |
| 109 | 110 | require dirname( __FILE__ ) . '/testcase-rest-api.php'; |
| 110 | 111 | require dirname( __FILE__ ) . '/testcase-rest-controller.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 | */ |
| | 11 | class 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 | } |
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 { |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | 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 | |
| 25 | 39 | return wp_insert_user( $args ); |
| 26 | 40 | } |
| 27 | 41 | |
diff --git tests/phpunit/multisite.xml tests/phpunit/multisite.xml
index 0d60efbe1c..4ce0937a91 100644
|
|
|
|
| 24 | 24 | <groups> |
| 25 | 25 | <exclude> |
| 26 | 26 | <group>ajax</group> |
| | 27 | <group>factory</group> |
| 27 | 28 | <group>ms-files</group> |
| 28 | 29 | <group>ms-excluded</group> |
| 29 | 30 | <group>external-http</group> |
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 | */ |
| | 7 | class 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 | } |