ILIAS  trunk Revision v12.0_alpha-1338-g8f7e531aa3c
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 renderInsideContainer(FormInput $component): string
45 {
46 // @todo: this should be removed by decoupling rendering unit tests with stubs.
47 $context_stub = $this->createMock(\ILIAS\UI\Component\Component::class);
48 $context_stub->method('getCanonicalName')->willReturn('StandardFormContainerInput');
49
50 return $this->brutallyTrimHTML(
51 $this->getDefaultRenderer(null, [], [$context_stub])->render($component)
52 );
53 }
54
55 protected function testWithError(FormInput $component): void
56 {
57 $error = "an_error";
58 $expected = '<div class="c-input__error-msg alert alert-danger"';
59 $expected2 = 'ui_error:</span>' . $error . '</div>';
60 $html = $this->renderInsideContainer($component->withError($error));
61 $this->assertStringContainsString($expected, $html);
62 $this->assertStringContainsString($expected2, $html);
63 }
64
65 protected function testWithRequired(FormInput $component): void
66 {
67 $expected = '<span class="sr-only">required_field</span><span class="asterisk" aria-hidden="true">*</span></label>';
68 $this->assertStringContainsString($expected, $this->renderInsideContainer($component->withRequired(true)));
69 }
70
71 protected function testWithNoByline(FormInput $component): void
72 {
73 $expected = '<div class="c-input__help-byline">';
74 $this->assertStringNotContainsString($expected, $this->renderInsideContainer($component));
75 }
76
77 protected function testWithDisabled(FormInput $component): void
78 {
79 $type = $this->getDefaultRenderer()->getComponentCanonicalNameAttribute($component);
80 $expected = '<fieldset class="c-input" data-il-ui-component="' . $type . '" data-il-ui-input-name="name_0" disabled="disabled"';
81 $this->assertStringContainsString($expected, $this->renderInsideContainer($component->withDisabled(true)));
82 }
83
84 protected function testWithAdditionalOnloadCodeRendersId(FormInput $component): void
85 {
86 $component = $component->withAdditionalOnLoadCode(
87 function (string $id): string {
88 return '';
89 }
90 );
91
92 $js_binding = new class () implements JavaScriptBinding {
93 public function createId(): string
94 {
95 return 'THE COMPONENT ID';
96 }
97 public function addOnLoadCode(string $code): void
98 {
99 }
100 public function getOnLoadCodeAsync(): string
101 {
102 return '';
103 }
104 };
105
106
107 $renderer = $this->getDefaultRenderer($js_binding);
108 $outerhtml = $this->brutallyTrimHTML($renderer->render($component));
109 if (method_exists($component, 'getInputs')) {
110 $innerhtml = $this->brutallyTrimHTML($renderer->render($component->getInputs()));
111 $outerhtml = str_replace($innerhtml, '', $outerhtml);
112 }
113
114 $this->assertStringContainsString(
115 'id="THE COMPONENT ID"',
116 $outerhtml
117 );
118 }
119
120 protected function getFormWrappedHtml(
121 string $type,
122 string $label,
123 string $payload_field,
124 ?string $byline = null,
125 ?string $label_id = null,
126 ?string $js_id = null,
127 ?string $name = 'name_0',
128 ): string {
129 $label_id = $label_id ? " for=\"$label_id\"" : '';
130 $tab = $label_id ? '' : ' tabindex="0"';
131 $js_id = $js_id ? " id=\"$js_id\"" : '';
132 if ($type === 'section-field-input') {
133 $headline_tag_open = "<h2>";
134 $headline_tag_close = "</h2>";
135 } else {
136 $headline_tag_open = "";
137 $headline_tag_close = "";
138 }
139
140 $html = '
141 <fieldset class="c-input" data-il-ui-component="' . $type . '" data-il-ui-input-name="' . $name . '"' . $js_id . $tab . '>
142 <label' . $label_id . '>' . $headline_tag_open . $label . $headline_tag_close . '</label>
143 <div class="c-input__field">';
144 $html .= $payload_field;
145 $html .= '
146 </div>';
147 if ($byline) {
148 $html .= '
149 <div class="c-input__help-byline">' . $byline . '</div>';
150 }
151 $html .= '
152 </fieldset>
153 ';
154 return $this->brutallyTrimHTML($html);
155 }
156}
$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()