@@ -45,6 +45,14 @@ class Tests_Abilities_API_WpPostTypeAbilitiesRest extends WP_Test_REST_TestCase
4545 * @param WP_UnitTest_Factory $factory Test factory.
4646 */
4747 public static function wpSetUpBeforeClass ( WP_UnitTest_Factory $ factory ): void {
48+ // Unregister any existing abilities and categories to start fresh.
49+ foreach ( wp_get_abilities () as $ ability ) {
50+ wp_unregister_ability ( $ ability ->get_name () );
51+ }
52+ foreach ( wp_get_ability_categories () as $ category ) {
53+ wp_unregister_ability_category ( $ category ->get_slug () );
54+ }
55+
4856 // Ensure core abilities are registered.
4957 remove_action ( 'wp_abilities_api_categories_init ' , '_unhook_core_ability_categories_registration ' , 1 );
5058 remove_action ( 'wp_abilities_api_init ' , '_unhook_core_abilities_registration ' , 1 );
@@ -198,6 +206,43 @@ public function set_up(): void {
198206 'show_in_abilities ' => true ,
199207 )
200208 );
209+
210+ // Unregister all existing abilities so we can re-register with updated schema.
211+ foreach ( wp_get_abilities () as $ ability ) {
212+ wp_unregister_ability ( $ ability ->get_name () );
213+ }
214+
215+ // Remove core abilities registration to prevent ALL abilities from being registered
216+ // when we only want to re-register post type abilities with updated schema.
217+ remove_action ( 'wp_abilities_api_init ' , 'wp_register_core_abilities ' );
218+
219+ // Simulate the init action to allow re-registration without "doing it wrong" warning.
220+ $ this ->simulate_doing_wp_abilities_init_action ();
221+
222+ // Re-register all post type abilities so the schema includes the meta keys enum.
223+ WP_Post_Type_Abilities::register ();
224+
225+ // Clean up the simulated action.
226+ $ this ->end_simulated_wp_abilities_init_action ();
227+ }
228+
229+ /**
230+ * Simulates the `wp_abilities_api_init` action.
231+ *
232+ * This makes `doing_action('wp_abilities_api_init')` return true without
233+ * firing all hooks registered on that action.
234+ */
235+ private function simulate_doing_wp_abilities_init_action (): void {
236+ global $ wp_current_filter ;
237+ $ wp_current_filter [] = 'wp_abilities_api_init ' ;
238+ }
239+
240+ /**
241+ * Ends the simulated `wp_abilities_api_init` action.
242+ */
243+ private function end_simulated_wp_abilities_init_action (): void {
244+ global $ wp_current_filter ;
245+ array_pop ( $ wp_current_filter );
201246 }
202247
203248 /**
@@ -628,7 +673,8 @@ public function test_meta_query_with_invalid_key_returns_error(): void {
628673 $ this ->assertSame ( 400 , $ response ->get_status () );
629674
630675 $ data = $ response ->get_data ();
631- $ this ->assertSame ( 'invalid_meta_key ' , $ data ['code ' ] );
676+ // Schema validation catches invalid meta keys.
677+ $ this ->assertSame ( 'ability_invalid_input ' , $ data ['code ' ] );
632678 }
633679
634680 /**
@@ -692,7 +738,8 @@ public function test_tax_query_with_non_public_taxonomy_returns_error(): void {
692738 $ this ->assertSame ( 400 , $ response ->get_status () );
693739
694740 $ data = $ response ->get_data ();
695- $ this ->assertSame ( 'invalid_taxonomy ' , $ data ['code ' ] );
741+ // Schema validation catches non-public taxonomies.
742+ $ this ->assertSame ( 'ability_invalid_input ' , $ data ['code ' ] );
696743
697744 // Clean up.
698745 unregister_taxonomy ( 'private_tax ' );
0 commit comments