ILIAS  release_7 Revision v7.30-3-g800a261c036
NumericInputTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2017 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
5require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
6require_once(__DIR__ . "/../../../Base.php");
7require_once(__DIR__ . "/InputTest.php");
8
10use \ILIAS\UI\Component\Input\Field;
11use \ILIAS\Data;
13
15{
16 public function setUp() : void
17 {
18 $this->name_source = new DefNamesource();
19 }
20
21
22 protected function buildFactory()
23 {
24 $df = new Data\Factory();
25 $language = $this->getLanguage();
27 new SignalGenerator(),
28 $df,
29 new \ILIAS\Refinery\Factory($df, $language),
30 $language
31 );
32 }
33
34
36 {
37 $f = $this->buildFactory();
38
39 $numeric = $f->numeric("label", "byline");
40
41 $this->assertInstanceOf(Field\Input::class, $numeric);
42 $this->assertInstanceOf(Field\Numeric::class, $numeric);
43 }
44
45
46 public function test_render()
47 {
48 $f = $this->buildFactory();
49 $label = "label";
50 $byline = "byline";
51 $name = "name_0";
52 $numeric = $f->numeric($label, $byline)->withNameFrom($this->name_source);
53
54 $r = $this->getDefaultRenderer();
55 $html = $this->brutallyTrimHTML($r->render($numeric));
56
57 $expected = $this->brutallyTrimHTML('
58<div class="form-group row">
59 <label for="id_1" class="control-label col-sm-3">label</label>
60 <div class="col-sm-9">
61 <input id="id_1" type="number" name="name_0" class="form-control form-control-sm" />
62 <div class="help-block">byline</div>
63 </div>
64</div>
65');
66 $this->brutallyTrimHTML($expected, $html);
67 $this->assertEquals($expected, $html);
68 }
69
70
71 public function test_render_error()
72 {
73 $f = $this->buildFactory();
74 $label = "label";
75 $byline = "byline";
76 $name = "name_0";
77 $error = "an_error";
78 $numeric = $f->numeric($label, $byline)->withNameFrom($this->name_source)->withError($error);
79
80 $r = $this->getDefaultRenderer();
81 $html = $this->brutallyTrimHTML($r->render($numeric));
82
83 $expected = $this->brutallyTrimHTML('
84<div class="form-group row">
85 <label for="id_1" class="control-label col-sm-3">label</label>
86 <div class="col-sm-9">
87 <div class="help-block alert alert-danger" role="alert">an_error</div>
88 <input id="id_1" type="number" name="name_0" class="form-control form-control-sm" />
89 <div class="help-block">byline</div>
90 </div>
91</div>');
92 $this->assertEquals($expected, $html);
93 }
94
95
96 public function test_render_no_byline()
97 {
98 $f = $this->buildFactory();
99 $label = "label";
100 $name = "name_0";
101 $numeric = $f->numeric($label)->withNameFrom($this->name_source);
102
103 $r = $this->getDefaultRenderer();
104 $html = $this->brutallyTrimHTML($r->render($numeric));
105
106 $expected = $this->brutallyTrimHTML('
107<div class="form-group row">
108 <label for="id_1" class="control-label col-sm-3">label</label>
109 <div class="col-sm-9"> <input id="id_1" type="number" name="name_0" class="form-control form-control-sm" /> </div>
110</div>
111');
112 $this->assertEquals($expected, $html);
113 }
114
115
116 public function test_render_value()
117 {
118 $f = $this->buildFactory();
119 $label = "label";
120 $value = "10";
121 $name = "name_0";
122 $numeric = $f->numeric($label)->withValue($value)->withNameFrom($this->name_source);
123
124 $r = $this->getDefaultRenderer();
125 $html = $this->brutallyTrimHTML($r->render($numeric));
126
127 $expected = $this->brutallyTrimHTML('
128<div class="form-group row">
129 <label for="id_1" class="control-label col-sm-3">label</label>
130 <div class="col-sm-9"> <input id="id_1" type="number" value="10" name="name_0" class="form-control form-control-sm" /> </div>
131</div>
132');
133 $this->assertEquals($expected, $html);
134 }
135
136 public function test_render_disabled()
137 {
138 $f = $this->buildFactory();
139 $label = "label";
140 $name = "name_0";
141 $numeric = $f->numeric($label)->withNameFrom($this->name_source)->withDisabled(true);
142
143 $r = $this->getDefaultRenderer();
144 $html = $this->brutallyTrimHTML($r->render($numeric));
145
146 $expected = $this->brutallyTrimHTML('
147<div class="form-group row">
148 <label for="id_1" class="control-label col-sm-3">label</label>
149 <div class="col-sm-9"> <input id="id_1" type="number" name="name_0" disabled="disabled" class="form-control form-control-sm" /> </div>
150</div>');
151 $this->assertEquals($expected, $html);
152 }
153
154 public function testNullValue()
155 {
156 $f = $this->buildFactory();
157 $post_data = new DefInputData(['name_0' => null]);
158 $field = $f->numeric('')->withNameFrom($this->name_source);
159 $field_required = $field->withRequired(true);
160
161 $value = $field->withInput($post_data)->getContent();
162 $this->assertTrue($value->isOk());
163 $this->assertNull($value->value());
164
165 $value = $field_required->withInput($post_data)->getContent();
166 $this->assertTrue($value->isError());
167 return $field;
168 }
169
173 public function testEmptyValue($field)
174 {
175 $post_data = new DefInputData(['name_0' => '']);
176 $field_required = $field->withRequired(true);
177
178 $value = $field->withInput($post_data)->getContent();
179 $this->assertTrue($value->isOk());
180 $this->assertNull($value->value());
181
182 $field_required = $field_required->withInput($post_data);
183 $value = $field_required->getContent();
184 $this->assertTrue($value->isError());
185 }
186
190 public function testZeroIsValidValue($field)
191 {
192 $post_data = new DefInputData(['name_0' => 0]);
193 $field_required = $field->withRequired(true);
194
195 $value = $field->withInput($post_data)->getContent();
196 $this->assertTrue($value->isOk());
197 $this->assertEquals(0, $value->value());
198
199 $value = $field_required->withInput($post_data)->getContent();
200 $this->assertTrue($value->isOK());
201 $this->assertEquals(0, $value->value());
202 }
203}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
Provides common functionality for UI tests.
Definition: Base.php:263
getDefaultRenderer(JavaScriptBinding $js_binding=null, $with_stub_renderings=[])
Definition: Base.php:311
brutallyTrimHTML($html)
A more radical version of normalizeHTML.
Definition: Base.php:392
testEmptyValue($field)
@depends testNullValue
testZeroIsValidValue($field)
@depends testNullValue
if($format !==null) $name
Definition: metadata.php:230
Class ChatMainBarProvider \MainMenu\Provider.