Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/wp-includes/abilities-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
* @return WP_Ability|null The registered ability instance on success, `null` on failure.
*/
function wp_register_ability( string $name, array $args ): ?WP_Ability {
if ( ! did_action( 'wp_abilities_api_init' ) ) {
if ( ! doing_action( 'wp_abilities_api_init' ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
Expand Down Expand Up @@ -465,7 +465,7 @@ function wp_get_abilities(): array {
* @return WP_Ability_Category|null The registered ability category instance on success, `null` on failure.
*/
function wp_register_ability_category( string $slug, array $args ): ?WP_Ability_Category {
if ( ! did_action( 'wp_abilities_api_categories_init' ) ) {
if ( ! doing_action( 'wp_abilities_api_categories_init' ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
Expand Down
6 changes: 4 additions & 2 deletions tests/phpunit/tests/abilities-api/wpAbilitiesRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ public function set_up(): void {

remove_all_filters( 'wp_register_ability_args' );

// Fire the init hook to allow test ability category registration.
do_action( 'wp_abilities_api_categories_init' );
// Simulates the Abilities API init hook to allow test ability category registration.
global $wp_current_filter;
$wp_current_filter[] = 'wp_abilities_api_categories_init';
wp_register_ability_category(
'math',
array(
'label' => 'Math',
'description' => 'Mathematical operations and calculations.',
)
);
array_pop( $wp_current_filter );

self::$test_ability_args = array(
'label' => 'Add numbers',
Expand Down
13 changes: 0 additions & 13 deletions tests/phpunit/tests/abilities-api/wpAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ class Tests_Abilities_API_WpAbility extends WP_UnitTestCase {
public function set_up(): void {
parent::set_up();

// Fire the init hook to allow test ability category registration.
do_action( 'wp_abilities_api_categories_init' );
wp_register_ability_category(
'math',
array(
'label' => 'Math',
'description' => 'Mathematical operations and calculations.',
)
);

Comment on lines -21 to -30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn’t mandatory at this level.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then remove wp_unregister_ability_category( 'math' ); from tear_down method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I’ll look into it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feedback applied 👍

self::$test_ability_properties = array(
'label' => 'Calculator',
'description' => 'Calculates the result of math operations.',
Expand Down Expand Up @@ -56,9 +46,6 @@ public function set_up(): void {
* Tear down after each test.
*/
public function tear_down(): void {
// Clean up registered test ability category.
wp_unregister_ability_category( 'math' );

parent::tear_down();
}

Expand Down
70 changes: 41 additions & 29 deletions tests/phpunit/tests/abilities-api/wpRegisterAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class Test_Abilities_API_WpRegisterAbility extends WP_UnitTestCase {
* Set up before each test.
*/
public function set_up(): void {
global $wp_current_filter;

parent::set_up();

// Fire the init hook to allow test ability category registration.
do_action( 'wp_abilities_api_categories_init' );
// Simulate the init hook for ability categories to allow test ability category registration.
$wp_current_filter[] = 'wp_abilities_api_categories_init';
wp_register_ability_category(
'math',
array(
Expand Down Expand Up @@ -86,6 +88,8 @@ public function set_up(): void {
* Tear down after each test.
*/
public function tear_down(): void {
global $wp_current_filter;

foreach ( wp_get_abilities() as $ability ) {
if ( ! str_starts_with( $ability->get_name(), 'test/' ) ) {
continue;
Expand All @@ -100,6 +104,15 @@ public function tear_down(): void {
parent::tear_down();
}

/**
* Simulates the `wp_abilities_api_init` action.
*/
private function simulate_doing_wp_abilities_init_action() {
global $wp_current_filter;

$wp_current_filter[] = 'wp_abilities_api_init';
}

/**
* Tests registering an ability with invalid name.
*
Expand All @@ -108,36 +121,25 @@ public function tear_down(): void {
* @expectedIncorrectUsage WP_Abilities_Registry::register
*/
public function test_register_ability_invalid_name(): void {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

$result = wp_register_ability( 'invalid_name', array() );

$this->assertNull( $result );
}

/**
* Tests registering an ability when `abilities_api_init` action has not fired.
* Tests registering an ability when `wp_abilities_api_init` action has not fired.
*
* @ticket 64098
*
* @expectedIncorrectUsage wp_register_ability
*/
public function test_register_ability_no_abilities_api_init_action(): void {
global $wp_actions;

// Store the original action count.
$original_count = isset( $wp_actions['wp_abilities_api_init'] ) ? $wp_actions['wp_abilities_api_init'] : 0;

// Reset the action count to simulate it not being fired.
unset( $wp_actions['wp_abilities_api_init'] );
$this->assertFalse( doing_action( 'wp_abilities_api_init' ) );

$result = wp_register_ability( self::$test_ability_name, self::$test_ability_args );

// Restore the original action count.
if ( $original_count > 0 ) {
$wp_actions['wp_abilities_api_init'] = $original_count;
}

$this->assertNull( $result );
}

Expand All @@ -151,14 +153,14 @@ public function test_register_ability_no_abilities_api_init_action(): void {
public function test_register_ability_no_init_action(): void {
global $wp_actions;

do_action( 'wp_abilities_api_init' );

// Store the original action count.
$original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0;

// Reset the action count to simulate it not being fired.
unset( $wp_actions['init'] );

$this->simulate_doing_wp_abilities_init_action();

$result = wp_register_ability( self::$test_ability_name, self::$test_ability_args );

// Restore the original action count.
Expand All @@ -175,7 +177,7 @@ public function test_register_ability_no_init_action(): void {
* @ticket 64098
*/
public function test_register_valid_ability(): void {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

$result = wp_register_ability( self::$test_ability_name, self::$test_ability_args );

Expand Down Expand Up @@ -225,7 +227,7 @@ public function test_register_valid_ability(): void {
* @ticket 64098
*/
public function test_register_ability_no_permissions(): void {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

self::$test_ability_args['permission_callback'] = static function (): bool {
return false;
Expand Down Expand Up @@ -260,7 +262,7 @@ public function test_register_ability_no_permissions(): void {
* @ticket 64098
*/
public function test_register_ability_custom_ability_class(): void {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

$result = wp_register_ability(
self::$test_ability_name,
Expand Down Expand Up @@ -302,7 +304,7 @@ public function test_register_ability_custom_ability_class(): void {
* @ticket 64098
*/
public function test_execute_ability_no_input_schema_match(): void {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

$result = wp_register_ability( self::$test_ability_name, self::$test_ability_args );

Expand Down Expand Up @@ -331,7 +333,7 @@ public function test_execute_ability_no_input_schema_match(): void {
* @ticket 64098
*/
public function test_execute_ability_no_output_schema_match(): void {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

self::$test_ability_args['execute_callback'] = static function (): bool {
return true;
Expand Down Expand Up @@ -362,7 +364,7 @@ public function test_execute_ability_no_output_schema_match(): void {
* @ticket 64098
*/
public function test_validate_input_no_input_schema_match(): void {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

$result = wp_register_ability( self::$test_ability_name, self::$test_ability_args );

Expand Down Expand Up @@ -391,7 +393,7 @@ public function test_validate_input_no_input_schema_match(): void {
* @ticket 64098
*/
public function test_permission_callback_receives_input(): void {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

$received_input = null;
self::$test_ability_args['permission_callback'] = static function ( array $input ) use ( &$received_input ): bool {
Expand Down Expand Up @@ -453,6 +455,8 @@ public function test_unregister_ability_no_init_action(): void {
// Reset the action count to simulate it not being fired.
unset( $wp_actions['init'] );

$this->simulate_doing_wp_abilities_init_action();

$result = wp_unregister_ability( self::$test_ability_name );

// Restore the original action count.
Expand All @@ -469,7 +473,7 @@ public function test_unregister_ability_no_init_action(): void {
* @ticket 64098
*/
public function test_unregister_existing_ability() {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

wp_register_ability( self::$test_ability_name, self::$test_ability_args );

Expand Down Expand Up @@ -497,6 +501,8 @@ public function test_get_ability_no_init_action(): void {
// Reset the action count to simulate it not being fired.
unset( $wp_actions['init'] );

$this->simulate_doing_wp_abilities_init_action();

$result = wp_get_ability( self::$test_ability_name );

// Restore the original action count.
Expand All @@ -513,6 +519,8 @@ public function test_get_ability_no_init_action(): void {
* @ticket 64098
*/
public function test_get_existing_ability_using_callback() {
$this->simulate_doing_wp_abilities_init_action();

$name = self::$test_ability_name;
$args = self::$test_ability_args;
$callback = static function ( $instance ) use ( $name, $args ) {
Expand Down Expand Up @@ -556,6 +564,8 @@ public function test_has_ability_no_init_action(): void {
// Reset the action count to simulate it not being fired.
unset( $wp_actions['init'] );

$this->simulate_doing_wp_abilities_init_action();

$result = wp_has_ability( self::$test_ability_name );

// Restore the original action count.
Expand All @@ -572,7 +582,7 @@ public function test_has_ability_no_init_action(): void {
* @ticket 64098
*/
public function test_has_registered_ability() {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

wp_register_ability( self::$test_ability_name, self::$test_ability_args );

Expand All @@ -587,7 +597,7 @@ public function test_has_registered_ability() {
* @ticket 64098
*/
public function test_has_registered_nonexistent_ability() {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

$result = wp_has_ability( 'test/non-existent' );

Expand All @@ -610,6 +620,8 @@ public function test_get_abilities_no_init_action(): void {
// Reset the action count to simulate it not being fired.
unset( $wp_actions['init'] );

$this->simulate_doing_wp_abilities_init_action();

$result = wp_get_abilities();

// Restore the original action count.
Expand All @@ -626,7 +638,7 @@ public function test_get_abilities_no_init_action(): void {
* @ticket 64098
*/
public function test_get_all_registered_abilities() {
do_action( 'wp_abilities_api_init' );
$this->simulate_doing_wp_abilities_init_action();

$ability_one_name = 'test/ability-one';
$ability_one_args = self::$test_ability_args;
Expand Down
Loading
Loading