mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
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
178 lines
5.4 KiB
PHP
178 lines
5.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Test the do_action method of WP_Hook
|
|
*
|
|
* @group hooks
|
|
* @covers WP_Hook::do_action
|
|
*/
|
|
class Tests_Hooks_DoAction extends WP_UnitTestCase {
|
|
private $events = array();
|
|
private $action_output = '';
|
|
private $hook;
|
|
|
|
public function set_up() {
|
|
parent::set_up();
|
|
$this->events = array();
|
|
}
|
|
|
|
public function test_do_action_with_callback() {
|
|
$a = new MockAction();
|
|
$callback = array( $a, 'action' );
|
|
$hook = new WP_Hook();
|
|
$hook_name = __FUNCTION__;
|
|
$priority = 1;
|
|
$accepted_args = 2;
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertSame( 1, $a->get_call_count() );
|
|
}
|
|
|
|
public function test_do_action_with_multiple_calls() {
|
|
$a = new MockAction();
|
|
$callback = array( $a, 'filter' );
|
|
$hook = new WP_Hook();
|
|
$hook_name = __FUNCTION__;
|
|
$priority = 1;
|
|
$accepted_args = 2;
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertSame( 2, $a->get_call_count() );
|
|
}
|
|
|
|
public function test_do_action_with_multiple_callbacks_on_same_priority() {
|
|
$a = new MockAction();
|
|
$b = new MockAction();
|
|
$callback_one = array( $a, 'filter' );
|
|
$callback_two = array( $b, 'filter' );
|
|
$hook = new WP_Hook();
|
|
$hook_name = __FUNCTION__;
|
|
$priority = 1;
|
|
$accepted_args = 2;
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args );
|
|
$hook->add_filter( $hook_name, $callback_two, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertSame( 1, $a->get_call_count() );
|
|
$this->assertSame( 1, $a->get_call_count() );
|
|
}
|
|
|
|
public function test_do_action_with_multiple_callbacks_on_different_priorities() {
|
|
$a = new MockAction();
|
|
$b = new MockAction();
|
|
$callback_one = array( $a, 'filter' );
|
|
$callback_two = array( $b, 'filter' );
|
|
$hook = new WP_Hook();
|
|
$hook_name = __FUNCTION__;
|
|
$priority = 1;
|
|
$accepted_args = 2;
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args );
|
|
$hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertSame( 1, $a->get_call_count() );
|
|
$this->assertSame( 1, $a->get_call_count() );
|
|
}
|
|
|
|
public function test_do_action_with_no_accepted_args() {
|
|
$callback = array( $this, '_action_callback' );
|
|
$hook = new WP_Hook();
|
|
$hook_name = __FUNCTION__;
|
|
$priority = 1;
|
|
$accepted_args = 0;
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertEmpty( $this->events[0]['args'] );
|
|
}
|
|
|
|
public function test_do_action_with_one_accepted_arg() {
|
|
$callback = array( $this, '_action_callback' );
|
|
$hook = new WP_Hook();
|
|
$hook_name = __FUNCTION__;
|
|
$priority = 1;
|
|
$accepted_args = 1;
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertCount( 1, $this->events[0]['args'] );
|
|
}
|
|
|
|
public function test_do_action_with_more_accepted_args() {
|
|
$callback = array( $this, '_action_callback' );
|
|
$hook = new WP_Hook();
|
|
$hook_name = __FUNCTION__;
|
|
$priority = 100;
|
|
$accepted_args = 1000;
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertCount( 1, $this->events[0]['args'] );
|
|
}
|
|
|
|
public function test_do_action_doesnt_change_value() {
|
|
$this->hook = new WP_Hook();
|
|
$this->action_output = '';
|
|
|
|
$this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value1' ), 10, 1 );
|
|
$this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10, 1 );
|
|
$this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value3' ), 11, 1 );
|
|
|
|
$this->hook->do_action( array( 'a' ) );
|
|
|
|
$this->assertSame( 'a1-b1b3-a2a3', $this->action_output );
|
|
}
|
|
|
|
public function _filter_do_action_doesnt_change_value1( $value ) {
|
|
$this->action_output .= $value . 1;
|
|
return 'x1';
|
|
}
|
|
public function _filter_do_action_doesnt_change_value2( $value ) {
|
|
$this->hook->remove_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10 );
|
|
|
|
$this->action_output .= '-';
|
|
$this->hook->do_action( array( 'b' ) );
|
|
$this->action_output .= '-';
|
|
|
|
$this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10, 1 );
|
|
|
|
$this->action_output .= $value . 2;
|
|
|
|
return 'x2';
|
|
}
|
|
|
|
public function _filter_do_action_doesnt_change_value3( $value ) {
|
|
$this->action_output .= $value . 3;
|
|
return 'x3';
|
|
}
|
|
|
|
/**
|
|
* Use this rather than MockAction so we can test callbacks with no args
|
|
*
|
|
* @param mixed ...$args Optional arguments passed to the action.
|
|
*/
|
|
public function _action_callback( ...$args ) {
|
|
$this->events[] = array(
|
|
'action' => __FUNCTION__,
|
|
'args' => $args,
|
|
);
|
|
}
|
|
}
|