ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
5 require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
6 require_once(__DIR__ . "/../../../Base.php");
7 require_once(__DIR__ . "/InputTest.php");
8 
10 use \ILIAS\UI\Component\Input\Field;
11 use \ILIAS\Data;
12 use \ILIAS\Validation;
13 use \ILIAS\Transformation;
14 
16 {
17  public function setUp()
18  {
19  $this->name_source = new DefNamesource();
20  }
21 
22 
23  protected function buildFactory()
24  {
25  $df = new Data\Factory();
27  new SignalGenerator(),
28  $df,
29  new Validation\Factory($df, $this->getLanguage()),
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 testNullValue()
121  {
122  $f = $this->buildFactory();
123  $post_data = new DefPostData(['name_0' => null]);
124  $field = $f->numeric('')->withNameFrom($this->name_source);
125  $field_required = $field->withRequired(true);
126 
127  $value = $field->withInput($post_data)->getContent();
128  $this->assertTrue($value->isOk());
129  $this->assertNull($value->value());
130 
131  $value = $field_required->withInput($post_data)->getContent();
132  $this->assertTrue($value->isError());
133  return $field;
134  }
135 
139  public function testEmptyValue($field)
140  {
141  $post_data = new DefPostData(['name_0' => '']);
142  $field_required = $field->withRequired(true);
143 
144  $value = $field->withInput($post_data)->getContent();
145  $this->assertTrue($value->isOk());
146  $this->assertNull($value->value());
147  $this->assertFalse($value->value() === '');
148 
149  $value = $field_required->withInput($post_data)->getContent();
150  $this->assertTrue($value->isError());
151  }
152 }
A transformation is a function from one datatype to another.
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:228
normalizeHTML($html)
Definition: Base.php:261
$r
Definition: example_031.php:79
Provides common functionality for UI tests.
Definition: Base.php:191
Builds data types.
Definition: Factory.php:14
testEmptyValue($field)
testNullValue
$html
Definition: example_001.php:87