-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathactions.php
More file actions
495 lines (415 loc) · 14.4 KB
/
actions.php
File metadata and controls
495 lines (415 loc) · 14.4 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
/**
* Action related tests
*
* @group plugins
* @since 0.1
*/
class Plugin_Actions_Tests extends PHPUnit_Framework_TestCase {
/**
* Check adding an action with a simple function name
*
* Syntax tested: yourls_add_action( $hook, 'func_name' );
*
* @since 0.1
*/
public function test_add_action_funcname() {
// Random function name
$hook = rand_str();
$this->assertFalse( yourls_has_action( $hook ) );
yourls_add_action( $hook, rand_str() );
$this->assertTrue( yourls_has_action( $hook ) );
// Specific function name to test with yourls_do_action
$hook = rand_str();
$this->assertFalse( yourls_has_action( $hook ) );
yourls_add_action( $hook, 'change_one_global' );
$this->assertTrue( yourls_has_action( $hook ) );
return $hook;
}
/**
* Remove an action
*
* @since 0.1
*/
public function test_remove_action() {
$hook = rand_str();
$action = rand_str();
$this->assertFalse( yourls_has_action( $hook ) );
yourls_add_action( $hook, $action );
$this->assertTrue( yourls_has_action( $hook ) );
$this->assertSame( 10, yourls_has_action( $hook, $action ) );
yourls_remove_action( $hook, $action );
$this->assertFalse( yourls_has_action( $hook ) );
}
/**
* Add several actions on the same hook
*
* @since 0.1
*/
public function test_add_several_actions_default_priority() {
$hook = rand_str();
$times = mt_rand( 5, 15 );
for ( $i = 1; $i <= $times; $i++ ) {
yourls_add_action( $hook, rand_str() );
}
$this->assertTrue( yourls_has_action( $hook ) );
global $yourls_filters;
$this->assertSame( $times, count( $yourls_filters[ $hook ][10] ) );
}
/**
* Add several actions on the same hook with different priorities
*
* @since 0.1
*/
public function test_add_several_actions_random_priorities() {
$hook = rand_str();
$times = mt_rand( 5, 15 );
for ( $i = 1; $i <= $times; $i++ ) {
yourls_add_action( $hook, rand_str(), mt_rand( 1, 10 ) );
}
$this->assertTrue( yourls_has_action( $hook ) );
global $yourls_filters;
$total = 0;
foreach( $yourls_filters[ $hook ] as $prio => $action ) {
$total += count( $yourls_filters[ $hook ][ $prio ] );
}
$this->assertSame( $times, $total );
}
/**
* Remove all actions on a hook
*
* @since 0.1
*/
public function test_remove_all_actions() {
$hook = rand_str();
$times = mt_rand( 5, 15 );
for ( $i = 1; $i <= $times; $i++ ) {
yourls_add_action( $hook, rand_str() );
}
$this->assertTrue( yourls_has_action( $hook ) );
yourls_remove_all_actions( $hook );
$this->assertFalse( yourls_has_action( $hook ) );
}
/**
* Remove all actions with random priorities on a hook
*
* @since 0.1
*/
public function test_remove_all_actions_random_prio() {
$hook = rand_str();
$times = mt_rand( 5, 15 );
for ( $i = 1; $i <= $times; $i++ ) {
yourls_add_action( $hook, rand_str(), mt_rand( 1, 10 ) );
}
$this->assertTrue( yourls_has_action( $hook ) );
yourls_remove_all_actions( $hook );
$this->assertFalse( yourls_has_action( $hook ) );
}
/**
* Remove all actions with specific priority
*
* @since 0.1
*/
public function test_remove_only_actions_with_given_prio() {
$hook = rand_str();
$priorities = array();
$times = mt_rand( 10, 30 );
for ( $i = 1; $i <= $times; $i++ ) {
$prio = mt_rand( 1, 100 );
$priorities[] = $prio;
yourls_add_action( $hook, rand_str(), $prio );
}
$this->assertTrue( yourls_has_action( $hook ) );
global $yourls_filters;
// Pick a random number of randomly picked priorities (but not all of them)
$priorities = array_unique( $priorities );
$random_priorities = (array) array_rand( $priorities, mt_rand( 1, count( $priorities ) - 1 ) );
// Count how many we're supposed to remove
$removed = 0;
foreach( $yourls_filters[ $hook ] as $prio => $action ) {
if( in_array( $prio, $random_priorities ) )
$removed += count( $yourls_filters[ $hook ][ $prio ] );
}
// Remove the randomly picked priorities
foreach( $random_priorities as $random_priority ) {
yourls_remove_all_actions( $hook, $random_priority );
}
$this->assertTrue( yourls_has_action( $hook ) );
// Count how many are left
$remaining = 0;
foreach( $yourls_filters[ $hook ] as $prio => $action ) {
$remaining += count( $yourls_filters[ $hook ][ $prio ] );
}
$this->assertSame( $remaining, $times - $removed );
}
/**
* Check 'doing' an action hooked with a simple function name
*
* @depends test_add_action_funcname
* @since 0.1
*/
public function test_do_action_funcname( $hook ) {
$var_name = rand_str();
$var_value = rand_str();
$GLOBALS['test_var'] = $var_name;
$GLOBALS[ $var_name ] = $var_value;
$this->assertSame( $var_value, $GLOBALS[ $var_name ] );
$this->assertSame( 0, yourls_did_action( $hook ) );
yourls_do_action( $hook );
$this->assertSame( 1, yourls_did_action( $hook ) );
$this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
return $hook;
}
/**
* Check we keep correct track of the number of time an action is done
*
* @since 0.1
*/
public function test_do_action_several_times_and_count() {
$hook = rand_str();
$this->assertSame( 0, yourls_did_action( $hook ) );
$times = mt_rand( 5, 15 );
for ( $i = 1; $i <= $times; $i++ ) {
yourls_do_action( $hook );
}
$this->assertSame( $times, yourls_did_action( $hook ) );
}
/**
* Check adding an action with an anonymous function using create_function()
*
* Syntax tested: yourls_add_action( $hook, create_function() );
*
* @since 0.1
*/
public function test_add_action_create_function() {
$hook = rand_str();
$this->assertFalse( yourls_has_action( $hook ) );
yourls_add_action( $hook, function() {
$var_name = $GLOBALS["test_var"]; $GLOBALS[ $var_name ] = rand_str();
} );
$this->assertTrue( yourls_has_action( $hook ) );
return $hook;
}
/**
* Check 'doing' an action hooked with an anonymous function using create_function()
*
* @depends test_add_action_create_function
* @since 0.1
*/
public function test_do_action_create_function( $hook ) {
$var_name = rand_str();
$var_value = rand_str();
$GLOBALS['test_var'] = $var_name;
$GLOBALS[ $var_name ] = $var_value;
$this->assertSame( $var_value, $GLOBALS[ $var_name ] );
$this->assertSame( 0, yourls_did_action( $hook ) );
yourls_do_action( $hook );
$this->assertSame( 1, yourls_did_action( $hook ) );
$this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
return $hook;
}
/**
* Check adding an action with function within class
*
* Syntax tested: yourls_add_action( $hook, 'Class::Function' );
*
* @since 0.1
*/
public function test_add_action_within_class() {
$hook = rand_str();
$this->assertFalse( yourls_has_action( $hook ) );
yourls_add_action( $hook, 'Change_One_Global::change_it' );
$this->assertTrue( yourls_has_action( $hook ) );
return $hook;
}
/**
* Check 'doing' an action hooked with function within class
*
* @depends test_add_action_within_class
* @since 0.1
*/
public function test_do_action_within_class( $hook ) {
$var_name = rand_str();
$var_value = rand_str();
$GLOBALS['test_var'] = $var_name;
$GLOBALS[ $var_name ] = $var_value;
$this->assertSame( $var_value, $GLOBALS[ $var_name ] );
$this->assertSame( 0, yourls_did_action( $hook ) );
yourls_do_action( $hook );
$this->assertSame( 1, yourls_did_action( $hook ) );
$this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
return $hook;
}
/**
* Check adding an action with function within class using an array
*
* Syntax tested: yourls_add_action( $hook, array( 'Class', 'Function' ) );
*
* @since 0.1
*/
public function test_add_action_within_class_array() {
$hook = rand_str();
$this->assertFalse( yourls_has_action( $hook ) );
yourls_add_action( $hook, array( 'Change_One_Global', 'change_it' ) );
$this->assertTrue( yourls_has_action( $hook ) );
return $hook;
}
/**
* Check 'doing' an action hooked with function within class
*
* @depends test_add_action_within_class_array
* @since 0.1
*/
public function test_do_action_within_class_array( $hook ) {
$var_name = rand_str();
$var_value = rand_str();
$GLOBALS['test_var'] = $var_name;
$GLOBALS[ $var_name ] = $var_value;
$this->assertSame( $var_value, $GLOBALS[ $var_name ] );
$this->assertSame( 0, yourls_did_action( $hook ) );
yourls_do_action( $hook );
$this->assertSame( 1, yourls_did_action( $hook ) );
$this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
return $hook;
}
/**
* Check adding an action with function within class instance
*
* Syntax tested: yourls_add_action( $hook, array( $class, 'function' ) );
*
* @since 0.1
*/
public function test_add_action_within_class_instance() {
$hook = rand_str();
$this->assertFalse( yourls_has_action( $hook ) );
yourls_add_action( $hook, array( $this, 'change_one_global' ) );
$this->assertTrue( yourls_has_action( $hook ) );
return $hook;
}
/**
* Check 'doing' an action hooked with function within class instance
*
* @depends test_add_action_within_class_instance
* @since 0.1
*/
public function test_do_action_within_class_instance( $hook ) {
$var_name = rand_str();
$var_value = rand_str();
$GLOBALS['test_var'] = $var_name;
$GLOBALS[ $var_name ] = $var_value;
$this->assertSame( $var_value, $GLOBALS[ $var_name ] );
$this->assertSame( 0, yourls_did_action( $hook ) );
yourls_do_action( $hook );
$this->assertSame( 1, yourls_did_action( $hook ) );
$this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
return $hook;
}
/**
* Check that hooking to 'Class::Method' or array( 'Class', 'Method') is the same
*
* @since 0.1
*/
public function test_add_action_class_and_array() {
$hook = rand_str();
$this->assertFalse( yourls_has_action( $hook ) );
yourls_add_action( $hook, array( 'Class', 'Method' ) );
$this->assertSame( 10, yourls_has_action( $hook, array( 'Class', 'Method' ) ) );
$this->assertSame( 10, yourls_has_action( $hook, 'Class::Method' ) );
}
/**
* Check adding an action with anonymous function using closure
*
* Syntax tested: yourls_add_action( $hook, function(){ // do stuff } );
*
* @since 0.1
*/
public function test_add_action_closure() {
$hook = rand_str();
$this->assertFalse( yourls_has_action( $hook ) );
yourls_add_action( $hook, function() { $var_name = $GLOBALS['test_var']; $GLOBALS[ $var_name ] = rand_str(); } );
$this->assertTrue( yourls_has_action( $hook ) );
return $hook;
}
/**
* Check 'doing' an action hooked with anonymous function using closure
*
* @depends test_add_action_closure
* @since 0.1
*/
public function test_do_action_closure( $hook ) {
$var_name = rand_str();
$var_value = rand_str();
$GLOBALS['test_var'] = $var_name;
$GLOBALS[ $var_name ] = $var_value;
$this->assertSame( $var_value, $GLOBALS[ $var_name ] );
$this->assertSame( 0, yourls_did_action( $hook ) );
yourls_do_action( $hook );
$this->assertSame( 1, yourls_did_action( $hook ) );
$this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
return $hook;
}
/**
* Check that applied function must exist
*
* @expectedException PHPUnit_Framework_Error
* @since 0.1
*/
public function test_function_must_exist_if_applied() {
$hook = rand_str();
yourls_add_action( $hook, rand_str() );
// this will trigger an error, converted to an exception by PHPUnit
yourls_do_action( $hook );
}
/**
* Test yourls_do_action() with multiple params
*
* Note : this test will expectedly fail when YOURLS actions work the same as filters, see issue #1203
*
* @since 0.1
*/
public function test_do_action_no_params() {
$hook = rand_str();
yourls_add_action( $hook, array( $this, 'accept_multiple_params' ) );
$this->expectOutputString( "array (0 => '',)" );
yourls_do_action( $hook );
}
/**
* Test yourls_do_action() with multiple params
*
* Note : this test will expectedly fail when YOURLS actions work the same as filters, see issue #1203
*
* @since 0.1
*/
public function test_do_action_1_params() {
$hook = rand_str();
yourls_add_action( $hook, array( $this, 'accept_multiple_params' ) );
$this->expectOutputString( "array (0 => 'hello',)" );
yourls_do_action( $hook, 'hello' );
}
/**
* Test yourls_do_action() with multiple params
*
* Note : this test will expectedly fail when YOURLS actions work the same as filters, see issue #1203
*
* @since 0.1
*/
public function test_do_action_2_params() {
$hook = rand_str();
yourls_add_action( $hook, array( $this, 'accept_multiple_params' ) );
$this->expectOutputString( "array (0 => 'hello',1 => 'world',)" );
yourls_do_action( $hook, 'hello', 'world' );
}
/**
* Dummy function -- just modifies the value of a global var
*/
public function change_one_global() {
$var_name = $GLOBALS['test_var'];
$GLOBALS[ $var_name ] = rand_str();
}
/**
* Dummy function -- echo in one line arguments passed
*/
public function accept_multiple_params( $args ) {
echo preg_replace( '/\s{2,}|\t|\n|\r/', '', var_export( $args, true ) );;
}
}