Skip to content

fix(connector): tighten site regex and split initialize for testability#35

Merged
kojiromike merged 1 commit into
mainfrom
fix-issue-10-split-initialize
May 12, 2026
Merged

fix(connector): tighten site regex and split initialize for testability#35
kojiromike merged 1 commit into
mainfrom
fix-issue-10-split-initialize

Conversation

@kojiromike
Copy link
Copy Markdown
Contributor

@kojiromike kojiromike commented May 12, 2026

Fixes #10.

Summary

  • Extract validateSite, resolveGlobalsPath, prepareCliEnvironment, and verifyDatabase from OpenEMRConnector::initialize as protected helpers so the non-bootstrap-dependent pieces can be unit-tested without an OpenEMR runtime.
  • The require_once globals.php stays inline in initialize() so OpenEMR's bootstrap executes in the same local scope as before.
  • Tighten the site-validation regex with the D modifier — without it, PHP's $ anchor matches end-of-string OR just before a trailing newline, so --site=default\n would slip past validation. Surfaced by the new tests.
  • Tests reach the protected helpers via OpenEMRConnectorTestable, a test-only subclass that exposes them as call*() pass-throughs.

Coverage added

  • Site regex: 7 valid + 8 invalid cases (path traversal, slash, space, dot, null byte, newline, unicode).
  • resolveGlobalsPath: present, trailing-slash-tolerant, missing.
  • prepareCliEnvironment: defaults applied; existing $_SERVER vars preserved; $_GET[site] always overwritten.
  • verifyDatabase: missing $GLOBALS['dbh'], false $GLOBALS['dbh'], missing sqlQuery() function.
  • Getter defaults on a fresh connector and initialize() short-circuit when already initialized.

Test plan

  • composer check (php-lint, phpcs, phpstan level 9, rector) clean.
  • composer phpunit — 77 tests, 231 assertions, all passing.
  • task dev:start && task smoke — admin-unlock end-to-end against the dev container, confirming the inlined require_once still bootstraps OpenEMR correctly (OK: unlocked admin).

@kojiromike kojiromike force-pushed the fix-issue-10-split-initialize branch 2 times, most recently from b59324d to 79f63e8 Compare May 12, 2026 15:24
Extract validateSite, resolveGlobalsPath, prepareCliEnvironment, and
verifyDatabase as protected helpers so the non-bootstrap-dependent
parts (site regex, globals.php location, $_SERVER/$_GET defaults,
post-bootstrap DB checks) can be unit-tested without an OpenEMR
runtime. The require_once stays inline in initialize() so globals.php
executes in the same local scope as before.

Tests reach the protected helpers via OpenEMRConnectorTestable, a
test-only subclass that exposes each one as a call*() pass-through,
plus Reflection for the already-initialized short-circuit case.

Also tighten the site-validation regex with the D modifier — without
it, PHP's $ anchor matches end-of-string OR just before a trailing
newline, so "default\n" would slip past validation. The new test
cases surfaced this.

Closes #10

Assisted-by: Claude Code
@kojiromike kojiromike force-pushed the fix-issue-10-split-initialize branch from 79f63e8 to 7c25a29 Compare May 12, 2026 15:40
@kojiromike kojiromike changed the title refactor(connector): split initialize into testable pieces fix(connector): tighten site regex and split initialize for testability May 12, 2026
@kojiromike kojiromike merged commit 9783e53 into main May 12, 2026
5 checks passed
@kojiromike kojiromike deleted the fix-issue-10-split-initialize branch May 12, 2026 16:05
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.

Split OpenEMRConnector::initialize for testability

1 participant