ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
NumericInputTest.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__ . "/InputTest.php");
24require_once(__DIR__ . "/CommonFieldRendering.php");
25
29use ILIAS\Data;
30use ILIAS\Refinery\Factory as Refinery;
31use ILIAS\Data\Factory as DataFactory;
32
34{
35 use CommonFieldRendering;
36
38
39 public function setUp(): void
40 {
41 $this->name_source = new DefNamesource();
42 }
43
44 public function testImplementsFactoryInterface(): void
45 {
46 $f = $this->getFieldFactory();
47
48 $numeric = $f->numeric("label", "byline");
49
50 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $numeric);
51 $this->assertInstanceOf(Field\Numeric::class, $numeric);
52 }
53
54
55 public function testRender(): void
56 {
57 $f = $this->getFieldFactory();
58 $label = "label";
59 $byline = "byline";
60 $numeric = $f->numeric($label, $byline)->withNameFrom($this->name_source);
61
62 $expected = $this->getFormWrappedHtml(
63 'numeric-field-input',
64 $label,
65 '<input id="id_1" type="number" step="1" name="name_0" class="c-field-number" />',
66 $byline,
67 'id_1'
68 );
69 $this->assertEquals($expected, $this->render($numeric));
70 }
71
72 public function testCommonRendering(): void
73 {
74 $f = $this->getFieldFactory();
75 $label = "label";
76 $numeric = $f->numeric($label)->withNameFrom($this->name_source);
77
78 $this->testWithError($numeric);
79 $this->testWithNoByline($numeric);
80 $this->testWithRequired($numeric);
81 $this->testWithDisabled($numeric);
82 $this->testWithAdditionalOnloadCodeRendersId($numeric);
83 }
84
85 public function testRenderValue(): void
86 {
87 $f = $this->getFieldFactory();
88 $label = "label";
89 $value = "10";
90 $numeric = $f->numeric($label)->withValue($value)->withNameFrom($this->name_source);
91
92 $expected = $this->getFormWrappedHtml(
93 'numeric-field-input',
94 $label,
95 '<input id="id_1" type="number" step="1" value="10" name="name_0" class="c-field-number" />',
96 null,
97 'id_1'
98 );
99 $this->assertEquals($expected, $this->render($numeric));
100 }
101
103 {
104 $f = $this->getFieldFactory();
105 $post_data = new DefInputData(['name_0' => null]);
106 $field = $f->numeric('')->withNameFrom($this->name_source);
107 $field_required = $field->withRequired(true);
108
109 $value = $field->withInput($post_data)->getContent();
110 $this->assertTrue($value->isOk());
111 $this->assertNull($value->value());
112
113 $value = $field_required->withInput($post_data)->getContent();
114 $this->assertTrue($value->isError());
115 return $field;
116 }
117
118 #[\PHPUnit\Framework\Attributes\Depends('testNullValue')]
120 {
121 $post_data = new DefInputData(['name_0' => '']);
122 $field_required = $field->withRequired(true);
123
124 $value = $field->withInput($post_data)->getContent();
125 $this->assertTrue($value->isOk());
126 $this->assertNull($value->value());
127
128 $field_required = $field_required->withInput($post_data);
129 $value = $field_required->getContent();
130 $this->assertTrue($value->isError());
131 }
132
133 #[\PHPUnit\Framework\Attributes\Depends('testNullValue')]
135 {
136 $post_data = new DefInputData(['name_0' => 0]);
137 $field_required = $field->withRequired(true);
138
139 $value = $field->withInput($post_data)->getContent();
140 $this->assertTrue($value->isOk());
141 $this->assertEquals(0, $value->value());
142
143 $value = $field_required->withInput($post_data)->getContent();
144 $this->assertTrue($value->isOK());
145 $this->assertEquals(0, $value->value());
146 }
147
148 #[\PHPUnit\Framework\Attributes\Depends('testNullValue')]
150 {
151 $post_data = new DefInputData(['name_0' => 1.1]);
152 $field_required = $field->withRequired(true);
153
154 $value = $field->withInput($post_data)->getContent();
155 $this->assertTrue($value->isOk());
156 //Note, this float will be Transformed to int, since this input only accepts int
157 $this->assertEquals(1, $value->value());
158
159 $value = $field_required->withInput($post_data)->getContent();
160 $this->assertTrue($value->isOK());
161 $this->assertEquals(1, $value->value());
162 }
163
164 #[\PHPUnit\Framework\Attributes\Depends('testNullValue')]
166 {
167 $post_data = new DefInputData(['name_0' => 1.1]);
168 $value = $field
169 ->withStepSize(.1)
170 ->withInput($post_data)
171 ->getContent();
172 $this->assertTrue($value->isOk());
173 $this->assertEquals(1.1, $value->value());
174 }
175
176 #[\PHPUnit\Framework\Attributes\Depends('testNullValue')]
178 {
179 $post_data = new DefInputData(['name_0' => 1.2]);
180 $value = $field
181 ->withStepSize(.5)
182 ->withInput($post_data)
183 ->getContent();
184 $this->assertTrue($value->isOk());
185 $this->assertEquals(1.2, $value->value());
186 }
187
188 #[\PHPUnit\Framework\Attributes\Depends('testNullValue')]
190 {
191 $post_data = new DefInputData(['name_0' => 2]);
192 $value = $field
193 ->withStepSize(.5)
194 ->withInput($post_data)
195 ->getContent();
196 $this->assertTrue($value->isOk());
197 $this->assertEquals(2, $value->value());
198 $this->assertIsFloat($value->value());
199 }
200
201 public function testDecimalsTransformationsStack(): void
202 {
203 $data_factory = new DataFactory();
204 $language = $this->createMock(ILIAS\Language\Language::class);
205 $refinery = new Refinery($data_factory, $language);
206
207 $f = $this->getFieldFactory();
208 $field = $f->numeric('')->withNameFrom($this->name_source)
209 ->withStepSize(.2)
210 ->withAdditionalTransformation(
211 $refinery->custom()->transformation(fn(float $v): float => $v + 1)
212 );
213
214 $post_data = new DefInputData(['name_0' => 1]);
215
216 $value = $field
217 ->withInput($post_data)
218 ->getContent();
219 $this->assertTrue($value->isOk());
220 $this->assertIsFloat($value->value());
221 $this->assertEquals(2, $value->value());
222
223 $value = $field
224 ->withStepSize(0)
225 ->withInput($post_data)
226 ->getContent();
227 $this->assertIsInt($value->value());
228 $this->assertEquals(1, $value->value());
229 }
230}
Builds data types.
Definition: Factory.php:36
Definition: UI.php:24
Provides common functionality for UI tests.
Definition: Base.php:337
testConstraintForRequirementForFloat(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
testEmptyValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
testFloatValueOffsetStep(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
testZeroIsValidValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
testFloatValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
DefNamesource $name_source
testFloatValueForInt(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
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.