ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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->normalizeHTML($r->render($numeric));
56
57 $expected = "<div class=\"form-group row\">" . " <label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
58 . " <div class=\"col-sm-9\">" . " <input type=\"number\" name=\"$name\" class=\"form-control form-control-sm\" />"
59 . " <div class=\"help-block\">$byline</div>" . " " . " </div>" . "</div>";
60 $this->assertEquals($expected, $html);
61 }
62
63
64 public function test_render_error()
65 {
66 $f = $this->buildFactory();
67 $label = "label";
68 $byline = "byline";
69 $name = "name_0";
70 $error = "an_error";
71 $numeric = $f->numeric($label, $byline)->withNameFrom($this->name_source)->withError($error);
72
73 $r = $this->getDefaultRenderer();
74 $html = $this->normalizeHTML($r->render($numeric));
75
76 $expected = "<div class=\"form-group row\">" . " <label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
77 . " <div class=\"col-sm-9\">" . " <input type=\"number\" name=\"$name\" class=\"form-control form-control-sm\" />"
78 . " <div class=\"help-block\">$byline</div>" . " <div class=\"help-block alert alert-danger\" role=\"alert\">"
79 . " <img border=\"0\" src=\"./templates/default/images/icon_alert.svg\" alt=\"alert\" />" . " $error"
80 . " </div>" . " </div>" . "</div>";
81 $this->assertEquals($expected, $html);
82 }
83
84
85 public function test_render_no_byline()
86 {
87 $f = $this->buildFactory();
88 $label = "label";
89 $name = "name_0";
90 $numeric = $f->numeric($label)->withNameFrom($this->name_source);
91
92 $r = $this->getDefaultRenderer();
93 $html = $this->normalizeHTML($r->render($numeric));
94
95 $expected = "<div class=\"form-group row\">" . " <label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
96 . " <div class=\"col-sm-9\">" . " <input type=\"number\" name=\"$name\" class=\"form-control form-control-sm\" />"
97 . " " . " " . " </div>" . "</div>";
98 $this->assertEquals($expected, $html);
99 }
100
101
102 public function test_render_value()
103 {
104 $f = $this->buildFactory();
105 $label = "label";
106 $value = "10";
107 $name = "name_0";
108 $numeric = $f->numeric($label)->withValue($value)->withNameFrom($this->name_source);
109
110 $r = $this->getDefaultRenderer();
111 $html = $this->normalizeHTML($r->render($numeric));
112
113 $expected = "<div class=\"form-group row\">" . " <label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
114 . " <div class=\"col-sm-9\">"
115 . " <input type=\"number\" value=\"$value\" name=\"$name\" class=\"form-control form-control-sm\" />" . " " . " "
116 . " </div>" . "</div>";
117 $this->assertEquals($expected, $html);
118 }
119
120 public function test_render_disabled()
121 {
122 $f = $this->buildFactory();
123 $label = "label";
124 $name = "name_0";
125 $numeric = $f->numeric($label)->withNameFrom($this->name_source)->withDisabled(true);
126
127 $r = $this->getDefaultRenderer();
128 $html = $this->normalizeHTML($r->render($numeric));
129
130 $expected = "<div class=\"form-group row\">" . " <label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
131 . " <div class=\"col-sm-9\">" . " <input type=\"number\" name=\"$name\" disabled=\"disabled\" class=\"form-control form-control-sm\" />"
132 . " " . " " . " </div>" . "</div>";
133 $this->assertEquals($expected, $html);
134 }
135
136 public function testNullValue()
137 {
138 $f = $this->buildFactory();
139 $post_data = new DefInputData(['name_0' => null]);
140 $field = $f->numeric('')->withNameFrom($this->name_source);
141 $field_required = $field->withRequired(true);
142
143 $value = $field->withInput($post_data)->getContent();
144 $this->assertTrue($value->isOk());
145 $this->assertNull($value->value());
146
147 $value = $field_required->withInput($post_data)->getContent();
148 $this->assertTrue($value->isError());
149 return $field;
150 }
151
155 public function testEmptyValue($field)
156 {
157 $post_data = new DefInputData(['name_0' => '']);
158 $field_required = $field->withRequired(true);
159
160 $value = $field->withInput($post_data)->getContent();
161 $this->assertTrue($value->isOk());
162 $this->assertNull($value->value());
163
164 $field_required = $field_required->withInput($post_data);
165 $value = $field_required->getContent();
166 $this->assertTrue($value->isError());
167 }
168
172 public function testZeroIsValidValue($field)
173 {
174 $post_data = new DefInputData(['name_0' => 0]);
175 $field_required = $field->withRequired(true);
176
177 $value = $field->withInput($post_data)->getContent();
178 $this->assertTrue($value->isOk());
179 $this->assertEquals(0, $value->value());
180
181 $value = $field_required->withInput($post_data)->getContent();
182 $this->assertTrue($value->isOK());
183 $this->assertEquals(0, $value->value());
184 }
185}
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:225
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:268
normalizeHTML($html)
Definition: Base.php:317
testEmptyValue($field)
@depends testNullValue
testZeroIsValidValue($field)
@depends testNullValue
if($format !==null) $name
Definition: metadata.php:230
Class ChatMainBarProvider \MainMenu\Provider.