ILIAS  release_7 Revision v7.30-3-g800a261c036
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
5require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
6require_once(__DIR__ . "/../../../Base.php");
7
11use \ILIAS\UI\Component\Input\Field;
12use \ILIAS\Data;
14
16{
17 public function setUp() : void
18 {
19 $this->name_source = new DefNamesource();
20 }
21
22 protected function buildFactory()
23 {
24 $df = new Data\Factory();
25 $language = $this->createMock(\ilLanguage::class);
27 new SignalGenerator(),
28 $df,
29 new \ILIAS\Refinery\Factory($df, $language),
30 $language
31 );
32 }
33
34
36 {
37 $f = $this->buildFactory();
38 $options = array(
39 "1" => "Pick 1",
40 "2" => "Pick 2"
41 );
42 $ms = $f->multiSelect("label", $options, "byline");
43 $this->assertInstanceOf(Field\Input::class, $ms);
44 $this->assertInstanceOf(Field\MultiSelect::class, $ms);
45 }
46
47
48 public function test_options()
49 {
50 $f = $this->buildFactory();
51 $options = array(
52 "1" => "Pick 1",
53 "2" => "Pick 2"
54 );
55 $ms = $f->multiSelect("label", $options, "byline");
56 $this->assertEquals($options, $ms->getOptions());
57 }
58
59
61 {
62 $this->expectException(\InvalidArgumentException::class);
63 $f = $this->buildFactory();
64 $options = array(
65 "1" => "Pick 1",
66 "2" => "Pick 2"
67 );
68 $ms = $f->multiSelect("label", $options, "byline")
69 ->withNameFrom(new class() implements NameSource {
70 public function getNewName()
71 {
72 return "name";
73 }
74 });
75 $ms = $ms->withInput(new class() implements InputData {
76 public function getOr($_, $__)
77 {
78 return ["3"];
79 }
80 public function get($_)
81 {
82 }
83 });
84 $content = $ms->getContent();
85 }
86
87
88 public function test_render()
89 {
90 $r = $this->getDefaultRenderer();
91 $f = $this->buildFactory();
92 $options = array(
93 "1" => "Pick 1",
94 "2" => "Pick 2"
95 );
96 $ms = $f->multiSelect("label", $options, "byline")
97 ->withNameFrom($this->name_source);
98
99 $name = $ms->getName();
100 $label = $ms->getLabel();
101 $byline = $ms->getByline();
102 $expected = ""
103 . "<div class=\"form-group row\">"
104 . "<label class=\"control-label col-sm-3\">$label</label>"
105 . "<div class=\"col-sm-9\">"
106 . "<ul class=\"il-input-multiselect\" id=\"id_1\">";
107
108 foreach ($options as $opt_value => $opt_label) {
109 $expected .= ""
110 . "<li>"
111 . "<input type=\"checkbox\" name=\"$name" . "[]\" value=\"$opt_value\" />"
112 . "<span>$opt_label</span>"
113 . "</li>";
114 }
115
116 $expected .= ""
117 . "</ul>"
118 . "<div class=\"help-block\">$byline</div>"
119 . "</div>"
120 . "</div>";
121 $this->assertHTMLEquals($expected, $r->render($ms));
122 }
123
124
125 public function test_render_value()
126 {
127 $r = $this->getDefaultRenderer();
128 $f = $this->buildFactory();
129 $options = array(
130 "1" => "Pick 1",
131 "2" => "Pick 2"
132 );
133 $value = array_keys($options)[1];
134 $ms = $f->multiSelect("label", $options, "byline")
135 ->withNameFrom($this->name_source)
136 ->withValue([$value]);
137
138 $name = $ms->getName();
139 $label = $ms->getLabel();
140 $byline = $ms->getByline();
141 $expected = ""
142 . "<div class=\"form-group row\">"
143 . "<label class=\"control-label col-sm-3\">$label</label>"
144 . "<div class=\"col-sm-9\">"
145 . "<ul class=\"il-input-multiselect\" id=\"id_1\">";
146
147 foreach ($options as $opt_value => $opt_label) {
148 if ($opt_value === $value) {
149 $expected .= ""
150 . "<li>"
151 . "<input type=\"checkbox\" name=\"$name" . "[]\" value=\"$opt_value\" checked=\"checked\" />"
152 . "<span>$opt_label</span>"
153 . "</li>";
154 } else {
155 $expected .= ""
156 . "<li>"
157 . "<input type=\"checkbox\" name=\"$name" . "[]\" value=\"$opt_value\" />"
158 . "<span>$opt_label</span>"
159 . "</li>";
160 }
161 }
162
163 $expected .= ""
164 . "</ul>"
165 . "<div class=\"help-block\">$byline</div>"
166 . "</div>"
167 . "</div>";
168 $this->assertHTMLEquals($expected, $r->render($ms));
169 }
170
171 public function test_render_disabled()
172 {
173 $r = $this->getDefaultRenderer();
174 $f = $this->buildFactory();
175 $options = array(
176 "1" => "Pick 1",
177 "2" => "Pick 2"
178 );
179 $ms = $f->multiSelect("label", $options, "byline")
180 ->withNameFrom($this->name_source)->withDisabled(true);
181
182 $name = $ms->getName();
183 $label = $ms->getLabel();
184 $byline = $ms->getByline();
185 $expected = ""
186 . "<div class=\"form-group row\">"
187 . "<label class=\"control-label col-sm-3\">$label</label>"
188 . "<div class=\"col-sm-9\">"
189 . "<ul class=\"il-input-multiselect\" id=\"id_1\">";
190
191 foreach ($options as $opt_value => $opt_label) {
192 $expected .= ""
193 . "<li>"
194 . "<input type=\"checkbox\" name=\"$name" . "[]\" value=\"$opt_value\" disabled=\"disabled\" />"
195 . "<span>$opt_label</span>"
196 . "</li>";
197 }
198
199 $expected .= ""
200 . "</ul>"
201 . "<div class=\"help-block\">$byline</div>"
202 . "</div>"
203 . "</div>";
204 $this->assertHTMLEquals($expected, $r->render($ms));
205 }
206}
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:263
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:372
getDefaultRenderer(JavaScriptBinding $js_binding=null, $with_stub_renderings=[])
Definition: Base.php:311
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:13
Describes a source for input names.
Definition: NameSource.php:11
if($format !==null) $name
Definition: metadata.php:230
Class ChatMainBarProvider \MainMenu\Provider.