mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
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
This commit is contained in:
77
tests/phpunit/tests/functions/wpNonceField.php
Normal file
77
tests/phpunit/tests/functions/wpNonceField.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Tests for the wp_nonce_field() function.
|
||||
*
|
||||
* @since 6.1.0
|
||||
*
|
||||
* @group functions.php
|
||||
* @covers ::wp_nonce_field
|
||||
*/
|
||||
class Tests_Functions_wpNonceField extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @ticket 55578
|
||||
*/
|
||||
public function test_wp_nonce_field() {
|
||||
|
||||
wp_nonce_field();
|
||||
$this->expectOutputRegex( '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" /><input type="hidden" name="_wp_http_referer" value="" />$#' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @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' => '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" /><input type="hidden" name="_wp_http_referer" value="" />$#',
|
||||
),
|
||||
'nonce_name' => array(
|
||||
'action' => - 1,
|
||||
'name' => 'nonce_name',
|
||||
'referer' => true,
|
||||
'expected_reg_exp' => '#^<input type="hidden" id="nonce_name" name="nonce_name" value=".{10}" /><input type="hidden" name="_wp_http_referer" value="" />$#',
|
||||
),
|
||||
'action_name' => array(
|
||||
'action' => 'action_name',
|
||||
'name' => '_wpnonce',
|
||||
'referer' => true,
|
||||
'expected_reg_exp' => '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value="' . wp_create_nonce( 'action_name' ) . '" /><input type="hidden" name="_wp_http_referer" value="" />$#',
|
||||
),
|
||||
'no_referer' => array(
|
||||
'action' => - 1,
|
||||
'name' => '_wpnonce',
|
||||
'referer' => false,
|
||||
'expected_reg_exp' => '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" />$#',
|
||||
),
|
||||
'& in name' => array(
|
||||
'action' => - 1,
|
||||
'name' => 'a&b',
|
||||
'referer' => false,
|
||||
'expected_reg_exp' => '#^<input type="hidden" id="a\&b" name="a\&b" value=".{10}" />$#',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
32
tests/phpunit/tests/functions/wpRefererField.php
Normal file
32
tests/phpunit/tests/functions/wpRefererField.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Tests for the wp_referer_field() function.
|
||||
*
|
||||
* @since 6.1.0
|
||||
*
|
||||
* @group functions.php
|
||||
* @covers ::wp_referer_field
|
||||
*/
|
||||
class Tests_Functions_wpRefererField extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @ticket 55578
|
||||
*/
|
||||
public function test_wp_referer_field() {
|
||||
|
||||
$_SERVER['REQUEST_URI'] = '/test/';
|
||||
wp_referer_field();
|
||||
$this->expectOutputString( '<input type="hidden" name="_wp_http_referer" value="/test/" />' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55578
|
||||
*/
|
||||
public function test_wp_referer_field_return() {
|
||||
|
||||
$_SERVER['REQUEST_URI'] = '/test/';
|
||||
|
||||
$this->assertSame( '<input type="hidden" name="_wp_http_referer" value="/test/" />', wp_referer_field( false ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user