Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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