ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
FormWithoutSubmitButtonsTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30use Psr\Http\Message\ServerRequestInterface;
32
33require_once(__DIR__ . "/../../../../Base.php");
34
36{
37 public int $count = 0;
38
39 public function getNewName(): string
40 {
41 $name = "form_input_$this->count";
42 $this->count++;
43
44 return $name;
45 }
46
47 public function getNewDedicatedName(string $dedicated_name): string
48 {
49 $name = $dedicated_name . "_$this->count";
50 $this->count++;
51
52 return $name;
53 }
54}
55
60{
65 private ButtonFactory $button_factory;
66
67 public function setUp(): void
68 {
69 $this->signal_generator = new \SignalGeneratorMock();
70 $this->namesource = new InputNameSource();
71 $this->language = $this->getLanguage();
72 $this->refinery = new Refinery(
73 new \ILIAS\Data\Factory(),
74 $this->language
75 );
76
77 $this->button_factory = new ButtonFactory();
78
79 parent::setUp();
80 }
81
82 public function testRender(): void
83 {
84 $post_url = 'http://ilias.localhost/some_url?param1=foo&param2=bar';
85
86 $dummy_input = $this->buildInputFactory()->text('test_label');
87
88 $form = new StandardForm(
89 $this->signal_generator,
90 $this->buildInputFactory(),
91 $this->namesource,
92 $post_url,
93 [$dummy_input]
94 );
95
96 $expected_html =
97 "<form id=\"id_1\" class=\"c-form c-form--horizontal\" enctype=\"multipart/form-data\" action=\"$post_url\" method=\"post\">" .
98 $dummy_input->getCanonicalName() .
99 "</form>";
100
101 $context = $this->createMock(\ILIAS\UI\Component\Modal\RoundTrip::class);
102 $context->method('getCanonicalName')->willReturn('RoundTripModal');
103 $renderer = $this->getDefaultRenderer(null, [$dummy_input], [$context]);
104
105 $this->assertEquals(
106 $this->brutallyTrimHTML($expected_html),
107 $this->brutallyTrimHTML($renderer->render($form))
108 );
109 }
110
111 public function testRenderWithRequiredInputs(): void
112 {
113 $post_url = 'http://ilias.localhost/some_url?param1=foo&param2=bar';
114 $required_lang_var = 'required_field';
115
116 $dummy_input = $this->buildInputFactory()->text('test_label')->withRequired(true);
117
118 $form = new StandardForm(
119 $this->signal_generator,
120 $this->buildInputFactory(),
121 $this->namesource,
122 $post_url,
123 [$dummy_input]
124 );
125
126 $expected_html =
127 "<form id=\"id_1\" class=\"c-form c-form--horizontal\" enctype=\"multipart/form-data\" action=\"$post_url\" method=\"post\">" .
128 $dummy_input->getCanonicalName()
129 . "<div class=\"c-form__footer\">"
130 . "<div class=\"c-form__required\"><span class=\"asterisk\">*</span><span class=\"small\"> $required_lang_var</span></div>"
131 . "</div>"
132 . "</form>";
133
134 $context = $this->createMock(\ILIAS\UI\Component\Modal\RoundTrip::class);
135 $context->method('getCanonicalName')->willReturn('RoundTripModal');
136 $renderer = $this->getDefaultRenderer(null, [$dummy_input], [$context]);
137
138 $this->assertEquals(
139 $this->brutallyTrimHTML($expected_html),
140 $this->brutallyTrimHTML($renderer->render($form))
141 );
142 }
143
144 public function testRenderWithError(): void
145 {
146 $post_url = 'http://ilias.localhost/some_url?param1=foo&param2=bar';
147 $error_lang_var = 'ui_error';
148 $error_lang_var_in_group = 'ui_error_in_group';
149
150 $dummy_input = $this->buildInputFactory()->text('test_label')->withAdditionalTransformation(
151 $this->refinery->custom()->constraint(
152 static function ($value): bool {
153 return false; // always fail for testing purposes.
154 },
155 'this message does not matter because the input will not be properly rendered anyways.'
156 )
157 );
158
159 $form = new StandardForm(
160 $this->signal_generator,
161 $this->buildInputFactory(),
162 $this->namesource,
163 $post_url,
164 [$dummy_input]
165 );
166
167 $request = $this->createMock(ServerRequestInterface::class);
168 $request->method('getParsedBody')->willReturn([
169 'form_0/form_input_1' => '',
170 ]);
171
172 $form = $form->withRequest($request);
173 $data = $form->getData();
174
175 $expected_html = <<<EOT
176<form id="id_1" class="c-form c-form--horizontal" enctype="multipart/form-data" action="$post_url" method="post">
177 <div class="c-form__error-msg alert alert-danger"><span class="sr-only">$error_lang_var:</span>$error_lang_var_in_group
178 </div>{$dummy_input->getCanonicalName()}
179</form>
180EOT;
181
182 $context = $this->createMock(\ILIAS\UI\Component\Modal\RoundTrip::class);
183 $context->method('getCanonicalName')->willReturn('RoundTripModal');
184 $renderer = $this->getDefaultRenderer(null, [$dummy_input], [$context]);
185
186 $this->assertEquals(
187 $this->brutallyTrimHTML($expected_html),
188 $this->brutallyTrimHTML($renderer->render($form))
189 );
190 }
191
192 protected function buildInputFactory(): InputFactory
193 {
194 $df = new \ILIAS\Data\Factory();
195 return new \ILIAS\UI\Implementation\Component\Input\Field\Factory(
196 $this->createMock(\ILIAS\UI\Implementation\Component\Input\Field\Node\Factory::class),
197 $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
198 $this->signal_generator,
199 $df,
200 $this->refinery,
201 $this->language
202 );
203 }
204
205 public function getUIFactory(): \NoUIFactory
206 {
207 return new class ($this->button_factory) extends \NoUIFactory {
208 public function __construct(
209 protected ButtonFactory $button_factory,
210 ) {
211 }
212
213 public function button(): ButtonFactory
214 {
216 }
217 };
218 }
219}
$renderer
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Definition: UI.php:24
Provides common functionality for UI tests.
Definition: Base.php:337
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
This is how a factory for forms looks like.
Definition: Factory.php:27
This is what a factory for input fields looks like.
Definition: Factory.php:31
Describes a source for input names.
Definition: NameSource.php:27
form( $class_path, string $cmd, string $submit_caption="")
button(string $caption, string $cmd)
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
getLanguage()
$context
Definition: webdav.php:31