From 286aa22f46a9148adb3f08db9e38cb3f3cc6b41c Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Mon, 22 May 2023 12:01:12 +1000 Subject: [PATCH] Tests: Simplify the cookie management. See #528 --- tests/class-two-factor-core.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/class-two-factor-core.php b/tests/class-two-factor-core.php index 7af3f03e..0f3a1595 100644 --- a/tests/class-two-factor-core.php +++ b/tests/class-two-factor-core.php @@ -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' ] ); } /** @@ -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 ] ); } /** @@ -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. *