Skip to content

Commit 286aa22

Browse files
committed
Tests: Simplify the cookie management.
See #528
1 parent af7ad52 commit 286aa22

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/class-two-factor-core.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Test_ClassTwoFactorCore extends WP_UnitTestCase {
2727
*/
2828
public static function wpSetUpBeforeClass() {
2929
set_error_handler( array( 'Test_ClassTwoFactorCore', 'error_handler' ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
30+
add_action( 'set_auth_cookie', [ __CLASS__, 'set_auth_cookie' ] );
31+
add_action( 'set_logged_in_cookie', [ __CLASS__, 'set_logged_in_cookie' ] );
3032
}
3133

3234
/**
@@ -36,6 +38,17 @@ public static function wpSetUpBeforeClass() {
3638
*/
3739
public static function wpTearDownAfterClass() {
3840
restore_error_handler();
41+
remove_action( 'set_auth_cookie', [ __CLASS__, 'set_auth_cookie' ] );
42+
remove_action( 'set_logged_in_cookie', [ __CLASS__, 'set_logged_in_cookie' ] );
43+
}
44+
45+
/**
46+
* Cleanup after each test.
47+
*/
48+
public function tearDown(): void {
49+
parent::tearDown();
50+
51+
unset( $_COOKIE[ AUTH_COOKIE ], $_COOKIE[ LOGGED_IN_COOKIE ] );
3952
}
4053

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

72+
/**
73+
* Simulate the auth cookie having being sent.
74+
*/
75+
public static function set_auth_cookie( $auth_cookie ) {
76+
$_COOKIE[ AUTH_COOKIE ] = $auth_cookie;
77+
}
78+
79+
/**
80+
* Simulate the logged_in cookie having being sent.
81+
*/
82+
public static function set_logged_in_cookie( $logged_in_cookie ) {
83+
$_COOKIE[ LOGGED_IN_COOKIE ] = $logged_in_cookie;
84+
}
85+
5986
/**
6087
* Get a dummy user object.
6188
*

0 commit comments

Comments
 (0)