ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
NumericInputTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/../../../../../../../vendor/composer/vendor/autoload.php");
22 require_once(__DIR__ . "/../../../Base.php");
23 require_once(__DIR__ . "/InputTest.php");
24 require_once(__DIR__ . "/CommonFieldRendering.php");
25 
29 use ILIAS\Data;
32 
34 {
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 
102  public function testNullValue(): \ILIAS\UI\Component\Input\Container\Form\FormInput
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')]
119  public function testEmptyValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field): void
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')]
134  public function testZeroIsValidValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field): void
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')]
149  public function testConstraintForRequirementForFloat(\ILIAS\UI\Component\Input\Container\Form\FormInput $field): void
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')]
165  public function testFloatValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field): void
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')]
177  public function testFloatValueOffsetStep(\ILIAS\UI\Component\Input\Container\Form\FormInput $field): void
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')]
189  public function testFloatValueForInt(\ILIAS\UI\Component\Input\Container\Form\FormInput $field): void
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 }
testFloatValueForInt(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
testEmptyValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testConstraintForRequirementForFloat(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
testFloatValueOffsetStep(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
DefNamesource $name_source
testFloatValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)
This describes inputs that can be used in forms.
Definition: FormInput.php:32
testZeroIsValidValue(\ILIAS\UI\Component\Input\Container\Form\FormInput $field)