Files
Helpddesk/Modules/Stylist/Tests/Feature/ThemeAccessibilityValidatorTest.php
2026-08-25 12:36:04 +01:00

94 lines
2.7 KiB
PHP

<?php
namespace Modules\Stylist\Tests\Feature;
use Illuminate\Filesystem\Filesystem;
use Modules\Stylist\Services\ThemeAccessibilityValidator;
use Modules\Stylist\Theme\Theme;
use Tests\TestCase;
final class ThemeAccessibilityValidatorTest extends TestCase
{
private string $themePath;
protected function setUp(): void
{
parent::setUp();
$this->themePath = storage_path('framework/testing/stylist-accessibility-'.uniqid());
mkdir($this->themePath.'/views', 0777, true);
}
protected function tearDown(): void
{
app(Filesystem::class)->deleteDirectory($this->themePath);
parent::tearDown();
}
public function test_blade_object_access_does_not_break_image_alt_detection(): void
{
file_put_contents(
$this->themePath.'/views/page.blade.php',
'<img src="{{ $post->featuredImageUrl() }}" alt="{{ $post->title }}">'
);
$this->assertSame([], $this->validate());
}
public function test_image_without_alt_is_reported(): void
{
file_put_contents(
$this->themePath.'/views/page.blade.php',
'<img src="{{ $post->featuredImageUrl() }}">'
);
$issues = $this->validate();
$this->assertCount(1, $issues);
$this->assertSame('error', $issues[0]['severity']);
$this->assertSame('1 image(s) missing alt text.', $issues[0]['message']);
}
public function test_aria_labelled_icon_button_is_not_reported(): void
{
file_put_contents(
$this->themePath.'/views/page.blade.php',
'<button type="button" aria-label="Open navigation"><span></span><span></span></button>'
);
$this->assertSame([], $this->validate());
}
public function test_button_with_visible_text_is_not_reported(): void
{
file_put_contents(
$this->themePath.'/views/page.blade.php',
'<button type="submit"><span>Save</span></button>'
);
$this->assertSame([], $this->validate());
}
public function test_empty_unlabelled_button_is_reported(): void
{
file_put_contents(
$this->themePath.'/views/page.blade.php',
'<button type="button"><span></span></button>'
);
$issues = $this->validate();
$this->assertCount(1, $issues);
$this->assertSame('warning', $issues[0]['severity']);
$this->assertSame('1 button(s) may lack an accessible name.', $issues[0]['message']);
}
/** @return array<int, array{severity:string,message:string,file:string}> */
private function validate(): array
{
$theme = new Theme('test', 'Test', $this->themePath);
return app(ThemeAccessibilityValidator::class)->validate($theme);
}
}