ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
RadioInputTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once(__DIR__ . "/../../../../../../../vendor/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../../Base.php");
23require_once(__DIR__ . "/CommonFieldRendering.php");
24require_once(__DIR__ . "/HasOptionFilterTestHelper.php");
25
29use ILIAS\Data;
30use ILIAS\Refinery\Factory as Refinery;
31
33{
34 use CommonFieldRendering;
35 use HasOptionFilterTestHelper;
36
38
39 public function setUp(): void
40 {
41 $this->name_source = new DefNamesource();
42 }
43
44 protected function buildRadio(bool $with_has_option_filter = false): \ILIAS\UI\Component\Input\Container\Form\FormInput
45 {
46 $f = $this->getFieldFactory();
47 $label = "label";
48 $byline = "byline";
49 $return = $f
50 ->radio($label, $byline)
51 ->withOption('value0', 'label0', 'byline0')
52 ->withOption('1', 'label1', 'byline1')
53 ->withNameFrom($this->name_source);
54 if ($with_has_option_filter) {
55 $return = $return->withHasOptionFilter();
56 }
57 return $return;
58 }
59
60 public function testImplementsFactoryInterface(): void
61 {
62 $f = $this->getFieldFactory();
63 $radio = $f->radio("label", "byline");
64 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $radio);
65 $this->assertInstanceOf(Field\Radio::class, $radio);
66 }
67
68 public function testRender(): void
69 {
70 $r = $this->getDefaultRenderer();
71 $radio = $this->buildRadio();
72 $name = $radio->getName();
73 $label = $radio->getLabel();
74 $byline = $radio->getByline();
75 $options = $radio->getOptions();
76
77 $expected_options = "";
78 foreach ($options as $opt_value => $opt_label) {
79 $expected_options .= ""
80 . '<div class="c-field-radio__item">'
81 . "<input type=\"radio\" id=\"id_1_" . $opt_value . "_opt\" name=\"$name\" value=\"$opt_value\" />"
82 . "<label for=\"id_1_" . $opt_value . "_opt\">$opt_label</label>"
83 . "<div class=\"c-input__help-byline\">{$radio->getBylineFor((string) $opt_value)}</div>"
84 . '</div>';
85 }
86 $expected = $this->getFormWrappedHtml(
87 'radio-field-input',
88 $label,
89 '<div class="c-field-radio">' . $expected_options . '</div>',
90 $byline,
91 null
92 );
93 $this->assertEquals($expected, $this->render($radio));
94 }
95
96 public function testRenderValue(): void
97 {
98 $r = $this->getDefaultRenderer();
99 $radio = $this->buildRadio();
100 $name = $radio->getName();
101 $label = $radio->getLabel();
102 $byline = $radio->getByline();
103 $options = $radio->getOptions();
104 $value = '1';
105 $radio = $radio->withValue($value);
106 $expected_options = "";
107 foreach ($options as $opt_value => $opt_label) {
108 $expected_options .= '<div class="c-field-radio__item">';
109 if ($opt_value == $value) {
110 $expected_options .= "<input type=\"radio\" id=\"id_1_" . $opt_value . "_opt\" name=\"$name\" value=\"$opt_value\" checked=\"checked\" />";
111 } else {
112 $expected_options .= "<input type=\"radio\" id=\"id_1_" . $opt_value . "_opt\" name=\"$name\" value=\"$opt_value\" />";
113 }
114 $expected_options .= ""
115 . "<label for=\"id_1_" . $opt_value . "_opt\">$opt_label</label>"
116 . "<div class=\"c-input__help-byline\">{$radio->getBylineFor((string) $opt_value)}</div>"
117 . '</div>';
118 }
119 $expected = $this->getFormWrappedHtml(
120 'radio-field-input',
121 $label,
122 '<div class="c-field-radio">' . $expected_options . '</div>',
123 $byline,
124 null
125 );
126 $this->assertEquals($expected, $this->render($radio));
127 }
128
129 public function testRenderWithHasOptionFilter(): void
130 {
131 $radio = $this->buildRadio(true);
132
133 $this->testHasOptionFilter($radio);
134 }
135
136 public function testCommonRendering(): void
137 {
138 $f = $this->getFieldFactory();
139 $radio = $f->radio('label', null)->withNameFrom($this->name_source);
140
141 $this->testWithError($radio);
142 $this->testWithNoByline($radio);
143 $this->testWithRequired($radio);
144 $this->testWithDisabled($radio);
145 $this->testWithAdditionalOnloadCodeRendersId($radio);
146 }
147
148 public function testRadioValueOK(): void
149 {
150 $radio = $this->buildRadio()->withValue('value0');
151 $this->assertEquals('value0', $radio->getValue());
152 }
153
154 public function testRadioValueNotOK(): void
155 {
156 $this->expectException(\InvalidArgumentException::class);
157 $radio = $this->buildRadio()->withValue('something not in options');
158 }
159
160 public function testRadioValueNotOKType(): void
161 {
162 $this->expectException(\InvalidArgumentException::class);
163 $radio = $this->buildRadio()->withValue('');
164 }
165
166 public function testRadioValueOKForNull(): void
167 {
168 $radio = $this->buildRadio()->withValue(null);
169 $this->assertNull($radio->getValue());
170 }
171}
Builds data types.
Definition: Factory.php:36
Definition: UI.php:24
Provides common functionality for UI tests.
Definition: Base.php:337
testImplementsFactoryInterface()
buildRadio(bool $with_has_option_filter=false)
DefNamesource $name_source
This describes inputs that can be used in forms.
Definition: FormInput.php:33
This describes commonalities between all inputs.
Definition: Input.php:47
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.