|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ipl\Tests\Web\Compat\FormDecorator; |
| 4 | + |
| 5 | +use ipl\Html\FormDecoration\FormElementDecorationResult; |
| 6 | +use ipl\Html\FormElement\CheckboxElement; |
| 7 | +use ipl\Html\FormElement\TextElement; |
| 8 | +use ipl\I18n\NoopTranslator; |
| 9 | +use ipl\I18n\StaticTranslator; |
| 10 | +use ipl\Tests\Html\TestCase as IplHtmlTestCase; |
| 11 | +use ipl\Web\Compat\FormDecorator\AutosubmitDecorator; |
| 12 | + |
| 13 | +class AutosubmitDecoratorTest extends IplHtmlTestCase |
| 14 | +{ |
| 15 | + protected AutosubmitDecorator $decorator; |
| 16 | + |
| 17 | + public function setUp(): void |
| 18 | + { |
| 19 | + $this->decorator = new AutosubmitDecorator(); |
| 20 | + StaticTranslator::$instance = new NoopTranslator(); |
| 21 | + } |
| 22 | + |
| 23 | + public function testNoDecorationForNonCheckboxElement(): void |
| 24 | + { |
| 25 | + $result = new FormElementDecorationResult(); |
| 26 | + $this->decorator->decorateFormElement( |
| 27 | + $result, |
| 28 | + new TextElement('test', ['class' => 'autosubmit', 'id' => 'test-id']) |
| 29 | + ); |
| 30 | + |
| 31 | + $this->assertSame('', $result->assemble()->render()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testNoDecorationForCheckboxWithoutAutosubmitClass(): void |
| 35 | + { |
| 36 | + $element = new CheckboxElement('test', ['id' => 'test-id']); |
| 37 | + $result = new FormElementDecorationResult(); |
| 38 | + $this->decorator->decorateFormElement($result, $element); |
| 39 | + |
| 40 | + $this->assertSame('', $result->assemble()->render()); |
| 41 | + $this->assertFalse($element->getAttributes()->has('aria-describedby')); |
| 42 | + } |
| 43 | + |
| 44 | + public function testDecorationForAutosubmitCheckboxWithId(): void |
| 45 | + { |
| 46 | + $element = new CheckboxElement('test', ['class' => 'autosubmit', 'id' => 'test-id']); |
| 47 | + $result = new FormElementDecorationResult(); |
| 48 | + $this->decorator->decorateFormElement($result, $element); |
| 49 | + |
| 50 | + $title = 'This page will be automatically updated upon change of the value'; |
| 51 | + $html = <<<HTML |
| 52 | +<i class="icon fa-rotate-right spinner fa" aria-hidden="true" role="img" title="$title"></i> |
| 53 | +<span class="sr-only" id="autosubmit_spinner_test-id">$title</span> |
| 54 | +HTML; |
| 55 | + |
| 56 | + $this->assertHtml($html, $result->assemble()); |
| 57 | + $this->assertSame( |
| 58 | + 'autosubmit_spinner_test-id', |
| 59 | + $element->getAttributes()->get('aria-describedby')->getValue() |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public function testDecoratorGeneratesIdWhenNoneIsSet(): void |
| 64 | + { |
| 65 | + $element = new CheckboxElement('test', ['class' => 'autosubmit']); |
| 66 | + $result = new FormElementDecorationResult(); |
| 67 | + $this->decorator->decorateFormElement($result, $element); |
| 68 | + |
| 69 | + $ariaDescribedBy = $element->getAttributes()->get('aria-describedby')->getValue(); |
| 70 | + $this->assertNotEmpty($ariaDescribedBy); |
| 71 | + $this->assertStringStartsWith('autosubmit_spinner_', $ariaDescribedBy); |
| 72 | + } |
| 73 | + |
| 74 | + public function testCustomUniqueNameCallback(): void |
| 75 | + { |
| 76 | + $this->decorator->getAttributes()->set('uniqueName', fn($name) => 'custom-' . $name); |
| 77 | + |
| 78 | + $element = new CheckboxElement('test', ['class' => 'autosubmit']); |
| 79 | + $result = new FormElementDecorationResult(); |
| 80 | + $this->decorator->decorateFormElement($result, $element); |
| 81 | + |
| 82 | + $this->assertSame( |
| 83 | + 'autosubmit_spinner_custom-test', |
| 84 | + $element->getAttributes()->get('aria-describedby')->getValue() |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + public function testSpinnerIdUsesExplicitIdOverGeneratedOne(): void |
| 89 | + { |
| 90 | + $element = new CheckboxElement('test', ['class' => 'autosubmit', 'id' => 'explicit-id']); |
| 91 | + $result = new FormElementDecorationResult(); |
| 92 | + $this->decorator->decorateFormElement($result, $element); |
| 93 | + |
| 94 | + $this->assertSame( |
| 95 | + 'autosubmit_spinner_explicit-id', |
| 96 | + $element->getAttributes()->get('aria-describedby')->getValue() |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
0 commit comments