Skip to content

Commit

Permalink
Tests: Add hook priority call order tests.
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://develop.svn.wordpress.org/trunk@57257 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Jan 9, 2024
1 parent 20f8b30 commit 52720ef
Show file tree
Hide file tree
Showing 4 changed files with 493 additions and 43 deletions.
138 changes: 118 additions & 20 deletions tests/phpunit/tests/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,35 +229,133 @@ public function test_action_args_with_php4_syntax() {
$this->assertSame( array( $val ), array_pop( $argsvar ) );
}

public function test_action_priority() {
$a = new MockAction();
/**
* @ticket 60193
*
* @dataProvider data_priority_callback_order_with_integers
* @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
*
* @covers ::do_action
*
* @param array $priorities {
* Indexed array of the priorities for the MockAction callbacks.
*
* @type mixed $0 Priority for 'action' callback.
* @type mixed $1 Priority for 'action2' callback.
* }
* @param array $expected_call_order An array of callback names in expected call order.
* @param string $expected_deprecation Optional. Deprecation message. Default ''.
*/
public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) {
$mock = new MockAction();
$hook_name = __FUNCTION__;

add_action( $hook_name, array( &$a, 'action' ), 10 );
add_action( $hook_name, array( &$a, 'action2' ), 9 );
if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
$this->expectDeprecation();
$this->expectDeprecationMessage( $expected_deprecation );
}

add_action( $hook_name, array( $mock, 'action' ), $priorities[0] );
add_action( $hook_name, array( $mock, 'action2' ), $priorities[1] );
do_action( $hook_name );

// Two events, one per action.
$this->assertSame( 2, $a->get_call_count() );
$this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );

$actual_call_order = wp_list_pluck( $mock->get_events(), 'action' );
$this->assertSame( $expected_call_order, $actual_call_order, 'The action callback order does not match the expected order' );
}

$expected = array(
// 'action2' is called first because it has priority 9.
array(
'action' => 'action2',
'hook_name' => $hook_name,
'tag' => $hook_name, // Back compat.
'args' => array( '' ),
/**
* Happy path data provider.
*
* @return array[]
*/
public function data_priority_callback_order_with_integers() {
return array(
'int DESC' => array(
'priorities' => array( 10, 9 ),
'expected_call_order' => array( 'action2', 'action' ),
),
// 'action' is called second.
array(
'action' => 'action',
'hook_name' => $hook_name,
'tag' => $hook_name, // Back compat.
'args' => array( '' ),
'int ASC' => array(
'priorities' => array( 9, 10 ),
'expected_call_order' => array( 'action', 'action2' ),
),
);
}

/**
* Unhappy path data provider.
*
* @return array[]
*/
public function data_priority_callback_order_with_unhappy_path_nonintegers() {
return array(
// Numbers as strings and floats.
'int as string DESC' => array(
'priorities' => array( '10', '9' ),
'expected_call_order' => array( 'action2', 'action' ),
),
'int as string ASC' => array(
'priorities' => array( '9', '10' ),
'expected_call_order' => array( 'action', 'action2' ),
),
'float DESC' => array(
'priorities' => array( 10.0, 9.5 ),
'expected_call_order' => array( 'action2', 'action' ),
'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
),
'float ASC' => array(
'priorities' => array( 9.5, 10.0 ),
'expected_call_order' => array( 'action', 'action2' ),
'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
),
'float as string DESC' => array(
'priorities' => array( '10.0', '9.5' ),
'expected_call_order' => array( 'action2', 'action' ),
),
'float as string ASC' => array(
'priorities' => array( '9.5', '10.0' ),
'expected_call_order' => array( 'action', 'action2' ),
),

$this->assertSame( $expected, $a->get_events() );
// Non-numeric.
'null' => array(
'priorities' => array( null, null ),
'expected_call_order' => array( 'action', 'action2' ),
),
'bool DESC' => array(
'priorities' => array( true, false ),
'expected_call_order' => array( 'action2', 'action' ),
),
'bool ASC' => array(
'priorities' => array( false, true ),
'expected_call_order' => array( 'action', 'action2' ),
),
'non-numerical string DESC' => array(
'priorities' => array( 'test1', 'test2' ),
'expected_call_order' => array( 'action', 'action2' ),
),
'non-numerical string ASC' => array(
'priorities' => array( 'test1', 'test2' ),
'expected_call_order' => array( 'action', 'action2' ),
),
'int, non-numerical string DESC' => array(
'priorities' => array( 10, 'test' ),
'expected_call_order' => array( 'action2', 'action' ),
),
'int, non-numerical string ASC' => array(
'priorities' => array( 'test', 10 ),
'expected_call_order' => array( 'action', 'action2' ),
),
'float, non-numerical string DESC' => array(
'priorities' => array( 10.0, 'test' ),
'expected_call_order' => array( 'action2', 'action' ),
),
'float, non-numerical string ASC' => array(
'priorities' => array( 'test', 10.0 ),
'expected_call_order' => array( 'action', 'action2' ),
),
);
}

/**
Expand Down
142 changes: 119 additions & 23 deletions tests/phpunit/tests/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,133 @@ public function test_filter_args_2() {
$this->assertSame( array( $val ), array_pop( $argsvar2 ) );
}

public function test_filter_priority() {
$a = new MockAction();
/**
* @ticket 60193
*
* @dataProvider data_priority_callback_order_with_integers
* @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
*
* @covers ::apply_filters
*
* @param array $priorities {
* Indexed array of the priorities for the MockAction callbacks.
*
* @type mixed $0 Priority for 'action' callback.
* @type mixed $1 Priority for 'action2' callback.
* }
* @param array $expected_call_order An array of callback names in expected call order.
* @param string $expected_deprecation Optional. Deprecation message. Default ''.
*/
public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) {
$mock = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';

// Make two filters with different priorities.
add_filter( $hook_name, array( $a, 'filter' ), 10 );
add_filter( $hook_name, array( $a, 'filter2' ), 9 );
$this->assertSame( $val, apply_filters( $hook_name, $val ) );
if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
$this->expectDeprecation();
$this->expectDeprecationMessage( $expected_deprecation );
}

add_filter( $hook_name, array( $mock, 'filter' ), $priorities[0] );
add_filter( $hook_name, array( $mock, 'filter2' ), $priorities[1] );
apply_filters( $hook_name, __FUNCTION__ . '_val' );

$this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );

// There should be two events, one per filter.
$this->assertSame( 2, $a->get_call_count() );
$actual_call_order = wp_list_pluck( $mock->get_events(), 'filter' );
$this->assertSame( $expected_call_order, $actual_call_order, 'The filter callback order does not match the expected order' );
}

$expected = array(
// 'filter2' is called first because it has priority 9.
array(
'filter' => 'filter2',
'hook_name' => $hook_name,
'tag' => $hook_name, // Back compat.
'args' => array( $val ),
/**
* Happy path data provider.
*
* @return array[]
*/
public function data_priority_callback_order_with_integers() {
return array(
'int DESC' => array(
'priorities' => array( 10, 9 ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
// 'filter' is called second.
array(
'filter' => 'filter',
'hook_name' => $hook_name,
'tag' => $hook_name, // Back compat.
'args' => array( $val ),
'int ASC' => array(
'priorities' => array( 9, 10 ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
);
}

$this->assertSame( $expected, $a->get_events() );
/**
* Unhappy path data provider.
*
* @return array[]
*/
public function data_priority_callback_order_with_unhappy_path_nonintegers() {
return array(
// Numbers as strings and floats.
'int as string DESC' => array(
'priorities' => array( '10', '9' ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
'int as string ASC' => array(
'priorities' => array( '9', '10' ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'float DESC' => array(
'priorities' => array( 10.0, 9.5 ),
'expected_call_order' => array( 'filter2', 'filter' ),
'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
),
'float ASC' => array(
'priorities' => array( 9.5, 10.0 ),
'expected_call_order' => array( 'filter', 'filter2' ),
'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
),
'float as string DESC' => array(
'priorities' => array( '10.0', '9.5' ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
'float as string ASC' => array(
'priorities' => array( '9.5', '10.0' ),
'expected_call_order' => array( 'filter', 'filter2' ),
),

// Non-numeric.
'null' => array(
'priorities' => array( null, null ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'bool DESC' => array(
'priorities' => array( true, false ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
'bool ASC' => array(
'priorities' => array( false, true ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'non-numerical string DESC' => array(
'priorities' => array( 'test1', 'test2' ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'non-numerical string ASC' => array(
'priorities' => array( 'test1', 'test2' ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'int, non-numerical string DESC' => array(
'priorities' => array( 10, 'test' ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
'int, non-numerical string ASC' => array(
'priorities' => array( 'test', 10 ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'float, non-numerical string DESC' => array(
'priorities' => array( 10.0, 'test' ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
'float, non-numerical string ASC' => array(
'priorities' => array( 'test', 10.0 ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
);
}

/**
Expand Down
Loading

0 comments on commit 52720ef

Please sign in to comment.