From 8ebf642267b631ebdfd709f70672241f9e8f6912 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 6 Oct 2026 20:04:45 +0100 Subject: [PATCH] Abilities API: Restore the hooks the core abilities tests change to register them `set_up_before_class()` removed the test suite's unhook callbacks and added the core registration callbacks, and only `tear_down_after_class()` put the unhook callbacks back. The first test of a run snapshots the hooks that every test is reset to, so when this class ran first, the changed hooks leaked into the rest of the run: later tests registered the core abilities whenever the registry initialized, and `test_get_existing_ability_using_callback` failed because the `site` category was not registered. The hooks are now restored as soon as the core abilities are registered. --- .../abilities-api/wpRegisterCoreAbilities.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php index 85a4e5c1e82e4..92232eee1ad15 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php @@ -30,6 +30,16 @@ public static function set_up_before_class(): void { add_action( 'wp_abilities_api_init', 'wp_register_core_abilities' ); do_action( 'wp_abilities_api_categories_init' ); do_action( 'wp_abilities_api_init' ); + + /* + * Restore the hooks right away instead of after the class. The first test of a run + * snapshots the hooks and every test resets them to that snapshot, so changes left + * here would leak into every later test whenever this class runs first. + */ + remove_action( 'wp_abilities_api_categories_init', 'wp_register_core_ability_categories' ); + remove_action( 'wp_abilities_api_init', 'wp_register_core_abilities' ); + add_action( 'wp_abilities_api_categories_init', '_unhook_core_ability_categories_registration', 1 ); + add_action( 'wp_abilities_api_init', '_unhook_core_abilities_registration', 1 ); } /** @@ -38,10 +48,6 @@ public static function set_up_before_class(): void { * @since 6.9.0 */ public static function tear_down_after_class(): void { - // Re-add the unhook functions for subsequent tests. - add_action( 'wp_abilities_api_categories_init', '_unhook_core_ability_categories_registration', 1 ); - add_action( 'wp_abilities_api_init', '_unhook_core_abilities_registration', 1 ); - // Remove the core abilities and their categories. foreach ( wp_get_abilities() as $ability ) { wp_unregister_ability( $ability->get_name() );