ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
StandardFormTest.php
Go to the documentation of this file.
1 <?php
18 declare(strict_types=1);
19 
20 require_once(__DIR__ . "/../../../../../../libs/composer/vendor/autoload.php");
21 require_once(__DIR__ . "/../../../../Base.php");
22 require_once(__DIR__ . "/FormTest.php");
23 
25 use \ILIAS\Data;
28 
30 {
31  protected $button_factory;
32 
33 
34  public function __construct($button_factory)
35  {
36  $this->button_factory = $button_factory;
37  }
38 
39 
40  public function button()
41  {
42  return $this->button_factory;
43  }
44 }
45 
50 {
51  protected function buildFactory()
52  {
53  return new ILIAS\UI\Implementation\Component\Input\Container\Form\Factory($this->buildInputFactory());
54  }
55 
56 
57  protected function buildInputFactory()
58  {
59  $df = new Data\Factory();
60  $language = $this->createMock(\ilLanguage::class);
62  new SignalGenerator(),
63  $df,
64  new \ILIAS\Refinery\Factory($df, $language),
65  $language
66  );
67  }
68 
69  protected function buildButtonFactory()
70  {
72  }
73 
74 
75  public function getUIFactory()
76  {
77  return new WithButtonNoUIFactory($this->buildButtonFactory());
78  }
79 
80 
81  public function test_getPostURL()
82  {
83  $f = $this->buildFactory();
84  $if = $this->buildInputFactory();
85  $url = "MY_URL";
86  $form = $f->standard($url, [$if->text("label")]);
87  $this->assertEquals($url, $form->getPostURL());
88  }
89 
90 
91  public function test_render()
92  {
93  $f = $this->buildFactory();
94  $if = $this->buildInputFactory();
95 
96  $url = "MY_URL";
97  $form = $f->standard($url, [
98  $if->text("label", "byline"),
99  ]);
100 
101  $r = $this->getDefaultRenderer();
102  $html = $this->brutallyTrimHTML($r->render($form));
103 
104  $expected = $this->brutallyTrimHTML('
105 <form role="form" class="il-standard-form form-horizontal" enctype="multipart/form-data" action="MY_URL" method="post" novalidate="novalidate">
106  <div class="il-standard-form-header clearfix">
107  <div class="il-standard-form-cmd"><button class="btn btn-default" data-action="">save</button></div>
108  </div>
109  <div class="form-group row">
110  <label for="id_1" class="control-label col-sm-3">label</label>
111  <div class="col-sm-9">
112  <input id="id_1" type="text" name="form_input_1" class="form-control form-control-sm"/>
113  <div class="help-block">byline</div>
114  </div>
115  </div>
116  <div class="il-standard-form-footer clearfix">
117  <div class="il-standard-form-cmd"><button class="btn btn-default" data-action="">save</button></div>
118  </div>
119 </form>
120  ');
121  $this->assertHTMLEquals($expected, $html);
122  }
123 
124  public function test_render_no_url()
125  {
126  $f = $this->buildFactory();
127  $if = $this->buildInputFactory();
128 
129  $url = "";
130  $form = $f->standard($url, [
131  $if->text("label", "byline"),
132  ]);
133 
134  $r = $this->getDefaultRenderer();
135  $html = $this->brutallyTrimHTML($r->render($form));
136 
137  $expected = $this->brutallyTrimHTML('
138 <form role="form" class="il-standard-form form-horizontal" enctype="multipart/form-data" method="post" novalidate="novalidate">
139  <div class="il-standard-form-header clearfix">
140  <div class="il-standard-form-cmd"><button class="btn btn-default" data-action="">save</button></div>
141  </div>
142  <div class="form-group row">
143  <label for="id_1" class="control-label col-sm-3">label</label>
144  <div class="col-sm-9">
145  <input id="id_1" type="text" name="form_input_1" class="form-control form-control-sm"/>
146  <div class="help-block">byline</div>
147  </div>
148  </div>
149  <div class="il-standard-form-footer clearfix">
150  <div class="il-standard-form-cmd"><button class="btn btn-default" data-action="">save</button></div>
151  </div>
152 </form>
153  ');
154  $this->assertHTMLEquals($expected, $html);
155  }
156 
157 
158  public function testRenderWithErrorOnField()
159  {
160  $r = $this->getDefaultRenderer();
161  $df = new Data\Factory();
162  $language = $this->createMock(\ilLanguage::class);
163  $language
164  ->expects($this->once())
165  ->method("txt")
166  ->willReturn('testing error message');
167 
168  $refinery = new \ILIAS\Refinery\Factory($df, $language);
169 
171  new SignalGenerator(),
172  $df,
173  $refinery,
174  $language
175  );
176 
177  $fail = $refinery->custom()->constraint(function ($v) {
178  return false;
179  }, "This is invalid...");
180  $input = $if->text("label", "byline");
181 
182  $input = $input->withAdditionalTransformation($fail);
183 
184  $form = new Form\Standard($if, '', [$input]);
185 
186  $request = $this->createMock(ServerRequestInterface::class);
187  $request
188  ->expects($this->once())
189  ->method("getParsedBody")
190  ->willReturn([
191  'form_input_1' => ''
192  ]);
193 
194  $form = $form->withRequest($request);
195  $this->assertNull($form->getData());
196 
197  $html = $this->brutallyTrimHTML($r->render($form));
198  $expected = $this->brutallyTrimHTML('
199  <form role="form" class="il-standard-form form-horizontal" enctype="multipart/form-data" method="post" novalidate="novalidate">
200  <div class="il-standard-form-header clearfix">
201  <div class="il-standard-form-cmd"><button class="btn btn-default" data-action="">save</button></div>
202  </div>
203 
204  <div class="help-block alert alert-danger" role="alert">testing error message</div>
205 
206  <div class="form-group row">
207  <label for="id_1" class="control-label col-sm-3">label</label>
208  <div class="col-sm-9">
209  <div class="help-block alert alert-danger" role="alert">This is invalid...</div>
210  <input id="id_1" type="text" name="form_input_1" class="form-control form-control-sm" />
211  <div class="help-block">byline</div>
212  </div>
213  </div>
214  <div class="il-standard-form-footer clearfix">
215  <div class="il-standard-form-cmd"><button class="btn btn-default" data-action="">save</button></div>
216  </div>
217  </form>
218  ');
219  $this->assertHTMLEquals($expected, $html);
220  }
221 
222 
223  public function testRenderWithErrorOnForm()
224  {
225  $r = $this->getDefaultRenderer();
226  $df = new Data\Factory();
227  $language = $this->createMock(\ilLanguage::class);
228  $refinery = new \ILIAS\Refinery\Factory($df, $language);
229 
231  new SignalGenerator(),
232  $df,
233  $refinery,
234  $language
235  );
236 
237  $fail = $refinery->custom()->constraint(function ($v) {
238  return false;
239  }, "This is a fail on form.");
240  $input = $if->text("label", "byline");
241 
242  $form = new Form\Standard($if, '', [$input]);
243  $form = $form->withAdditionalTransformation($fail);
244 
245  $request = $this->createMock(ServerRequestInterface::class);
246  $request
247  ->expects($this->once())
248  ->method("getParsedBody")
249  ->willReturn([
250  'form_input_1' => ''
251  ]);
252 
253  $form = $form->withRequest($request);
254  $this->assertNull($form->getData());
255 
256  $html = $this->brutallyTrimHTML($r->render($form));
257  $expected = $this->brutallyTrimHTML('
258  <form role="form" class="il-standard-form form-horizontal" enctype="multipart/form-data" method="post" novalidate="novalidate">
259  <div class="il-standard-form-header clearfix">
260  <div class="il-standard-form-cmd"><button class="btn btn-default" data-action="">save</button></div>
261  </div>
262 
263  <div class="help-block alert alert-danger" role="alert">This is a fail on form.</div>
264 
265  <div class="form-group row">
266  <label for="id_1" class="control-label col-sm-3">label</label>
267  <div class="col-sm-9">
268  <input id="id_1" type="text" name="form_input_1" class="form-control form-control-sm" />
269  <div class="help-block">byline</div>
270  </div>
271  </div>
272  <div class="il-standard-form-footer clearfix">
273  <div class="il-standard-form-cmd"><button class="btn btn-default" data-action="">save</button></div>
274  </div>
275  </form>
276  ');
277  $this->assertHTMLEquals($expected, $html);
278  }
279 
280  public function testStandardFormRenderWithRequired(): void
281  {
282  $f = $this->buildFactory();
283  $if = $this->buildInputFactory();
284 
285  $url = "MY_URL";
286  $form = $f->standard($url, [$if->text("label", "byline")->withRequired(true)]);
287 
288  $r = $this->getDefaultRenderer();
289  $html = $this->brutallyTrimHTML($r->render($form));
290 
291  $expected = $this->brutallyTrimHTML('
292 <form role="form" class="il-standard-form form-horizontal" enctype="multipart/form-data" action="MY_URL" method="post" novalidate="novalidate">
293  <div class="il-standard-form-header clearfix">
294  <div class="il-standard-form-cmd"><button class="btn btn-default" data-action="">save</button></div>
295  </div>
296  <div class="form-group row">
297  <label for="id_1" class="control-label col-sm-3">label<span class="asterisk">*</span></label>
298  <div class="col-sm-9">
299  <input id="id_1" type="text" name="form_input_1" class="form-control form-control-sm"/>
300  <div class="help-block">byline</div>
301  </div>
302  </div>
303  <div class="il-standard-form-footer clearfix">
304  <span class="asterisk">*</span><span class="small"> required_field</span>
305  </div>
306  <div class="il-standard-form-footer clearfix">
307  <div class="il-standard-form-cmd"><button class="btn btn-default" data-action="">save</button></div>
308  </div>
309 </form>
310  ');
311  $this->assertHTMLEquals($expected, $html);
312  }
313 }
Class ChatMainBarProvider .
button()
description: purpose: > Buttons trigger interactions that change the system’s or view&#39;s status...
__construct($button_factory)
Provides common functionality for UI tests.
Definition: Base.php:262
Builds data types.
Definition: Factory.php:19
Test on standard form implementation.
$url