ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
5 require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
6 require_once(__DIR__ . "/../../../Base.php");
7 require_once(__DIR__ . "/InputTest.php");
8 
11 use \ILIAS\UI\Component\Input\Field;
12 use \ILIAS\Data;
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 = $this->brutallyTrimHTML($r->render($checkbox));
57 
58  $expected = $this->brutallyTrimHTML('
59  <div class="form-group row">
60  <label for="id_1" class="control-label col-sm-3">label</label>
61  <div class="col-sm-9">
62  <input type="checkbox" id="id_1" value="checked" name="name_0" class="form-control form-control-sm"/>
63  <div class="help-block">byline</div>
64  </div>
65  </div>
66  ');
67  $this->assertHTMLEquals($expected, $html);
68  }
69 
70 
71  public function testRenderError()
72  {
73  $f = $this->buildFactory();
74  $label = "label";
75  $byline = "byline";
76  $error = "an_error";
77  $checkbox = $f->checkbox($label, $byline)->withNameFrom($this->name_source)->withError($error);
78 
79  $r = $this->getDefaultRenderer();
80  $html = $this->brutallyTrimHTML($r->render($checkbox));
81  $expected = $this->brutallyTrimHTML('
82  <div class="form-group row">
83  <label for="id_1" class="control-label col-sm-3">label</label>
84  <div class="col-sm-9">
85  <div class="help-block alert alert-danger" role="alert">an_error</div>
86  <input type="checkbox" id="id_1" value="checked" name="name_0" class="form-control form-control-sm"/>
87  <div class="help-block">byline</div>
88  </div>
89  </div>
90  ');
91 
92  $this->assertHTMLEquals($expected, $html);
93  }
94 
95 
96  public function testRenderNoByline()
97  {
98  $f = $this->buildFactory();
99  $label = "label";
100  $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
101 
102  $r = $this->getDefaultRenderer();
103  $html = $this->brutallyTrimHTML($r->render($checkbox));
104 
105  $expected = $this->brutallyTrimHTML('
106  <div class="form-group row">
107  <label for="id_1" class="control-label col-sm-3">label</label>
108  <div class="col-sm-9">
109  <input type="checkbox" id="id_1" value="checked" name="name_0" class="form-control form-control-sm" />
110  </div>
111  </div>
112  ');
113  $this->assertHTMLEquals($expected, $html);
114  }
115 
116 
117  public function testRenderValue()
118  {
119  $f = $this->buildFactory();
120  $label = "label";
121  $value = true;
122  $checkbox = $f->checkbox($label)->withValue($value)->withNameFrom($this->name_source);
123 
124  $r = $this->getDefaultRenderer();
125  $html = $this->brutallyTrimHTML($r->render($checkbox));
126  $expected = $this->brutallyTrimHTML('
127  <div class="form-group row">
128  <label for="id_1" class="control-label col-sm-3">label</label>
129  <div class="col-sm-9">
130  <input type="checkbox" id="id_1" value="checked" checked="checked" name="name_0" class="form-control form-control-sm" />
131  </div>
132  </div>
133  ');
134  $this->assertHTMLEquals($expected, $html);
135  }
136 
137  public function testHandleInvalidValue()
138  {
139  $f = $this->buildFactory();
140  $label = "label";
141  $value = "invalid";
142  try {
143  $f->checkbox($label)->withValue($value);
144  $this->assertFalse(true);
145  } catch (InvalidArgumentException $e) {
146  $this->assertTrue(true);
147  }
148  }
149 
150 
151  public function testRenderRequired()
152  {
153  $f = $this->buildFactory();
154  $label = "label";
155  $checkbox = $f->checkbox($label)->withNameFrom($this->name_source)->withRequired(true);
156 
157  $r = $this->getDefaultRenderer();
158  $html = $this->brutallyTrimHTML($r->render($checkbox));
159 
160  $expected = $this->brutallyTrimHTML('
161  <div class="form-group row">
162  <label for="id_1" class="control-label col-sm-3">label<span class="asterisk">*</span></label>
163  <div class="col-sm-9"><input type="checkbox" id="id_1" value="checked" name="name_0" class="form-control form-control-sm"/></div>
164  </div>
165  ');
166 
167  $this->assertHTMLEquals($expected, $html);
168  }
169 
170  public function testRenderDisabled()
171  {
172  $f = $this->buildFactory();
173  $label = "label";
174  $checkbox = $f->checkbox($label)->withNameFrom($this->name_source)->withDisabled(true);
175 
176  $r = $this->getDefaultRenderer();
177  $html = $this->brutallyTrimHTML($r->render($checkbox));
178 
179  $expected = $this->brutallyTrimHTML('
180  <div class="form-group row">
181  <label for="id_1" class="control-label col-sm-3">label</label>
182  <div class="col-sm-9"><input type="checkbox" id="id_1" value="checked" name="name_0" disabled="disabled" class="form-control form-control-sm"/></div>
183  </div>
184  ');
185 
186  $this->assertHTMLEquals($expected, $html);
187  }
188 
189  public function testTrueContent()
190  {
191  $f = $this->buildFactory();
192  $label = "label";
193  $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
194 
195  $input_data = $this->createMock(InputData::class);
196  $input_data
197  ->expects($this->atLeastOnce())
198  ->method("getOr")
199  ->with("name_0", "")
200  ->willReturn("checked");
201 
202  $checkbox_true = $checkbox->withInput($input_data);
203 
204  $this->assertIsBool($checkbox_true->getContent()->value());
205  $this->assertTrue($checkbox_true->getContent()->value());
206  }
207 
208  public function testFalseContent()
209  {
210  $f = $this->buildFactory();
211  $label = "label";
212  $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
213 
214  $input_data = $this->createMock(InputData::class);
215  $input_data
216  ->expects($this->atLeastOnce())
217  ->method("getOr")
218  ->with("name_0", "")
219  ->willReturn("");
220 
221  $checkbox_false = $checkbox->withInput($input_data);
222 
223  $this->assertIsBool($checkbox_false->getContent()->value());
224  $this->assertFalse($checkbox_false->getContent()->value());
225  }
226 
227  public function testDisabledContent()
228  {
229  $f = $this->buildFactory();
230  $label = "label";
231  $checkbox = $f->checkbox($label)
232  ->withNameFrom($this->name_source)
233  ->withDisabled(true)
234  ->withValue(true)
235  ->withInput($this->createMock(InputData::class))
236  ;
237 
238  $this->assertIsBool($checkbox->getContent()->value());
239  $this->assertTrue($checkbox->getContent()->value());
240  }
241 
242  public function testTransformation()
243  {
244  $f = $this->buildFactory();
245  $label = "label";
246  $called = false;
247  $new_value = "NEW_VALUE";
248  $checkbox = $f->checkbox($label)
249  ->withNameFrom($this->name_source)
250  ->withDisabled(true)
251  ->withValue(true)
252  ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called, $new_value) {
253  $called = $v;
254  return $new_value;
255  }))
256  ->withInput($this->createMock(InputData::class))
257  ;
258 
259  $this->assertIsString($checkbox->getContent()->value());
260  $this->assertEquals($new_value, $checkbox->getContent()->value());
261  }
262 
263  public function testNullValue() : void
264  {
265  $f = $this->buildFactory();
266  $checkbox = $f->checkbox("label");
267  $checkbox->withValue(null);
268  $this->assertEquals(false, $checkbox->getValue());
269  }
270 }
Class ChatMainBarProvider .
Provides common functionality for UI tests.
Definition: Base.php:262
Builds data types.
Definition: Factory.php:19
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:372
brutallyTrimHTML($html)
A more radical version of normalizeHTML.
Definition: Base.php:392
getDefaultRenderer(JavaScriptBinding $js_binding=null, $with_stub_renderings=[])
Definition: Base.php:311