Skip to content

Commit fd41ccd

Browse files
Abilities API: Use stricter doing_action check when registering.
Applies feedback provided that doing_action would be better check on this situation to avoid developers facing future registration timing issues. Developed in #10452. Props gziolo, jorgefilipecosta, flixos90, mukesh27, jason_the_adams. See #64098. git-svn-id: https://develop.svn.wordpress.org/trunk@61130 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7e36c64 commit fd41ccd

File tree

8 files changed

+150
-98
lines changed

8 files changed

+150
-98
lines changed

src/wp-includes/abilities-api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@
276276
* @return WP_Ability|null The registered ability instance on success, `null` on failure.
277277
*/
278278
function wp_register_ability( string $name, array $args ): ?WP_Ability {
279-
if ( ! did_action( 'wp_abilities_api_init' ) ) {
279+
if ( ! doing_action( 'wp_abilities_api_init' ) ) {
280280
_doing_it_wrong(
281281
__FUNCTION__,
282282
sprintf(
@@ -465,7 +465,7 @@ function wp_get_abilities(): array {
465465
* @return WP_Ability_Category|null The registered ability category instance on success, `null` on failure.
466466
*/
467467
function wp_register_ability_category( string $slug, array $args ): ?WP_Ability_Category {
468-
if ( ! did_action( 'wp_abilities_api_categories_init' ) ) {
468+
if ( ! doing_action( 'wp_abilities_api_categories_init' ) ) {
469469
_doing_it_wrong(
470470
__FUNCTION__,
471471
sprintf(

tests/phpunit/tests/abilities-api/wpAbilitiesRegistry.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ public function set_up(): void {
2929

3030
remove_all_filters( 'wp_register_ability_args' );
3131

32-
// Fire the init hook to allow test ability category registration.
33-
do_action( 'wp_abilities_api_categories_init' );
32+
// Simulates the Abilities API init hook to allow test ability category registration.
33+
global $wp_current_filter;
34+
$wp_current_filter[] = 'wp_abilities_api_categories_init';
3435
wp_register_ability_category(
3536
'math',
3637
array(
3738
'label' => 'Math',
3839
'description' => 'Mathematical operations and calculations.',
3940
)
4041
);
42+
array_pop( $wp_current_filter );
4143

4244
self::$test_ability_args = array(
4345
'label' => 'Add numbers',

tests/phpunit/tests/abilities-api/wpAbility.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ class Tests_Abilities_API_WpAbility extends WP_UnitTestCase {
1818
public function set_up(): void {
1919
parent::set_up();
2020

21-
// Fire the init hook to allow test ability category registration.
22-
do_action( 'wp_abilities_api_categories_init' );
23-
wp_register_ability_category(
24-
'math',
25-
array(
26-
'label' => 'Math',
27-
'description' => 'Mathematical operations and calculations.',
28-
)
29-
);
30-
3121
self::$test_ability_properties = array(
3222
'label' => 'Calculator',
3323
'description' => 'Calculates the result of math operations.',
@@ -56,9 +46,6 @@ public function set_up(): void {
5646
* Tear down after each test.
5747
*/
5848
public function tear_down(): void {
59-
// Clean up registered test ability category.
60-
wp_unregister_ability_category( 'math' );
61-
6249
parent::tear_down();
6350
}
6451

tests/phpunit/tests/abilities-api/wpRegisterAbility.php

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)