ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
MarkdownTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
36 
37 require_once(__DIR__ . "/../../../../../../../vendor/composer/vendor/autoload.php");
38 require_once(__DIR__ . "/../../../Base.php");
39 require_once(__DIR__ . "/InputTest.php");
40 
45 {
46  protected const TEST_ASYNC_URL = 'https://localhost';
47  protected const TEST_PARAMETER_NAME = 'preview';
48 
52 
53  protected ViewControlMode $view_control_mock;
60 
61  public function setUp(): void
62  {
63  $this->markdown_renderer = $this->getMarkdownRendererMock();
64  $this->factory = $this->buildMinimalFieldFactory();
65  $this->name_source = new DefNamesource();
66 
67  $this->view_control_mock = $this->getViewControlModeStub();
68  $this->numberedlist_glyph_mock = $this->getGlyphStub('numberedlist');
69  $this->bulledpoint_glyph_mock = $this->getGlyphStub('bulletpoint');
70  $this->header_glyph_mock = $this->getGlyphStub('header');
71  $this->italic_glyph_mock = $this->getGlyphStub('italic');
72  $this->link_glyph_mock = $this->getGlyphStub('link');
73  $this->bold_glyph_mock = $this->getGlyphStub('bold');
74 
75  parent::setUp();
76  }
77 
86  public function getUIFactory(): NoUIFactory
87  {
88  return new class (
90  $this->buildButtonFactory(),
91  $this->getSymbolFactoryMock(),
92  ) extends NoUIFactory {
93  protected ViewControlFactory $view_control_factory;
94  protected ButtonFactory $button_factory;
95  protected SymbolFactory $symbol_factory;
96 
97  public function __construct(
98  ViewControlFactory $view_control_factory,
99  ButtonFactory $button_factory,
100  SymbolFactory $symbol_factory,
101  ) {
102  $this->view_control_factory = $view_control_factory;
103  $this->button_factory = $button_factory;
104  $this->symbol_factory = $symbol_factory;
105  }
106 
107  public function viewControl(): ViewControlFactory
108  {
109  return $this->view_control_factory;
110  }
111 
112  public function button(): ButtonFactory
113  {
114  return $this->button_factory;
115  }
116 
117  public function symbol(): SymbolFactory
118  {
119  return $this->symbol_factory;
120  }
121  };
122  }
123 
124  public function testRender(): void
125  {
126  $label = 'test_label';
127 
128  $input = $this->factory->markdown($this->markdown_renderer, $label)->withNameFrom($this->name_source);
129 
130  $expected = $this->brutallyTrimHTML(
131  "
132  <fieldset class=\"c-input\" data-il-ui-component=\"markdown-field-input\" data-il-ui-input-name=\"name_0\" id=\"id_8\" tabindex=\"0\">
133  <label>$label</label>
134  <div class=\"c-input__field\">
135 
136  <div class=\"c-field-markdown\">
137  <div class=\"c-field-markdown__controls\">
138  view_control_mode
139  <div class=\"c-field-markdown__actions\">
140  <span data-action=\"insert-heading\">
141  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_2\">header</button>
142  </span>
143  <span data-action=\"insert-link\">
144  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_3\">link</button>
145  </span>
146  <span data-action=\"insert-bold\">
147  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_4\">bold</button>
148  </span>
149  <span data-action=\"insert-italic\">
150  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_5\">italic</button>
151  </span>
152  <span data-action=\"insert-bullet-points\">
153  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_7\">bulletpoint</button>
154  </span>
155  <span data-action=\"insert-enumeration\">
156  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_6\">numberedlist</button>
157  </span>
158  </div>
159  </div>
160  <textarea id=\"id_1\" class=\"c-field-textarea\" name=\"name_0\"></textarea>
161  <div class=\"c-field-markdown__preview hidden\">
162  </div>
163  </div>
164 
165  </div>
166  </fieldset>
167  "
168  );
169 
170  $html = $this->brutallyTrimHTML($this->getRendererWithStubs()->render($input));
171  $this->assertEquals($expected, $html);
172  }
173 
174  public function testRenderWithByline(): void
175  {
176  $label = 'test_label';
177  $byline = 'test_byline';
178 
179  $input = $this->factory->markdown(
180  $this->markdown_renderer,
181  $label,
182  $byline
183  )->withNameFrom($this->name_source);
184 
185  $expected = $this->brutallyTrimHTML(
186  "
187  <fieldset class=\"c-input\" data-il-ui-component=\"markdown-field-input\" data-il-ui-input-name=\"name_0\" id=\"id_8\" tabindex=\"0\">
188  <label>$label</label>
189  <div class=\"c-input__field\">
190 
191  <div class=\"c-field-markdown\">
192  <div class=\"c-field-markdown__controls\">
193  view_control_mode
194  <div class=\"c-field-markdown__actions\">
195  <span data-action=\"insert-heading\">
196  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_2\">header</button>
197  </span>
198  <span data-action=\"insert-link\">
199  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_3\">link</button>
200  </span>
201  <span data-action=\"insert-bold\">
202  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_4\">bold</button>
203  </span>
204  <span data-action=\"insert-italic\">
205  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_5\">italic</button>
206  </span>
207  <span data-action=\"insert-bullet-points\">
208  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_7\">bulletpoint</button>
209  </span>
210  <span data-action=\"insert-enumeration\">
211  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_6\">numberedlist</button>
212  </span>
213  </div>
214  </div>
215 
216  <textarea id=\"id_1\" class=\"c-field-textarea\" name=\"name_0\"></textarea>
217 
218  <div class=\"c-field-markdown__preview hidden\">
219  </div>
220 
221  </div>
222 
223  </div>
224  <div class=\"c-input__help-byline\">$byline</div>
225  </fieldset>
226  "
227  );
228 
229  $html = $this->brutallyTrimHTML($this->getRendererWithStubs()->render($input));
230 
231  $this->assertEquals($expected, $html);
232  }
233 
234  public function testRenderWithLimits(): void
235  {
236  $label = 'test_label';
237  $byline = 'test_byline';
238  $min = 1;
239  $max = 9;
240 
241  $input = $this->factory->markdown(
242  $this->markdown_renderer,
243  $label,
244  $byline
245  )->withMinLimit($min)->withMaxLimit($max)->withNameFrom($this->name_source);
246 
247  $expected = $this->brutallyTrimHTML(
248  "
249  <fieldset class=\"c-input\" data-il-ui-component=\"markdown-field-input\" data-il-ui-input-name=\"name_0\" id=\"id_8\" tabindex=\"0\">
250  <label>$label</label>
251  <div class=\"c-input__field\">
252 
253  <div class=\"c-field-markdown\">
254  <div class=\"c-field-markdown__controls\">
255  view_control_mode
256  <div class=\"c-field-markdown__actions\">
257  <span data-action=\"insert-heading\">
258  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_2\">header</button>
259  </span>
260  <span data-action=\"insert-link\">
261  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_3\">link</button>
262  </span>
263  <span data-action=\"insert-bold\">
264  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_4\">bold</button>
265  </span>
266  <span data-action=\"insert-italic\">
267  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_5\">italic</button>
268  </span>
269  <span data-action=\"insert-bullet-points\">
270  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_7\">bulletpoint</button>
271  </span>
272  <span data-action=\"insert-enumeration\">
273  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_6\">numberedlist</button>
274  </span>
275  </div>
276  </div>
277  <textarea id=\"id_1\" class=\"c-field-textarea\" name=\"name_0\" minlength=\"$min\" maxlength=\"$max\"></textarea>
278  <div class=\"ui-input-textarea-remainder\"> ui_chars_remaining<span data-action=\"remainder\">$max</span></div>
279  <div class=\"c-field-markdown__preview hidden\">
280  </div>
281  </div>
282 
283  </div>
284  <div class=\"c-input__help-byline\">$byline</div>
285  </fieldset>
286  "
287  );
288 
289  $html = $this->brutallyTrimHTML($this->getRendererWithStubs()->render($input));
290 
291  $this->assertEquals($expected, $html);
292  }
293 
294  public function testRenderWithDisabled(): void
295  {
296  $label = 'test_label';
297  $byline = 'test_byline';
298 
299  $input = $this->factory->markdown(
300  $this->markdown_renderer,
301  $label,
302  $byline
303  )->withDisabled(true)->withNameFrom($this->name_source);
304 
305  $expected = $this->brutallyTrimHTML(
306  "
307  <fieldset class=\"c-input\" data-il-ui-component=\"markdown-field-input\" data-il-ui-input-name=\"name_0\" disabled=\"disabled\" id=\"id_2\" tabindex=\"0\">
308  <label>$label</label>
309  <div class=\"c-input__field\">
310 
311  <div class=\"c-field-markdown\">
312  <div class=\"c-field-markdown__controls\">
313  view_control_mode
314  <div class=\"c-field-markdown__actions\">
315  <span data-action=\"insert-heading\">
316  <button class=\"btn btn-default\" data-action=\"#\" disabled=\"disabled\">header</button>
317  </span>
318  <span data-action=\"insert-link\">
319  <button class=\"btn btn-default\" data-action=\"#\" disabled=\"disabled\">link</button>
320  </span>
321  <span data-action=\"insert-bold\">
322  <button class=\"btn btn-default\" data-action=\"#\" disabled=\"disabled\">bold</button>
323  </span>
324  <span data-action=\"insert-italic\">
325  <button class=\"btn btn-default\" data-action=\"#\" disabled=\"disabled\">italic</button>
326  </span>
327  <span data-action=\"insert-bullet-points\">
328  <button class=\"btn btn-default\" data-action=\"#\" disabled=\"disabled\">bulletpoint</button>
329  </span>
330  <span data-action=\"insert-enumeration\">
331  <button class=\"btn btn-default\" data-action=\"#\" disabled=\"disabled\">numberedlist</button>
332  </span>
333  </div>
334  </div>
335  <textarea id=\"id_1\" class=\"c-field-textarea\" name=\"name_0\"></textarea>
336  <div class=\"c-field-markdown__preview hidden\">
337  </div>
338  </div>
339 
340  </div>
341  <div class=\"c-input__help-byline\">$byline</div>
342  </fieldset>
343  "
344  );
345 
346  $html = $this->brutallyTrimHTML($this->getRendererWithStubs()->render($input));
347 
348  $this->assertEquals($expected, $html);
349  }
350 
351  public function testRenderWithRequired(): void
352  {
353  $label = 'test_label';
354  $byline = 'test_byline';
355 
356  $input = $this->factory->markdown(
357  $this->markdown_renderer,
358  $label,
359  $byline
360  )->withRequired(true)->withNameFrom($this->name_source);
361 
362  $expected = $this->brutallyTrimHTML(
363  "
364  <fieldset class=\"c-input\" data-il-ui-component=\"markdown-field-input\" data-il-ui-input-name=\"name_0\" id=\"id_8\" tabindex=\"0\">
365  <label>$label<span class=\"asterisk\" aria-label=\"required_field\">*</span></label>
366  <div class=\"c-input__field\">
367 
368  <div class=\"c-field-markdown\">
369  <div class=\"c-field-markdown__controls\">
370  view_control_mode
371  <div class=\"c-field-markdown__actions\">
372  <span data-action=\"insert-heading\">
373  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_2\">header</button>
374  </span>
375  <span data-action=\"insert-link\">
376  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_3\">link</button>
377  </span>
378  <span data-action=\"insert-bold\">
379  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_4\">bold</button>
380  </span>
381  <span data-action=\"insert-italic\">
382  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_5\">italic</button>
383  </span>
384  <span data-action=\"insert-bullet-points\">
385  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_7\">bulletpoint</button>
386  </span>
387  <span data-action=\"insert-enumeration\">
388  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_6\">numberedlist</button>
389  </span>
390  </div>
391  </div>
392  <textarea id=\"id_1\" class=\"c-field-textarea\" name=\"name_0\"></textarea>
393  <div class=\"c-field-markdown__preview hidden\">
394  </div>
395  </div>
396 
397  </div>
398  <div class=\"c-input__help-byline\">$byline</div>
399  </fieldset>
400  "
401  );
402 
403  $html = $this->brutallyTrimHTML($this->getRendererWithStubs()->render($input));
404 
405  $this->assertEquals($expected, $html);
406  }
407 
408  public function testRenderWithError(): void
409  {
410  $label = 'test_label';
411  $byline = 'test_byline';
412  $error = 'test_error';
413 
414  $input = $this->factory->markdown(
415  $this->markdown_renderer,
416  $label,
417  $byline
418  )->withError($error)->withNameFrom($this->name_source);
419 
420  $expected = $this->brutallyTrimHTML(
421  "
422  <fieldset class=\"c-input\" data-il-ui-component=\"markdown-field-input\" data-il-ui-input-name=\"name_0\" aria-describedby=\"id_9\" id=\"id_8\" tabindex=\"0\">
423  <label>$label</label>
424  <div class=\"c-input__field\">
425 
426  <div class=\"c-field-markdown\">
427  <div class=\"c-field-markdown__controls\">
428  view_control_mode
429  <div class=\"c-field-markdown__actions\">
430  <span data-action=\"insert-heading\">
431  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_2\">header</button>
432  </span>
433  <span data-action=\"insert-link\">
434  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_3\">link</button>
435  </span>
436  <span data-action=\"insert-bold\">
437  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_4\">bold</button>
438  </span>
439  <span data-action=\"insert-italic\">
440  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_5\">italic</button>
441  </span>
442  <span data-action=\"insert-bullet-points\">
443  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_7\">bulletpoint</button>
444  </span>
445  <span data-action=\"insert-enumeration\">
446  <button class=\"btn btn-default\" data-action=\"#\" id=\"id_6\">numberedlist</button>
447  </span>
448  </div>
449  </div>
450  <textarea id=\"id_1\" class=\"c-field-textarea\" name=\"name_0\"></textarea>
451  <div class=\"c-field-markdown__preview hidden\">
452  </div>
453  </div>
454 
455  </div>
456  <div class=\"c-input__error-msg alert alert-danger\" id=\"id_9\"><span class=\"sr-only\">ui_error:</span>$error</div>
457  <div class=\"c-input__help-byline\">$byline</div>
458  </fieldset>
459  "
460  );
461 
462  $html = $this->brutallyTrimHTML($this->getRendererWithStubs()->render($input));
463 
464  $this->assertEquals($expected, $html);
465  }
466 
468  {
469  return $this->getDefaultRenderer(null, [
470  $this->view_control_mock,
471  $this->header_glyph_mock,
472  $this->italic_glyph_mock,
473  $this->bold_glyph_mock,
474  $this->link_glyph_mock,
475  $this->numberedlist_glyph_mock,
476  $this->bulledpoint_glyph_mock,
477  ]);
478  }
479 
481  {
482  return new FieldFactory(
483  $this->createMock(UploadLimitResolver::class),
484  new SignalGenerator(),
485  $this->createMock(DataFactory::class),
486  $this->createMock(Refinery::class),
487  $this->getLanguage()
488  );
489  }
490 
491  protected function buildButtonFactory(): ButtonFactory
492  {
493  return new ButtonFactory();
494  }
495 
497  {
498  $markdown_renderer = $this->createMock(MarkdownRenderer::class);
499  $markdown_renderer->method('getAsyncUrl')->willReturn(self::TEST_ASYNC_URL);
500  $markdown_renderer->method('getParameterName')->willReturn(self::TEST_PARAMETER_NAME);
501  $markdown_renderer->method('render')->willReturnCallback(
502  static function ($value) {
503  return $value;
504  }
505  );
506 
507  return $markdown_renderer;
508  }
509 
510  protected function getSymbolFactoryMock(): SymbolFactory
511  {
512  $glyph_factory = $this->createMock(GlyphFactory::class);
513  $glyph_factory->method('header')->willReturn($this->header_glyph_mock);
514  $glyph_factory->method('italic')->willReturn($this->italic_glyph_mock);
515  $glyph_factory->method('bold')->willReturn($this->bold_glyph_mock);
516  $glyph_factory->method('link')->willReturn($this->link_glyph_mock);
517  $glyph_factory->method('numberedlist')->willReturn($this->numberedlist_glyph_mock);
518  $glyph_factory->method('bulletlist')->willReturn($this->bulledpoint_glyph_mock);
519 
520  $symbol_factory = $this->createMock(SymbolFactory::class);
521  $symbol_factory->method('glyph')->willReturn($glyph_factory);
522 
523  return $symbol_factory;
524  }
525 
527  {
528  $view_control_factory = $this->createMock(ViewControlFactory::class);
529  $view_control_factory->method('mode')->willReturn($this->getViewControlModeStub());
530 
531  return $view_control_factory;
532  }
533 
534  protected function getViewControlModeStub(): ViewControlMode
535  {
536  $view_control = $this->createMock(ViewControlMode::class);
537  $view_control->method('getCanonicalName')->willReturn('view_control_mode');
538 
539  return $view_control;
540  }
541 
542  protected function getGlyphStub(string $name): Glyph
543  {
544  $glyph = $this->createMock(Glyph::class);
545  $glyph->method('getCanonicalName')->willReturn($name);
546  // will be called in the rendering process of this input.
547  $glyph->method('withUnavailableAction')->willReturnSelf();
548 
549  return $glyph;
550  }
551 }
const TEST_ASYNC_URL
button(string $caption, string $cmd)
getUIFactory()
The rendering of this input requires actual or minimal instances of:
Glyph $header_glyph_mock
factory()
Glyph $bulledpoint_glyph_mock
DefNamesource $name_source
Glyph $link_glyph_mock
ViewControlMode $view_control_mock
buildMinimalFieldFactory()
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Glyph $numberedlist_glyph_mock
Glyph $bold_glyph_mock
getViewControlFactoryMock()
getLanguage()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
FieldFactory $factory
__construct(Container $dic, ilPlugin $plugin)
const TEST_PARAMETER_NAME
getGlyphStub(string $name)
Glyph $italic_glyph_mock
MarkdownRenderer $markdown_renderer