Opened 6 weeks ago
Closed 3 weeks ago
#65422 closed defect (bug) (fixed)
AI Client: generate_result() missing test coverage when wp_supports_ai() returns false
| Reported by: | sagardeshmukh | Owned by: | gziolo |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.1 |
| Component: | AI | Version: | 7.0 |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | tests |
Description
Problem
WP_AI_Client_Prompt_Builder::call() has two distinct code paths when
a prompt is prevented from executing:
- AI disabled — when wp_supports_ai() returns false: Sets error message: "AI features are not supported in this environment."
- 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 $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 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.
Change History (4)
This ticket was mentioned in PR #12109 on WordPress/wordpress-develop by @nimeshatxecurify.
6 weeks ago
#1
- Keywords has-unit-tests added
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Ensure that
generate_result()returns aWP_Errorwith theprompt_preventedcode when AI features are not supported in the environment.Trac ticket: https://core.trac.wordpress.org/ticket/65422
## Use of AI Tools
N / A