Problem statement

The ai_google_analytics module has zero test coverage. The two most important code paths — benchmark threshold evaluation and the Canvas page presave hook — involve conditional branching, fallback behavior, and state management that are difficult to verify manually and easy to break during future changes.

Dependency

This issue depends on #3585742: Refactor to Service Architecture with DI, Config Schema, and Packaging "Refactor to service architecture with dependency injection, config schema, and packaging". The test files mock BenchmarkEvaluator and construct GoogleAnalyticsHooks via constructor injection — both require that issue's changes. Please merge the architecture refactor first.

Proposed resolution

Two PHPUnit test files covering the BenchmarkEvaluator service and the presave/delete hook logic. Both use UnitTestCase (no Drupal bootstrap required) and are compatible with PHPUnit 11 and PHP 8.3+.

BenchmarkEvaluatorTest — 8 test cases

  • All benchmarks pass
  • All benchmarks fail
  • Single benchmark fails
  • Per-page override takes precedence (lenient)
  • Per-page override causes failure (stricter)
  • Empty metrics pass
  • Empty string metrics treated as NULL
  • Boundary values pass, just-past boundary fails

GoogleAnalyticsHooksPresaveTest — 7 test cases

  • Unchanged metrics skip evaluation
  • Passing benchmarks clear stale state
  • Failing benchmarks call agent and update state
  • Agent failure falls back to deterministic text
  • Non-Canvas-page entities are skipped
  • New entities (no original) are skipped
  • Entity delete clears state

The test constructs GoogleAnalyticsHooks directly via constructor injection, passing mocks for all nine dependencies. Entity mocks use stdClass for field item lists rather than FieldItemListInterface mocks, because PHPUnit 11 does not support dynamic properties on interface mocks in PHP 8.3.

Running the tests

phpunit --group=ai_google_analytics
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alex ua created an issue.