Skip to content

Commit

Permalink
Tests: Simplify the cookie management.
Browse files Browse the repository at this point in the history
See #528
  • Loading branch information
dd32 committed May 22, 2023
1 parent af7ad52 commit 286aa22
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/class-two-factor-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Test_ClassTwoFactorCore extends WP_UnitTestCase {
*/
public static function wpSetUpBeforeClass() {
set_error_handler( array( 'Test_ClassTwoFactorCore', 'error_handler' ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
add_action( 'set_auth_cookie', [ __CLASS__, 'set_auth_cookie' ] );
add_action( 'set_logged_in_cookie', [ __CLASS__, 'set_logged_in_cookie' ] );
}

/**
Expand All @@ -36,6 +38,17 @@ public static function wpSetUpBeforeClass() {
*/
public static function wpTearDownAfterClass() {
restore_error_handler();
remove_action( 'set_auth_cookie', [ __CLASS__, 'set_auth_cookie' ] );
remove_action( 'set_logged_in_cookie', [ __CLASS__, 'set_logged_in_cookie' ] );
}

/**
* Cleanup after each test.
*/
public function tearDown(): void {
parent::tearDown();

unset( $_COOKIE[ AUTH_COOKIE ], $_COOKIE[ LOGGED_IN_COOKIE ] );
}

/**
Expand All @@ -56,6 +69,20 @@ public static function error_handler( $errno, $errstr ) {
return true;
}

/**
* Simulate the auth cookie having being sent.
*/
public static function set_auth_cookie( $auth_cookie ) {
$_COOKIE[ AUTH_COOKIE ] = $auth_cookie;
}

/**
* Simulate the logged_in cookie having being sent.
*/
public static function set_logged_in_cookie( $logged_in_cookie ) {
$_COOKIE[ LOGGED_IN_COOKIE ] = $logged_in_cookie;
}

/**
* Get a dummy user object.
*
Expand Down

0 comments on commit 286aa22

Please sign in to comment.