ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
SelectInputTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once(__DIR__ . "/../../../Base.php");
22require_once(__DIR__ . "/CommonFieldRendering.php");
23
24use ILIAS\Data;
25use ILIAS\Refinery\Factory as Refinery;
29
31{
32 public function _isClientSideValueOk($value): bool
33 {
34 return $this->isClientSideValueOk($value);
35 }
36}
37
39{
40 use CommonFieldRendering;
41
43
44 public function setUp(): void
45 {
46 $this->name_source = new DefNamesource();
47 }
48
50 {
51 $options = ["one" => "Eins", "two" => "Zwei", "three" => "Drei"];
52 $select = new SelectForTest(
53 $this->createMock(ILIAS\Data\Factory::class),
54 $this->createMock(ILIAS\Refinery\Factory::class),
55 "",
56 $options,
57 ""
58 );
59
60 $this->assertTrue($select->_isClientSideValueOk("one"));
61 $this->assertTrue($select->_isClientSideValueOk("two"));
62 $this->assertTrue($select->_isClientSideValueOk("three"));
63 $this->assertFalse($select->_isClientSideValueOk("four"));
64 }
65
67 {
68 $options = [];
69 $select = new SelectForTest(
70 $this->createMock(ILIAS\Data\Factory::class),
71 $this->createMock(ILIAS\Refinery\Factory::class),
72 "",
73 $options,
74 ""
75 );
76
77 $this->assertTrue($select->_isClientSideValueOk(""));
78 }
79
81 {
82 $options = [];
83 $select = $this->getFieldFactory()->select(
84 "",
85 $options,
86 ""
87 )
88 ->withRequired(true)
89 ->withNameFrom($this->name_source);
90
91 $data = $this->createMock(InputData::class);
92 $data->expects($this->once())
93 ->method("getOr")
94 ->willReturn(null);
95 $select = $select->withInput(
96 $data
97 );
98
99 $this->assertNotEquals(null, $select->getError());
100 }
101
103 {
104 $options = [];
105 $select = (new SelectForTest(
106 $this->createMock(ILIAS\Data\Factory::class),
107 $this->createMock(ILIAS\Refinery\Factory::class),
108 "",
109 $options,
110 ""
111 ))->withRequired(true);
112
113 $this->assertTrue($select->_isClientSideValueOk(""));
114 }
115
116 public function testRender(): void
117 {
118 $f = $this->getFieldFactory();
119 $label = "label";
120 $byline = "byline";
121 $options = ["one" => "One", "two" => "Two", "three" => "Three"];
122 $select = $f->select($label, $options, $byline)->withNameFrom($this->name_source);
123 $expected = $this->getFormWrappedHtml(
124 'select-field-input',
125 $label,
126 '
127 <select id="id_1" name="name_0">
128 <option selected="selected" value="">-</option>
129 <option value="one">One</option>
130 <option value="two">Two</option>
131 <option value="three">Three</option>
132 </select>
133 ',
134 $byline,
135 'id_1'
136 );
137 $this->assertEquals($expected, $this->render($select));
138 }
139
140
141 public function testRenderValue(): void
142 {
143 $f = $this->getFieldFactory();
144 $label = "label";
145 $byline = "byline";
146 $options = ["one" => "One", "two" => "Two", "three" => "Three"];
147 $select = $f->select($label, $options, $byline)->withNameFrom($this->name_source)->withValue("one");
148 $expected = $this->getFormWrappedHtml(
149 'select-field-input',
150 $label,
151 '
152 <select id="id_1" name="name_0">
153 <option value="">-</option>
154 <option selected="selected" value="one">One</option>
155 <option value="two">Two</option>
156 <option value="three">Three</option>
157 </select>
158 ',
159 $byline,
160 'id_1'
161 );
162 $this->assertEquals($expected, $this->render($select));
163 }
164
165 public function testCommonRendering(): void
166 {
167 $f = $this->getFieldFactory();
168 $label = "label";
169 $select = $f->select($label, [], null)->withNameFrom($this->name_source);
170
171 $this->testWithError($select);
172 $this->testWithNoByline($select);
173 $this->testWithRequired($select);
174 $this->testWithDisabled($select);
175 $this->testWithAdditionalOnloadCodeRendersId($select);
176 }
177
178
180 {
181 $f = $this->getFieldFactory();
182 $label = "label";
183 $byline = "byline";
184 $options = ["something_value" => "something"];
185 $select = $f->select($label, $options, $byline)
186 ->withNameFrom($this->name_source);
187
188 $html_without = $this->brutallyTrimHTML($this->getDefaultRenderer()->render($select));
189
190 $this->assertTrue(str_contains($html_without, ">-</option>"));
191 $this->assertTrue(str_contains($html_without, "value=\"\""));
192
193 $select = $select->withRequired(true);
194 $html_with_required = $this->brutallyTrimHTML($this->getDefaultRenderer()->render($select));
195
196 $this->assertTrue(str_contains($html_with_required, ">ui_select_dropdown_label</option>"));
197 $this->assertTrue(str_contains($html_with_required, "value=\"\""));
198
199 $select = $select->withRequired(false)->withValue("something_value");
200 $html_with_value = $this->brutallyTrimHTML($this->getDefaultRenderer()->render($select));
201
202 $this->assertTrue(str_contains($html_with_value, ">-</option>"));
203 $this->assertTrue(str_contains($html_with_value, "value=\"\""));
204
205 $select = $select->withRequired(true);
206
207 $html_with_value_and_required = $this->brutallyTrimHTML($this->getDefaultRenderer()->render($select));
208
209 $this->assertFalse(str_contains($html_with_value_and_required, ">-</option>"));
210 $this->assertFalse(str_contains($html_with_value_and_required, "value=\"\""));
211 }
212}
Builds data types.
Definition: Factory.php:36
Provides common functionality for UI tests.
Definition: Base.php:337
_isClientSideValueOk($value)
testEmptyStringIsAcceptableClientSideValueIfSelectIsNotRequired()
DefNamesource $name_source
testEmptyStringIsAnAcceptableClientSideValueEvenIfSelectIsRequired()
testEmptyStringCreatesErrorIfSelectIsRequired()
testOnlyValuesFromOptionsAreAcceptableClientSideValues()
testWithValueAndRequiredDoesNotContainNull()
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.