ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
MultiSelectInputTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/../../../../../../../vendor/composer/vendor/autoload.php");
22 require_once(__DIR__ . "/../../../Base.php");
23 require_once(__DIR__ . "/CommonFieldRendering.php");
24 
30 use ILIAS\Data;
32 
34 {
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 }
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
has(string $class_name)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Describes a source for input names.
Definition: NameSource.php:26
$r