ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
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->createMock(\ilLanguage::class)),
31  );
32  }
33 
34 
36  {
37  $f = $this->buildFactory();
38 
39  $checkbox = $f->checkbox("label", "byline");
40 
41  $this->assertInstanceOf(Field\Input::class, $checkbox);
42  $this->assertInstanceOf(Field\Checkbox::class, $checkbox);
43  }
44 
45 
46  public function test_render()
47  {
48  $f = $this->buildFactory();
49  $label = "label";
50  $byline = "byline";
51  $checkbox = $f->checkbox($label, $byline)->withNameFrom($this->name_source);
52 
53  $r = $this->getDefaultRenderer();
54  $html = $r->render($checkbox);
55 
56  $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>";
57  $this->assertHTMLEquals($expected, $html);
58  }
59 
60 
61  public function test_render_error()
62  {
63  $f = $this->buildFactory();
64  $label = "label";
65  $byline = "byline";
66  $error = "an_error";
67  $checkbox = $f->checkbox($label, $byline)->withNameFrom($this->name_source)->withError($error);
68 
69  $r = $this->getDefaultRenderer();
70  $html = $r->render($checkbox);
71 
72  $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"
73  . " </div></div></div>";
74  $this->assertHTMLEquals($expected, $html);
75  }
76 
77 
78  public function test_render_no_byline()
79  {
80  $f = $this->buildFactory();
81  $label = "label";
82  $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
83 
84  $r = $this->getDefaultRenderer();
85  $html = $r->render($checkbox);
86 
87  $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>";
88  $this->assertHTMLEquals($expected, $html);
89  }
90 
91 
92  public function test_render_value()
93  {
94  $f = $this->buildFactory();
95  $label = "label";
96  $value = "checked";
97  $checkbox = $f->checkbox($label)->withValue($value)->withNameFrom($this->name_source);
98 
99  $r = $this->getDefaultRenderer();
100  $html = $r->render($checkbox);
101 
102  $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>";
103  $this->assertHTMLEquals($expected, $html);
104  }
105 
106  public function test_handle_invalid_value()
107  {
108  $f = $this->buildFactory();
109  $label = "label";
110  $value = "invalid";
111  try {
112  $f->checkbox($label)->withValue($value);
113  $this->assertFalse(true);
114  } catch (InvalidArgumentException $e) {
115  $this->assertTrue(true);
116  }
117  }
118 
119 
120  public function test_render_required()
121  {
122  $f = $this->buildFactory();
123  $label = "label";
124  $checbox = $f->checkbox($label)->withNameFrom($this->name_source)->withRequired(true);
125 
126  $r = $this->getDefaultRenderer();
127  $html = $r->render($checbox);
128 
129  $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>";
130  $this->assertHTMLEquals($expected, $html);
131  }
132 }
A transformation is a function from one datatype to another.
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:228
$r
Definition: example_031.php:79
Provides common functionality for UI tests.
Definition: Base.php:191
Builds data types.
Definition: Factory.php:14
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:270
$html
Definition: example_001.php:87