ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ButtonTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/../../../../libs/composer/vendor/autoload.php");
22 require_once(__DIR__ . "/../../Base.php");
23 
24 use ILIAS\UI\Component as C;
27 use ILIAS\UI\Help;
28 
33 {
34  public const NOT_APPLICABLE = true;
35 
36  public function getButtonFactory(): Factory
37  {
38  return new Factory();
39  }
40 
41  public static array $canonical_css_classes = [
42  "standard" => "btn btn-default",
43  "primary" => "btn btn-default btn-primary",
44  "shy" => "btn btn-link",
45  "tag" => "btn btn-tag btn-tag-relevance-veryhigh"
46  ];
47 
48  public function testImplementsFactoryInterface(): void
49  {
50  $f = $this->getButtonFactory();
51 
52  $this->assertInstanceOf("ILIAS\\UI\\Component\\Button\\Factory", $f);
53  $this->assertInstanceOf(
54  "ILIAS\\UI\\Component\\Button\\Standard",
55  $f->standard("label", "http://www.ilias.de")
56  );
57  $this->assertInstanceOf(
58  "ILIAS\\UI\\Component\\Button\\Primary",
59  $f->primary("label", "http://www.ilias.de")
60  );
61  $this->assertInstanceOf(
62  "ILIAS\\UI\\Component\\Button\\Close",
63  $f->close()
64  );
65  $this->assertInstanceOf(
66  "ILIAS\\UI\\Component\\Button\\Shy",
67  $f->shy("label", "http://www.ilias.de")
68  );
69  }
70 
74  public function testButtonLabelOrGlyphOnly(string $factory_method): void
75  {
76  $this->expectException(TypeError::class);
77  $f = $this->getButtonFactory();
78  $f->$factory_method($this, "http://www.ilias.de");
79  }
80 
84  public function testButtonStringActionOnly(string $factory_method): void
85  {
86  $this->expectException(InvalidArgumentException::class);
87  $f = $this->getButtonFactory();
88  $f->$factory_method("label", $this);
89  }
90 
94  public function testButtonLabel(string $factory_method): void
95  {
96  $f = $this->getButtonFactory();
97  $b = $f->$factory_method("label", "http://www.ilias.de");
98 
99  $this->assertEquals("label", $b->getLabel());
100  }
101 
105  public function testButtonWithLabel(string $factory_method): void
106  {
107  $f = $this->getButtonFactory();
108  $b = $f->$factory_method("label", "http://www.ilias.de");
109 
110  $b2 = $b->withLabel("label2");
111 
112  $this->assertEquals("label", $b->getLabel());
113  $this->assertEquals("label2", $b2->getLabel());
114  }
115 
119  public function testButtonAction(string $factory_method): void
120  {
121  $f = $this->getButtonFactory();
122  $b = $f->$factory_method("label", "http://www.ilias.de");
123 
124  $this->assertEquals("http://www.ilias.de", $b->getAction());
125  }
126 
130  public function testButtonActivatedOnDefault(string $factory_method): void
131  {
132  $f = $this->getButtonFactory();
133  $b = $f->$factory_method("label", "http://www.ilias.de");
134 
135  $this->assertTrue($b->isActive());
136  }
137 
141  public function testButtonDeactivation(string $factory_method): void
142  {
143  $f = $this->getButtonFactory();
144  $b = $f->$factory_method("label", "http://www.ilias.de")
145  ->withUnavailableAction();
146 
147  $this->assertFalse($b->isActive());
148  $this->assertEquals("http://www.ilias.de", $b->getAction());
149 
150  $b = $b->withUnavailableAction(false);
151  $this->assertTrue($b->isActive());
152  }
153 
157  public function testButtonWithLoadingAnimation(): void
158  {
159  $f = $this->getButtonFactory();
160  foreach (["standard", "primary"] as $method) {
161  $b = $f->$method("label", "http://www.ilias.de");
162 
163  $this->assertFalse($b->hasLoadingAnimationOnClick());
164 
165  $b = $b->withLoadingAnimationOnClick(true);
166 
167  $this->assertTrue($b->hasLoadingAnimationOnClick());
168  }
169  }
170 
174  public function testRenderButtonLabel(string $factory_method): void
175  {
176  $ln = "http://www.ilias.de";
177  $f = $this->getButtonFactory();
178  $b = $f->$factory_method("label", $ln);
179  $r = $this->getDefaultRenderer();
180 
181  $html = $this->normalizeHTML($r->render($b));
182 
183  $css_classes = self::$canonical_css_classes[$factory_method];
184  $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"id_1\">" .
185  "label" .
186  "</button>";
187  $this->assertHTMLEquals($expected, $html);
188  }
189 
193  public function testRenderButtonDisabled(string $factory_method): void
194  {
195  $ln = "http://www.ilias.de";
196  $f = $this->getButtonFactory();
197  $b = $f->$factory_method("label", $ln)
198  ->withUnavailableAction();
199  $r = $this->getDefaultRenderer();
200 
201  $html = $this->normalizeHTML($r->render($b));
202 
203  $css_classes = self::$canonical_css_classes[$factory_method];
204  $expected = "<button class=\"$css_classes\" data-action=\"$ln\" disabled=\"disabled\">" .
205  "label" .
206  "</button>";
207  $this->assertHTMLEquals($expected, $html);
208  }
209 
210  public function testRenderCloseButton(): void
211  {
212  $f = $this->getButtonFactory();
213  $r = $this->getDefaultRenderer();
214  $b = $f->close();
215 
216  $html = $this->normalizeHTML($r->render($b));
217 
218  $expected = "<button type=\"button\" class=\"close\" aria-label=\"close\">" .
219  " <span aria-hidden=\"true\">&times;</span>" .
220  "</button>";
221  $this->assertEquals($expected, $html);
222  }
223 
224  public function testRenderMinimizeButton(): void
225  {
226  $f = $this->getButtonFactory();
227  $r = $this->getDefaultRenderer();
228  $b = $f->minimize();
229 
230  $html = $this->normalizeHTML($r->render($b));
231 
232  $expected = "<button type=\"button\" class=\"minimize\" aria-label=\"minimize\">" .
233  " <span aria-hidden=\"true\">−</span>" .
234  "</button>";
235  $this->assertEquals($expected, $html);
236  }
237 
241  public function testRenderButtonWithOnLoadCode(string $factory_method): void
242  {
243  $ln = "http://www.ilias.de";
244  $f = $this->getButtonFactory();
245  $r = $this->getDefaultRenderer();
246  $ids = array();
247  $b = $f->$factory_method("label", $ln)
248  ->withOnLoadCode(function ($id) use (&$ids): string {
249  $ids[] = $id;
250  return "";
251  });
252 
253  $html = $this->normalizeHTML($r->render($b));
254 
255  $this->assertCount(1, $ids);
256 
257  $id = $ids[0];
258  $css_classes = self::$canonical_css_classes[$factory_method];
259  $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"$id\">" .
260  "label" .
261  "</button>";
262  $this->assertHTMLEquals($expected, $html);
263  }
264 
265  public function testRenderCloseButtonWithOnLoadCode(): void
266  {
267  $f = $this->getButtonFactory();
268  $r = $this->getDefaultRenderer();
269  $ids = array();
270  $b = $f->close()
271  ->withOnLoadCode(function ($id) use (&$ids): string {
272  $ids[] = $id;
273  return "";
274  });
275 
276  $html = $this->normalizeHTML($r->render($b));
277 
278  $this->assertCount(1, $ids);
279 
280  $id = $ids[0];
281  $expected = "<button type=\"button\" class=\"close\" aria-label=\"close\" id=\"$id\">" .
282  " <span aria-hidden=\"true\">&times;</span>" .
283  "</button>";
284  $this->assertEquals($expected, $html);
285  }
286 
287  public function testBtnTagRelevance(): void
288  {
289  $f = $this->getButtonFactory();
290  $b = $f->tag('tag', '#');
291 
292  $this->expectException(TypeError::class);
293  $b->withRelevance(0);
294 
295  $this->expectException(TypeError::class);
296  $b->withRelevance('notsoimportant');
297  }
298 
299  public function testRenderBtnTagRelevance(): void
300  {
301  $expectations = array(
302  '<button class="btn btn-tag btn-tag-relevance-verylow" data-action="#" id="id_1">tag</button>',
303  '<button class="btn btn-tag btn-tag-relevance-low" data-action="#" id="id_2">tag</button>',
304  '<button class="btn btn-tag btn-tag-relevance-middle" data-action="#" id="id_3">tag</button>',
305  '<button class="btn btn-tag btn-tag-relevance-high" data-action="#" id="id_4">tag</button>',
306  '<button class="btn btn-tag btn-tag-relevance-veryhigh" data-action="#" id="id_5">tag</button>'
307  );
308 
309  $f = $this->getButtonFactory();
310  $r = $this->getDefaultRenderer();
311  $t = $f->tag('tag', '#');
312  $possible_relevances = array(
313  $t::REL_VERYLOW,
314  $t::REL_LOW,
315  $t::REL_MID,
316  $t::REL_HIGH,
317  $t::REL_VERYHIGH
318  );
319  foreach ($possible_relevances as $w) {
320  $html = $this->normalizeHTML(
321  $r->render($t->withRelevance($w))
322  );
323  $expected = $expectations[array_search($w, $possible_relevances)];
324  $this->assertEquals($expected, $html);
325  }
326  }
327 
328  public function testRenderBtnTagColors(): void
329  {
330  $f = $this->getButtonFactory();
331  $r = $this->getDefaultRenderer();
332  $df = new \ILIAS\Data\Factory();
333 
334  $bgcol = $df->color('#00ff00');
335 
336  $b = $f->tag('tag', '#')
337  ->withBackgroundColor($bgcol);
338  $html = $this->normalizeHTML($r->render($b));
339  $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #000000;" data-action="#" id="id_1">tag</button>';
340  $this->assertEquals($expected, $html);
341 
342  $fcol = $df->color('#ddd');
343  $b = $b->withForegroundColor($fcol);
344  $html = $this->normalizeHTML($r->render($b));
345  $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #dddddd;" data-action="#" id="id_2">tag</button>';
346  $this->assertEquals($expected, $html);
347  }
348 
349  public function testRenderBtnTagClasses(): void
350  {
351  $f = $this->getButtonFactory();
352  $r = $this->getDefaultRenderer();
353  $df = new \ILIAS\Data\Factory();
354 
355  $classes = array('cl1', 'cl2');
356  $b = $f->tag('tag', '#')
357  ->withClasses($classes);
358  $this->assertEquals($classes, $b->getClasses());
359 
360  $html = $this->normalizeHTML($r->render($b));
361  $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh cl1 cl2" data-action="#" id="id_1">tag</button>';
362  $this->assertEquals($expected, $html);
363  }
364 
368  public function testButtonWithAriaLabel(string $factory_method): void
369  {
370  $f = $this->getButtonFactory();
371  $b = $f->$factory_method("label", "http://www.ilias.de")->withAriaLabel("ariatext");
372  $this->assertEquals("ariatext", $b->getAriaLabel());
373  }
374 
378  public function testButtonWithEngageable(string $factory_method): void
379  {
380  $f = $this->getButtonFactory();
381  $b = $f->$factory_method("label", "http://www.ilias.de");
382  if ($b instanceof C\Button\Engageable) {
383  $this->assertEquals(false, $b->isEngageable());
384  $b2 = $f->$factory_method("label", "http://www.ilias.de")->withEngagedState(false);
385  $this->assertEquals(true, $b2->isEngageable());
386  } else {
387  $this->assertTrue(self::NOT_APPLICABLE);
388  }
389  }
390 
394  public function testButtonWithEngaged(string $factory_method): void
395  {
396  $f = $this->getButtonFactory();
397  $b = $f->$factory_method("label", "http://www.ilias.de");
398  if ($b instanceof C\Button\Engageable) {
399  $b = $b->withEngagedState(false);
400  $this->assertEquals(false, $b->isEngaged());
401  $b2 = $f->$factory_method("label", "http://www.ilias.de")->withEngagedState(true);
402  $this->assertEquals(true, $b2->isEngaged());
403  } else {
404  $this->assertTrue(self::NOT_APPLICABLE);
405  }
406  }
407 
411  public function testRenderButtonWithAriaLabel(string $factory_method): void
412  {
413  $ln = "http://www.ilias.de";
414  $f = $this->getButtonFactory();
415  $r = $this->getDefaultRenderer();
416  $b = $f->$factory_method("label", $ln)->withAriaLabel("aria label text");
417  $aria_label = $b->getAriaLabel();
418 
419  $html = $this->normalizeHTML($r->render($b));
420  $css_classes = self::$canonical_css_classes[$factory_method];
421  $expected = "<button class=\"$css_classes\" aria-label=\"$aria_label\" data-action=\"$ln\" id=\"id_1\">" .
422  "label" .
423  "</button>";
424  $this->assertHTMLEquals($expected, $html);
425  }
426 
430  public function testRenderButtonWithAriaPressed(string $factory_method): void
431  {
432  $ln = "http://www.ilias.de";
433  $f = $this->getButtonFactory();
434  $r = $this->getDefaultRenderer();
435  $b = $f->$factory_method("label", $ln);
436  if ($b instanceof C\Button\Engageable) {
437  $b = $b->withEngagedState(true);
438 
439  $html = $this->normalizeHTML($r->render($b));
440  $css_classes = self::$canonical_css_classes[$factory_method];
441  $css_classes .= ' engaged';
442  $expected = "<button class=\"$css_classes\" aria-pressed=\"true\" data-action=\"$ln\" id=\"id_1\">" .
443  "label" .
444  "</button>";
445  $this->assertHTMLEquals($expected, $html);
446  } else {
447  $this->assertTrue(self::NOT_APPLICABLE);
448  }
449  }
450 
454  public function testWithOnClickRemovesAction(string $factory_method): void
455  {
456  $f = $this->getButtonFactory();
457  $signal = $this->createMock(C\Signal::class);
458  $button = $f->$factory_method("label", "http://www.example.com");
459  $this->assertEquals("http://www.example.com", $button->getAction());
460 
461  $button = $button->withOnClick($signal);
462 
463  $this->assertEquals([$signal], $button->getAction());
464  }
465 
469  public function testAppendOnClickAppendsToAction(string $factory_method): void
470  {
471  $f = $this->getButtonFactory();
472  $signal1 = $this->createMock(C\Signal::class);
473  $signal2 = $this->createMock(C\Signal::class);
474  $button = $f->$factory_method("label", "http://www.example.com");
475 
476  $button = $button->withOnClick($signal1)->appendOnClick($signal2);
477 
478  $this->assertEquals([$signal1, $signal2], $button->getAction());
479  }
480 
484  public function testRenderButtonWithSignal(string $factory_method): void
485  {
486  $ln = "http://www.ilias.de";
487  $f = $this->getButtonFactory();
488  $signal = $this->createMock(Signal::class);
489  $signal->method("__toString")
490  ->willReturn("MOCK_SIGNAL");
491 
492  $b = $f->$factory_method("label", $ln)
493  ->withOnClick($signal);
494  $r = $this->getDefaultRenderer();
495 
496  $html = $this->normalizeHTML($r->render($b));
497 
498  $css_classes = self::$canonical_css_classes[$factory_method];
499  $expected = "<button class=\"$css_classes\" id=\"id_1\">" .
500  "label" .
501  "</button>";
502  $this->assertHTMLEquals($expected, $html);
503  }
504 
508  public function testRenderButtonWithOnClickAnimation(): void
509  {
510  foreach (["primary", "standard"] as $method) {
511  $ln = "http://www.ilias.de";
512  $f = $this->getButtonFactory();
513  $r = $this->getDefaultRenderer();
514  $b = $f->$method("label", $ln)
515  ->withLoadingAnimationOnClick(true);
516 
517  $html = $this->normalizeHTML($r->render($b));
518 
519  $css_classes = self::$canonical_css_classes[$method];
520  $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"id_1\">" .
521  "label" .
522  "</button>";
523  $this->assertHTMLEquals($expected, $html);
524  }
525  }
526 
530  public function testButtonRendersTooltip(string $factory_method): void
531  {
532  $f = $this->getButtonFactory();
533  $r = $this->getDefaultRenderer();
534  $ln = "http://www.ilias.de";
535 
536  $button =
537  $f->$factory_method("label", $ln)
538  ->withHelpTopics(new Help\Topic("a"), new Help\Topic("b"));
539 
540  $css_classes = self::$canonical_css_classes[$factory_method];
541  $expected =
542  "<div class=\"c-tooltip__container\">" .
543  "<button class=\"$css_classes\" aria-describedby=\"id_2\" data-action=\"$ln\" id=\"id_1\" >" .
544  "label" .
545  "</button>" .
546  "<div id=\"id_2\" role=\"tooltip\" class=\"c-tooltip c-tooltip--hidden\">" .
547  "<p>tooltip: a</p>" .
548  "<p>tooltip: b</p>" .
549  "</div>" .
550  "</div>";
551 
552  $html = $this->normalizeHTML($r->render($button));
553  $this->assertHTMLEquals($expected, $html);
554  }
555 
556 
557  // TODO: We are missing a test for the rendering of a button with an signal
558  // here. Does it still render the action js?
559 
563  public function testFactoryAcceptsSignalAsAction(string $factory_method): void
564  {
565  $f = $this->getButtonFactory();
566  $signal = $this->createMock(C\Signal::class);
567 
568  $button = $f->$factory_method("label", $signal);
569 
570  $this->assertEquals([$signal], $button->getAction());
571  }
572 
573  public function getButtonTypeProvider(): array
574  {
575  return [
576  ['standard'],
577  ['primary'],
578  ['shy'],
579  ['tag']
580  ];
581  }
582 }
testRenderButtonWithOnLoadCode(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:241
getDefaultRenderer(JavaScriptBinding $js_binding=null, array $with_stub_renderings=[])
Definition: Base.php:377
testButtonWithAriaLabel(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:368
testButtonRendersTooltip(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:530
This is just a class that marks a string as a help topic.
Definition: Topic.php:26
testFactoryAcceptsSignalAsAction(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:563
testButtonWithEngaged(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:394
testRenderBtnTagRelevance()
Definition: ButtonTest.php:299
testRenderCloseButton()
Definition: ButtonTest.php:210
testButtonLabelOrGlyphOnly(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:74
testButtonWithLabel(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:105
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testAppendOnClickAppendsToAction(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:469
Test on button implementation.
Definition: ButtonTest.php:32
testRenderButtonWithAriaLabel(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:411
getButtonTypeProvider()
Definition: ButtonTest.php:573
testButtonWithLoadingAnimation()
test loading animation
Definition: ButtonTest.php:157
assertHTMLEquals(string $expected_html_as_string, string $html_as_string)
Definition: Base.php:458
testImplementsFactoryInterface()
Definition: ButtonTest.php:48
testRenderButtonDisabled(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:193
testRenderBtnTagClasses()
Definition: ButtonTest.php:349
testButtonActivatedOnDefault(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:130
testRenderCloseButtonWithOnLoadCode()
Definition: ButtonTest.php:265
getButtonFactory()
Definition: ButtonTest.php:36
testBtnTagRelevance()
Definition: ButtonTest.php:287
Provides common functionality for UI tests.
Definition: Base.php:310
testWithOnClickRemovesAction(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:454
static array $canonical_css_classes
Definition: ButtonTest.php:41
testRenderButtonWithOnClickAnimation()
test rendering with on click animation
Definition: ButtonTest.php:508
testRenderButtonLabel(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:174
testRenderButtonWithSignal(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:484
testRenderMinimizeButton()
Definition: ButtonTest.php:224
testButtonStringActionOnly(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:84
testRenderButtonWithAriaPressed(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:430
const NOT_APPLICABLE
Definition: ButtonTest.php:34
testButtonWithEngageable(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:378
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
testRenderBtnTagColors()
Definition: ButtonTest.php:328
normalizeHTML(string $html)
Definition: Base.php:453
testButtonDeactivation(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:141
testButtonLabel(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:94
testButtonAction(string $factory_method)
getButtonTypeProvider
Definition: ButtonTest.php:119
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Purpose.php:21
$r