Make WordPress Core

Opened 5 months ago

Closed 5 months 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:

  1. AI disabled — when wp_supports_ai() returns false: Sets error message: "AI features are not supported in this environment."
  1. 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.


5 months ago
#1

  • Keywords has-unit-tests added

Ensure that generate_result() returns a WP_Error with the prompt_prevented code when AI features are not supported in the environment.

Trac ticket: https://core.trac.wordpress.org/ticket/65422

## Use of AI Tools

N / A

#2 @nimeshatxecurify
5 months ago

  • Focuses tests added

#3 @jorbin
5 months ago

  • Milestone Awaiting Review → 7.1

Moving to 7.1 for consideration.

#4 @gziolo
5 months ago

  • Owner set to gziolo
  • Resolution → fixed
  • Status new → closed

In 62556:

Tests: Add coverage for AI Client prompts when AI is unsupported

Adds a test to ensure WP_AI_Client_Prompt_Builder::generate_result() returns a WP_Error with the prompt_prevented code and the AI-disabled message when wp_supports_ai() returns false.

Follow-up for #64591.

Props nimeshatxecurify.
Fixes #65422.

Note: See TracTickets for help on using tickets.