[wp-trac] [WordPress Trac] #65422: AI Client: generate_result() missing test coverage when wp_supports_ai() returns false
WordPress Trac
noreply at wordpress.org
Fri Jun 5 11:25:57 UTC 2026
#65422: AI Client: generate_result() missing test coverage when wp_supports_ai()
returns false
---------------------------+-----------------------------
Reporter: sagardeshmukh | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: AI | Version: 7.0
Severity: normal | Keywords: has-patch
Focuses: |
---------------------------+-----------------------------
== Problem
WP_AI_Client_Prompt_Builder::__call() has two distinct code paths when
a prompt is prevented from executing:
1. AI disabled — when wp_supports_ai() returns false:
Sets error message: "AI features are not supported in this
environment."
2. Filter prevented — when wp_ai_client_prevent_prompt filter returns
true:
Sets error message: "Prompt execution was prevented by a filter."
Both paths set the same error code (prompt_prevented) but different
messages. This distinction matters — a developer catching a WP_Error
from generate_result() needs to know whether AI is globally disabled
or whether a filter blocked the specific prompt.
The filter-prevented path (path 2) is covered by an existing test:
tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php
test_generate_result_returns_wp_error_when_filter_prevents_prompt()
However, the AI-disabled path (path 1) is only partially tested.
The existing test test_is_supported_returns_false_when_ai_not_supported()
only calls is_supported() — it never calls generate_result(). The
"AI features are not supported in this environment." message returned
by generate_result() is therefore untested.
== Relevant code
src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php
{{{#!php
<?php
$error_message = $is_ai_disabled
? __( 'AI features are not supported in this environment.' )
: __( 'Prompt execution was prevented by a filter.' );
$this->error = new WP_Error(
'prompt_prevented',
$error_message,
array( 'status' => 503 )
);
}}}
== Proposed fix
Add a test that calls generate_result() when wp_supports_ai() returns
false, and asserts both the error code and the specific error message:
{{{#!php
<?php
public function
test_generate_result_returns_wp_error_when_ai_not_supported(): void {
add_filter( 'wp_supports_ai', '__return_false' );
$builder = new WP_AI_Client_Prompt_Builder(
AiClient::defaultRegistry(), 'Test prompt' );
$result = $builder->generate_result();
$this->assertWPError( $result );
$this->assertSame( 'prompt_prevented', $result->get_error_code() );
$this->assertSame( 'AI features are not supported in this
environment.', $result->get_error_message() );
}
}}}
== Introduced in
This code was introduced in #64591.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/65422>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list