From a904cf42f88360a45a2bba68e5f695df27d07c16 Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Fri, 7 Oct 2022 22:17:06 +0000 Subject: [PATCH] Build/Test Tools: Add tests for `wp_nonce_field()` and `wp_referer_field()`. This changeset adds missing unit tests for these two functions. Props pbearne, costdev, audrasjb. Fixes #55578. git-svn-id: https://develop.svn.wordpress.org/trunk@54420 602fd350-edb4-49c9-b593-d223f7449a82 --- .../phpunit/tests/functions/wpNonceField.php | 77 +++++++++++++++++++ .../tests/functions/wpRefererField.php | 32 ++++++++ 2 files changed, 109 insertions(+) create mode 100644 tests/phpunit/tests/functions/wpNonceField.php create mode 100644 tests/phpunit/tests/functions/wpRefererField.php diff --git a/tests/phpunit/tests/functions/wpNonceField.php b/tests/phpunit/tests/functions/wpNonceField.php new file mode 100644 index 0000000000..ec2c68727d --- /dev/null +++ b/tests/phpunit/tests/functions/wpNonceField.php @@ -0,0 +1,77 @@ +expectOutputRegex( '#^$#' ); + } + + /** + * @ticket 55578 + * + * @dataProvider data_wp_nonce_field + * + * @param int|string $action Action name. + * @param string $name Nonce name. + * @param bool $referer Whether to set the referer field fior validation. + * @param string $expected_reg_exp The expected regular expression. + */ + public function test_wp_nonce_field_return( $action, $name, $referer, $expected_reg_exp ) { + + $this->assertMatchesRegularExpression( $expected_reg_exp, wp_nonce_field( $action, $name, $referer, false ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_wp_nonce_field() { + + return array( + 'default' => array( + 'action' => - 1, + 'name' => '_wpnonce', + 'referer' => true, + 'expected_reg_exp' => '#^$#', + ), + 'nonce_name' => array( + 'action' => - 1, + 'name' => 'nonce_name', + 'referer' => true, + 'expected_reg_exp' => '#^$#', + ), + 'action_name' => array( + 'action' => 'action_name', + 'name' => '_wpnonce', + 'referer' => true, + 'expected_reg_exp' => '#^$#', + ), + 'no_referer' => array( + 'action' => - 1, + 'name' => '_wpnonce', + 'referer' => false, + 'expected_reg_exp' => '#^$#', + ), + '& in name' => array( + 'action' => - 1, + 'name' => 'a&b', + 'referer' => false, + 'expected_reg_exp' => '#^$#', + ), + ); + } +} diff --git a/tests/phpunit/tests/functions/wpRefererField.php b/tests/phpunit/tests/functions/wpRefererField.php new file mode 100644 index 0000000000..d0642f4088 --- /dev/null +++ b/tests/phpunit/tests/functions/wpRefererField.php @@ -0,0 +1,32 @@ +expectOutputString( '' ); + } + + /** + * @ticket 55578 + */ + public function test_wp_referer_field_return() { + + $_SERVER['REQUEST_URI'] = '/test/'; + + $this->assertSame( '', wp_referer_field( false ) ); + } +}