|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TwigA11y\Tests\Rules\Aria; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use TwigA11y\Rules\Aria\RoleCatalog; |
| 10 | + |
| 11 | +/** |
| 12 | + * @internal |
| 13 | + */ |
| 14 | +#[CoversClass(RoleCatalog::class)] |
| 15 | +final class RoleCatalogTest extends TestCase |
| 16 | +{ |
| 17 | + public function testGetAllowedRolesReturnsNonEmptyArray(): void |
| 18 | + { |
| 19 | + $roles = RoleCatalog::getAllowedRoles(); |
| 20 | + |
| 21 | + self::assertIsArray($roles); |
| 22 | + self::assertNotEmpty($roles); |
| 23 | + } |
| 24 | + |
| 25 | + public function testGetAllowedRolesContainsKnownWaiAriaRoles(): void |
| 26 | + { |
| 27 | + $roles = RoleCatalog::getAllowedRoles(); |
| 28 | + |
| 29 | + $expected = [ |
| 30 | + 'alert', 'alertdialog', 'application', 'article', 'banner', |
| 31 | + 'button', 'checkbox', 'combobox', 'dialog', 'document', |
| 32 | + 'feed', 'figure', 'form', 'grid', 'group', 'heading', 'img', |
| 33 | + 'link', 'list', 'listbox', 'listitem', 'log', 'main', 'menu', |
| 34 | + 'menubar', 'menuitem', 'navigation', 'none', 'option', |
| 35 | + 'presentation', 'progressbar', 'radio', 'radiogroup', 'region', |
| 36 | + 'row', 'rowgroup', 'scrollbar', 'search', 'searchbox', |
| 37 | + 'separator', 'slider', 'spinbutton', 'status', 'switch', |
| 38 | + 'tab', 'table', 'tablist', 'tabpanel', 'textbox', 'timer', |
| 39 | + 'toolbar', 'tooltip', 'tree', 'treegrid', 'treeitem', |
| 40 | + ]; |
| 41 | + |
| 42 | + foreach ($expected as $role) { |
| 43 | + self::assertContains($role, $roles, \sprintf('Expected role "%s" to be in the allowed roles list.', $role)); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public function testGetAllowedRolesContainsOnlyStrings(): void |
| 48 | + { |
| 49 | + foreach (RoleCatalog::getAllowedRoles() as $role) { |
| 50 | + self::assertIsString($role); |
| 51 | + self::assertNotEmpty($role); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public function testGetCatalogReturnsNonEmptyArray(): void |
| 56 | + { |
| 57 | + $catalog = RoleCatalog::getCatalog(); |
| 58 | + |
| 59 | + self::assertIsArray($catalog); |
| 60 | + self::assertNotEmpty($catalog); |
| 61 | + } |
| 62 | + |
| 63 | + public function testGetCatalogEntriesHaveExpectedShape(): void |
| 64 | + { |
| 65 | + foreach (RoleCatalog::getCatalog() as $role => $entry) { |
| 66 | + self::assertIsString($role, 'Catalog key must be a string role name.'); |
| 67 | + self::assertArrayHasKey('required_attrs', $entry, \sprintf('Role "%s" must have required_attrs.', $role)); |
| 68 | + self::assertArrayHasKey('required_children', $entry, \sprintf('Role "%s" must have required_children.', $role)); |
| 69 | + self::assertIsArray($entry['required_attrs']); |
| 70 | + self::assertIsArray($entry['required_children']); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public function testGetCatalogContainsCompositeWidgets(): void |
| 75 | + { |
| 76 | + $catalog = RoleCatalog::getCatalog(); |
| 77 | + |
| 78 | + self::assertArrayHasKey('table', $catalog); |
| 79 | + self::assertContains('row', $catalog['table']['required_children']); |
| 80 | + |
| 81 | + self::assertArrayHasKey('tablist', $catalog); |
| 82 | + self::assertContains('tab', $catalog['tablist']['required_children']); |
| 83 | + |
| 84 | + self::assertArrayHasKey('list', $catalog); |
| 85 | + self::assertContains('listitem', $catalog['list']['required_children']); |
| 86 | + |
| 87 | + self::assertArrayHasKey('radiogroup', $catalog); |
| 88 | + self::assertContains('radio', $catalog['radiogroup']['required_children']); |
| 89 | + |
| 90 | + self::assertArrayHasKey('tree', $catalog); |
| 91 | + self::assertContains('treeitem', $catalog['tree']['required_children']); |
| 92 | + |
| 93 | + self::assertArrayHasKey('grid', $catalog); |
| 94 | + self::assertContains('row', $catalog['grid']['required_children']); |
| 95 | + |
| 96 | + self::assertArrayHasKey('listbox', $catalog); |
| 97 | + self::assertContains('option', $catalog['listbox']['required_children']); |
| 98 | + |
| 99 | + self::assertArrayHasKey('menu', $catalog); |
| 100 | + self::assertContains('menuitem', $catalog['menu']['required_children']); |
| 101 | + |
| 102 | + self::assertArrayHasKey('menubar', $catalog); |
| 103 | + self::assertContains('menuitem', $catalog['menubar']['required_children']); |
| 104 | + } |
| 105 | + |
| 106 | + public function testGetCatalogRolesWithRequiredAttrs(): void |
| 107 | + { |
| 108 | + $catalog = RoleCatalog::getCatalog(); |
| 109 | + |
| 110 | + self::assertArrayHasKey('tab', $catalog); |
| 111 | + self::assertContains('aria-selected', $catalog['tab']['required_attrs']); |
| 112 | + |
| 113 | + self::assertArrayHasKey('checkbox', $catalog); |
| 114 | + self::assertContains('aria-checked', $catalog['checkbox']['required_attrs']); |
| 115 | + |
| 116 | + self::assertArrayHasKey('radio', $catalog); |
| 117 | + self::assertContains('aria-checked', $catalog['radio']['required_attrs']); |
| 118 | + } |
| 119 | + |
| 120 | + public function testGetCatalogRolesWithNoRequiredAttrs(): void |
| 121 | + { |
| 122 | + $catalog = RoleCatalog::getCatalog(); |
| 123 | + |
| 124 | + foreach (['button', 'table', 'list', 'menu', 'menubar', 'row'] as $role) { |
| 125 | + self::assertArrayHasKey($role, $catalog); |
| 126 | + self::assertSame([], $catalog[$role]['required_attrs'], \sprintf('Role "%s" should have no required_attrs.', $role)); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments