Make WordPress Core


Ignore:
Timestamp:
01/09/2024 04:32:14 PM (13 months ago)
Author:
hellofromTonya
Message:

Tests: Add hook priority call order tests.

Adds happy (integer) and unhappy (non-integer) tests for validating the priority call order for:

  • do_action()
  • WP_Hook::do_action()
  • apply_filters()
  • WP_Hook::apply_filters()

As each of these functions have differing code, the tests are added to each to ensure expected results and protect against future regressions.

Follow-up to [53804], [52010], [25002], [25/tests], [62/tests].

Props hellofromTonya, mukesh27, dd32, valendesigns, drrobotnik.
Fixes #60193.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/hooks/applyFilters.php

    r56547 r57257  
    4343        $this->assertSame( 2, $a->get_call_count() );
    4444    }
     45
     46    /**
     47     * @ticket 60193
     48     *
     49     * @dataProvider data_priority_callback_order_with_integers
     50     * @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
     51     *
     52     * @param array $priorities {
     53     *     Indexed array of the priorities for the MockAction callbacks.
     54     *
     55     *     @type mixed $0 Priority for 'action' callback.
     56     *     @type mixed $1 Priority for 'action2' callback.
     57     * }
     58     * @param array  $expected_call_order  An array of callback names in expected call order.
     59     * @param string $expected_deprecation Optional. Deprecation message. Default ''.
     60     */
     61    public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) {
     62        $mock      = new MockAction();
     63        $hook      = new WP_Hook();
     64        $hook_name = __FUNCTION__;
     65
     66        if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
     67            $this->expectDeprecation();
     68            $this->expectDeprecationMessage( $expected_deprecation );
     69        }
     70
     71        $hook->add_filter( $hook_name, array( $mock, 'filter' ), $priorities[0], 1 );
     72        $hook->add_filter( $hook_name, array( $mock, 'filter2' ), $priorities[1], 1 );
     73        $hook->apply_filters( __FUNCTION__ . '_val', array( '' ) );
     74
     75        $this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );
     76
     77        $actual_call_order = wp_list_pluck( $mock->get_events(), 'filter' );
     78        $this->assertSame( $expected_call_order, $actual_call_order, 'The filter callback order does not match the expected order' );
     79    }
     80
     81    /**
     82     * Happy path data provider.
     83     *
     84     * @return array[]
     85     */
     86    public function data_priority_callback_order_with_integers() {
     87        return array(
     88            'int DESC' => array(
     89                'priorities'          => array( 10, 9 ),
     90                'expected_call_order' => array( 'filter2', 'filter' ),
     91            ),
     92            'int ASC'  => array(
     93                'priorities'          => array( 9, 10 ),
     94                'expected_call_order' => array( 'filter', 'filter2' ),
     95            ),
     96        );
     97    }
     98
     99    /**
     100     * Unhappy path data provider.
     101     *
     102     * @return array[]
     103     */
     104    public function data_priority_callback_order_with_unhappy_path_nonintegers() {
     105        return array(
     106            // Numbers as strings and floats.
     107            'int as string DESC'               => array(
     108                'priorities'          => array( '10', '9' ),
     109                'expected_call_order' => array( 'filter2', 'filter' ),
     110            ),
     111            'int as string ASC'                => array(
     112                'priorities'          => array( '9', '10' ),
     113                'expected_call_order' => array( 'filter', 'filter2' ),
     114            ),
     115            'float DESC'                       => array(
     116                'priorities'           => array( 10.0, 9.5 ),
     117                'expected_call_order'  => array( 'filter2', 'filter' ),
     118                'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
     119            ),
     120            'float ASC'                        => array(
     121                'priorities'           => array( 9.5, 10.0 ),
     122                'expected_call_order'  => array( 'filter', 'filter2' ),
     123                'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
     124            ),
     125            'float as string DESC'             => array(
     126                'priorities'          => array( '10.0', '9.5' ),
     127                'expected_call_order' => array( 'filter2', 'filter' ),
     128            ),
     129            'float as string ASC'              => array(
     130                'priorities'          => array( '9.5', '10.0' ),
     131                'expected_call_order' => array( 'filter', 'filter2' ),
     132            ),
     133
     134            // Non-numeric.
     135            'null'                             => array(
     136                'priorities'          => array( null, null ),
     137                'expected_call_order' => array( 'filter', 'filter2' ),
     138            ),
     139            'bool DESC'                        => array(
     140                'priorities'          => array( true, false ),
     141                'expected_call_order' => array( 'filter2', 'filter' ),
     142            ),
     143            'bool ASC'                         => array(
     144                'priorities'          => array( false, true ),
     145                'expected_call_order' => array( 'filter', 'filter2' ),
     146            ),
     147            'non-numerical string DESC'        => array(
     148                'priorities'          => array( 'test1', 'test2' ),
     149                'expected_call_order' => array( 'filter', 'filter2' ),
     150            ),
     151            'non-numerical string ASC'         => array(
     152                'priorities'          => array( 'test1', 'test2' ),
     153                'expected_call_order' => array( 'filter', 'filter2' ),
     154            ),
     155            'int, non-numerical string DESC'   => array(
     156                'priorities'          => array( 10, 'test' ),
     157                'expected_call_order' => array( 'filter2', 'filter' ),
     158            ),
     159            'int, non-numerical string ASC'    => array(
     160                'priorities'          => array( 'test', 10 ),
     161                'expected_call_order' => array( 'filter', 'filter2' ),
     162            ),
     163            'float, non-numerical string DESC' => array(
     164                'priorities'          => array( 10.0, 'test' ),
     165                'expected_call_order' => array( 'filter2', 'filter' ),
     166            ),
     167            'float, non-numerical string ASC'  => array(
     168                'priorities'          => array( 'test', 10.0 ),
     169                'expected_call_order' => array( 'filter', 'filter2' ),
     170            ),
     171        );
     172    }
    45173}
Note: See TracChangeset for help on using the changeset viewer.