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