ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
TagInputTest.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__ . "/InputTest.php");
24require_once(__DIR__ . "/CommonFieldRendering.php");
25
27use ILIAS\Data;
30
37{
38 use CommonFieldRendering;
39
41
42 public function setUp(): void
43 {
44 $this->name_source = new DefNamesource();
45 }
46
47 #[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
48 public function testImplementsFactoryInterface(): void
49 {
50 $f = $this->getFieldFactory();
51
52 $f->tag(
53 "label",
54 ["lorem", "ipsum", "dolor"],
55 "byline"
56 );
57 }
58
59 public function testRender(): void
60 {
61 $f = $this->getFieldFactory();
62 $label = "label";
63 $byline = "byline";
64 $tags = ["lorem", "ipsum", "dolor",];
65 $tag = $f->tag($label, $tags, $byline)->withNameFrom($this->name_source);
66 $expected = $this->getFormWrappedHtml(
67 'tag-field-input',
68 $label,
69 '
70 <div class="c-field-tag__wrapper">
71 <input id="id_1" name="name_0" class="c-field-tag" value=""/>
72 </div>
73 ',
74 $byline,
75 'id_1',
76 'id_2'
77 );
78 $this->assertEquals($expected, $this->renderInsideContainer($tag));
79 }
80
81 public function testCommonRendering(): void
82 {
83 $f = $this->getFieldFactory();
84 $tag = $f->tag('label', [], null)->withNameFrom($this->name_source);
85
86 $this->testWithError($tag);
87 $this->testWithNoByline($tag);
88 $this->testWithRequired($tag);
89 $this->testWithDisabled($tag);
90 $this->testWithAdditionalOnloadCodeRendersId($tag);
91 }
92
93 public function testValueRequired(): void
94 {
95 $f = $this->getFieldFactory();
96 $label = "label";
97 $name = "name_0";
98 $tags = ["lorem", "ipsum", "dolor",];
100 $tag = $f->tag($label, $tags)->withNameFrom($this->name_source)->withRequired(true);
101
102 $raw_value1 = "lorem,ipsum";
103 $expected_result = ['lorem', 'ipsum'];
104 $tag1 = $tag->withInput(new DefInputData([$name => $raw_value1]));
105 $value1 = $tag1->getContent();
106 $this->assertTrue($value1->isOk());
107 $value = $value1->value();
108 $this->assertEquals($expected_result, $value);
109 }
110
111 public function testEmptyStringAsInputLeadToException(): void
112 {
113 $f = $this->getFieldFactory();
114 $label = "label";
115 $name = "name_0";
116 $tags = ["lorem", "ipsum", "dolor",];
118 $tag = $f->tag($label, $tags)->withNameFrom($this->name_source)->withRequired(true);
119
120 $tag2 = $tag->withInput(new DefInputData([$name => '']));
121 $result = $tag2->getContent();
122 $this->assertFalse($result->isOk());
123 try {
124 $result->value();
125 $this->fail();
126 } catch (Exception $e) {
127 $this->assertInstanceOf('ILIAS\Data\NotOKException', $e);
128 }
129 }
130
131 public function testStringAsInputAsRequired(): void
132 {
133 $f = $this->getFieldFactory();
134 $label = "label";
135 $name = "name_0";
136 $tags = ["lorem", "ipsum", "dolor",];
138 $tag = $f->tag($label, $tags)->withNameFrom($this->name_source)->withRequired(true);
139
140 $tag2 = $tag->withInput(new DefInputData([$name => 'test']));
141 $result = $tag2->getContent();
142 $this->assertTrue($result->isOk());
143 $this->assertEquals(['test'], $result->value());
144 }
145
146 public function testNullValueLeadsToException(): void
147 {
148 $f = $this->getFieldFactory();
149 $label = "label";
150 $name = "name_0";
151 $tags = ["lorem", "ipsum", "dolor",];
152
153 $tag = $f->tag($label, $tags)->withNameFrom($this->name_source)->withRequired(true);
154 $tag2 = $tag->withInput(new DefInputData([$name => null]));
155 $value2 = $tag2->getContent();
156 $this->assertTrue($value2->isError());
157 }
158
159 public function testUserCreatedNotAllowed(): void
160 {
161 $this->markTestSkipped("This is supposed to work, but currently does not.");
162
163 $f = $this->getFieldFactory();
164 $tags = ["lorem", "ipsum", "dolor",];
165 $tag = $f->tag("label", $tags)->withUserCreatedTagsAllowed(false)->withNameFrom($this->name_source);
166
167 $tag1 = $tag->withInput(
168 new DefInputData(
169 ["name_0" => "lorem,ipsum"]
170 )
171 );
172 $value1 = $tag1->getContent();
173 $this->assertTrue($value1->isOk());
174 $value = $value1->value();
175 $this->assertEquals(
176 ["lorem", "ipsum"],
177 $value
178 );
179
180 $tag1 = $tag->withInput(
181 new DefInputData(
182 ["name_0" => "conseptetuer,ipsum"]
183 )
184 );
185 $value1 = $tag1->getContent();
186 $this->assertTrue($value1->isError());
187 }
188
189 public function testMaxTagsOk(): void
190 {
191 $f = $this->getFieldFactory();
192
193 $tag = $f->tag("label", [])->withMaxTags(3)->withNameFrom($this->name_source)->withInput(
194 new DefInputData(["name_0" => "lorem,ipsum"])
195 );
196 $value = $tag->getContent();
197 $this->assertTrue($value->isOk());
198 }
199
200 public function testMaxTagsNotOk(): void
201 {
202 $f = $this->getFieldFactory();
203
204 $this->expectException(InvalidArgumentException::class);
205 $f->tag("label", [])->withMaxTags(2)->withNameFrom($this->name_source)->withInput(
206 new DefInputData(
207 ["name_0" => "lorem,ipsum,dolor"]
208 )
209 );
210 }
211
212 public function testConfigurationDoesNotLimitAmountOfTagsByDefault(): void
213 {
214 $f = $this->getFieldFactory();
215
217 $tag = $f->tag("label", []);
218
219 $this->assertSame(I\Input\Field\Tag::INFINITE, $tag->getMaxTags());
220 $this->assertSame(I\Input\Field\Tag::INFINITE, $tag->getConfiguration()->maxItems);
221 }
222
223 public function testConfigurationReflectsMaxTags(): void
224 {
225 $f = $this->getFieldFactory();
226
228 $tag = $f->tag("label", [])->withMaxTags(42);
229
230 $this->assertSame(42, $tag->getMaxTags());
231 $this->assertSame(42, $tag->getConfiguration()->maxItems);
232 }
233
234 public function testMaxTaglengthTagsOk(): void
235 {
236 $f = $this->getFieldFactory();
237
238 $tag = $f->tag("label", [])->withTagMaxLength(10)->withNameFrom($this->name_source)->withInput(
239 new DefInputData(["name_0" => "lorem,ipsum"])
240 );
241 $value = $tag->getContent();
242 $this->assertTrue($value->isOk());
243 }
244
245 public function testMaxTaglengthTagsNotOk(): void
246 {
247 $f = $this->getFieldFactory();
248
249 $this->expectException(InvalidArgumentException::class);
250 $f->tag("label", [])->withTagMaxLength(2)->withNameFrom($this->name_source)->withInput(
251 new DefInputData(
252 ["name_0" => "lorem,ipsum,dolor"]
253 )
254 );
255 }
256
257 public static function getUITagSpecialCharValues(): array
258 {
259 return [
260 ['1', '2', '3'],
261 ['++1#*', '[-2]', '{?3}'],
262 ['some\'thing "else"', '&/\\'],
263 ['fünf, sechs', 'sieben, acht'],
264 ];
265 }
266
268 public function testUITagInputSpecialChars(string ...$tags): void
269 {
270 $f = $this->getFieldFactory();
271 $name = "name_0";
272 $tag = $f->tag('', $tags)->withNameFrom($this->name_source);
273
274 $encoded_tags = array_map('rawurlencode', $tags);
275
276 $this->assertEquals(
277 $encoded_tags,
278 array_map(
279 fn($o) => $o['value'],
280 $tag->getConfiguration(fn(string $txt) => $txt)->options
281 )
282 );
283
284 $raw_value = implode(',', $encoded_tags);
285 $tag_with_input = $tag->withInput(new DefInputData([$name => $raw_value]));
286 $content = $tag_with_input->getContent();
287 $this->assertTrue($content->isOk());
288 $this->assertEquals($tags, $content->value());
289 }
290
291 public function testTagWithAutocompleteEndpoint(): void
292 {
293 $url_builder = new URLBuilder(new Data\URI('http://wwww.ilias.de?ref_id=1'));
294 $token = new URLBuilderToken(['t'], 't');
295 $f = $this->getFieldFactory();
296 $tag = $f->tag('my_tag', []);
297
298 $this->assertEquals(null, $tag->getAsyncAutocompleteEndpoint());
299 $this->assertEquals(null, $tag->getAsyncAutocompleteToken());
300
301 $tag = $tag->withAsyncAutocomplete(
302 $url_builder,
303 $token
304 );
305 $this->assertEquals($url_builder, $tag->getAsyncAutocompleteEndpoint());
306 $this->assertEquals($token, $tag->getAsyncAutocompleteToken());
307 }
308
310 {
311 $token = $this->createMock(URLBuilderToken::class);
312 $token->expects($this->once())
313 ->method('render');
314 $url_builder = $this->createMock(URLBuilder::class);
315 $url_builder->expects($this->once())
316 ->method('renderObject')
317 ->with([$token]);
318
319 $f = $this->getFieldFactory();
320 $tag = $f->tag('my_tag', [])->withAsyncAutocomplete(
321 $url_builder,
322 $token
323 );
324
325 $renderer = $this->getDefaultRenderer();
326 $renderer->render($tag);
327 }
328
329 public function testWithoutStripTags(): void
330 {
331 $tag = $this->getFieldFactory()->tag('my_tag', [])
332 ->withoutStripTags()
333 ->withNameFrom($this->name_source)
334 ->withInput(new DefInputData(['name_0' => '<member>']));
335
336 $result = $tag->getContent();
337 $this->assertTrue($result->isOk());
338 $this->assertSame(['<member>'], $result->value());
339 }
340}
$renderer
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
Provides common functionality for UI tests.
Definition: Base.php:346
Class TagInputTest.
testMaxTaglengthTagsNotOk()
static getUITagSpecialCharValues()
testNullValueLeadsToException()
testUITagInputSpecialChars(string ... $tags)
@dataProvider getUITagSpecialCharValues
testTagWithAutocompleteEndpoint()
DefNamesource $name_source
testUserCreatedNotAllowed()
testImplementsFactoryInterface()
testTagWithAutocompleteEndpointJSAdded()
$token
Definition: xapitoken.php:69