@@ -29,10 +29,12 @@ class Test_Abilities_API_WpRegisterAbility extends WP_UnitTestCase {
2929 * Set up before each test.
3030 */
3131 public function set_up (): void {
32+ global $ wp_current_filter ;
33+
3234 parent ::set_up ();
3335
34- // Fire the init hook to allow test ability category registration.
35- do_action ( 'wp_abilities_api_categories_init ' ) ;
36+ // Simulate the init hook for ability categories to allow test ability category registration.
37+ $ wp_current_filter [] = 'wp_abilities_api_categories_init ' ;
3638 wp_register_ability_category (
3739 'math ' ,
3840 array (
@@ -86,6 +88,8 @@ public function set_up(): void {
8688 * Tear down after each test.
8789 */
8890 public function tear_down (): void {
91+ global $ wp_current_filter ;
92+
8993 foreach ( wp_get_abilities () as $ ability ) {
9094 if ( ! str_starts_with ( $ ability ->get_name (), 'test/ ' ) ) {
9195 continue ;
@@ -100,6 +104,15 @@ public function tear_down(): void {
100104 parent ::tear_down ();
101105 }
102106
107+ /**
108+ * Simulates the `wp_abilities_api_init` action.
109+ */
110+ private function simulate_doing_wp_abilities_init_action () {
111+ global $ wp_current_filter ;
112+
113+ $ wp_current_filter [] = 'wp_abilities_api_init ' ;
114+ }
115+
103116 /**
104117 * Tests registering an ability with invalid name.
105118 *
@@ -108,36 +121,25 @@ public function tear_down(): void {
108121 * @expectedIncorrectUsage WP_Abilities_Registry::register
109122 */
110123 public function test_register_ability_invalid_name (): void {
111- do_action ( ' wp_abilities_api_init ' );
124+ $ this -> simulate_doing_wp_abilities_init_action ( );
112125
113126 $ result = wp_register_ability ( 'invalid_name ' , array () );
114127
115128 $ this ->assertNull ( $ result );
116129 }
117130
118131 /**
119- * Tests registering an ability when `abilities_api_init ` action has not fired.
132+ * Tests registering an ability when `wp_abilities_api_init ` action has not fired.
120133 *
121134 * @ticket 64098
122135 *
123136 * @expectedIncorrectUsage wp_register_ability
124137 */
125138 public function test_register_ability_no_abilities_api_init_action (): void {
126- global $ wp_actions ;
127-
128- // Store the original action count.
129- $ original_count = isset ( $ wp_actions ['wp_abilities_api_init ' ] ) ? $ wp_actions ['wp_abilities_api_init ' ] : 0 ;
130-
131- // Reset the action count to simulate it not being fired.
132- unset( $ wp_actions ['wp_abilities_api_init ' ] );
139+ $ this ->assertFalse ( doing_action ( 'wp_abilities_api_init ' ) );
133140
134141 $ result = wp_register_ability ( self ::$ test_ability_name , self ::$ test_ability_args );
135142
136- // Restore the original action count.
137- if ( $ original_count > 0 ) {
138- $ wp_actions ['wp_abilities_api_init ' ] = $ original_count ;
139- }
140-
141143 $ this ->assertNull ( $ result );
142144 }
143145
@@ -151,14 +153,14 @@ public function test_register_ability_no_abilities_api_init_action(): void {
151153 public function test_register_ability_no_init_action (): void {
152154 global $ wp_actions ;
153155
154- do_action ( 'wp_abilities_api_init ' );
155-
156156 // Store the original action count.
157157 $ original_count = isset ( $ wp_actions ['init ' ] ) ? $ wp_actions ['init ' ] : 0 ;
158158
159159 // Reset the action count to simulate it not being fired.
160160 unset( $ wp_actions ['init ' ] );
161161
162+ $ this ->simulate_doing_wp_abilities_init_action ();
163+
162164 $ result = wp_register_ability ( self ::$ test_ability_name , self ::$ test_ability_args );
163165
164166 // Restore the original action count.
@@ -175,7 +177,7 @@ public function test_register_ability_no_init_action(): void {
175177 * @ticket 64098
176178 */
177179 public function test_register_valid_ability (): void {
178- do_action ( ' wp_abilities_api_init ' );
180+ $ this -> simulate_doing_wp_abilities_init_action ( );
179181
180182 $ result = wp_register_ability ( self ::$ test_ability_name , self ::$ test_ability_args );
181183
@@ -225,7 +227,7 @@ public function test_register_valid_ability(): void {
225227 * @ticket 64098
226228 */
227229 public function test_register_ability_no_permissions (): void {
228- do_action ( ' wp_abilities_api_init ' );
230+ $ this -> simulate_doing_wp_abilities_init_action ( );
229231
230232 self ::$ test_ability_args ['permission_callback ' ] = static function (): bool {
231233 return false ;
@@ -260,7 +262,7 @@ public function test_register_ability_no_permissions(): void {
260262 * @ticket 64098
261263 */
262264 public function test_register_ability_custom_ability_class (): void {
263- do_action ( ' wp_abilities_api_init ' );
265+ $ this -> simulate_doing_wp_abilities_init_action ( );
264266
265267 $ result = wp_register_ability (
266268 self ::$ test_ability_name ,
@@ -302,7 +304,7 @@ public function test_register_ability_custom_ability_class(): void {
302304 * @ticket 64098
303305 */
304306 public function test_execute_ability_no_input_schema_match (): void {
305- do_action ( ' wp_abilities_api_init ' );
307+ $ this -> simulate_doing_wp_abilities_init_action ( );
306308
307309 $ result = wp_register_ability ( self ::$ test_ability_name , self ::$ test_ability_args );
308310
@@ -331,7 +333,7 @@ public function test_execute_ability_no_input_schema_match(): void {
331333 * @ticket 64098
332334 */
333335 public function test_execute_ability_no_output_schema_match (): void {
334- do_action ( ' wp_abilities_api_init ' );
336+ $ this -> simulate_doing_wp_abilities_init_action ( );
335337
336338 self ::$ test_ability_args ['execute_callback ' ] = static function (): bool {
337339 return true ;
@@ -362,7 +364,7 @@ public function test_execute_ability_no_output_schema_match(): void {
362364 * @ticket 64098
363365 */
364366 public function test_validate_input_no_input_schema_match (): void {
365- do_action ( ' wp_abilities_api_init ' );
367+ $ this -> simulate_doing_wp_abilities_init_action ( );
366368
367369 $ result = wp_register_ability ( self ::$ test_ability_name , self ::$ test_ability_args );
368370
@@ -391,7 +393,7 @@ public function test_validate_input_no_input_schema_match(): void {
391393 * @ticket 64098
392394 */
393395 public function test_permission_callback_receives_input (): void {
394- do_action ( ' wp_abilities_api_init ' );
396+ $ this -> simulate_doing_wp_abilities_init_action ( );
395397
396398 $ received_input = null ;
397399 self ::$ test_ability_args ['permission_callback ' ] = static function ( array $ input ) use ( &$ received_input ): bool {
@@ -453,6 +455,8 @@ public function test_unregister_ability_no_init_action(): void {
453455 // Reset the action count to simulate it not being fired.
454456 unset( $ wp_actions ['init ' ] );
455457
458+ $ this ->simulate_doing_wp_abilities_init_action ();
459+
456460 $ result = wp_unregister_ability ( self ::$ test_ability_name );
457461
458462 // Restore the original action count.
@@ -469,7 +473,7 @@ public function test_unregister_ability_no_init_action(): void {
469473 * @ticket 64098
470474 */
471475 public function test_unregister_existing_ability () {
472- do_action ( ' wp_abilities_api_init ' );
476+ $ this -> simulate_doing_wp_abilities_init_action ( );
473477
474478 wp_register_ability ( self ::$ test_ability_name , self ::$ test_ability_args );
475479
@@ -497,6 +501,8 @@ public function test_get_ability_no_init_action(): void {
497501 // Reset the action count to simulate it not being fired.
498502 unset( $ wp_actions ['init ' ] );
499503
504+ $ this ->simulate_doing_wp_abilities_init_action ();
505+
500506 $ result = wp_get_ability ( self ::$ test_ability_name );
501507
502508 // Restore the original action count.
@@ -513,6 +519,8 @@ public function test_get_ability_no_init_action(): void {
513519 * @ticket 64098
514520 */
515521 public function test_get_existing_ability_using_callback () {
522+ $ this ->simulate_doing_wp_abilities_init_action ();
523+
516524 $ name = self ::$ test_ability_name ;
517525 $ args = self ::$ test_ability_args ;
518526 $ callback = static function ( $ instance ) use ( $ name , $ args ) {
@@ -556,6 +564,8 @@ public function test_has_ability_no_init_action(): void {
556564 // Reset the action count to simulate it not being fired.
557565 unset( $ wp_actions ['init ' ] );
558566
567+ $ this ->simulate_doing_wp_abilities_init_action ();
568+
559569 $ result = wp_has_ability ( self ::$ test_ability_name );
560570
561571 // Restore the original action count.
@@ -572,7 +582,7 @@ public function test_has_ability_no_init_action(): void {
572582 * @ticket 64098
573583 */
574584 public function test_has_registered_ability () {
575- do_action ( ' wp_abilities_api_init ' );
585+ $ this -> simulate_doing_wp_abilities_init_action ( );
576586
577587 wp_register_ability ( self ::$ test_ability_name , self ::$ test_ability_args );
578588
@@ -587,7 +597,7 @@ public function test_has_registered_ability() {
587597 * @ticket 64098
588598 */
589599 public function test_has_registered_nonexistent_ability () {
590- do_action ( ' wp_abilities_api_init ' );
600+ $ this -> simulate_doing_wp_abilities_init_action ( );
591601
592602 $ result = wp_has_ability ( 'test/non-existent ' );
593603
@@ -610,6 +620,8 @@ public function test_get_abilities_no_init_action(): void {
610620 // Reset the action count to simulate it not being fired.
611621 unset( $ wp_actions ['init ' ] );
612622
623+ $ this ->simulate_doing_wp_abilities_init_action ();
624+
613625 $ result = wp_get_abilities ();
614626
615627 // Restore the original action count.
@@ -626,7 +638,7 @@ public function test_get_abilities_no_init_action(): void {
626638 * @ticket 64098
627639 */
628640 public function test_get_all_registered_abilities () {
629- do_action ( ' wp_abilities_api_init ' );
641+ $ this -> simulate_doing_wp_abilities_init_action ( );
630642
631643 $ ability_one_name = 'test/ability-one ' ;
632644 $ ability_one_args = self ::$ test_ability_args ;
0 commit comments