From 8208061207b4c1e2b4fdeb30ee4edfcc80ed21cf Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Thu, 20 Aug 2026 09:34:03 +1000 Subject: [PATCH] [#756] Made accessibility console summary opt-in via 'BEHAT_ACCESSIBILITY_PRINT'. --- STEPS.md | 5 ++++ src/AccessibilityTrait.php | 34 +++++++++++++++++----- tests/behat/features/accessibility.feature | 31 ++++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/STEPS.md b/STEPS.md index 52f511fe..99b452d7 100644 --- a/STEPS.md +++ b/STEPS.md @@ -91,6 +91,11 @@ > file is written per run, so a run never overwrites a previous one. The > aggregate accumulates in process-global state, so under parallel Behat each > process writes its own report. +>

+> Console output. A one-line per-page summary can be printed to the console +> as pages are assessed. Printing is off by default; set the +> `BEHAT_ACCESSIBILITY_PRINT` environment variable to a non-empty value other +> than `0`, or override `accessibilityGetPrintCli()`, to enable it.
diff --git a/src/AccessibilityTrait.php b/src/AccessibilityTrait.php index 966ce199..910e19d5 100644 --- a/src/AccessibilityTrait.php +++ b/src/AccessibilityTrait.php @@ -41,6 +41,11 @@ * file is written per run, so a run never overwrites a previous one. The * aggregate accumulates in process-global state, so under parallel Behat each * process writes its own report. + * + * Console output. A one-line per-page summary can be printed to the console + * as pages are assessed. Printing is off by default; set the + * `BEHAT_ACCESSIBILITY_PRINT` environment variable to a non-empty value other + * than `0`, or override `accessibilityGetPrintCli()`, to enable it. */ trait AccessibilityTrait { @@ -459,6 +464,19 @@ protected function accessibilityGetFailOnIncomplete(): bool { return FALSE; } + /** + * Return TRUE to print a one-line per-page summary to the console. + * + * Default: enabled only when the `BEHAT_ACCESSIBILITY_PRINT` environment + * variable is set to a non-empty value other than `0`. Override to + * hardcode either behaviour. + */ + protected function accessibilityGetPrintCli(): bool { + $value = getenv('BEHAT_ACCESSIBILITY_PRINT'); + + return !in_array($value, [FALSE, '', '0'], TRUE); + } + /** * Return the canonical impact levels in descending severity order. * @@ -709,13 +727,15 @@ protected function accessibilityAssess(string $rules): array { $this->accessibilityResults[] = ['url' => $url, 'rules' => $rules, 'result' => $normalized]; $this->accessibilityLastCheckedUrl = $url; - fwrite(STDOUT, sprintf("\n[accessibility] %s: %d violations, %d passes, %d incomplete (rules: %s)\n", - $this->accessibilityFormatUrl($url), - count($normalized['violations'] ?? []), - count($normalized['passes'] ?? []), - count($normalized['incomplete'] ?? []), - $rules - )); + if ($this->accessibilityGetPrintCli()) { + fwrite(STDOUT, sprintf("\n[accessibility] %s: %d violations, %d passes, %d incomplete (rules: %s)\n", + $this->accessibilityFormatUrl($url), + count($normalized['violations'] ?? []), + count($normalized['passes'] ?? []), + count($normalized['incomplete'] ?? []), + $rules + )); + } return $normalized; } diff --git a/tests/behat/features/accessibility.feature b/tests/behat/features/accessibility.feature index 7205ca94..75cea5eb 100644 --- a/tests/behat/features/accessibility.feature +++ b/tests/behat/features/accessibility.feature @@ -123,3 +123,34 @@ Feature: Check that AccessibilityTrait works """ """ + + @trait:AccessibilityTrait + Scenario: Console summary is not printed by default + Given some behat configuration + And scenario steps tagged with "@javascript @phpserver": + """ + Given I visit "http://cli:8888/accessibility_clean.html" + Then the current page should pass accessibility checks + """ + When I run "behat --no-colors" + Then it should pass + And the output should not contain: + """ + [accessibility] + """ + + @trait:AccessibilityTrait + Scenario: Console summary is printed when the environment variable is set + Given some behat configuration + And scenario steps tagged with "@javascript @phpserver": + """ + Given I visit "http://cli:8888/accessibility_clean.html" + Then the current page should pass accessibility checks + """ + When the "BEHAT_ACCESSIBILITY_PRINT" environment variable is set to "1" + And I run "behat --no-colors" + Then it should pass + And the output should contain: + """ + [accessibility] http://cli:8888/accessibility_clean.html: 0 violations, + """