ILIAS  release_8 Revision v8.24
ButtonTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21require_once(__DIR__ . "/../../../../libs/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../Base.php");
23
27
32{
33 public const NOT_APPLICABLE = true;
34
35 public function getButtonFactory(): Factory
36 {
37 return new Factory();
38 }
39
40 public static array $canonical_css_classes = [
41 "standard" => "btn btn-default",
42 "primary" => "btn btn-default btn-primary",
43 "shy" => "btn btn-link",
44 "tag" => "btn btn-tag btn-tag-relevance-veryhigh"
45 ];
46
47 public function test_implements_factory_interface(): void
48 {
49 $f = $this->getButtonFactory();
50
51 $this->assertInstanceOf("ILIAS\\UI\\Component\\Button\\Factory", $f);
52 $this->assertInstanceOf(
53 "ILIAS\\UI\\Component\\Button\\Standard",
54 $f->standard("label", "http://www.ilias.de")
55 );
56 $this->assertInstanceOf(
57 "ILIAS\\UI\\Component\\Button\\Primary",
58 $f->primary("label", "http://www.ilias.de")
59 );
60 $this->assertInstanceOf(
61 "ILIAS\\UI\\Component\\Button\\Close",
62 $f->close()
63 );
64 $this->assertInstanceOf(
65 "ILIAS\\UI\\Component\\Button\\Shy",
66 $f->shy("label", "http://www.ilias.de")
67 );
68 }
69
73 public function test_button_label_or_glyph_only(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
83 public function test_button_string_action_only(string $factory_method): void
84 {
85 $this->expectException(InvalidArgumentException::class);
86 $f = $this->getButtonFactory();
87 $f->$factory_method("label", $this);
88 }
89
93 public function test_button_label(string $factory_method): void
94 {
95 $f = $this->getButtonFactory();
96 $b = $f->$factory_method("label", "http://www.ilias.de");
97
98 $this->assertEquals("label", $b->getLabel());
99 }
100
104 public function test_button_with_label(string $factory_method): void
105 {
106 $f = $this->getButtonFactory();
107 $b = $f->$factory_method("label", "http://www.ilias.de");
108
109 $b2 = $b->withLabel("label2");
110
111 $this->assertEquals("label", $b->getLabel());
112 $this->assertEquals("label2", $b2->getLabel());
113 }
114
118 public function test_button_action(string $factory_method): void
119 {
120 $f = $this->getButtonFactory();
121 $b = $f->$factory_method("label", "http://www.ilias.de");
122
123 $this->assertEquals("http://www.ilias.de", $b->getAction());
124 }
125
129 public function test_button_activated_on_default(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
140 public function test_button_deactivation(string $factory_method): void
141 {
142 $f = $this->getButtonFactory();
143 $b = $f->$factory_method("label", "http://www.ilias.de")
144 ->withUnavailableAction();
145
146 $this->assertFalse($b->isActive());
147 $this->assertEquals("http://www.ilias.de", $b->getAction());
148 }
149
154 {
155 $f = $this->getButtonFactory();
156 foreach (["standard", "primary"] as $method) {
157 $b = $f->$method("label", "http://www.ilias.de");
158
159 $this->assertFalse($b->hasLoadingAnimationOnClick());
160
161 $b = $b->withLoadingAnimationOnClick(true);
162
163 $this->assertTrue($b->hasLoadingAnimationOnClick());
164 }
165 }
166
170 public function test_render_button_label(string $factory_method): void
171 {
172 $ln = "http://www.ilias.de";
173 $f = $this->getButtonFactory();
174 $b = $f->$factory_method("label", $ln);
175 $r = $this->getDefaultRenderer();
176
177 $html = $this->normalizeHTML($r->render($b));
178
179 $css_classes = self::$canonical_css_classes[$factory_method];
180 $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"id_1\">" .
181 "label" .
182 "</button>";
183 $this->assertHTMLEquals($expected, $html);
184 }
185
189 public function test_render_button_disabled(string $factory_method): void
190 {
191 $ln = "http://www.ilias.de";
192 $f = $this->getButtonFactory();
193 $b = $f->$factory_method("label", $ln)
194 ->withUnavailableAction();
195 $r = $this->getDefaultRenderer();
196
197 $html = $this->normalizeHTML($r->render($b));
198
199 $css_classes = self::$canonical_css_classes[$factory_method];
200 $expected = "<button class=\"$css_classes\" data-action=\"$ln\" disabled=\"disabled\">" .
201 "label" .
202 "</button>";
203 $this->assertHTMLEquals($expected, $html);
204 }
205
206 public function test_render_close_button(): void
207 {
208 $f = $this->getButtonFactory();
209 $r = $this->getDefaultRenderer();
210 $b = $f->close();
211
212 $html = $this->normalizeHTML($r->render($b));
213
214 $expected = "<button type=\"button\" class=\"close\" aria-label=\"close\">" .
215 " <span aria-hidden=\"true\">&times;</span>" .
216 "</button>";
217 $this->assertEquals($expected, $html);
218 }
219
220 public function test_render_minimize_button(): void
221 {
222 $f = $this->getButtonFactory();
223 $r = $this->getDefaultRenderer();
224 $b = $f->minimize();
225
226 $html = $this->normalizeHTML($r->render($b));
227
228 $expected = "<button type=\"button\" class=\"minimize\" aria-label=\"minimize\">" .
229 " <span aria-hidden=\"true\">−</span>" .
230 "</button>";
231 $this->assertEquals($expected, $html);
232 }
233
237 public function test_render_button_with_on_load_code(string $factory_method): void
238 {
239 $ln = "http://www.ilias.de";
240 $f = $this->getButtonFactory();
241 $r = $this->getDefaultRenderer();
242 $ids = array();
243 $b = $f->$factory_method("label", $ln)
244 ->withOnLoadCode(function ($id) use (&$ids): string {
245 $ids[] = $id;
246 return "";
247 });
248
249 $html = $this->normalizeHTML($r->render($b));
250
251 $this->assertCount(1, $ids);
252
253 $id = $ids[0];
254 $css_classes = self::$canonical_css_classes[$factory_method];
255 $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"$id\">" .
256 "label" .
257 "</button>";
258 $this->assertHTMLEquals($expected, $html);
259 }
260
262 {
263 $f = $this->getButtonFactory();
264 $r = $this->getDefaultRenderer();
265 $ids = array();
266 $b = $f->close()
267 ->withOnLoadCode(function ($id) use (&$ids): string {
268 $ids[] = $id;
269 return "";
270 });
271
272 $html = $this->normalizeHTML($r->render($b));
273
274 $this->assertCount(1, $ids);
275
276 $id = $ids[0];
277 $expected = "<button type=\"button\" class=\"close\" aria-label=\"close\" id=\"$id\">" .
278 " <span aria-hidden=\"true\">&times;</span>" .
279 "</button>";
280 $this->assertEquals($expected, $html);
281 }
282
283 public function test_btn_tag_relevance(): void
284 {
285 $f = $this->getButtonFactory();
286 $b = $f->tag('tag', '#');
287
288 $this->expectException(TypeError::class);
289 $b->withRelevance(0);
290
291 $this->expectException(TypeError::class);
292 $b->withRelevance('notsoimportant');
293 }
294
295 public function test_render_btn_tag_relevance(): void
296 {
297 $expectations = array(
298 '<button class="btn btn-tag btn-tag-relevance-verylow" data-action="#" id="id_1">tag</button>',
299 '<button class="btn btn-tag btn-tag-relevance-low" data-action="#" id="id_2">tag</button>',
300 '<button class="btn btn-tag btn-tag-relevance-middle" data-action="#" id="id_3">tag</button>',
301 '<button class="btn btn-tag btn-tag-relevance-high" data-action="#" id="id_4">tag</button>',
302 '<button class="btn btn-tag btn-tag-relevance-veryhigh" data-action="#" id="id_5">tag</button>'
303 );
304
305 $f = $this->getButtonFactory();
306 $r = $this->getDefaultRenderer();
307 $t = $f->tag('tag', '#');
308 $possible_relevances = array(
309 $t::REL_VERYLOW,
310 $t::REL_LOW,
311 $t::REL_MID,
312 $t::REL_HIGH,
313 $t::REL_VERYHIGH
314 );
315 foreach ($possible_relevances as $w) {
316 $html = $this->normalizeHTML(
317 $r->render($t->withRelevance($w))
318 );
319 $expected = $expectations[array_search($w, $possible_relevances)];
320 $this->assertEquals($expected, $html);
321 }
322 }
323
324 public function test_render_btn_tag_colors(): void
325 {
326 $f = $this->getButtonFactory();
327 $r = $this->getDefaultRenderer();
328 $df = new \ILIAS\Data\Factory();
329
330 $bgcol = $df->color('#00ff00');
331
332 $b = $f->tag('tag', '#')
333 ->withBackgroundColor($bgcol);
334 $html = $this->normalizeHTML($r->render($b));
335 $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #000000;" data-action="#" id="id_1">tag</button>';
336 $this->assertEquals($expected, $html);
337
338 $fcol = $df->color('#ddd');
339 $b = $b->withForegroundColor($fcol);
340 $html = $this->normalizeHTML($r->render($b));
341 $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #dddddd;" data-action="#" id="id_2">tag</button>';
342 $this->assertEquals($expected, $html);
343 }
344
345 public function test_render_btn_tag_classes(): void
346 {
347 $f = $this->getButtonFactory();
348 $r = $this->getDefaultRenderer();
349 $df = new \ILIAS\Data\Factory();
350
351 $classes = array('cl1', 'cl2');
352 $b = $f->tag('tag', '#')
353 ->withClasses($classes);
354 $this->assertEquals($classes, $b->getClasses());
355
356 $html = $this->normalizeHTML($r->render($b));
357 $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh cl1 cl2" data-action="#" id="id_1">tag</button>';
358 $this->assertEquals($expected, $html);
359 }
360
364 public function test_button_with_aria_label(string $factory_method): void
365 {
366 $f = $this->getButtonFactory();
367 $b = $f->$factory_method("label", "http://www.ilias.de")->withAriaLabel("ariatext");
368 $this->assertEquals("ariatext", $b->getAriaLabel());
369 }
370
374 public function test_button_with_engageable(string $factory_method): void
375 {
376 $f = $this->getButtonFactory();
377 $b = $f->$factory_method("label", "http://www.ilias.de");
378 if ($b instanceof C\Button\Engageable) {
379 $this->assertEquals(false, $b->isEngageable());
380 $b2 = $f->$factory_method("label", "http://www.ilias.de")->withEngagedState(false);
381 $this->assertEquals(true, $b2->isEngageable());
382 } else {
383 $this->assertTrue(self::NOT_APPLICABLE);
384 }
385 }
386
390 public function test_button_with_engaged(string $factory_method): void
391 {
392 $f = $this->getButtonFactory();
393 $b = $f->$factory_method("label", "http://www.ilias.de");
394 if ($b instanceof C\Button\Engageable) {
395 $b = $b->withEngagedState(false);
396 $this->assertEquals(false, $b->isEngaged());
397 $b2 = $f->$factory_method("label", "http://www.ilias.de")->withEngagedState(true);
398 $this->assertEquals(true, $b2->isEngaged());
399 } else {
400 $this->assertTrue(self::NOT_APPLICABLE);
401 }
402 }
403
407 public function test_render_button_with_aria_label(string $factory_method): void
408 {
409 $ln = "http://www.ilias.de";
410 $f = $this->getButtonFactory();
411 $r = $this->getDefaultRenderer();
412 $b = $f->$factory_method("label", $ln)->withAriaLabel("aria label text");
413 $aria_label = $b->getAriaLabel();
414
415 $html = $this->normalizeHTML($r->render($b));
416 $css_classes = self::$canonical_css_classes[$factory_method];
417 $expected = "<button class=\"$css_classes\" aria-label=\"$aria_label\" data-action=\"$ln\" id=\"id_1\">" .
418 "label" .
419 "</button>";
420 $this->assertHTMLEquals($expected, $html);
421 }
422
426 public function test_render_button_with_aria_pressed(string $factory_method): void
427 {
428 $ln = "http://www.ilias.de";
429 $f = $this->getButtonFactory();
430 $r = $this->getDefaultRenderer();
431 $b = $f->$factory_method("label", $ln);
432 if ($b instanceof C\Button\Engageable) {
433 $b = $b->withEngagedState(true);
434
435 $html = $this->normalizeHTML($r->render($b));
436 $css_classes = self::$canonical_css_classes[$factory_method];
437 $css_classes .= ' engaged';
438 $expected = "<button class=\"$css_classes\" aria-pressed=\"true\" data-action=\"$ln\" id=\"id_1\">" .
439 "label" .
440 "</button>";
441 $this->assertHTMLEquals($expected, $html);
442 } else {
443 $this->assertTrue(self::NOT_APPLICABLE);
444 }
445 }
446
450 public function test_withOnClick_removes_action(string $factory_method): void
451 {
452 $f = $this->getButtonFactory();
453 $signal = $this->createMock(C\Signal::class);
454 $button = $f->$factory_method("label", "http://www.example.com");
455 $this->assertEquals("http://www.example.com", $button->getAction());
456
457 $button = $button->withOnClick($signal);
458
459 $this->assertEquals([$signal], $button->getAction());
460 }
461
465 public function test_appendOnClick_appends_to_action(string $factory_method): void
466 {
467 $f = $this->getButtonFactory();
468 $signal1 = $this->createMock(C\Signal::class);
469 $signal2 = $this->createMock(C\Signal::class);
470 $button = $f->$factory_method("label", "http://www.example.com");
471
472 $button = $button->withOnClick($signal1)->appendOnClick($signal2);
473
474 $this->assertEquals([$signal1, $signal2], $button->getAction());
475 }
476
480 public function test_render_button_with_signal(string $factory_method): void
481 {
482 $ln = "http://www.ilias.de";
483 $f = $this->getButtonFactory();
484 $signal = $this->createMock(Signal::class);
485 $signal->method("__toString")
486 ->willReturn("MOCK_SIGNAL");
487
488 $b = $f->$factory_method("label", $ln)
489 ->withOnClick($signal);
490 $r = $this->getDefaultRenderer();
491
492 $html = $this->normalizeHTML($r->render($b));
493
494 $css_classes = self::$canonical_css_classes[$factory_method];
495 $expected = "<button class=\"$css_classes\" id=\"id_1\">" .
496 "label" .
497 "</button>";
498 $this->assertHTMLEquals($expected, $html);
499 }
500
505 {
506 foreach (["primary", "standard"] as $method) {
507 $ln = "http://www.ilias.de";
508 $f = $this->getButtonFactory();
509 $r = $this->getDefaultRenderer();
510 $b = $f->$method("label", $ln)
511 ->withLoadingAnimationOnClick(true);
512
513 $html = $this->normalizeHTML($r->render($b));
514
515 $css_classes = self::$canonical_css_classes[$method];
516 $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"id_1\">" .
517 "label" .
518 "</button>";
519 $this->assertHTMLEquals($expected, $html);
520 }
521 }
522
523
524 // TODO: We are missing a test for the rendering of a button with an signal
525 // here. Does it still render the action js?
526
530 public function test_factory_accepts_signal_as_action(string $factory_method): void
531 {
532 $f = $this->getButtonFactory();
533 $signal = $this->createMock(C\Signal::class);
534
535 $button = $f->$factory_method("label", $signal);
536
537 $this->assertEquals([$signal], $button->getAction());
538 }
539
540 public function button_type_provider(): array
541 {
542 return [
543 ['standard'],
544 ['primary'],
545 ['shy'],
546 ['tag']
547 ];
548 }
549}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Test on button implementation.
Definition: ButtonTest.php:32
test_render_button_with_aria_pressed(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:426
test_____render_close_button_with_on_load_code()
Definition: ButtonTest.php:261
button_type_provider()
Definition: ButtonTest.php:540
test_withOnClick_removes_action(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:450
test_render_button_disabled(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:189
getButtonFactory()
Definition: ButtonTest.php:35
test_implements_factory_interface()
Definition: ButtonTest.php:47
test_button_with_aria_label(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:364
test_btn_tag_relevance()
Definition: ButtonTest.php:283
const NOT_APPLICABLE
Definition: ButtonTest.php:33
test_render_btn_tag_colors()
Definition: ButtonTest.php:324
test_button_label_or_glyph_only(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:73
test_render_button_with_on_load_code(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:237
test_button_label(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:93
test_render_close_button()
Definition: ButtonTest.php:206
test_render_button_with_aria_label(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:407
test_render_minimize_button()
Definition: ButtonTest.php:220
test_render_button_label(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:170
test_button_string_action_only(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:83
test_button_action(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:118
test_button_with_loading_animation()
test loading animation
Definition: ButtonTest.php:153
test_render_btn_tag_classes()
Definition: ButtonTest.php:345
test_render_btn_tag_relevance()
Definition: ButtonTest.php:295
test_button_with_label(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:104
test_factory_accepts_signal_as_action(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:530
static array $canonical_css_classes
Definition: ButtonTest.php:40
test_button_with_engageable(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:374
test_button_activated_on_default(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:129
test_button_with_engaged(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:390
test_render_button_with_signal(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:480
test_appendOnClick_appends_to_action(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:465
test_render_button_with_on_click_animation()
test rendering with on click animation
Definition: ButtonTest.php:504
test_button_deactivation(string $factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:140
Builds a Color from either hex- or rgb values.
Definition: Factory.php:17
Provides common functionality for UI tests.
Definition: Base.php:299
normalizeHTML(string $html)
Definition: Base.php:422
assertHTMLEquals(string $expected_html_as_string, string $html_as_string)
Definition: Base.php:427
getDefaultRenderer(JavaScriptBinding $js_binding=null, array $with_stub_renderings=[])
Definition: Base.php:355
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Bulky.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...