Tests: Correct a flaky wp_nonce_field() test.

The test for `wp_nonce_field()` with a custom action name verifies that the nonce value matches the one returned by `wp_create_nonce()` with the same action name.

The created nonce, in turn, depends on `wp_nonce_tick()`, which returns a different result in the first and the second half of the nonce's lifespan, one day by default:
* 00:00:01 to 12:00:00 — First tick
* 12:00:01 to 00:00:00 — Second tick

In practice, due to a delay between initializing data providers and running the actual tests, it is possible for the nonce tick to change in the process, for example if the test suite run starts at 11:59:30, and the affected test runs at 12:00:30, causing a test failure.

This commit reduces the chance of a race condition by moving the `wp_create_nonce()` call from the data provider into the test itself.

Includes wrapping long lines with the expected results for better readability.

Follow-up to [54420].

Props NekoJonez, SergeyBiryukov.
See #56793.

git-svn-id: https://develop.svn.wordpress.org/trunk@55006 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-12-19 14:43:02 +00:00
parent e27c5a38e3
commit dd9fa21839

View File

@ -15,7 +15,10 @@ class Tests_Functions_wpNonceField extends WP_UnitTestCase {
*/
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="" />$#' );
$this->expectOutputRegex(
'#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" />' .
'<input type="hidden" name="_wp_http_referer" value="" />$#'
);
}
/**
@ -29,6 +32,11 @@ class Tests_Functions_wpNonceField extends WP_UnitTestCase {
* @param string $expected_regexp The expected regular expression.
*/
public function test_wp_nonce_field_return( $action, $name, $referer, $expected_regexp ) {
if ( -1 !== $action ) {
$nonce_value = wp_create_nonce( $action );
$expected_regexp = str_replace( '%%NONCE_VALUE%%', $nonce_value, $expected_regexp );
}
$this->assertMatchesRegularExpression( $expected_regexp, wp_nonce_field( $action, $name, $referer, false ) );
}
@ -43,31 +51,39 @@ class Tests_Functions_wpNonceField extends WP_UnitTestCase {
'action' => -1,
'name' => '_wpnonce',
'referer' => true,
'expected_regexp' => '#^<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_regexp' => '#^<input type="hidden" id="nonce_name" name="nonce_name" value=".{10}" /><input type="hidden" name="_wp_http_referer" value="" />$#',
'expected_regexp' =>
'#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" />' .
'<input type="hidden" name="_wp_http_referer" value="" />$#',
),
'action_name' => array(
'action' => 'action_name',
'name' => '_wpnonce',
'referer' => true,
'expected_regexp' => '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value="' . wp_create_nonce( 'action_name' ) . '" /><input type="hidden" name="_wp_http_referer" value="" />$#',
'expected_regexp' =>
'#^<input type="hidden" id="_wpnonce" name="_wpnonce" value="%%NONCE_VALUE%%" />' .
'<input type="hidden" name="_wp_http_referer" value="" />$#',
),
'nonce_name' => array(
'action' => -1,
'name' => 'nonce_name',
'referer' => true,
'expected_regexp' =>
'#^<input type="hidden" id="nonce_name" name="nonce_name" value=".{10}" />' .
'<input type="hidden" name="_wp_http_referer" value="" />$#',
),
'no_referer' => array(
'action' => -1,
'name' => '_wpnonce',
'referer' => false,
'expected_regexp' => '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" />$#',
'expected_regexp' =>
'#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" />$#',
),
'& in name' => array(
'action' => -1,
'name' => 'a&b',
'referer' => false,
'expected_regexp' => '#^<input type="hidden" id="a\&amp;b" name="a\&amp;b" value=".{10}" />$#',
'expected_regexp' =>
'#^<input type="hidden" id="a\&amp;b" name="a\&amp;b" value=".{10}" />$#',
),
);
}