Changeset 61130
- Timestamp:
- 11/04/2025 04:32:45 PM (5 weeks ago)
- Location:
- trunk
- Files:
-
- 8 edited
-
src/wp-includes/abilities-api.php (modified) (2 diffs)
-
tests/phpunit/tests/abilities-api/wpAbilitiesRegistry.php (modified) (2 diffs)
-
tests/phpunit/tests/abilities-api/wpAbility.php (modified) (2 diffs)
-
tests/phpunit/tests/abilities-api/wpRegisterAbility.php (modified) (24 diffs)
-
tests/phpunit/tests/abilities-api/wpRegisterAbilityCategory.php (modified) (14 diffs)
-
tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php (modified) (3 diffs)
-
tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php (modified) (9 diffs)
-
tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/abilities-api.php
r61086 r61130 277 277 */ 278 278 function wp_register_ability( string $name, array $args ): ?WP_Ability { 279 if ( ! d id_action( 'wp_abilities_api_init' ) ) {279 if ( ! doing_action( 'wp_abilities_api_init' ) ) { 280 280 _doing_it_wrong( 281 281 __FUNCTION__, … … 466 466 */ 467 467 function wp_register_ability_category( string $slug, array $args ): ?WP_Ability_Category { 468 if ( ! d id_action( 'wp_abilities_api_categories_init' ) ) {468 if ( ! doing_action( 'wp_abilities_api_categories_init' ) ) { 469 469 _doing_it_wrong( 470 470 __FUNCTION__, -
trunk/tests/phpunit/tests/abilities-api/wpAbilitiesRegistry.php
r61032 r61130 30 30 remove_all_filters( 'wp_register_ability_args' ); 31 31 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'; 34 35 wp_register_ability_category( 35 36 'math', … … 39 40 ) 40 41 ); 42 array_pop( $wp_current_filter ); 41 43 42 44 self::$test_ability_args = array( -
trunk/tests/phpunit/tests/abilities-api/wpAbility.php
r61032 r61130 18 18 public function set_up(): void { 19 19 parent::set_up(); 20 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 20 31 21 self::$test_ability_properties = array( … … 57 47 */ 58 48 public function tear_down(): void { 59 // Clean up registered test ability category.60 wp_unregister_ability_category( 'math' );61 62 49 parent::tear_down(); 63 50 } -
trunk/tests/phpunit/tests/abilities-api/wpRegisterAbility.php
r61032 r61130 30 30 */ 31 31 public function set_up(): void { 32 global $wp_current_filter; 33 32 34 parent::set_up(); 33 35 34 // Fire the init hookto 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'; 36 38 wp_register_ability_category( 37 39 'math', … … 87 89 */ 88 90 public function tear_down(): void { 91 global $wp_current_filter; 92 89 93 foreach ( wp_get_abilities() as $ability ) { 90 94 if ( ! str_starts_with( $ability->get_name(), 'test/' ) ) { … … 102 106 103 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 116 /** 104 117 * Tests registering an ability with invalid name. 105 118 * … … 109 122 */ 110 123 public function test_register_ability_invalid_name(): void { 111 do_action( 'wp_abilities_api_init');124 $this->simulate_doing_wp_abilities_init_action(); 112 125 113 126 $result = wp_register_ability( 'invalid_name', array() ); … … 117 130 118 131 /** 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. 120 133 * 121 134 * @ticket 64098 … … 124 137 */ 125 138 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'] ); 133 134 $result = wp_register_ability( self::$test_ability_name, self::$test_ability_args ); 135 136 // Restore the original action count. 137 if ( $original_count > 0 ) { 138 $wp_actions['wp_abilities_api_init'] = $original_count; 139 } 139 $this->assertFalse( doing_action( 'wp_abilities_api_init' ) ); 140 141 $result = wp_register_ability( self::$test_ability_name, self::$test_ability_args ); 140 142 141 143 $this->assertNull( $result ); … … 152 154 global $wp_actions; 153 155 154 do_action( 'wp_abilities_api_init' );155 156 156 // Store the original action count. 157 157 $original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0; … … 159 159 // Reset the action count to simulate it not being fired. 160 160 unset( $wp_actions['init'] ); 161 162 $this->simulate_doing_wp_abilities_init_action(); 161 163 162 164 $result = wp_register_ability( self::$test_ability_name, self::$test_ability_args ); … … 176 178 */ 177 179 public function test_register_valid_ability(): void { 178 do_action( 'wp_abilities_api_init');180 $this->simulate_doing_wp_abilities_init_action(); 179 181 180 182 $result = wp_register_ability( self::$test_ability_name, self::$test_ability_args ); … … 226 228 */ 227 229 public function test_register_ability_no_permissions(): void { 228 do_action( 'wp_abilities_api_init');230 $this->simulate_doing_wp_abilities_init_action(); 229 231 230 232 self::$test_ability_args['permission_callback'] = static function (): bool { … … 261 263 */ 262 264 public function test_register_ability_custom_ability_class(): void { 263 do_action( 'wp_abilities_api_init');265 $this->simulate_doing_wp_abilities_init_action(); 264 266 265 267 $result = wp_register_ability( … … 303 305 */ 304 306 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(); 306 308 307 309 $result = wp_register_ability( self::$test_ability_name, self::$test_ability_args ); … … 332 334 */ 333 335 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(); 335 337 336 338 self::$test_ability_args['execute_callback'] = static function (): bool { … … 363 365 */ 364 366 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(); 366 368 367 369 $result = wp_register_ability( self::$test_ability_name, self::$test_ability_args ); … … 392 394 */ 393 395 public function test_permission_callback_receives_input(): void { 394 do_action( 'wp_abilities_api_init');396 $this->simulate_doing_wp_abilities_init_action(); 395 397 396 398 $received_input = null; … … 454 456 unset( $wp_actions['init'] ); 455 457 458 $this->simulate_doing_wp_abilities_init_action(); 459 456 460 $result = wp_unregister_ability( self::$test_ability_name ); 457 461 … … 470 474 */ 471 475 public function test_unregister_existing_ability() { 472 do_action( 'wp_abilities_api_init');476 $this->simulate_doing_wp_abilities_init_action(); 473 477 474 478 wp_register_ability( self::$test_ability_name, self::$test_ability_args ); … … 497 501 // Reset the action count to simulate it not being fired. 498 502 unset( $wp_actions['init'] ); 503 504 $this->simulate_doing_wp_abilities_init_action(); 499 505 500 506 $result = wp_get_ability( self::$test_ability_name ); … … 514 520 */ 515 521 public function test_get_existing_ability_using_callback() { 522 $this->simulate_doing_wp_abilities_init_action(); 523 516 524 $name = self::$test_ability_name; 517 525 $args = self::$test_ability_args; … … 557 565 unset( $wp_actions['init'] ); 558 566 567 $this->simulate_doing_wp_abilities_init_action(); 568 559 569 $result = wp_has_ability( self::$test_ability_name ); 560 570 … … 573 583 */ 574 584 public function test_has_registered_ability() { 575 do_action( 'wp_abilities_api_init');585 $this->simulate_doing_wp_abilities_init_action(); 576 586 577 587 wp_register_ability( self::$test_ability_name, self::$test_ability_args ); … … 588 598 */ 589 599 public function test_has_registered_nonexistent_ability() { 590 do_action( 'wp_abilities_api_init');600 $this->simulate_doing_wp_abilities_init_action(); 591 601 592 602 $result = wp_has_ability( 'test/non-existent' ); … … 610 620 // Reset the action count to simulate it not being fired. 611 621 unset( $wp_actions['init'] ); 622 623 $this->simulate_doing_wp_abilities_init_action(); 612 624 613 625 $result = wp_get_abilities(); … … 627 639 */ 628 640 public function test_get_all_registered_abilities() { 629 do_action( 'wp_abilities_api_init');641 $this->simulate_doing_wp_abilities_init_action(); 630 642 631 643 $ability_one_name = 'test/ability-one'; -
trunk/tests/phpunit/tests/abilities-api/wpRegisterAbilityCategory.php
r61032 r61130 46 46 47 47 /** 48 * Test registering ability category before `abilities_api_categories_init` hook. 48 * Simulates the `wp_abilities_api_categories_init` action. 49 */ 50 private function simulate_doing_wp_ability_categories_init_action() { 51 global $wp_current_filter; 52 53 $wp_current_filter[] = 'wp_abilities_api_categories_init'; 54 } 55 56 /** 57 * Test registering ability category before `wp_abilities_api_categories_init` hook. 49 58 * 50 59 * @ticket 64098 … … 53 62 */ 54 63 public function test_register_category_before_init_hook(): void { 64 $this->assertFalse( doing_action( 'wp_abilities_api_categories_init' ) ); 65 55 66 $result = wp_register_ability_category( 56 67 self::$test_ability_category_name, … … 71 82 global $wp_actions; 72 83 73 do_action( 'wp_abilities_api_categories_init' );74 75 // Store the original action count. 76 $original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0;77 78 // Reset the action count to simulate it not being fired. 79 unset( $wp_actions['init']);84 // Store the original action count. 85 $original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0; 86 87 // Reset the action count to simulate it not being fired. 88 unset( $wp_actions['init'] ); 89 90 $this->simulate_doing_wp_ability_categories_init_action(); 80 91 81 92 $result = wp_register_ability_category( … … 98 109 */ 99 110 public function test_register_valid_category(): void { 100 do_action( 'wp_abilities_api_categories_init');111 $this->simulate_doing_wp_ability_categories_init_action(); 101 112 102 113 $result = wp_register_ability_category( … … 121 132 global $wp_actions; 122 133 123 do_action( 'wp_abilities_api_categories_init' );124 125 // Store the original action count. 126 $original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0;127 128 // Reset the action count to simulate it not being fired. 129 unset( $wp_actions['init']);134 // Store the original action count. 135 $original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0; 136 137 // Reset the action count to simulate it not being fired. 138 unset( $wp_actions['init'] ); 139 140 $this->simulate_doing_wp_ability_categories_init_action(); 130 141 131 142 $result = wp_unregister_ability_category( self::$test_ability_category_name ); … … 147 158 */ 148 159 public function test_unregister_nonexistent_category(): void { 149 do_action( 'wp_abilities_api_categories_init');160 $this->simulate_doing_wp_ability_categories_init_action(); 150 161 151 162 $result = wp_unregister_ability_category( 'test-nonexistent' ); … … 160 171 */ 161 172 public function test_unregister_existing_category(): void { 162 do_action( 'wp_abilities_api_categories_init');173 $this->simulate_doing_wp_ability_categories_init_action(); 163 174 164 175 wp_register_ability_category( … … 183 194 global $wp_actions; 184 195 185 do_action( 'wp_abilities_api_categories_init' );186 187 // Store the original action count. 188 $original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0;189 190 // Reset the action count to simulate it not being fired. 191 unset( $wp_actions['init']);196 // Store the original action count. 197 $original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0; 198 199 // Reset the action count to simulate it not being fired. 200 unset( $wp_actions['init'] ); 201 202 $this->simulate_doing_wp_ability_categories_init_action(); 192 203 193 204 $result = wp_has_ability_category( self::$test_ability_category_name ); … … 207 218 */ 208 219 public function test_has_registered_nonexistent_ability_category(): void { 209 do_action( 'wp_abilities_api_categories_init');220 $this->simulate_doing_wp_ability_categories_init_action(); 210 221 211 222 $result = wp_has_ability_category( 'test/non-existent' ); … … 220 231 */ 221 232 public function test_has_registered_ability_category(): void { 222 do_action( 'wp_abilities_api_categories_init');233 $this->simulate_doing_wp_ability_categories_init_action(); 223 234 224 235 $category_slug = self::$test_ability_category_name; … … 244 255 global $wp_actions; 245 256 246 do_action( 'wp_abilities_api_categories_init' );247 248 // Store the original action count. 249 $original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0;250 251 // Reset the action count to simulate it not being fired. 252 unset( $wp_actions['init']);257 // Store the original action count. 258 $original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0; 259 260 // Reset the action count to simulate it not being fired. 261 unset( $wp_actions['init'] ); 262 263 $this->simulate_doing_wp_ability_categories_init_action(); 253 264 254 265 $result = wp_get_ability_category( self::$test_ability_category_name ); … … 270 281 */ 271 282 public function test_get_nonexistent_category(): void { 272 do_action( 'wp_abilities_api_categories_init');283 $this->simulate_doing_wp_ability_categories_init_action(); 273 284 274 285 $result = wp_get_ability_category( 'test-nonexistent' ); … … 317 328 global $wp_actions; 318 329 319 do_action( 'wp_abilities_api_categories_init' );320 321 // Store the original action count. 322 $original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0;323 324 // Reset the action count to simulate it not being fired. 325 unset( $wp_actions['init']);330 // Store the original action count. 331 $original_count = isset( $wp_actions['init'] ) ? $wp_actions['init'] : 0; 332 333 // Reset the action count to simulate it not being fired. 334 unset( $wp_actions['init'] ); 335 336 $this->simulate_doing_wp_ability_categories_init_action(); 326 337 327 338 $result = wp_get_ability_categories( self::$test_ability_category_name ); … … 341 352 */ 342 353 public function test_get_all_categories(): void { 343 do_action( 'wp_abilities_api_categories_init');354 $this->simulate_doing_wp_ability_categories_init_action(); 344 355 345 356 wp_register_ability_category( -
trunk/tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php
r61045 r61130 63 63 do_action( 'rest_api_init' ); 64 64 65 // Initialize the API and register test ability categories.66 do_action( 'wp_abilities_api_categories_init' );67 65 $this->register_test_ability_categories(); 68 66 … … 94 92 */ 95 93 public function register_test_ability_categories(): void { 94 // Simulates the init hook to allow test ability categories registration. 95 global $wp_current_filter; 96 $wp_current_filter[] = 'wp_abilities_api_categories_init'; 97 96 98 wp_register_ability_category( 97 99 'test-data-retrieval', … … 131 133 ); 132 134 } 135 136 array_pop( $wp_current_filter ); 133 137 } 134 138 -
trunk/tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php
r61032 r61130 38 38 ); 39 39 40 // Fire the init hook to allow test ability categories registration.41 do_action( 'wp_abilities_api_categories_init' );42 40 self::register_test_categories(); 43 41 } … … 68 66 do_action( 'rest_api_init' ); 69 67 70 // Initialize Abilities API.71 do_action( 'wp_abilities_api_init' );72 68 $this->register_test_abilities(); 73 69 … … 100 96 */ 101 97 public static function register_test_categories(): void { 98 // Simulates the init hook to allow test ability categories registration. 99 global $wp_current_filter; 100 $wp_current_filter[] = 'wp_abilities_api_categories_init'; 101 102 102 wp_register_ability_category( 103 103 'math', … … 123 123 ) 124 124 ); 125 126 array_pop( $wp_current_filter ); 127 } 128 129 /** 130 * Helper to register a test ability. 131 * 132 * @param string $name Ability name. 133 * @param array $args Ability arguments. 134 */ 135 private function register_test_ability( string $name, array $args ): void { 136 // Simulates the init hook to allow test abilities registration. 137 global $wp_current_filter; 138 $wp_current_filter[] = 'wp_abilities_api_init'; 139 140 wp_register_ability( $name, $args ); 141 142 array_pop( $wp_current_filter ); 125 143 } 126 144 … … 130 148 private function register_test_abilities(): void { 131 149 // Register a regular ability. 132 wp_register_ability(150 $this->register_test_ability( 133 151 'test/calculator', 134 152 array( … … 174 192 175 193 // Register a read-only ability. 176 wp_register_ability(194 $this->register_test_ability( 177 195 'test/system-info', 178 196 array( … … 221 239 222 240 // Ability that does not show in REST. 223 wp_register_ability(241 $this->register_test_ability( 224 242 'test/not-show-in-rest', 225 243 array( … … 236 254 // Register multiple abilities for pagination testing 237 255 for ( $i = 1; $i <= 60; $i++ ) { 238 wp_register_ability(256 $this->register_test_ability( 239 257 "test/ability-{$i}", 240 258 array( … … 590 608 public function test_ability_name_with_valid_special_characters(): void { 591 609 // Register ability with hyphen (valid). 592 wp_register_ability(610 $this->register_test_ability( 593 611 'test-hyphen/ability', 594 612 array( -
trunk/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php
r61047 r61130 50 50 ); 51 51 52 // Fire the init hook to allow test ability categories registration.53 do_action( 'wp_abilities_api_categories_init' );54 52 self::register_test_categories(); 55 53 } … … 79 77 do_action( 'rest_api_init' ); 80 78 81 // Initialize Abilities API.82 do_action( 'wp_abilities_api_init' );83 79 $this->register_test_abilities(); 84 80 … … 110 106 */ 111 107 public static function register_test_categories(): void { 108 // Simulates the init hook to allow test ability category registration. 109 global $wp_current_filter; 110 $wp_current_filter[] = 'wp_abilities_api_categories_init'; 111 112 112 wp_register_ability_category( 113 113 'math', … … 133 133 ) 134 134 ); 135 136 array_pop( $wp_current_filter ); 137 } 138 139 /** 140 * Helper to register a test ability. 141 * 142 * @param string $name Ability name. 143 * @param array $args Ability arguments. 144 */ 145 private function register_test_ability( string $name, array $args ): void { 146 // Simulates the init hook to allow test abilities registration. 147 global $wp_current_filter; 148 $wp_current_filter[] = 'wp_abilities_api_init'; 149 150 wp_register_ability( $name, $args ); 151 152 array_pop( $wp_current_filter ); 135 153 } 136 154 … … 140 158 private function register_test_abilities(): void { 141 159 // Regular ability (POST only). 142 wp_register_ability(160 $this->register_test_ability( 143 161 'test/calculator', 144 162 array( … … 177 195 178 196 // Read-only ability (GET method). 179 wp_register_ability(197 $this->register_test_ability( 180 198 'test/user-info', 181 199 array( … … 223 241 224 242 // Destructive ability (DELETE method). 225 wp_register_ability(243 $this->register_test_ability( 226 244 'test/delete-user', 227 245 array( … … 264 282 265 283 // Ability with contextual permissions 266 wp_register_ability(284 $this->register_test_ability( 267 285 'test/restricted', 268 286 array( … … 295 313 296 314 // Ability that does not show in REST. 297 wp_register_ability(315 $this->register_test_ability( 298 316 'test/not-show-in-rest', 299 317 array( … … 309 327 310 328 // Ability that returns null 311 wp_register_ability(329 $this->register_test_ability( 312 330 'test/null-return', 313 331 array( … … 326 344 327 345 // Ability that returns WP_Error 328 wp_register_ability(346 $this->register_test_ability( 329 347 'test/error-return', 330 348 array( … … 343 361 344 362 // Ability with invalid output 345 wp_register_ability(363 $this->register_test_ability( 346 364 'test/invalid-output', 347 365 array( … … 363 381 364 382 // Read-only ability for query params testing. 365 wp_register_ability(383 $this->register_test_ability( 366 384 'test/query-params', 367 385 array( … … 464 482 */ 465 483 public function test_regular_ability_requires_post(): void { 466 wp_register_ability(484 $this->register_test_ability( 467 485 'test/open-tool', 468 486 array( … … 794 812 public function test_output_validation_failure_returns_error(): void { 795 813 // Register ability with strict output schema. 796 wp_register_ability(814 $this->register_test_ability( 797 815 'test/strict-output', 798 816 array( … … 843 861 public function test_input_validation_failure_returns_error(): void { 844 862 // Register ability with strict input schema. 845 wp_register_ability(863 $this->register_test_ability( 846 864 'test/strict-input', 847 865 array( … … 892 910 public function test_ability_without_annotations_defaults_to_post_method(): void { 893 911 // Register ability without annotations. 894 wp_register_ability(912 $this->register_test_ability( 895 913 'test/no-annotations', 896 914 array( … … 927 945 */ 928 946 public function test_empty_input_handling_get_method(): void { 929 wp_register_ability(947 $this->register_test_ability( 930 948 'test/read-only-empty', 931 949 array( … … 959 977 */ 960 978 public function test_empty_input_handling_get_method_with_normalized_input(): void { 961 wp_register_ability(979 $this->register_test_ability( 962 980 'test/read-only-empty-array', 963 981 array( … … 995 1013 */ 996 1014 public function test_empty_input_handling_post_method(): void { 997 wp_register_ability(1015 $this->register_test_ability( 998 1016 'test/regular-empty', 999 1017 array( … … 1066 1084 public function test_php_type_strings_in_input(): void { 1067 1085 // Register ability that accepts any input 1068 wp_register_ability(1086 $this->register_test_ability( 1069 1087 'test/echo', 1070 1088 array( … … 1115 1133 public function test_mixed_encoding_in_input(): void { 1116 1134 // Register ability that accepts any input 1117 wp_register_ability(1135 $this->register_test_ability( 1118 1136 'test/echo-encoding', 1119 1137 array( … … 1184 1202 public function test_invalid_http_methods( string $method ): void { 1185 1203 // Register an ability with no permission requirements for this test 1186 wp_register_ability(1204 $this->register_test_ability( 1187 1205 'test/method-test', 1188 1206 array(
Note: See TracChangeset
for help on using the changeset viewer.