ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
MultiSelectInputTest.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2018 Nils Haagen <nils.haagen@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 
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  protected function buildFactory()
23  {
24  $df = new Data\Factory();
26  new SignalGenerator(),
27  $df,
28  new Validation\Factory($df, $this->createMock(\ilLanguage::class)),
30  );
31  }
32 
33 
35  {
36  $f = $this->buildFactory();
37  $options = array(
38  "1" => "Pick 1",
39  "2" => "Pick 2"
40  );
41  $ms = $f->multiSelect("label", $options, "byline");
42  $this->assertInstanceOf(Field\Input::class, $ms);
43  $this->assertInstanceOf(Field\MultiSelect::class, $ms);
44  }
45 
46 
47  public function test_options()
48  {
49  $f = $this->buildFactory();
50  $options = array(
51  "1" => "Pick 1",
52  "2" => "Pick 2"
53  );
54  $ms = $f->multiSelect("label", $options, "byline");
55  $this->assertEquals($options, $ms->getOptions());
56  }
57 
58 
60  {
61  $this->expectException(\InvalidArgumentException::class);
62  $f = $this->buildFactory();
63  $options = array(
64  "1" => "Pick 1",
65  "2" => "Pick 2"
66  );
67  $ms = $f->multiSelect("label", $options, "byline");
68  $ms = $ms->withInput(new class() implements PostData {
69  public function getOr($_, $__)
70  {
71  return ["3"];
72  }
73  public function get($_)
74  {
75  }
76  });
77  $content = $ms->getContent();
78  }
79 
80 
81  public function test_render()
82  {
83  $r = $this->getDefaultRenderer();
84  $f = $this->buildFactory();
85  $options = array(
86  "1" => "Pick 1",
87  "2" => "Pick 2"
88  );
89  $ms = $f->multiSelect("label", $options, "byline")
90  ->withNameFrom($this->name_source);
91 
92  $name = $ms->getName();
93  $label = $ms->getLabel();
94  $byline = $ms->getByline();
95  $expected = ""
96  . "<div class=\"form-group row\">"
97  . "<label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
98  . "<div class=\"col-sm-9\">"
99  . "<ul class=\"il-input-multiselect\">";
100 
101  foreach ($options as $opt_value => $opt_label) {
102  $expected .= ""
103  . "<li>"
104  . "<input type=\"checkbox\" name=\"$name" . "[]\" value=\"$opt_value\" />"
105  . "<span>$opt_label</span>"
106  . "</li>";
107  }
108 
109  $expected .= ""
110  . "</ul>"
111  . "<div class=\"help-block\">$byline</div>"
112  . "</div>"
113  . "</div>";
114  $this->assertHTMLEquals($expected, $r->render($ms));
115  }
116 
117 
118  public function test_render_value()
119  {
120  $r = $this->getDefaultRenderer();
121  $f = $this->buildFactory();
122  $options = array(
123  "1" => "Pick 1",
124  "2" => "Pick 2"
125  );
126  $value = array_keys($options)[1];
127  $ms = $f->multiSelect("label", $options, "byline")
128  ->withNameFrom($this->name_source)
129  ->withValue([$value]);
130 
131  $name = $ms->getName();
132  $label = $ms->getLabel();
133  $byline = $ms->getByline();
134  $expected = ""
135  . "<div class=\"form-group row\">"
136  . "<label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
137  . "<div class=\"col-sm-9\">"
138  . "<ul class=\"il-input-multiselect\">";
139 
140  foreach ($options as $opt_value => $opt_label) {
141  if ($opt_value === $value) {
142  $expected .= ""
143  . "<li>"
144  . "<input type=\"checkbox\" name=\"$name" . "[]\" value=\"$opt_value\" checked=\"checked\" />"
145  . "<span>$opt_label</span>"
146  . "</li>";
147  } else {
148  $expected .= ""
149  . "<li>"
150  . "<input type=\"checkbox\" name=\"$name" . "[]\" value=\"$opt_value\" />"
151  . "<span>$opt_label</span>"
152  . "</li>";
153  }
154  }
155 
156  $expected .= ""
157  . "</ul>"
158  . "<div class=\"help-block\">$byline</div>"
159  . "</div>"
160  . "</div>";
161  $this->assertHTMLEquals($expected, $r->render($ms));
162  }
163 }
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
Describes how Input-Elements want to interact with posted data.
Definition: PostData.php:12
Builds data types.
Definition: Factory.php:14
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:270