This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
generated from a8cteam51/team51-plugin-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
brent
committed
Oct 18, 2024
1 parent
ef120b2
commit bfda5c6
Showing
3 changed files
with
166 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
/** | ||
* Class UpdatePasswordEventTest | ||
* | ||
* @package Sift_Decisions | ||
*/ | ||
|
||
require_once 'EventTest.php'; | ||
|
||
// phpcs:disable Universal.Arrays.DisallowShortArraySyntax.Found, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound | ||
|
||
use WPCOMSpecialProjects\SiftDecisions\WooCommerce_Actions\Events; | ||
|
||
/** | ||
* Test case. | ||
*/ | ||
class UpdatePasswordEventTest extends EventTest { | ||
/** | ||
* Test that the $update_password event is triggered. | ||
* | ||
* @return void | ||
*/ | ||
public function test_set_password() { | ||
// Arrange | ||
// - create a user | ||
$user_id = $this->factory()->user->create(); | ||
$user = get_user_by( 'ID', $user_id ); | ||
|
||
// Act | ||
// - update the user | ||
$password = wp_generate_password(); | ||
wp_set_password( $password, $user_id ); | ||
|
||
// Assert | ||
UpdateAccountEventTest::assertNoUpdateAccountEvent( $user_id ); | ||
static::assertUpdatePasswordEvent( $user_id ); | ||
|
||
// Clean up | ||
wp_delete_user( $user_id ); | ||
} | ||
|
||
/** | ||
* Test that the $update_password event is triggered. | ||
* | ||
* @return void | ||
*/ | ||
public function test_update_password_event() { | ||
// Arrange | ||
// - create a user | ||
$user_id = $this->factory()->user->create(); | ||
$user = get_user_by( 'ID', $user_id ); | ||
|
||
// Act | ||
// - update the user | ||
$password = wp_generate_password(); | ||
// wp_insert_user() will not hash the password on an update, so we'll use wp_update_user(). | ||
wp_update_user( | ||
[ | ||
'ID' => $user->ID, | ||
'user_login' => $user->user_login, | ||
'user_pass' => $password, | ||
] | ||
); | ||
|
||
// Assert | ||
// Might as well 😆 (currently only testing wp_insert_user() so this adds another check). | ||
UpdateAccountEventTest::assertUpdateAccountEvent( $user_id ); | ||
static::assertUpdatePasswordEvent( $user_id ); | ||
|
||
// Clean up | ||
wp_delete_user( $user_id ); | ||
} | ||
|
||
/** | ||
* Test that the $update_password event is NOT triggered. | ||
* | ||
* @return void | ||
*/ | ||
public function test_no_update_password_event() { | ||
// Arrange | ||
// - create a user | ||
$user_id = $this->factory()->user->create(); | ||
$user = get_user_by( 'ID', $user_id ); | ||
|
||
// Act | ||
// - update the user | ||
$password = wp_generate_password(); | ||
// wp_insert_user() will not hash the password on an update, so we'll use wp_update_user(). | ||
wp_update_user( | ||
[ | ||
'ID' => $user->ID, | ||
'user_login' => $user->user_login, | ||
] | ||
); | ||
|
||
// Assert | ||
// Might as well 😆 (currently only testing wp_insert_user() so this adds another check). | ||
UpdateAccountEventTest::assertUpdateAccountEvent( $user_id ); | ||
static::assertNoUpdatePasswordEvent( $user_id ); | ||
|
||
// Clean up | ||
wp_delete_user( $user_id ); | ||
} | ||
|
||
/** | ||
* Assert $update_password event is triggered. | ||
* | ||
* @param integer $user_id User ID. | ||
* | ||
* @return void | ||
*/ | ||
public static function assertUpdatePasswordEvent( $user_id ) { | ||
$events = static::filter_events( | ||
[ | ||
'event' => '$update_password', | ||
'properties.$user_id' => $user_id, | ||
] | ||
); | ||
static::assertGreaterThanOrEqual( 1, count( $events ), 'No $update_password event found' ); | ||
} | ||
|
||
/** | ||
* Assert $update_password event is triggered. | ||
* | ||
* @param integer $user_id User ID. | ||
* | ||
* @return void | ||
*/ | ||
public static function assertNoUpdatePasswordEvent( $user_id ) { | ||
$events = static::filter_events( | ||
[ | ||
'event' => '$update_password', | ||
'properties.$user_id' => $user_id, | ||
] | ||
); | ||
static::assertEquals( 0, count( $events ), '$update_password event found' ); | ||
} | ||
} |