Skip to content

P2.9: convert tests to exercise real Craft APIs instead of synthetic event objects#115

Draft
sjelfull wants to merge 1 commit into
p2.8-comment-cleanupfrom
p2.9-real-api-tests
Draft

P2.9: convert tests to exercise real Craft APIs instead of synthetic event objects#115
sjelfull wants to merge 1 commit into
p2.8-comment-cleanupfrom
p2.9-real-api-tests

Conversation

@sjelfull
Copy link
Copy Markdown
Owner

@sjelfull sjelfull commented Apr 28, 2026

P2.9: convert 24 tests to real Craft API calls

Replaces synthetic event triggers with real Craft API calls across 5 test files. Previously, tests like SchemaEventsTest would fire Craft::$app->getDb()->trigger(DbEvent::EVENT_AFTER_OPEN) directly — which exercises the handler's listener but not the actual Craft code path that fires the event. This branch rewires those tests to call the real API (Craft::$app->getEntries()->saveSection(), Craft::$app->getRoutes()->saveRoute(), etc.) so the full production code path runs.

The conversion surfaced 3 real bugs that synthetic tests had been hiding: (1) user permissions are lowercased before persist, so assertions need to match lowercase; (2) section saves require pre-saved entry types attached to the section model; (3) Routes::saveRoute() returns a UID string, not a bool. All three are now correctly handled in the tests. Some tests are intentionally kept handler-direct with explanatory comments: PluginEventsTest (no fixture plugin to install), UserEventsTest (no real session in headless test environment), DatabaseEventsTest (real backups are slow and have filesystem side effects).

What changed

  • tests/Pest.php (+40 LOC) — adds _audit_register_listeners_for_tests(), a reflection-based helper that registers handler listeners without going through the full plugin bootstrap. Used by tests that call real Craft APIs but need the audit listeners wired up.
  • tests/Feature/SchemaEventsTest.php — largest change (+108/-98): all schema-change tests now call Craft::$app->getDb()->createTable() etc. instead of triggering events directly.
  • tests/Feature/UserSecurityEventsTest.php (+71/-38) — login/logout/password tests use real Craft::$app->getUsers() calls.
  • tests/Feature/PermissionEventsTest.php (+55/-63) — permission grant/revoke tests use real Craft::$app->getUserPermissions() calls; fixes the lowercase-permissions bug.
  • tests/Feature/RouteEventsTest.php (+30/-24) — route tests use Craft::$app->getRoutes()->saveRoute(); fixes the UID-not-bool return.
  • tests/Feature/PluginEventsTest.php (+18/-3) — adds why-comment explaining handler-direct approach.
  • tests/Feature/UserEventsTest.php (+11/-1) — adds why-comment for headless session limitation.
  • tests/Feature/DatabaseEventsTest.php (+14) — adds why-comment for backup side effects.
  • tests/Unit/ProjectConfigTrackerTest.php (+26) — adds real project-config round-trip coverage.

Tests

  • 24 tests converted to real API calls
  • ECS clean
  • PHPStan clean
  • Fresh-DB green

Stacked on

p2.8-comment-cleanup (#114)

@sjelfull
Copy link
Copy Markdown
Owner Author

sjelfull commented Apr 28, 2026

This change is part of the following stack:

Change managed by git-spice.

@sjelfull sjelfull force-pushed the p2.9-real-api-tests branch from 49bb901 to fe6c741 Compare May 14, 2026 09:25
@sjelfull sjelfull force-pushed the p2.8-comment-cleanup branch from a5ae4c9 to e761f6e Compare May 15, 2026 17:33
@sjelfull sjelfull force-pushed the p2.9-real-api-tests branch from fe6c741 to 49bb901 Compare May 15, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant