Make WordPress Core


Ignore:
Timestamp:
01/09/2024 04:32:14 PM (3 years 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/filters.php

    r56548 r57257  
    119119        }
    120120
    121         public function test_filter_priority() {
    122                 $a         = new MockAction();
    123                 $hook_name = __FUNCTION__;
    124                 $val       = __FUNCTION__ . '_val';
    125 
    126                 // Make two filters with different priorities.
    127                 add_filter( $hook_name, array( $a, 'filter' ), 10 );
    128                 add_filter( $hook_name, array( $a, 'filter2' ), 9 );
    129                 $this->assertSame( $val, apply_filters( $hook_name, $val ) );
    130 
    131                 // There should be two events, one per filter.
    132                 $this->assertSame( 2, $a->get_call_count() );
    133 
    134                 $expected = array(
    135                         // 'filter2' is called first because it has priority 9.
    136                         array(
    137                                 'filter'    => 'filter2',
    138                                 'hook_name' => $hook_name,
    139                                 'tag'       => $hook_name, // Back compat.
    140                                 'args'      => array( $val ),
    141                         ),
    142                         // 'filter' is called second.
    143                         array(
    144                                 'filter'    => 'filter',
    145                                 'hook_name' => $hook_name,
    146                                 'tag'       => $hook_name, // Back compat.
    147                                 'args'      => array( $val ),
     121        /**
     122         * @ticket 60193
     123         *
     124         * @dataProvider data_priority_callback_order_with_integers
     125         * @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
     126         *
     127         * @covers ::apply_filters
     128         *
     129         * @param array $priorities {
     130         *     Indexed array of the priorities for the MockAction callbacks.
     131         *
     132         *     @type mixed $0 Priority for 'action' callback.
     133         *     @type mixed $1 Priority for 'action2' callback.
     134         * }
     135         * @param array  $expected_call_order  An array of callback names in expected call order.
     136         * @param string $expected_deprecation Optional. Deprecation message. Default ''.
     137         */
     138        public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) {
     139                $mock      = new MockAction();
     140                $hook_name = __FUNCTION__;
     141
     142                if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
     143                        $this->expectDeprecation();
     144                        $this->expectDeprecationMessage( $expected_deprecation );
     145                }
     146
     147                add_filter( $hook_name, array( $mock, 'filter' ), $priorities[0] );
     148                add_filter( $hook_name, array( $mock, 'filter2' ), $priorities[1] );
     149                apply_filters( $hook_name, __FUNCTION__ . '_val' );
     150
     151                $this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );
     152
     153                $actual_call_order = wp_list_pluck( $mock->get_events(), 'filter' );
     154                $this->assertSame( $expected_call_order, $actual_call_order, 'The filter callback order does not match the expected order' );
     155        }
     156
     157        /**
     158         * Happy path data provider.
     159         *
     160         * @return array[]
     161         */
     162        public function data_priority_callback_order_with_integers() {
     163                return array(
     164                        'int DESC' => array(
     165                                'priorities'          => array( 10, 9 ),
     166                                'expected_call_order' => array( 'filter2', 'filter' ),
     167                        ),
     168                        'int ASC'  => array(
     169                                'priorities'          => array( 9, 10 ),
     170                                'expected_call_order' => array( 'filter', 'filter2' ),
    148171                        ),
    149172                );
    150 
    151                 $this->assertSame( $expected, $a->get_events() );
     173        }
     174
     175        /**
     176         * Unhappy path data provider.
     177         *
     178         * @return array[]
     179         */
     180        public function data_priority_callback_order_with_unhappy_path_nonintegers() {
     181                return array(
     182                        // Numbers as strings and floats.
     183                        'int as string DESC'               => array(
     184                                'priorities'          => array( '10', '9' ),
     185                                'expected_call_order' => array( 'filter2', 'filter' ),
     186                        ),
     187                        'int as string ASC'                => array(
     188                                'priorities'          => array( '9', '10' ),
     189                                'expected_call_order' => array( 'filter', 'filter2' ),
     190                        ),
     191                        'float DESC'                       => array(
     192                                'priorities'           => array( 10.0, 9.5 ),
     193                                'expected_call_order'  => array( 'filter2', 'filter' ),
     194                                'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
     195                        ),
     196                        'float ASC'                        => array(
     197                                'priorities'           => array( 9.5, 10.0 ),
     198                                'expected_call_order'  => array( 'filter', 'filter2' ),
     199                                'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
     200                        ),
     201                        'float as string DESC'             => array(
     202                                'priorities'          => array( '10.0', '9.5' ),
     203                                'expected_call_order' => array( 'filter2', 'filter' ),
     204                        ),
     205                        'float as string ASC'              => array(
     206                                'priorities'          => array( '9.5', '10.0' ),
     207                                'expected_call_order' => array( 'filter', 'filter2' ),
     208                        ),
     209
     210                        // Non-numeric.
     211                        'null'                             => array(
     212                                'priorities'          => array( null, null ),
     213                                'expected_call_order' => array( 'filter', 'filter2' ),
     214                        ),
     215                        'bool DESC'                        => array(
     216                                'priorities'          => array( true, false ),
     217                                'expected_call_order' => array( 'filter2', 'filter' ),
     218                        ),
     219                        'bool ASC'                         => array(
     220                                'priorities'          => array( false, true ),
     221                                'expected_call_order' => array( 'filter', 'filter2' ),
     222                        ),
     223                        'non-numerical string DESC'        => array(
     224                                'priorities'          => array( 'test1', 'test2' ),
     225                                'expected_call_order' => array( 'filter', 'filter2' ),
     226                        ),
     227                        'non-numerical string ASC'         => array(
     228                                'priorities'          => array( 'test1', 'test2' ),
     229                                'expected_call_order' => array( 'filter', 'filter2' ),
     230                        ),
     231                        'int, non-numerical string DESC'   => array(
     232                                'priorities'          => array( 10, 'test' ),
     233                                'expected_call_order' => array( 'filter2', 'filter' ),
     234                        ),
     235                        'int, non-numerical string ASC'    => array(
     236                                'priorities'          => array( 'test', 10 ),
     237                                'expected_call_order' => array( 'filter', 'filter2' ),
     238                        ),
     239                        'float, non-numerical string DESC' => array(
     240                                'priorities'          => array( 10.0, 'test' ),
     241                                'expected_call_order' => array( 'filter2', 'filter' ),
     242                        ),
     243                        'float, non-numerical string ASC'  => array(
     244                                'priorities'          => array( 'test', 10.0 ),
     245                                'expected_call_order' => array( 'filter', 'filter2' ),
     246                        ),
     247                );
    152248        }
    153249
Note: See TracChangeset for help on using the changeset viewer.