ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
MultiSelectInputTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once(__DIR__ . "/../../../../../../../vendor/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../../Base.php");
23require_once(__DIR__ . "/CommonFieldRendering.php");
24
30use ILIAS\Data;
31use ILIAS\Refinery\Factory as Refinery;
32
34{
35 use CommonFieldRendering;
36
38
39 public function setUp(): void
40 {
41 $this->name_source = new DefNamesource();
42 }
43
44 public function testImplementsFactoryInterface(): void
45 {
46 $f = $this->getFieldFactory();
47 $options = array(
48 "1" => "Pick 1",
49 "2" => "Pick 2"
50 );
51 $ms = $f->multiSelect("label", $options, "byline");
52 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $ms);
53 $this->assertInstanceOf(Field\MultiSelect::class, $ms);
54 }
55
56 public function testOptions(): void
57 {
58 $f = $this->getFieldFactory();
59 $options = array(
60 "1" => "Pick 1",
61 "2" => "Pick 2"
62 );
63 $ms = $f->multiSelect("label", $options, "byline");
64 $this->assertEquals($options, $ms->getOptions());
65 }
66
68 {
69 $this->expectException(InvalidArgumentException::class);
70 $f = $this->getFieldFactory();
71 $options = array(
72 "1" => "Pick 1",
73 "2" => "Pick 2"
74 );
75 $ms = $f->multiSelect("label", $options, "byline")
76 ->withNameFrom(new class () implements NameSource {
77 public function getNewName(): string
78 {
79 return "name";
80 }
81 });
82 $ms = $ms->withInput(new class () implements InputData {
86 public function getOr($_, $__): array
87 {
88 return ["3"];
89 }
90 public function get($_): void
91 {
92 }
93 public function has($name): bool
94 {
95 }
96 });
97 $ms->getContent();
98 }
99
100 public function testRender(): void
101 {
102 $r = $this->getDefaultRenderer();
103 $f = $this->getFieldFactory();
104 $options = array(
105 "1" => "Pick 1",
106 "2" => "Pick 2"
107 );
108 $ms = $f->multiSelect("label", $options, "byline")
109 ->withNameFrom($this->name_source);
110
111 $name = $ms->getName();
112 $label = $ms->getLabel();
113 $byline = $ms->getByline();
114 $expected_options = "";
115 foreach ($options as $opt_value => $opt_label) {
116 $expected_options .= ""
117 . "<li><label>"
118 . "<input type=\"checkbox\" name=\"$name" . "[]\" value=\"$opt_value\" />"
119 . ' <span class="c-field-multiselect__label-text">'
120 . $opt_label
121 . "</span></label></li>";
122 }
123 $expected = $this->getFormWrappedHtml(
124 'multi-select-field-input',
125 $label,
126 '<ul class="c-field-multiselect">'
127 . $expected_options .
128 '</ul>',
129 $byline,
130 null
131 );
132 $this->assertEquals($expected, $this->render($ms));
133 }
134
135 public function testRenderValue(): void
136 {
137 $r = $this->getDefaultRenderer();
138 $f = $this->getFieldFactory();
139 $options = array(
140 "1" => "Pick 1",
141 "2" => "Pick 2"
142 );
143 $value = array_keys($options)[1];
144 $ms = $f->multiSelect("label", $options, "byline")
145 ->withNameFrom($this->name_source)
146 ->withValue([$value]);
147
148 $name = $ms->getName();
149 $label = $ms->getLabel();
150 $byline = $ms->getByline();
151 $expected_options = "";
152 foreach ($options as $opt_value => $opt_label) {
153 if ($opt_value === $value) {
154 $expected_options .= ""
155 . "<li><label>"
156 . "<input type=\"checkbox\" name=\"$name" . "[]\" value=\"$opt_value\" checked=\"checked\" />"
157 . '<span class="c-field-multiselect__label-text">'
158 . $opt_label
159 . "</span></label></li>";
160 } else {
161 $expected_options .= ""
162 . "<li><label>"
163 . "<input type=\"checkbox\" name=\"$name" . "[]\" value=\"$opt_value\" />"
164 . '<span class="c-field-multiselect__label-text">'
165 . $opt_label
166 . "</span></label></li>";
167 }
168 }
169 $expected = $this->getFormWrappedHtml(
170 'multi-select-field-input',
171 $label,
172 '<ul class="c-field-multiselect">'
173 . $expected_options .
174 '</ul>',
175 $byline,
176 null
177 );
178 $this->assertEquals($expected, $this->render($ms));
179 }
180
181 public function testCommonRendering(): void
182 {
183 $f = $this->getFieldFactory();
184 $label = "label";
185 $multi_select = $f->multiSelect($label, [], null)->withNameFrom($this->name_source);
186
187 $this->testWithError($multi_select);
188 $this->testWithNoByline($multi_select);
189 $this->testWithRequired($multi_select);
190 $this->testWithDisabled($multi_select);
191 $this->testWithAdditionalOnloadCodeRendersId($multi_select);
192 }
193
194 public function testRenderNoOptions(): void
195 {
196 $r = $this->getDefaultRenderer();
197 $f = $this->getFieldFactory();
198 $options = [];
199 $ms = $f->multiSelect("label", $options, "byline")
200 ->withNameFrom($this->name_source)->withDisabled(true);
201
202 $name = $ms->getName();
203 $label = $ms->getLabel();
204 $byline = $ms->getByline();
205 $expected = '
206 <fieldset class="c-input" data-il-ui-component="multi-select-field-input" data-il-ui-input-name="name_0" disabled="disabled" tabindex="0">
207 <label>label</label>
208 <div class="c-input__field">
209 <ul class="c-field-multiselect">
210 <li>-</li>
211 </ul>
212 </div>
213 <div class="c-input__help-byline">byline</div>
214 </fieldset>';
215
216 $this->assertHTMLEquals($expected, $r->render($ms));
217 }
218}
Builds data types.
Definition: Factory.php:36
Definition: UI.php:24
Provides common functionality for UI tests.
Definition: Base.php:337
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
This describes commonalities between all inputs.
Definition: Input.php:47
Describes a source for input names.
Definition: NameSource.php:27
has(string $class_name)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.