Commit 1e0067322cd for woocommerce
commit 1e0067322cde00ab2bfcf29309892f1f79ddb232
Author: Darren Ethier <darren@roughsmootheng.in>
Date: Thu May 21 04:17:02 2026 -0400
Fix MCP provider ability registration tests (#65223)
diff --git a/plugins/woocommerce/changelog/65207-fix-mcp-provider-test-lifecycle b/plugins/woocommerce/changelog/65207-fix-mcp-provider-test-lifecycle
new file mode 100644
index 00000000000..4fdbfabde82
--- /dev/null
+++ b/plugins/woocommerce/changelog/65207-fix-mcp-provider-test-lifecycle
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+
+Fix MCP provider test Abilities API registry bootstrap.
diff --git a/plugins/woocommerce/tests/php/src/Internal/MCP/MCPAdapterProviderTest.php b/plugins/woocommerce/tests/php/src/Internal/MCP/MCPAdapterProviderTest.php
index 8f1a7f4cd2d..5422bf9c96f 100644
--- a/plugins/woocommerce/tests/php/src/Internal/MCP/MCPAdapterProviderTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/MCP/MCPAdapterProviderTest.php
@@ -66,6 +66,13 @@ class MCPAdapterProviderTest extends \WC_Unit_Test_Case {
*/
private $original_wp_abilities_api_categories_init_action_count;
+ /**
+ * Original value of $wp_actions['init'] to restore in tearDown.
+ *
+ * @var int|null
+ */
+ private $original_init_action_count;
+
/**
* Set up before each test.
*/
@@ -74,9 +81,13 @@ class MCPAdapterProviderTest extends \WC_Unit_Test_Case {
parent::setUp();
+ $this->original_init_action_count = $wp_actions['init'] ?? null;
$this->original_wp_abilities_api_init_action_count = $wp_actions['wp_abilities_api_init'] ?? null;
$this->original_wp_abilities_api_categories_init_action_count = $wp_actions['wp_abilities_api_categories_init'] ?? null;
+ // WordPress 6.9+ requires init to have fired before the Abilities API registry can be initialized.
+ $wp_actions['init'] = max( 1, (int) ( $wp_actions['init'] ?? 0 ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
+
// Bootstrap the WordPress Abilities API for tests.
if ( ! function_exists( 'wp_register_ability' ) ) {
$abilities_bootstrap = WP_PLUGIN_DIR . '/woocommerce/vendor/wordpress/abilities-api/includes/bootstrap.php';
@@ -161,6 +172,12 @@ class MCPAdapterProviderTest extends \WC_Unit_Test_Case {
unset( $wp_actions['wp_abilities_api_categories_init'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
+ if ( null !== $this->original_init_action_count ) {
+ $wp_actions['init'] = $this->original_init_action_count; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
+ } elseif ( isset( $wp_actions['init'] ) ) {
+ unset( $wp_actions['init'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
+ }
+
parent::tearDown();
}
@@ -426,6 +443,7 @@ class MCPAdapterProviderTest extends \WC_Unit_Test_Case {
*/
private function register_test_ability( string $ability_id, array $meta ): void {
$this->ensure_test_ability_category( 'woocommerce-rest' );
+ $this->ensure_abilities_registry_initialized();
$ability = null;
$callback = null;
@@ -474,6 +492,7 @@ class MCPAdapterProviderTest extends \WC_Unit_Test_Case {
if ( ! function_exists( 'wp_register_ability_category' ) || ! function_exists( 'wp_has_ability_category' ) ) {
return;
}
+ $this->ensure_ability_categories_registry_initialized();
if ( wp_has_ability_category( $category_id ) ) {
return;
@@ -509,4 +528,28 @@ class MCPAdapterProviderTest extends \WC_Unit_Test_Case {
$this->assertTrue( wp_has_ability_category( $category_id ), 'Test ability category should be available.' );
}
+
+ /**
+ * Ensure the ability registry is ready before adding one-off test callbacks.
+ *
+ * Registry initialization fires wp_abilities_api_init. If the test callback is
+ * already attached, the same ability can be registered twice and return null.
+ */
+ private function ensure_abilities_registry_initialized(): void {
+ if ( class_exists( '\WP_Abilities_Registry' ) ) {
+ \WP_Abilities_Registry::get_instance();
+ }
+ }
+
+ /**
+ * Ensure the ability category registry is ready before adding one-off test callbacks.
+ *
+ * Registry initialization fires wp_abilities_api_categories_init. If the test
+ * callback is already attached, the same category can be registered twice.
+ */
+ private function ensure_ability_categories_registry_initialized(): void {
+ if ( class_exists( '\WP_Ability_Categories_Registry' ) ) {
+ \WP_Ability_Categories_Registry::get_instance();
+ }
+ }
}