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/hooks/doAction.php

    r53804 r57257  
    8686        }
    8787
     88        /**
     89         * @ticket 60193
     90         *
     91         * @dataProvider data_priority_callback_order_with_integers
     92         * @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
     93         *
     94         * @param array $priorities {
     95         *     Indexed array of the priorities for the MockAction callbacks.
     96         *
     97         *     @type mixed $0 Priority for 'action' callback.
     98         *     @type mixed $1 Priority for 'action2' callback.
     99         * }
     100         * @param array  $expected_call_order  An array of callback names in expected call order.
     101         * @param string $expected_deprecation Optional. Deprecation message. Default ''.
     102         */
     103        public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) {
     104                $mock      = new MockAction();
     105                $hook      = new WP_Hook();
     106                $hook_name = __FUNCTION__;
     107
     108                if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
     109                        $this->expectDeprecation();
     110                        $this->expectDeprecationMessage( $expected_deprecation );
     111                }
     112
     113                $hook->add_filter( $hook_name, array( $mock, 'action' ), $priorities[0], 1 );
     114                $hook->add_filter( $hook_name, array( $mock, 'action2' ), $priorities[1], 1 );
     115                $hook->do_action( array( '' ) );
     116
     117                $this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );
     118
     119                $actual_call_order = wp_list_pluck( $mock->get_events(), 'action' );
     120                $this->assertSame( $expected_call_order, $actual_call_order, 'The action callback order does not match the expected order' );
     121        }
     122
     123        /**
     124         * Happy path data provider.
     125         *
     126         * @return array[]
     127         */
     128        public function data_priority_callback_order_with_integers() {
     129                return array(
     130                        'int DESC' => array(
     131                                'priorities'          => array( 10, 9 ),
     132                                'expected_call_order' => array( 'action2', 'action' ),
     133                        ),
     134                        'int ASC'  => array(
     135                                'priorities'          => array( 9, 10 ),
     136                                'expected_call_order' => array( 'action', 'action2' ),
     137                        ),
     138                );
     139        }
     140
     141        /**
     142         * Unhappy path data provider.
     143         *
     144         * @return array[]
     145         */
     146        public function data_priority_callback_order_with_unhappy_path_nonintegers() {
     147                return array(
     148                        // Numbers as strings and floats.
     149                        'int as string DESC'               => array(
     150                                'priorities'          => array( '10', '9' ),
     151                                'expected_call_order' => array( 'action2', 'action' ),
     152                        ),
     153                        'int as string ASC'                => array(
     154                                'priorities'          => array( '9', '10' ),
     155                                'expected_call_order' => array( 'action', 'action2' ),
     156                        ),
     157                        'float DESC'                       => array(
     158                                'priorities'           => array( 10.0, 9.5 ),
     159                                'expected_call_order'  => array( 'action2', 'action' ),
     160                                'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
     161                        ),
     162                        'float ASC'                        => array(
     163                                'priorities'           => array( 9.5, 10.0 ),
     164                                'expected_call_order'  => array( 'action', 'action2' ),
     165                                'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
     166                        ),
     167                        'float as string DESC'             => array(
     168                                'priorities'          => array( '10.0', '9.5' ),
     169                                'expected_call_order' => array( 'action2', 'action' ),
     170                        ),
     171                        'float as string ASC'              => array(
     172                                'priorities'          => array( '9.5', '10.0' ),
     173                                'expected_call_order' => array( 'action', 'action2' ),
     174                        ),
     175
     176                        // Non-numeric.
     177                        'null'                             => array(
     178                                'priorities'          => array( null, null ),
     179                                'expected_call_order' => array( 'action', 'action2' ),
     180                        ),
     181                        'bool DESC'                        => array(
     182                                'priorities'          => array( true, false ),
     183                                'expected_call_order' => array( 'action2', 'action' ),
     184                        ),
     185                        'bool ASC'                         => array(
     186                                'priorities'          => array( false, true ),
     187                                'expected_call_order' => array( 'action', 'action2' ),
     188                        ),
     189                        'non-numerical string DESC'        => array(
     190                                'priorities'          => array( 'test1', 'test2' ),
     191                                'expected_call_order' => array( 'action', 'action2' ),
     192                        ),
     193                        'non-numerical string ASC'         => array(
     194                                'priorities'          => array( 'test1', 'test2' ),
     195                                'expected_call_order' => array( 'action', 'action2' ),
     196                        ),
     197                        'int, non-numerical string DESC'   => array(
     198                                'priorities'          => array( 10, 'test' ),
     199                                'expected_call_order' => array( 'action2', 'action' ),
     200                        ),
     201                        'int, non-numerical string ASC'    => array(
     202                                'priorities'          => array( 'test', 10 ),
     203                                'expected_call_order' => array( 'action', 'action2' ),
     204                        ),
     205                        'float, non-numerical string DESC' => array(
     206                                'priorities'          => array( 10.0, 'test' ),
     207                                'expected_call_order' => array( 'action2', 'action' ),
     208                        ),
     209                        'float, non-numerical string ASC'  => array(
     210                                'priorities'          => array( 'test', 10.0 ),
     211                                'expected_call_order' => array( 'action', 'action2' ),
     212                        ),
     213                );
     214        }
     215
    88216        public function test_do_action_with_no_accepted_args() {
    89217                $callback      = array( $this, '_action_callback' );
Note: See TracChangeset for help on using the changeset viewer.