-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathdoAction.php
More file actions
372 lines (317 loc) · 12.1 KB
/
doAction.php
File metadata and controls
372 lines (317 loc) · 12.1 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?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() );
}
/**
* @ticket 60193
*
* @dataProvider data_priority_callback_order_with_integers
* @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
*
* @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 = new WP_Hook();
$hook_name = __FUNCTION__;
if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
$this->expectDeprecation();
$this->expectDeprecationMessage( $expected_deprecation );
}
$hook->add_filter( $hook_name, array( $mock, 'action' ), $priorities[0], 1 );
$hook->add_filter( $hook_name, array( $mock, 'action2' ), $priorities[1], 1 );
$hook->do_action( array( '' ) );
$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' );
}
/**
* 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' ),
),
'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' ),
),
// 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' ),
),
);
}
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';
}
/**
* Verify that a callback removing itself during execution does not cause
* the next priority to be skipped.
*
* When a callback is the sole entry at its priority and removes itself
* mid-iteration, resort_active_iterations() repositions the internal
* array pointer. Before the fix, the pointer ended up one position too
* far, causing apply_filters()'s next() call to skip the following
* priority entirely.
*
* @ticket 64653
*/
public function test_self_removing_callback_does_not_skip_next_priority() {
$hook = new WP_Hook();
$hook_name = __FUNCTION__;
$log = array();
$callback_10 = function () use ( &$log ) {
$log[] = 10;
};
// Callback that removes itself -- the only callback at priority 50.
$self_removing = function () use ( &$log, &$self_removing, $hook, $hook_name ) {
$hook->remove_filter( $hook_name, $self_removing, 50 );
$log[] = 50;
};
$callback_100 = function () use ( &$log ) {
$log[] = 100;
};
$hook->add_filter( $hook_name, $callback_10, 10, 0 );
$hook->add_filter( $hook_name, $self_removing, 50, 0 );
$hook->add_filter( $hook_name, $callback_100, 100, 0 );
$hook->do_action( array() );
$this->assertSame( array( 10, 50, 100 ), $log, 'Priority 100 should not be skipped when priority 50 removes itself during iteration.' );
}
/**
* Verify the fix when the self-removing callback is at the first priority.
*
* @ticket 64653
*/
public function test_self_removing_callback_at_lowest_priority() {
$hook = new WP_Hook();
$hook_name = __FUNCTION__;
$log = array();
$self_removing = function () use ( &$log, &$self_removing, $hook, $hook_name ) {
$hook->remove_filter( $hook_name, $self_removing, 10 );
$log[] = 10;
};
$callback_50 = function () use ( &$log ) {
$log[] = 50;
};
$hook->add_filter( $hook_name, $self_removing, 10, 0 );
$hook->add_filter( $hook_name, $callback_50, 50, 0 );
$hook->do_action( array() );
$this->assertSame( array( 10, 50 ), $log, 'Priority 50 should execute when priority 10 removes itself.' );
}
/**
* 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,
);
}
}