Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions STEPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
> <br/><br/>
> 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.


<details>
Expand Down
34 changes: 27 additions & 7 deletions src/AccessibilityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/behat/features/accessibility.feature
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,34 @@ Feature: Check that AccessibilityTrait works
"""
<system-out>
"""

@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,
"""