ILIAS  release_8 Revision v8.24
NumericInputTest.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
28use ILIAS\Data;
29use ILIAS\Refinery\Factory as Refinery;
30
32{
34
35 public function setUp(): void
36 {
37 $this->name_source = new DefNamesource();
38 }
39
40 protected function buildFactory(): I\Input\Field\Factory
41 {
42 $df = new Data\Factory();
43 $language = $this->getLanguage();
44 return new I\Input\Field\Factory(
45 $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
46 new SignalGenerator(),
47 $df,
48 new Refinery($df, $language),
49 $language
50 );
51 }
52
53 public function test_implements_factory_interface(): void
54 {
55 $f = $this->buildFactory();
56
57 $numeric = $f->numeric("label", "byline");
58
59 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $numeric);
60 $this->assertInstanceOf(Field\Numeric::class, $numeric);
61 }
62
63
64 public function test_render(): void
65 {
66 $f = $this->buildFactory();
67 $label = "label";
68 $byline = "byline";
69 $numeric = $f->numeric($label, $byline)->withNameFrom($this->name_source);
70
71 $r = $this->getDefaultRenderer();
72 $html = $this->brutallyTrimHTML($r->render($numeric));
73
74 $expected = $this->brutallyTrimHTML('
75<div class="form-group row">
76 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
77 <div class="col-sm-8 col-md-9 col-lg-10">
78 <input id="id_1" type="number" name="name_0" class="form-control form-control-sm" />
79 <div class="help-block">byline</div>
80 </div>
81</div>
82');
83 $this->assertEquals($expected, $html);
84 }
85
86 public function test_render_error(): void
87 {
88 $f = $this->buildFactory();
89 $label = "label";
90 $byline = "byline";
91 $error = "an_error";
92 $numeric = $f->numeric($label, $byline)->withNameFrom($this->name_source)->withError($error);
93
94 $r = $this->getDefaultRenderer();
95 $html = $this->brutallyTrimHTML($r->render($numeric));
96
97 $expected = $this->brutallyTrimHTML('
98<div class="form-group row">
99 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
100 <div class="col-sm-8 col-md-9 col-lg-10">
101 <div class="help-block alert alert-danger" aria-describedby="id_1" role="alert">an_error</div>
102 <input id="id_1" type="number" name="name_0" class="form-control form-control-sm" />
103 <div class="help-block">byline</div>
104 </div>
105</div>');
106 $this->assertEquals($expected, $html);
107 }
108
109 public function test_render_no_byline(): void
110 {
111 $f = $this->buildFactory();
112 $label = "label";
113 $numeric = $f->numeric($label)->withNameFrom($this->name_source);
114
115 $r = $this->getDefaultRenderer();
116 $html = $this->brutallyTrimHTML($r->render($numeric));
117
118 $expected = $this->brutallyTrimHTML('
119<div class="form-group row">
120 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
121 <div class="col-sm-8 col-md-9 col-lg-10"> <input id="id_1" type="number" name="name_0" class="form-control form-control-sm" /> </div>
122</div>
123');
124 $this->assertEquals($expected, $html);
125 }
126
127 public function test_render_value(): void
128 {
129 $f = $this->buildFactory();
130 $label = "label";
131 $value = "10";
132 $numeric = $f->numeric($label)->withValue($value)->withNameFrom($this->name_source);
133
134 $r = $this->getDefaultRenderer();
135 $html = $this->brutallyTrimHTML($r->render($numeric));
136
137 $expected = $this->brutallyTrimHTML('
138<div class="form-group row">
139 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
140 <div class="col-sm-8 col-md-9 col-lg-10"> <input id="id_1" type="number" value="10" name="name_0" class="form-control form-control-sm" /> </div>
141</div>
142');
143 $this->assertEquals($expected, $html);
144 }
145
146 public function test_render_disabled(): void
147 {
148 $f = $this->buildFactory();
149 $label = "label";
150 $numeric = $f->numeric($label)->withNameFrom($this->name_source)->withDisabled(true);
151
152 $r = $this->getDefaultRenderer();
153 $html = $this->brutallyTrimHTML($r->render($numeric));
154
155 $expected = $this->brutallyTrimHTML('
156<div class="form-group row">
157 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
158 <div class="col-sm-8 col-md-9 col-lg-10"> <input id="id_1" type="number" name="name_0" disabled="disabled" class="form-control form-control-sm" /> </div>
159</div>');
160 $this->assertEquals($expected, $html);
161 }
162
163 public function testNullValue(): \ILIAS\UI\Component\Input\Container\Form\FormInput
164 {
165 $f = $this->buildFactory();
166 $post_data = new DefInputData(['name_0' => null]);
167 $field = $f->numeric('')->withNameFrom($this->name_source);
168 $field_required = $field->withRequired(true);
169
170 $value = $field->withInput($post_data)->getContent();
171 $this->assertTrue($value->isOk());
172 $this->assertNull($value->value());
173
174 $value = $field_required->withInput($post_data)->getContent();
175 $this->assertTrue($value->isError());
176 return $field;
177 }
178
182 public function testEmptyValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field): void
183 {
184 $post_data = new DefInputData(['name_0' => '']);
185 $field_required = $field->withRequired(true);
186
187 $value = $field->withInput($post_data)->getContent();
188 $this->assertTrue($value->isOk());
189 $this->assertNull($value->value());
190
191 $field_required = $field_required->withInput($post_data);
192 $value = $field_required->getContent();
193 $this->assertTrue($value->isError());
194 }
195
199 public function testZeroIsValidValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field): void
200 {
201 $post_data = new DefInputData(['name_0' => 0]);
202 $field_required = $field->withRequired(true);
203
204 $value = $field->withInput($post_data)->getContent();
205 $this->assertTrue($value->isOk());
206 $this->assertEquals(0, $value->value());
207
208 $value = $field_required->withInput($post_data)->getContent();
209 $this->assertTrue($value->isOK());
210 $this->assertEquals(0, $value->value());
211 }
212
216 public function testConstraintForRequirementForFloat(\ILIAS\UI\Component\Input\Container\Form\FormInput $field): void
217 {
218 $post_data = new DefInputData(['name_0' => 1.1]);
219 $field_required = $field->withRequired(true);
220
221 $value = $field->withInput($post_data)->getContent();
222 $this->assertTrue($value->isOk());
223 //Note, this float will be Transformed to int, since this input only accepts int
224 $this->assertEquals(1, $value->value());
225
226 $value = $field_required->withInput($post_data)->getContent();
227 $this->assertTrue($value->isOK());
228 $this->assertEquals(1, $value->value());
229 }
230}
Builds data types.
Definition: Factory.php:21
Provides common functionality for UI tests.
Definition: Base.php:299
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
testConstraintForRequirementForFloat(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
@depends testNullValue
testEmptyValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
@depends testNullValue
testZeroIsValidValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
@depends testNullValue
DefNamesource $name_source
This describes inputs that can be used in forms.
Definition: FormInput.php:32
This is a legacy support of Component\Input\Field\Input that has been moved to Component\Input\Contai...
Definition: Input.php:32
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.