From 74edebac97f6d47f25da6c665bce899f9f517d7a Mon Sep 17 00:00:00 2001
From: Sergey Biryukov
Date: Fri, 13 Aug 2021 14:00:05 +0000
Subject: [PATCH] Tests: Add unit tests for the `wp_nonce_ays()` function.
Update the `wp_die_handler()` method to pass the response into `WPDieException` so that `expectExceptionCode()` calls work as expected.
Follow-up to [1221/tests], [3934], [4009], [12309].
Props pbearne, jrf, mukesh27.
Fixes #53882.
git-svn-id: https://develop.svn.wordpress.org/trunk@51608 602fd350-edb4-49c9-b593-d223f7449a82
---
tests/phpunit/includes/abstract-testcase.php | 18 ++++++++---
tests/phpunit/tests/functions/wpNonceAys.php | 34 ++++++++++++++++++++
2 files changed, 48 insertions(+), 4 deletions(-)
create mode 100644 tests/phpunit/tests/functions/wpNonceAys.php
diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php
index 3a9031d11b..be836f6707 100644
--- a/tests/phpunit/includes/abstract-testcase.php
+++ b/tests/phpunit/includes/abstract-testcase.php
@@ -452,11 +452,16 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
/**
* Throws an exception when called.
*
- * @throws WPDieException Exception containing the message.
+ * @since UT (3.7.0)
+ * @since 5.9.0 Added the `$title` and `$args` parameters.
*
- * @param string $message The `wp_die()` message.
+ * @throws WPDieException Exception containing the message and the response code.
+ *
+ * @param string|WP_Error $message The `wp_die()` message or WP_Error object.
+ * @param string $title The `wp_die()` title.
+ * @param string|array $args The `wp_die()` arguments.
*/
- public function wp_die_handler( $message ) {
+ public function wp_die_handler( $message, $title, $args ) {
if ( is_wp_error( $message ) ) {
$message = $message->get_error_message();
}
@@ -465,7 +470,12 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
$message = '0';
}
- throw new WPDieException( $message );
+ $code = 0;
+ if ( isset( $args['response'] ) ) {
+ $code = $args['response'];
+ }
+
+ throw new WPDieException( $message, $code );
}
/**
diff --git a/tests/phpunit/tests/functions/wpNonceAys.php b/tests/phpunit/tests/functions/wpNonceAys.php
new file mode 100644
index 0000000000..06b807da42
--- /dev/null
+++ b/tests/phpunit/tests/functions/wpNonceAys.php
@@ -0,0 +1,34 @@
+expectException( 'WPDieException' );
+ $this->expectExceptionMessage( 'The link you followed has expired.' );
+ $this->expectExceptionCode( 403 );
+
+ wp_nonce_ays( 'random_string' );
+ }
+
+ /**
+ * @ticket 53882
+ */
+ public function test_wp_nonce_ays_log_out() {
+ $this->expectException( 'WPDieException' );
+ $this->expectExceptionMessageMatches( '#You are attempting to log out of Test Blog
Do you really want to log out\?#m' );
+ $this->expectExceptionCode( 403 );
+
+ wp_nonce_ays( 'log-out' );
+ }
+}