ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CommonFieldRendering.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24use ILIAS\Refinery\Factory as Refinery;
25use ILIAS\Data;
28
29trait CommonFieldRendering
30{
31 protected function getFieldFactory(): I\Input\Field\Factory
32 {
33 $df = new Data\Factory();
34 return new I\Input\Field\Factory(
35 $this->createMock(\ILIAS\UI\Implementation\Component\Input\Field\Node\Factory::class),
36 $this->createMock(UploadLimitResolver::class),
37 new SignalGenerator(),
38 $df,
39 new Refinery($df, $this->getLanguage()),
40 $this->getLanguage()
41 );
42 }
43
44 protected function render(FormInput $component): string
45 {
46 return $this->brutallyTrimHTML(
47 $this->getDefaultRenderer()->render($component)
48 );
49 }
50
51 protected function testWithError(FormInput $component): void
52 {
53 $error = "an_error";
54 $expected = '<div class="c-input__error-msg alert alert-danger"';
55 $expected2 = 'ui_error:</span>' . $error . '</div>';
56 $html = $this->render($component->withError($error));
57 $this->assertStringContainsString($expected, $html);
58 $this->assertStringContainsString($expected2, $html);
59 }
60
61 protected function testWithRequired(FormInput $component): void
62 {
63 $expected = '<span class="asterisk" aria-label="required_field">*</span></label>';
64 $this->assertStringContainsString($expected, $this->render($component->withRequired(true)));
65 }
66
67 protected function testWithNoByline(FormInput $component): void
68 {
69 $expected = '<div class="c-input__help-byline">';
70 $this->assertStringNotContainsString($expected, $this->render($component));
71 }
72
73 protected function testWithDisabled(FormInput $component): void
74 {
75 $type = $this->getDefaultRenderer()->getComponentCanonicalNameAttribute($component);
76 $expected = '<fieldset class="c-input" data-il-ui-component="' . $type . '" data-il-ui-input-name="name_0" disabled="disabled"';
77 $this->assertStringContainsString($expected, $this->render($component->withDisabled(true)));
78 }
79
80 protected function testWithAdditionalOnloadCodeRendersId(FormInput $component): void
81 {
82 $component = $component->withAdditionalOnLoadCode(
83 function (string $id): string {
84 return '';
85 }
86 );
87
88 $js_binding = new class () implements JavaScriptBinding {
89 public function createId(): string
90 {
91 return 'THE COMPONENT ID';
92 }
93 public function addOnLoadCode(string $code): void
94 {
95 }
96 public function getOnLoadCodeAsync(): string
97 {
98 return '';
99 }
100 };
101
102
103 $renderer = $this->getDefaultRenderer($js_binding);
104 $outerhtml = $this->brutallyTrimHTML($renderer->render($component));
105 if (method_exists($component, 'getInputs')) {
106 $innerhtml = $this->brutallyTrimHTML($renderer->render($component->getInputs()));
107 $outerhtml = str_replace($innerhtml, '', $outerhtml);
108 }
109
110 $this->assertStringContainsString(
111 'id="THE COMPONENT ID"',
112 $outerhtml
113 );
114 }
115
116 protected function getFormWrappedHtml(
117 string $type,
118 string $label,
119 string $payload_field,
120 ?string $byline = null,
121 ?string $label_id = null,
122 ?string $js_id = null,
123 ?string $name = 'name_0',
124 ): string {
125 $label_id = $label_id ? " for=\"$label_id\"" : '';
126 $tab = $label_id ? '' : ' tabindex="0"';
127 $js_id = $js_id ? " id=\"$js_id\"" : '';
128 if ($type === 'section-field-input') {
129 $headline_tag_open = "<h2>";
130 $headline_tag_close = "</h2>";
131 } else {
132 $headline_tag_open = "";
133 $headline_tag_close = "";
134 }
135
136 $html = '
137 <fieldset class="c-input" data-il-ui-component="' . $type . '" data-il-ui-input-name="' . $name . '"' . $js_id . $tab . '>
138 <label' . $label_id . '>' . $headline_tag_open . $label . $headline_tag_close . '</label>
139 <div class="c-input__field">';
140 $html .= $payload_field;
141 $html .= '
142 </div>';
143 if ($byline) {
144 $html .= '
145 <div class="c-input__help-byline">' . $byline . '</div>';
146 }
147 $html .= '
148 </fieldset>
149 ';
150 return $this->brutallyTrimHTML($html);
151 }
152}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$renderer
Builds data types.
Definition: Factory.php:36
This describes inputs that can be used in forms.
Definition: FormInput.php:33
withRequired(bool $is_required, ?Constraint $requirement_constraint=null)
Get an input like this, but set the field to be required (or not).
withDisabled(bool $is_disabled)
Get an input like this, but set it to a disabled state.
withAdditionalOnLoadCode(Closure $binder)
Add some onload-code to the component instead of replacing the existing one.
Provides methods to interface with javascript.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
getLanguage()