forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoAction.php
More file actions
177 lines (143 loc) · 5.53 KB
/
doAction.php
File metadata and controls
177 lines (143 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?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 setUp() {
parent::setUp();
$this->events = array();
}
public function test_do_action_with_callback() {
$a = new MockAction();
$callback = array( $a, 'action' );
$hook = new WP_Hook();
$tag = __FUNCTION__;
$priority = rand( 1, 100 );
$accepted_args = rand( 1, 100 );
$arg = __FUNCTION__ . '_arg';
$hook->add_filter( $tag, $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();
$tag = __FUNCTION__;
$priority = rand( 1, 100 );
$accepted_args = rand( 1, 100 );
$arg = __FUNCTION__ . '_arg';
$hook->add_filter( $tag, $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();
$tag = __FUNCTION__;
$priority = rand( 1, 100 );
$accepted_args = rand( 1, 100 );
$arg = __FUNCTION__ . '_arg';
$hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
$hook->add_filter( $tag, $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();
$tag = __FUNCTION__;
$priority = rand( 1, 100 );
$accepted_args = rand( 1, 100 );
$arg = __FUNCTION__ . '_arg';
$hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
$hook->add_filter( $tag, $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();
$tag = __FUNCTION__;
$priority = rand( 1, 100 );
$accepted_args = 0;
$arg = __FUNCTION__ . '_arg';
$hook->add_filter( $tag, $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();
$tag = __FUNCTION__;
$priority = rand( 1, 100 );
$accepted_args = 1;
$arg = __FUNCTION__ . '_arg';
$hook->add_filter( $tag, $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();
$tag = __FUNCTION__;
$priority = rand( 1, 100 );
$accepted_args = 1000;
$arg = __FUNCTION__ . '_arg';
$hook->add_filter( $tag, $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,
);
}
}