Make WordPress Core

Changeset 55006


Ignore:
Timestamp:
12/19/2022 02:43:02 PM (2 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions/wpNonceField.php

    r54855 r55006  
    1616    public function test_wp_nonce_field() {
    1717        wp_nonce_field();
    18         $this->expectOutputRegex( '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" /><input type="hidden" name="_wp_http_referer" value="" />$#' );
     18        $this->expectOutputRegex(
     19            '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" />' .
     20            '<input type="hidden" name="_wp_http_referer" value="" />$#'
     21        );
    1922    }
    2023
     
    3033     */
    3134    public function test_wp_nonce_field_return( $action, $name, $referer, $expected_regexp ) {
     35        if ( -1 !== $action ) {
     36            $nonce_value     = wp_create_nonce( $action );
     37            $expected_regexp = str_replace( '%%NONCE_VALUE%%', $nonce_value, $expected_regexp );
     38        }
     39
    3240        $this->assertMatchesRegularExpression( $expected_regexp, wp_nonce_field( $action, $name, $referer, false ) );
    3341    }
     
    4452                'name'            => '_wpnonce',
    4553                'referer'         => true,
    46                 'expected_regexp' => '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" /><input type="hidden" name="_wp_http_referer" value="" />$#',
     54                'expected_regexp' =>
     55                    '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" />' .
     56                    '<input type="hidden" name="_wp_http_referer" value="" />$#',
     57            ),
     58            'action_name' => array(
     59                'action'          => 'action_name',
     60                'name'            => '_wpnonce',
     61                'referer'         => true,
     62                'expected_regexp' =>
     63                    '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value="%%NONCE_VALUE%%" />' .
     64                    '<input type="hidden" name="_wp_http_referer" value="" />$#',
    4765            ),
    4866            'nonce_name'  => array(
     
    5068                'name'            => 'nonce_name',
    5169                'referer'         => true,
    52                 'expected_regexp' => '#^<input type="hidden" id="nonce_name" name="nonce_name" value=".{10}" /><input type="hidden" name="_wp_http_referer" value="" />$#',
    53             ),
    54             'action_name' => array(
    55                 'action'          => 'action_name',
    56                 'name'            => '_wpnonce',
    57                 'referer'         => true,
    58                 'expected_regexp' => '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value="' . wp_create_nonce( 'action_name' ) . '" /><input type="hidden" name="_wp_http_referer" value="" />$#',
     70                'expected_regexp' =>
     71                    '#^<input type="hidden" id="nonce_name" name="nonce_name" value=".{10}" />' .
     72                    '<input type="hidden" name="_wp_http_referer" value="" />$#',
    5973            ),
    6074            'no_referer'  => array(
     
    6276                'name'            => '_wpnonce',
    6377                'referer'         => false,
    64                 'expected_regexp' => '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" />$#',
     78                'expected_regexp' =>
     79                    '#^<input type="hidden" id="_wpnonce" name="_wpnonce" value=".{10}" />$#',
    6580            ),
    6681            '& in name'   => array(
     
    6883                'name'            => 'a&b',
    6984                'referer'         => false,
    70                 'expected_regexp' => '#^<input type="hidden" id="a\&amp;b" name="a\&amp;b" value=".{10}" />$#',
     85                'expected_regexp' =>
     86                    '#^<input type="hidden" id="a\&amp;b" name="a\&amp;b" value=".{10}" />$#',
    7187            ),
    7288        );
Note: See TracChangeset for help on using the changeset viewer.