Testing
The AI Context module includes unit, kernel, and functional tests.
Test structure
tests/src/
├── Unit/ # Fast, isolated tests (no Drupal bootstrap)
├── Kernel/ # Integration tests (partial Drupal bootstrap)
├── Functional/ # Full browser tests (complete Drupal installation)
├── FunctionalJavascript/ # JavaScript interaction tests
└── Traits/ # Shared test helpers
Running tests with DDEV
Run all tests
ddev exec phpunit --configuration=web/core \
web/modules/contrib/ai_context/tests
Run by test type
# Unit tests (fastest)
ddev exec phpunit --configuration=web/core \
web/modules/contrib/ai_context/tests/src/Unit
# Kernel tests
ddev exec phpunit --configuration=web/core \
web/modules/contrib/ai_context/tests/src/Kernel
# Functional tests (slowest)
ddev exec phpunit --configuration=web/core \
web/modules/contrib/ai_context/tests/src/Functional
Run a single test file
ddev exec phpunit --configuration=web/core \
web/modules/contrib/ai_context/tests/src/Kernel/AiContextScopeManagerTest.php
Run a specific test method
ddev exec phpunit --configuration=web/core \
--filter=testPathMatchesPattern \
web/modules/contrib/ai_context/tests/src/Unit/AiContextScopeSiteSectionTest.php
Verbose output
# Show test names
ddev exec phpunit --configuration=web/core \
web/modules/contrib/ai_context/tests/src/Unit --testdox
# Show detailed failure info
ddev exec phpunit --configuration=web/core \
web/modules/contrib/ai_context/tests/src/Kernel -v
Running without DDEV
# Using PHPUnit directly
vendor/bin/phpunit web/modules/contrib/ai_context/tests
# Using Drupal's test runner
php core/scripts/run-tests.sh --verbose --color --module ai_context
Conventions
- Use intent-first method names
- Use one metadata style per class (PHPDoc blocks or attributes, not both)
- Add
@runTestsInSeparateProcessesfor Kernel/Functional tests - Use Arrange/Act/Assert comment blocks in non-trivial tests
- Call
assertConfigObjectSchemaIsValid()after settings-form submits - Reuse shared helpers from
tests/src/Traits/
Test coverage by component
| Component | Test type | Description |
|---|---|---|
AiContextScopeManager |
Kernel | Plugin discovery, sorting, value aggregation |
AiContextScopeMatcher |
Kernel | Score calculation, filtering, sorting |
AiContextScopePluginTest |
Kernel | Individual scope plugin behavior |
AiContextScopeEntityTest |
Kernel | Scope field get/set, persistence, revisions |
AiContextSelectorTranslationTest |
Kernel | Translation functionality |
AiContextRendererTest |
Kernel | Context rendering with token budget |
AiContextScopeSiteSectionTest |
Unit | Path pattern matching algorithm |
AiContextScopeScoreCalculationTest |
Unit | Scope scoring algorithm |
MarkdownRendererTest |
Unit | Markdown to HTML conversion |
AiContextScopeContextItemFormTest |
Functional | Scope selection UI |
AiContextScopeAgentFormTest |
Functional | Agent subscription UI |
AiContextItemCrudTest |
Functional | CRUD operations |
AiContextItemFormTest |
Functional | Entity form behavior |
Test coverage
See the tests folder to see which tests are covered by the test suite.