Tests: Update the terminology used for action or filter names in hook tests.

This replaces the `$tag` variables with `$hook_name`, to match the core function signatures.

Follow-up to [24/tests], [62/tests], [866/tests], [1294/tests], [38571], [50807].

See #55652.

git-svn-id: https://develop.svn.wordpress.org/trunk@53804 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2022-07-31 15:03:46 +00:00
parent 65e266ede6
commit 0ec5fb339b
14 changed files with 411 additions and 403 deletions
+144 -138
View File
@@ -11,16 +11,16 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::do_action
*/
public function test_simple_action() {
$a = new MockAction();
$tag = __FUNCTION__;
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( $tag, array( &$a, 'action' ) );
do_action( $tag );
add_action( $hook_name, array( &$a, 'action' ) );
do_action( $hook_name );
// Only one event occurred for the hook, with empty args.
$this->assertSame( 1, $a->get_call_count() );
// Only our hook was called.
$this->assertSame( array( $tag ), $a->get_tags() );
$this->assertSame( array( $hook_name ), $a->get_tags() );
$argsvar = $a->get_args();
$args = array_pop( $argsvar );
@@ -31,21 +31,21 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::remove_action
*/
public function test_remove_action() {
$a = new MockAction();
$tag = __FUNCTION__;
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( $tag, array( &$a, 'action' ) );
do_action( $tag );
add_action( $hook_name, array( &$a, 'action' ) );
do_action( $hook_name );
// Make sure our hook was called correctly.
$this->assertSame( 1, $a->get_call_count() );
$this->assertSame( array( $tag ), $a->get_tags() );
$this->assertSame( array( $hook_name ), $a->get_tags() );
// Now remove the action, do it again, and make sure it's not called this time.
remove_action( $tag, array( &$a, 'action' ) );
do_action( $tag );
remove_action( $hook_name, array( &$a, 'action' ) );
do_action( $hook_name );
$this->assertSame( 1, $a->get_call_count() );
$this->assertSame( array( $tag ), $a->get_tags() );
$this->assertSame( array( $hook_name ), $a->get_tags() );
}
@@ -53,17 +53,19 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::has_action
*/
public function test_has_action() {
$tag = __FUNCTION__;
$func = __FUNCTION__ . '_func';
$hook_name = __FUNCTION__;
$callback = __FUNCTION__ . '_func';
$this->assertFalse( has_action( $tag, $func ) );
$this->assertFalse( has_action( $tag ) );
add_action( $tag, $func );
$this->assertSame( 10, has_action( $tag, $func ) );
$this->assertTrue( has_action( $tag ) );
remove_action( $tag, $func );
$this->assertFalse( has_action( $tag, $func ) );
$this->assertFalse( has_action( $tag ) );
$this->assertFalse( has_action( $hook_name, $callback ) );
$this->assertFalse( has_action( $hook_name ) );
add_action( $hook_name, $callback );
$this->assertSame( 10, has_action( $hook_name, $callback ) );
$this->assertTrue( has_action( $hook_name ) );
remove_action( $hook_name, $callback );
$this->assertFalse( has_action( $hook_name, $callback ) );
$this->assertFalse( has_action( $hook_name ) );
}
/**
@@ -72,15 +74,15 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::do_action
*/
public function test_multiple_actions() {
$a1 = new MockAction();
$a2 = new MockAction();
$tag = __FUNCTION__;
$a1 = new MockAction();
$a2 = new MockAction();
$hook_name = __FUNCTION__;
// Add both actions to the hook.
add_action( $tag, array( &$a1, 'action' ) );
add_action( $tag, array( &$a2, 'action' ) );
add_action( $hook_name, array( &$a1, 'action' ) );
add_action( $hook_name, array( &$a2, 'action' ) );
do_action( $tag );
do_action( $hook_name );
// Both actions called once each.
$this->assertSame( 1, $a1->get_call_count() );
@@ -93,13 +95,13 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::do_action
*/
public function test_action_args_1() {
$a = new MockAction();
$tag = __FUNCTION__;
$val = __FUNCTION__ . '_val';
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';
add_action( $tag, array( &$a, 'action' ) );
add_action( $hook_name, array( &$a, 'action' ) );
// Call the action with a single argument.
do_action( $tag, $val );
do_action( $hook_name, $val );
$call_count = $a->get_call_count();
$this->assertSame( 1, $call_count );
@@ -113,17 +115,17 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::do_action
*/
public function test_action_args_2() {
$a1 = new MockAction();
$a2 = new MockAction();
$tag = __FUNCTION__;
$val1 = __FUNCTION__ . '_val1';
$val2 = __FUNCTION__ . '_val2';
$a1 = new MockAction();
$a2 = new MockAction();
$hook_name = __FUNCTION__;
$val1 = __FUNCTION__ . '_val1';
$val2 = __FUNCTION__ . '_val2';
// $a1 accepts two arguments, $a2 doesn't.
add_action( $tag, array( &$a1, 'action' ), 10, 2 );
add_action( $tag, array( &$a2, 'action' ) );
add_action( $hook_name, array( &$a1, 'action' ), 10, 2 );
add_action( $hook_name, array( &$a2, 'action' ) );
// Call the action with two arguments.
do_action( $tag, $val1, $val2 );
do_action( $hook_name, $val1, $val2 );
$call_count = $a1->get_call_count();
// $a1 should be called with both args.
@@ -147,19 +149,19 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::do_action
*/
public function test_action_args_3() {
$a1 = new MockAction();
$a2 = new MockAction();
$a3 = new MockAction();
$tag = __FUNCTION__;
$val1 = __FUNCTION__ . '_val1';
$val2 = __FUNCTION__ . '_val2';
$a1 = new MockAction();
$a2 = new MockAction();
$a3 = new MockAction();
$hook_name = __FUNCTION__;
$val1 = __FUNCTION__ . '_val1';
$val2 = __FUNCTION__ . '_val2';
// $a1 accepts two arguments, $a2 doesn't, $a3 accepts two arguments.
add_action( $tag, array( &$a1, 'action' ), 10, 2 );
add_action( $tag, array( &$a2, 'action' ) );
add_action( $tag, array( &$a3, 'action' ), 10, 2 );
add_action( $hook_name, array( &$a1, 'action' ), 10, 2 );
add_action( $hook_name, array( &$a2, 'action' ) );
add_action( $hook_name, array( &$a3, 'action' ), 10, 2 );
// Call the action with two arguments.
do_action( $tag, $val1, $val2 );
do_action( $hook_name, $val1, $val2 );
$call_count = $a1->get_call_count();
// $a1 should be called with both args.
@@ -186,13 +188,13 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::do_action
*/
public function test_action_args_with_php4_syntax() {
$a = new MockAction();
$tag = __FUNCTION__;
$val = new stdClass();
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = new stdClass();
add_action( $tag, array( &$a, 'action' ) );
add_action( $hook_name, array( &$a, 'action' ) );
// Call the action with PHP 4 notation for passing object by reference.
do_action( $tag, array( &$val ) );
do_action( $hook_name, array( &$val ) );
$call_count = $a->get_call_count();
$argsvar = $a->get_args();
@@ -200,12 +202,12 @@ class Tests_Actions extends WP_UnitTestCase {
}
public function test_action_priority() {
$a = new MockAction();
$tag = __FUNCTION__;
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( $tag, array( &$a, 'action' ), 10 );
add_action( $tag, array( &$a, 'action2' ), 9 );
do_action( $tag );
add_action( $hook_name, array( &$a, 'action' ), 10 );
add_action( $hook_name, array( &$a, 'action2' ), 9 );
do_action( $hook_name );
// Two events, one per action.
$this->assertSame( 2, $a->get_call_count() );
@@ -214,13 +216,13 @@ class Tests_Actions extends WP_UnitTestCase {
// 'action2' is called first because it has priority 9.
array(
'action' => 'action2',
'tag' => $tag,
'tag' => $hook_name,
'args' => array( '' ),
),
// 'action' is called second.
array(
'action' => 'action',
'tag' => $tag,
'tag' => $hook_name,
'args' => array( '' ),
),
);
@@ -232,23 +234,23 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::did_action
*/
public function test_did_action() {
$tag1 = 'action1';
$tag2 = 'action2';
$hook_name1 = 'action1';
$hook_name2 = 'action2';
// Do action $tag1 but not $tag2.
do_action( $tag1 );
$this->assertSame( 1, did_action( $tag1 ) );
$this->assertSame( 0, did_action( $tag2 ) );
// Do action $hook_name1 but not $hook_name2.
do_action( $hook_name1 );
$this->assertSame( 1, did_action( $hook_name1 ) );
$this->assertSame( 0, did_action( $hook_name2 ) );
// Do action $tag2 10 times.
// Do action $hook_name2 10 times.
$count = 10;
for ( $i = 0; $i < $count; $i++ ) {
do_action( $tag2 );
do_action( $hook_name2 );
}
// $tag1's count hasn't changed, $tag2 should be correct.
$this->assertSame( 1, did_action( $tag1 ) );
$this->assertSame( $count, did_action( $tag2 ) );
// $hook_name1's count hasn't changed, $hook_name2 should be correct.
$this->assertSame( 1, did_action( $hook_name1 ) );
$this->assertSame( $count, did_action( $hook_name2 ) );
}
@@ -256,23 +258,23 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::do_action
*/
public function test_all_action() {
$a = new MockAction();
$tag1 = __FUNCTION__ . '_1';
$tag2 = __FUNCTION__ . '_2';
$a = new MockAction();
$hook_name1 = __FUNCTION__ . '_1';
$hook_name2 = __FUNCTION__ . '_2';
// Add an 'all' action.
add_action( 'all', array( &$a, 'action' ) );
$this->assertSame( 10, has_filter( 'all', array( &$a, 'action' ) ) );
// Do some actions.
do_action( $tag1 );
do_action( $tag2 );
do_action( $tag1 );
do_action( $tag1 );
do_action( $hook_name1 );
do_action( $hook_name2 );
do_action( $hook_name1 );
do_action( $hook_name1 );
// Our action should have been called once for each tag.
$this->assertSame( 4, $a->get_call_count() );
// Only our hook was called.
$this->assertSame( array( $tag1, $tag2, $tag1, $tag1 ), $a->get_tags() );
$this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_tags() );
remove_action( 'all', array( &$a, 'action' ) );
$this->assertFalse( has_filter( 'all', array( &$a, 'action' ) ) );
@@ -283,36 +285,36 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::remove_action
*/
public function test_remove_all_action() {
$a = new MockAction();
$tag = __FUNCTION__;
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( 'all', array( &$a, 'action' ) );
$this->assertSame( 10, has_filter( 'all', array( &$a, 'action' ) ) );
do_action( $tag );
do_action( $hook_name );
// Make sure our hook was called correctly.
$this->assertSame( 1, $a->get_call_count() );
$this->assertSame( array( $tag ), $a->get_tags() );
$this->assertSame( array( $hook_name ), $a->get_tags() );
// Now remove the action, do it again, and make sure it's not called this time.
remove_action( 'all', array( &$a, 'action' ) );
$this->assertFalse( has_filter( 'all', array( &$a, 'action' ) ) );
do_action( $tag );
do_action( $hook_name );
$this->assertSame( 1, $a->get_call_count() );
$this->assertSame( array( $tag ), $a->get_tags() );
$this->assertSame( array( $hook_name ), $a->get_tags() );
}
/**
* @covers ::do_action_ref_array
*/
public function test_action_ref_array() {
$obj = new stdClass();
$a = new MockAction();
$tag = __FUNCTION__;
$obj = new stdClass();
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( $tag, array( &$a, 'action' ) );
add_action( $hook_name, array( &$a, 'action' ) );
do_action_ref_array( $tag, array( &$obj ) );
do_action_ref_array( $hook_name, array( &$obj ) );
$args = $a->get_args();
$this->assertSame( $args[0][0], $obj );
@@ -329,12 +331,12 @@ class Tests_Actions extends WP_UnitTestCase {
public function test_action_keyed_array() {
$a = new MockAction();
$tag = __FUNCTION__;
$hook_name = __FUNCTION__;
add_action( $tag, array( &$a, 'action' ) );
add_action( $hook_name, array( &$a, 'action' ) );
$context = array( 'key1' => 'val1' );
do_action( $tag, $context );
do_action( $hook_name, $context );
$args = $a->get_args();
$this->assertSame( $args[0][0], $context );
@@ -343,7 +345,7 @@ class Tests_Actions extends WP_UnitTestCase {
'key2' => 'val2',
'key3' => 'val3',
);
do_action( $tag, $context2 );
do_action( $hook_name, $context2 );
$args = $a->get_args();
$this->assertSame( $args[1][0], $context2 );
@@ -369,14 +371,14 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::do_action
*/
public function test_action_recursion() {
$tag = __FUNCTION__;
$a = new MockAction();
$b = new MockAction();
$hook_name = __FUNCTION__;
$a = new MockAction();
$b = new MockAction();
add_action( $tag, array( $a, 'action' ), 11, 1 );
add_action( $tag, array( $b, 'action' ), 13, 1 );
add_action( $tag, array( $this, 'action_that_causes_recursion' ), 12, 1 );
do_action( $tag, $tag );
add_action( $hook_name, array( $a, 'action' ), 11, 1 );
add_action( $hook_name, array( $b, 'action' ), 13, 1 );
add_action( $hook_name, array( $this, 'action_that_causes_recursion' ), 12, 1 );
do_action( $hook_name, $hook_name );
$this->assertSame( 2, $a->get_call_count(), 'recursive actions should call all callbacks with earlier priority' );
$this->assertSame( 2, $b->get_call_count(), 'recursive actions should call callbacks with later priority' );
@@ -385,11 +387,11 @@ class Tests_Actions extends WP_UnitTestCase {
/**
* @covers ::do_action
*/
public function action_that_causes_recursion( $tag ) {
public function action_that_causes_recursion( $hook_name ) {
static $recursing = false;
if ( ! $recursing ) {
$recursing = true;
do_action( $tag, $tag );
do_action( $hook_name, $hook_name );
}
$recursing = false;
}
@@ -402,19 +404,19 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::add_action
*/
public function test_action_callback_manipulation_while_running() {
$tag = __FUNCTION__;
$a = new MockAction();
$b = new MockAction();
$c = new MockAction();
$d = new MockAction();
$e = new MockAction();
$hook_name = __FUNCTION__;
$a = new MockAction();
$b = new MockAction();
$c = new MockAction();
$d = new MockAction();
$e = new MockAction();
add_action( $tag, array( $a, 'action' ), 11, 2 );
add_action( $tag, array( $this, 'action_that_manipulates_a_running_hook' ), 12, 2 );
add_action( $tag, array( $b, 'action' ), 12, 2 );
add_action( $hook_name, array( $a, 'action' ), 11, 2 );
add_action( $hook_name, array( $this, 'action_that_manipulates_a_running_hook' ), 12, 2 );
add_action( $hook_name, array( $b, 'action' ), 12, 2 );
do_action( $tag, $tag, array( $a, $b, $c, $d, $e ) );
do_action( $tag, $tag, array( $a, $b, $c, $d, $e ) );
do_action( $hook_name, $hook_name, array( $a, $b, $c, $d, $e ) );
do_action( $hook_name, $hook_name, array( $a, $b, $c, $d, $e ) );
$this->assertSame( 2, $a->get_call_count(), 'callbacks should run unless otherwise instructed' );
$this->assertSame( 1, $b->get_call_count(), 'callback removed by same priority callback should still get called' );
@@ -423,11 +425,11 @@ class Tests_Actions extends WP_UnitTestCase {
$this->assertSame( 1, $e->get_call_count(), 'callback added by later priority callback should not get called' );
}
public function action_that_manipulates_a_running_hook( $tag, $mocks ) {
remove_action( $tag, array( $mocks[1], 'action' ), 12, 2 );
add_action( $tag, array( $mocks[2], 'action' ), 12, 2 );
add_action( $tag, array( $mocks[3], 'action' ), 13, 2 );
add_action( $tag, array( $mocks[4], 'action' ), 10, 2 );
public function action_that_manipulates_a_running_hook( $hook_name, $mocks ) {
remove_action( $hook_name, array( $mocks[1], 'action' ), 12, 2 );
add_action( $hook_name, array( $mocks[2], 'action' ), 12, 2 );
add_action( $hook_name, array( $mocks[3], 'action' ), 13, 2 );
add_action( $hook_name, array( $mocks[4], 'action' ), 10, 2 );
}
/**
@@ -439,12 +441,12 @@ class Tests_Actions extends WP_UnitTestCase {
* @covers ::remove_filter
*/
public function test_remove_anonymous_callback() {
$tag = __FUNCTION__;
$a = new MockAction();
add_action( $tag, array( $a, 'action' ), 12, 1 );
$this->assertTrue( has_action( $tag ) );
$hook_name = __FUNCTION__;
$a = new MockAction();
add_action( $hook_name, array( $a, 'action' ), 12, 1 );
$this->assertTrue( has_action( $hook_name ) );
$hook = $GLOBALS['wp_filter'][ $tag ];
$hook = $GLOBALS['wp_filter'][ $hook_name ];
// From http://wordpress.stackexchange.com/a/57088/6445
foreach ( $hook as $priority => $filter ) {
@@ -454,7 +456,7 @@ class Tests_Actions extends WP_UnitTestCase {
&& 'action' === $function['function'][1]
) {
remove_filter(
$tag,
$hook_name,
array( $function['function'][0], 'action' ),
$priority
);
@@ -462,7 +464,7 @@ class Tests_Actions extends WP_UnitTestCase {
}
}
$this->assertFalse( has_action( $tag ) );
$this->assertFalse( has_action( $hook_name ) );
}
@@ -477,23 +479,24 @@ class Tests_Actions extends WP_UnitTestCase {
*/
public function test_array_access_of_wp_filter_global() {
global $wp_filter;
$tag = __FUNCTION__;
add_action( $tag, '__return_null', 11, 1 );
$hook_name = __FUNCTION__;
$this->assertArrayHasKey( 11, $wp_filter[ $tag ] );
$this->assertArrayHasKey( '__return_null', $wp_filter[ $tag ][11] );
add_action( $hook_name, '__return_null', 11, 1 );
unset( $wp_filter[ $tag ][11] );
$this->assertFalse( has_action( $tag, '__return_null' ) );
$this->assertArrayHasKey( 11, $wp_filter[ $hook_name ] );
$this->assertArrayHasKey( '__return_null', $wp_filter[ $hook_name ][11] );
$wp_filter[ $tag ][11] = array(
unset( $wp_filter[ $hook_name ][11] );
$this->assertFalse( has_action( $hook_name, '__return_null' ) );
$wp_filter[ $hook_name ][11] = array(
'__return_null' => array(
'function' => '__return_null',
'accepted_args' => 1,
),
);
$this->assertSame( 11, has_action( $tag, '__return_null' ) );
$this->assertSame( 11, has_action( $hook_name, '__return_null' ) );
}
/**
@@ -505,6 +508,7 @@ class Tests_Actions extends WP_UnitTestCase {
*/
public function test_current_action() {
global $wp_current_filter;
$wp_current_filter[] = 'first';
$wp_current_filter[] = 'second'; // Let's say a second action was invoked.
@@ -518,6 +522,7 @@ class Tests_Actions extends WP_UnitTestCase {
*/
public function test_doing_filter() {
global $wp_current_filter;
$wp_current_filter = array(); // Set to an empty array first.
$this->assertFalse( doing_filter() ); // No filter is passed in, and no filter is being processed.
@@ -539,6 +544,7 @@ class Tests_Actions extends WP_UnitTestCase {
*/
public function test_doing_action() {
global $wp_current_filter;
$wp_current_filter = array(); // Set to an empty array first.
$this->assertFalse( doing_action() ); // No action is passed in, and no filter is being processed.