From dd9fa218397c5e79b914c58a16553b4267015e32 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 19 Dec 2022 14:43:02 +0000 Subject: [PATCH] Tests: Correct a flaky `wp_nonce_field()` test. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../phpunit/tests/functions/wpNonceField.php | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/tests/phpunit/tests/functions/wpNonceField.php b/tests/phpunit/tests/functions/wpNonceField.php index 2596402bb5..78116f01e5 100644 --- a/tests/phpunit/tests/functions/wpNonceField.php +++ b/tests/phpunit/tests/functions/wpNonceField.php @@ -15,7 +15,10 @@ class Tests_Functions_wpNonceField extends WP_UnitTestCase { */ public function test_wp_nonce_field() { wp_nonce_field(); - $this->expectOutputRegex( '#^$#' ); + $this->expectOutputRegex( + '#^' . + '$#' + ); } /** @@ -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' => '#^$#', - ), - 'nonce_name' => array( - 'action' => -1, - 'name' => 'nonce_name', - 'referer' => true, - 'expected_regexp' => '#^$#', + 'expected_regexp' => + '#^' . + '$#', ), 'action_name' => array( 'action' => 'action_name', 'name' => '_wpnonce', 'referer' => true, - 'expected_regexp' => '#^$#', + 'expected_regexp' => + '#^' . + '$#', + ), + 'nonce_name' => array( + 'action' => -1, + 'name' => 'nonce_name', + 'referer' => true, + 'expected_regexp' => + '#^' . + '$#', ), 'no_referer' => array( 'action' => -1, 'name' => '_wpnonce', 'referer' => false, - 'expected_regexp' => '#^$#', + 'expected_regexp' => + '#^$#', ), '& in name' => array( 'action' => -1, 'name' => 'a&b', 'referer' => false, - 'expected_regexp' => '#^$#', + 'expected_regexp' => + '#^$#', ), ); }