ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
CheckboxInputTest.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
11use \ILIAS\UI\Component\Input\Field;
12use \ILIAS\Data;
13use ILIAS\Refinery\Factory as Refinery;
14
16{
17 public function setUp() : void
18 {
19 $this->name_source = new DefNamesource();
20 $this->refinery = new Refinery($this->createMock(Data\Factory::class), $this->createMock(\ilLanguage::class));
21 }
22
23
24 protected function buildFactory()
25 {
26 $df = new Data\Factory();
27 $language = $this->createMock(\ilLanguage::class);
29 new SignalGenerator(),
30 $df,
31 new \ILIAS\Refinery\Factory($df, $language),
32 $language
33 );
34 }
35
36
38 {
39 $f = $this->buildFactory();
40
41 $checkbox = $f->checkbox("label", "byline");
42
43 $this->assertInstanceOf(Field\Input::class, $checkbox);
44 $this->assertInstanceOf(Field\Checkbox::class, $checkbox);
45 }
46
47
48 public function testRender()
49 {
50 $f = $this->buildFactory();
51 $label = "label";
52 $byline = "byline";
53 $checkbox = $f->checkbox($label, $byline)->withNameFrom($this->name_source);
54
55 $r = $this->getDefaultRenderer();
56 $html = $r->render($checkbox);
57
58 $expected = "<div class=\"form-group row\"> <label for=\"name_0\" class=\"control-label col-sm-3\">label</label> <div class=\"col-sm-9\"> <input type=\"checkbox\" value=\"checked\" name=\"name_0\" class=\"form-control form-control-sm\" /> <div class=\"help-block\">byline</div> </div></div>";
59 $this->assertHTMLEquals($expected, $html);
60 }
61
62
63 public function testRenderError()
64 {
65 $f = $this->buildFactory();
66 $label = "label";
67 $byline = "byline";
68 $error = "an_error";
69 $checkbox = $f->checkbox($label, $byline)->withNameFrom($this->name_source)->withError($error);
70
71 $r = $this->getDefaultRenderer();
72 $html = $r->render($checkbox);
73
74 $expected = "<div class=\"form-group row\"> <label for=\"name_0\" class=\"control-label col-sm-3\">label</label> <div class=\"col-sm-9\"> <input type=\"checkbox\" value=\"checked\" name=\"name_0\" class=\"form-control form-control-sm\" /> <div class=\"help-block\">byline</div> <div class=\"help-block alert alert-danger\" role=\"alert\"> <img border=\"0\" src=\" ./templates/default/images/icon_alert.svg\" alt=\"alert\" />" . " $error"
75 . " </div></div></div>";
76 $this->assertHTMLEquals($expected, $html);
77 }
78
79
80 public function testRenderNoByline()
81 {
82 $f = $this->buildFactory();
83 $label = "label";
84 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
85
86 $r = $this->getDefaultRenderer();
87 $html = $r->render($checkbox);
88
89 $expected = "<div class=\"form-group row\"> <label for=\"name_0\" class=\"control-label col-sm-3\">label</label> <div class=\"col-sm-9\"> <input type=\"checkbox\" value=\"checked\" name=\"name_0\" class=\"form-control form-control-sm\" /> </div></div>";
90 $this->assertHTMLEquals($expected, $html);
91 }
92
93
94 public function testRenderValue()
95 {
96 $f = $this->buildFactory();
97 $label = "label";
98 $value = true;
99 $checkbox = $f->checkbox($label)->withValue($value)->withNameFrom($this->name_source);
100
101 $r = $this->getDefaultRenderer();
102 $html = $r->render($checkbox);
103
104 $expected = "<div class=\"form-group row\"> <label for=\"name_0\" class=\"control-label col-sm-3\">label</label> <div class=\"col-sm-9\"> <input type=\"checkbox\" value=\"checked\" checked=\"checked\" name=\"name_0\" class=\"form-control form-control-sm\" /> </div></div>";
105 $this->assertHTMLEquals($expected, $html);
106 }
107
108 public function testHandleInvalidValue()
109 {
110 $f = $this->buildFactory();
111 $label = "label";
112 $value = "invalid";
113 try {
114 $f->checkbox($label)->withValue($value);
115 $this->assertFalse(true);
116 } catch (InvalidArgumentException $e) {
117 $this->assertTrue(true);
118 }
119 }
120
121
122 public function testRenderRequired()
123 {
124 $f = $this->buildFactory();
125 $label = "label";
126 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source)->withRequired(true);
127
128 $r = $this->getDefaultRenderer();
129 $html = $r->render($checkbox);
130
131 $expected = "<div class=\"form-group row\"> <label for=\"name_0\" class=\"control-label col-sm-3\">label<span class=\"asterisk\">*</span></label> <div class=\"col-sm-9\"> <input type=\"checkbox\" value=\"checked\" name=\"name_0\" class=\"form-control form-control-sm\" /> </div></div>";
132 $this->assertHTMLEquals($expected, $html);
133 }
134
135 public function testRenderDisabled()
136 {
137 $f = $this->buildFactory();
138 $label = "label";
139 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source)->withDisabled(true);
140
141 $r = $this->getDefaultRenderer();
142 $html = $r->render($checkbox);
143
144 $expected = "<div class=\"form-group row\"> <label for=\"name_0\" class=\"control-label col-sm-3\">label</label> <div class=\"col-sm-9\"> <input type=\"checkbox\" value=\"checked\" name=\"name_0\" disabled=\"disabled\" class=\"form-control form-control-sm\" /> </div></div>";
145 $this->assertHTMLEquals($expected, $html);
146 }
147
148 public function testTrueContent()
149 {
150 $f = $this->buildFactory();
151 $label = "label";
152 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
153
154 $input_data = $this->createMock(InputData::class);
155 $input_data
156 ->expects($this->atLeastOnce())
157 ->method("getOr")
158 ->with("name_0", "")
159 ->willReturn("checked");
160
161 $checkbox_true = $checkbox->withInput($input_data);
162
163 $this->assertIsBool($checkbox_true->getContent()->value());
164 $this->assertTrue($checkbox_true->getContent()->value());
165 }
166
167 public function testFalseContent()
168 {
169 $f = $this->buildFactory();
170 $label = "label";
171 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
172
173 $input_data = $this->createMock(InputData::class);
174 $input_data
175 ->expects($this->atLeastOnce())
176 ->method("getOr")
177 ->with("name_0", "")
178 ->willReturn("");
179
180 $checkbox_false = $checkbox->withInput($input_data);
181
182 $this->assertIsBool($checkbox_false->getContent()->value());
183 $this->assertFalse($checkbox_false->getContent()->value());
184 }
185
186 public function testDisabledContent()
187 {
188 $f = $this->buildFactory();
189 $label = "label";
190 $checkbox = $f->checkbox($label)
191 ->withNameFrom($this->name_source)
192 ->withDisabled(true)
193 ->withValue(true)
194 ->withInput($this->createMock(InputData::class))
195 ;
196
197 $this->assertIsBool($checkbox->getContent()->value());
198 $this->assertTrue($checkbox->getContent()->value());
199 }
200
201 public function testTransformation()
202 {
203 $f = $this->buildFactory();
204 $label = "label";
205 $called = false;
206 $new_value = "NEW_VALUE";
207 $checkbox = $f->checkbox($label)
208 ->withNameFrom($this->name_source)
209 ->withDisabled(true)
210 ->withValue(true)
211 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called, $new_value) {
212 $called = $v;
213 return $new_value;
214 }))
215 ->withInput($this->createMock(InputData::class))
216 ;
217
218 $this->assertIsString($checkbox->getContent()->value());
219 $this->assertEquals($new_value, $checkbox->getContent()->value());
220 }
221}
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
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:326
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:268
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:13
Class ChatMainBarProvider \MainMenu\Provider.