ILIAS  release_8 Revision v8.23
NoSubmitFormTest.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
21 
25 use ILIAS\UI\Component\Input\Field\Factory as InputFactory;
29 
30 require_once(__DIR__ . "/../../../../Base.php");
31 
32 class InputNameSource implements NameSource
33 {
34  public int $count = 0;
35 
36  public function getNewName(): string
37  {
38  $name = "form_input_$this->count";
39  $this->count++;
40 
41  return $name;
42  }
43 
44  public function getNewDedicatedName(string $dedicated_name): string
45  {
46  $name = $dedicated_name . "_$this->count";
47  $this->count++;
48 
49  return $name;
50  }
51 }
52 
57 {
60  protected Refinery $refinery;
61  protected \ilLanguageMock $language;
62 
63  public function setUp(): void
64  {
65  $this->signal_generator = new \SignalGeneratorMock();
66  $this->namesource = new InputNameSource();
67  $this->language = $this->getLanguage();
68  $this->refinery = new Refinery(
69  new \ILIAS\Data\Factory(),
70  $this->language
71  );
72 
73  parent::setUp();
74  }
75 
76  public function test_render(): void
77  {
78  $post_url = 'http://ilias.localhost/some_url?param1=foo&param2=bar';
79 
80  $dummy_input = $this->buildInputFactory()->text('test_label');
81 
82  $form = new FormWithoutSubmitButton(
83  $this->signal_generator,
84  $this->buildInputFactory(),
85  $this->namesource,
86  $post_url,
87  [$dummy_input]
88  );
89 
90  $expected_html =
91  "<form id=\"id_1\" role=\"form\" class=\"il-standard-form form-horizontal\" enctype=\"multipart/form-data\" action=\"$post_url\" method=\"post\" novalidate=\"novalidate\">" .
92  $dummy_input->getCanonicalName() .
93  "</form>";
94 
95  $renderer = $this->getDefaultRenderer(null, [$dummy_input]);
96 
97  $this->assertEquals(
98  $this->brutallyTrimHTML($expected_html),
99  $this->brutallyTrimHTML($renderer->render($form))
100  );
101  }
102 
103  public function test_render_with_required_inputs(): void
104  {
105  $post_url = 'http://ilias.localhost/some_url?param1=foo&param2=bar';
106  $required_lang_var = 'required_field';
107 
108  $dummy_input = $this->buildInputFactory()->text('test_label')->withRequired(true);
109 
110  $form = new FormWithoutSubmitButton(
111  $this->signal_generator,
112  $this->buildInputFactory(),
113  $this->namesource,
114  $post_url,
115  [$dummy_input]
116  );
117 
118  $expected_html =
119  "<form id=\"id_1\" role=\"form\" class=\"il-standard-form form-horizontal\" enctype=\"multipart/form-data\" action=\"$post_url\" method=\"post\" novalidate=\"novalidate\">" .
120  $dummy_input->getCanonicalName() .
121  "<div class=\"il-standard-form-footer clearfix\"><span class=\"asterisk\">*</span><span class=\"small\"> $required_lang_var</span></div>" .
122  "</form>";
123 
124  $renderer = $this->getDefaultRenderer(null, [$dummy_input]);
125 
126  $this->assertEquals(
127  $this->brutallyTrimHTML($expected_html),
128  $this->brutallyTrimHTML($renderer->render($form))
129  );
130  }
131 
132  public function test_render_with_error(): void
133  {
134  $post_url = 'http://ilias.localhost/some_url?param1=foo&param2=bar';
135  $error_lang_var = 'ui_error_in_group';
136 
137  $dummy_input = $this->buildInputFactory()->text('test_label')->withAdditionalTransformation(
138  $this->refinery->custom()->constraint(
139  static function ($value): bool {
140  return false; // always fail for testing purposes.
141  },
142  'this message does not matter because the input will not be properly rendered anyways.'
143  )
144  );
145 
146  $form = new FormWithoutSubmitButton(
147  $this->signal_generator,
148  $this->buildInputFactory(),
149  $this->namesource,
150  $post_url,
151  [$dummy_input]
152  );
153 
154  $request = $this->createMock(ServerRequestInterface::class);
155  $request->method('getParsedBody')->willReturn([
156  'form_0/form_input_1' => '',
157  ]);
158 
159  $form = $form->withRequest($request);
160  $data = $form->getData();
161 
162  $expected_html =
163  "<form id=\"id_1\" role=\"form\" class=\"il-standard-form form-horizontal\" enctype=\"multipart/form-data\" action=\"$post_url\" method=\"post\" novalidate=\"novalidate\">" .
164  "<div class=\"help-block alert alert-danger\" role=\"alert\">$error_lang_var</div>" .
165  $dummy_input->getCanonicalName() .
166  "</form>";
167 
168  $renderer = $this->getDefaultRenderer(null, [$dummy_input]);
169 
170  $this->assertEquals(
171  $this->brutallyTrimHTML($expected_html),
172  $this->brutallyTrimHTML($renderer->render($form))
173  );
174  }
175 
176  protected function buildInputFactory(): InputFactory
177  {
178  $df = new \ILIAS\Data\Factory();
179  return new \ILIAS\UI\Implementation\Component\Input\Field\Factory(
180  $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
181  $this->signal_generator,
182  $df,
183  $this->refinery,
184  $this->language
185  );
186  }
187 }
This is what a factory for input fields looks like.
Definition: Factory.php:28
Class Factory.
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($format !==null) $name
Definition: metadata.php:247
This is how a factory for forms looks like.
Definition: Factory.php:26
Provides common functionality for UI tests.
Definition: Base.php:298
Describes a source for input names.
Definition: NameSource.php:26