ILIAS  release_8 Revision v8.24
CheckboxInputTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../../Base.php");
23require_once(__DIR__ . "/InputTest.php");
24
29use ILIAS\Data;
30use ILIAS\Refinery\Factory as Refinery;
31
33{
35 protected Refinery $refinery;
36
37 public function setUp(): void
38 {
39 $this->name_source = new DefNamesource();
40 $this->refinery = new Refinery($this->createMock(Data\Factory::class), $this->createMock(ilLanguage::class));
41 }
42
43 protected function buildFactory(): I\Input\Field\Factory
44 {
45 $df = new Data\Factory();
46 $language = $this->createMock(ilLanguage::class);
47 return new I\Input\Field\Factory(
48 $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
49 new SignalGenerator(),
50 $df,
51 new Refinery($df, $language),
52 $language
53 );
54 }
55
56 public function testImplementsFactoryInterface(): void
57 {
58 $f = $this->buildFactory();
59
60 $checkbox = $f->checkbox("label", "byline");
61
62 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $checkbox);
63 $this->assertInstanceOf(Field\Checkbox::class, $checkbox);
64 }
65
66 public function testRender(): void
67 {
68 $f = $this->buildFactory();
69 $label = "label";
70 $byline = "byline";
71 $checkbox = $f->checkbox($label, $byline)->withNameFrom($this->name_source);
72
73 $r = $this->getDefaultRenderer();
74 $html = $this->brutallyTrimHTML($r->render($checkbox));
75
76 $expected = $this->brutallyTrimHTML('
77 <div class="form-group row">
78 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
79 <div class="col-sm-8 col-md-9 col-lg-10">
80 <input type="checkbox" id="id_1" value="checked" name="name_0" class="form-control form-control-sm"/>
81 <div class="help-block">byline</div>
82 </div>
83 </div>
84 ');
85 $this->assertHTMLEquals($expected, $html);
86 }
87
88 public function testRenderError(): void
89 {
90 $f = $this->buildFactory();
91 $label = "label";
92 $byline = "byline";
93 $error = "an_error";
94 $checkbox = $f->checkbox($label, $byline)->withNameFrom($this->name_source)->withError($error);
95
96 $r = $this->getDefaultRenderer();
97 $html = $this->brutallyTrimHTML($r->render($checkbox));
98 $expected = $this->brutallyTrimHTML('
99 <div class="form-group row">
100 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
101 <div class="col-sm-8 col-md-9 col-lg-10">
102 <div class="help-block alert alert-danger" aria-describedby="id_1" role="alert">an_error</div>
103 <input type="checkbox" id="id_1" value="checked" name="name_0" class="form-control form-control-sm"/>
104 <div class="help-block">byline</div>
105 </div>
106 </div>
107 ');
108
109 $this->assertHTMLEquals($expected, $html);
110 }
111
112 public function testRenderNoByline(): void
113 {
114 $f = $this->buildFactory();
115 $label = "label";
116 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
117
118 $r = $this->getDefaultRenderer();
119 $html = $this->brutallyTrimHTML($r->render($checkbox));
120
121 $expected = $this->brutallyTrimHTML('
122 <div class="form-group row">
123 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
124 <div class="col-sm-8 col-md-9 col-lg-10">
125 <input type="checkbox" id="id_1" value="checked" name="name_0" class="form-control form-control-sm" />
126 </div>
127 </div>
128 ');
129 $this->assertHTMLEquals($expected, $html);
130 }
131
132 public function testRenderValue(): void
133 {
134 $f = $this->buildFactory();
135 $label = "label";
136 $value = true;
137 $checkbox = $f->checkbox($label)->withValue($value)->withNameFrom($this->name_source);
138
139 $r = $this->getDefaultRenderer();
140 $html = $this->brutallyTrimHTML($r->render($checkbox));
141 $expected = $this->brutallyTrimHTML('
142 <div class="form-group row">
143 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
144 <div class="col-sm-8 col-md-9 col-lg-10">
145 <input type="checkbox" id="id_1" value="checked" checked="checked" name="name_0" class="form-control form-control-sm" />
146 </div>
147 </div>
148 ');
149 $this->assertHTMLEquals($expected, $html);
150 }
151
152 public function testHandleInvalidValue(): void
153 {
154 $f = $this->buildFactory();
155 $label = "label";
156 $value = "invalid";
157 try {
158 $f->checkbox($label)->withValue($value);
159 $this->fail();
160 } catch (InvalidArgumentException $e) {
161 $this->assertTrue(true);
162 }
163 }
164
165 public function testRenderRequired(): void
166 {
167 $f = $this->buildFactory();
168 $label = "label";
169 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source)->withRequired(true);
170
171 $r = $this->getDefaultRenderer();
172 $html = $this->brutallyTrimHTML($r->render($checkbox));
173
174 $expected = $this->brutallyTrimHTML('
175 <div class="form-group row">
176 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label<span class="asterisk">*</span></label>
177 <div class="col-sm-8 col-md-9 col-lg-10"><input type="checkbox" id="id_1" value="checked" name="name_0" class="form-control form-control-sm"/></div>
178 </div>
179 ');
180
181 $this->assertHTMLEquals($expected, $html);
182 }
183
184 public function testRenderDisabled(): void
185 {
186 $f = $this->buildFactory();
187 $label = "label";
188 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source)->withDisabled(true);
189
190 $r = $this->getDefaultRenderer();
191 $html = $this->brutallyTrimHTML($r->render($checkbox));
192
193 $expected = $this->brutallyTrimHTML('
194 <div class="form-group row">
195 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
196 <div class="col-sm-8 col-md-9 col-lg-10"><input type="checkbox" id="id_1" value="checked" name="name_0" disabled="disabled" class="form-control form-control-sm"/></div>
197 </div>
198 ');
199
200 $this->assertHTMLEquals($expected, $html);
201 }
202
203 public function testTrueContent(): void
204 {
205 $f = $this->buildFactory();
206 $label = "label";
207 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
208
209 $input_data = $this->createMock(InputData::class);
210 $input_data
211 ->expects($this->atLeastOnce())
212 ->method("getOr")
213 ->with("name_0", "")
214 ->willReturn("checked");
215
216 $checkbox_true = $checkbox->withInput($input_data);
217
218 $this->assertIsBool($checkbox_true->getContent()->value());
219 $this->assertTrue($checkbox_true->getContent()->value());
220 }
221
222 public function testFalseContent(): void
223 {
224 $f = $this->buildFactory();
225 $label = "label";
226 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
227
228 $input_data = $this->createMock(InputData::class);
229 $input_data
230 ->expects($this->atLeastOnce())
231 ->method("getOr")
232 ->with("name_0", "")
233 ->willReturn("");
234
235 $checkbox_false = $checkbox->withInput($input_data);
236
237 $this->assertIsBool($checkbox_false->getContent()->value());
238 $this->assertFalse($checkbox_false->getContent()->value());
239 }
240
241 public function testDisabledContent(): void
242 {
243 $f = $this->buildFactory();
244 $label = "label";
245 $checkbox = $f->checkbox($label)
246 ->withNameFrom($this->name_source)
247 ->withDisabled(true)
248 ->withValue(true)
249 ->withInput($this->createMock(InputData::class))
250 ;
251
252 $this->assertIsBool($checkbox->getContent()->value());
253 $this->assertTrue($checkbox->getContent()->value());
254 }
255
256 public function testTransformation(): void
257 {
258 $f = $this->buildFactory();
259 $label = "label";
260 $new_value = "NEW_VALUE";
261 $checkbox = $f->checkbox($label)
262 ->withNameFrom($this->name_source)
263 ->withDisabled(true)
264 ->withValue(true)
265 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called, $new_value): string {
266 $called = $v;
267 return $new_value;
268 }))
269 ->withInput($this->createMock(InputData::class))
270 ;
271
272 $this->assertIsString($checkbox->getContent()->value());
273 $this->assertEquals($new_value, $checkbox->getContent()->value());
274 }
275
276 public function testNullValue(): void
277 {
278 $f = $this->buildFactory();
279 $checkbox = $f->checkbox("label");
280 $checkbox->withValue(null);
281 $this->assertEquals(false, $checkbox->getValue());
282 }
283}
static return function(ContainerConfigurator $containerConfigurator)
Definition: basic_rector.php:9
DefNamesource $name_source
Builds data types.
Definition: Factory.php:21
Provides common functionality for UI tests.
Definition: Base.php:299
assertHTMLEquals(string $expected_html_as_string, string $html_as_string)
Definition: Base.php:427
brutallyTrimHTML(string $html)
A more radical version of normalizeHTML.
Definition: Base.php:444
getDefaultRenderer(JavaScriptBinding $js_binding=null, array $with_stub_renderings=[])
Definition: Base.php:355
This is a legacy support of Component\Input\Field\Input that has been moved to Component\Input\Contai...
Definition: Input.php:32
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider \MainMenu\Provider.
Class Factory.