ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ButtonTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
5require_once(__DIR__ . "/../../../../libs/composer/vendor/autoload.php");
6require_once(__DIR__ . "/../../Base.php");
7
8use \ILIAS\UI\Component as C;
9use \ILIAS\UI\Implementation\Component\Signal;
10
15{
16 public function getButtonFactory()
17 {
18 return new \ILIAS\UI\Implementation\Component\Button\Factory();
19 }
20
21 public static $canonical_css_classes = array( "standard" => "btn btn-default"
22 , "primary" => "btn btn-default btn-primary"
23 , "shy" => "btn btn-link"
24 , "tag" => "btn btn-tag btn-tag-relevance-veryhigh"
25 );
26
27 public static $canonical_css_inactivation_classes = array( "standard" => "ilSubmitInactive disabled"
28 , "primary" => "ilSubmitInactive disabled"
29 , "shy" => "ilSubmitInactive disabled"
30 , "tag" => "btn-tag-inactive"
31 );
32
33
35 {
36 $f = $this->getButtonFactory();
37
38 $this->assertInstanceOf("ILIAS\\UI\\Component\\Button\\Factory", $f);
39 $this->assertInstanceOf(
40 "ILIAS\\UI\\Component\\Button\\Standard",
41 $f->standard("label", "http://www.ilias.de")
42 );
43 $this->assertInstanceOf(
44 "ILIAS\\UI\\Component\\Button\\Primary",
45 $f->primary("label", "http://www.ilias.de")
46 );
47 $this->assertInstanceOf(
48 "ILIAS\\UI\\Component\\Button\\Close",
49 $f->close()
50 );
51 $this->assertInstanceOf(
52 "ILIAS\\UI\\Component\\Button\\Shy",
53 $f->shy("label", "http://www.ilias.de")
54 );
55 }
56
60 public function test_button_label_or_glyph_only($factory_method)
61 {
62 $f = $this->getButtonFactory();
63 try {
64 $f->$factory_method($this, "http://www.ilias.de");
65 $this->assertFalse("This should not happen");
66 } catch (\InvalidArgumentException $e) {
67 }
68 }
69
73 public function test_button_string_action_only($factory_method)
74 {
75 $f = $this->getButtonFactory();
76 try {
77 $f->$factory_method("label", $this);
78 $this->assertFalse("This should not happen");
79 } catch (\InvalidArgumentException $e) {
80 }
81 }
82
86 public function test_button_label($factory_method)
87 {
88 $f = $this->getButtonFactory();
89 $b = $f->$factory_method("label", "http://www.ilias.de");
90
91 $this->assertEquals("label", $b->getLabel());
92 }
93
97 public function test_button_with_label($factory_method)
98 {
99 $f = $this->getButtonFactory();
100 $b = $f->$factory_method("label", "http://www.ilias.de");
101
102 $b2 = $b->withLabel("label2");
103
104 $this->assertEquals("label", $b->getLabel());
105 $this->assertEquals("label2", $b2->getLabel());
106 }
107
111 public function test_button_action($factory_method)
112 {
113 $f = $this->getButtonFactory();
114 $b = $f->$factory_method("label", "http://www.ilias.de");
115
116 $this->assertEquals("http://www.ilias.de", $b->getAction());
117 }
118
122 public function test_button_activated_on_default($factory_method)
123 {
124 $f = $this->getButtonFactory();
125 $b = $f->$factory_method("label", "http://www.ilias.de");
126
127 $this->assertTrue($b->isActive());
128 }
129
133 public function test_button_deactivation($factory_method)
134 {
135 $f = $this->getButtonFactory();
136 $b = $f->$factory_method("label", "http://www.ilias.de")
137 ->withUnavailableAction();
138
139 $this->assertFalse($b->isActive());
140 $this->assertEquals("http://www.ilias.de", $b->getAction());
141 }
142
147 {
148 $f = $this->getButtonFactory();
149 foreach (["standard", "primary"] as $method) {
150 $b = $f->$method("label", "http://www.ilias.de");
151
152 $this->assertFalse($b->hasLoadingAnimationOnClick());
153
154 $b = $b->withLoadingAnimationOnClick(true);
155
156 $this->assertTrue($b->hasLoadingAnimationOnClick());
157 }
158 }
159
163 public function test_render_button_label($factory_method)
164 {
165 $ln = "http://www.ilias.de";
166 $f = $this->getButtonFactory();
167 $b = $f->$factory_method("label", $ln);
168 $r = $this->getDefaultRenderer();
169
170 $html = $this->normalizeHTML($r->render($b));
171
172 $css_classes = self::$canonical_css_classes[$factory_method];
173 $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"id_1\">" .
174 "label" .
175 "</button>";
176 $this->assertHTMLEquals($expected, $html);
177 }
178
182 public function test_render_button_disabled($factory_method)
183 {
184 $ln = "http://www.ilias.de";
185 $f = $this->getButtonFactory();
186 $b = $f->$factory_method("label", $ln)
187 ->withUnavailableAction();
188 $r = $this->getDefaultRenderer();
189
190 $html = $this->normalizeHTML($r->render($b));
191
192 $css_classes = self::$canonical_css_classes[$factory_method];
193 $css_class_inactive = self::$canonical_css_inactivation_classes[$factory_method];
194 $expected = "<button class=\"$css_classes $css_class_inactive\" data-action=\"$ln\">" .
195 "label" .
196 "</button>";
197 $this->assertHTMLEquals($expected, $html);
198 }
199
200 public function test_render_close_button()
201 {
202 $f = $this->getButtonFactory();
203 $r = $this->getDefaultRenderer();
204 $b = $f->close();
205
206 $html = $this->normalizeHTML($r->render($b));
207
208 $expected = "<button type=\"button\" class=\"close\" data-dismiss=\"modal\">" .
209 " <span aria-hidden=\"true\">&times;</span>" .
210 " <span class=\"sr-only\">Close</span>" .
211 "</button>";
212 $this->assertEquals($expected, $html);
213 }
214
218 public function test_render_button_with_on_load_code($factory_method)
219 {
220 $ln = "http://www.ilias.de";
221 $f = $this->getButtonFactory();
222 $r = $this->getDefaultRenderer();
223 $ids = array();
224 $b = $f->$factory_method("label", $ln)
225 ->withOnLoadCode(function ($id) use (&$ids) {
226 $ids[] = $id;
227 return "";
228 });
229
230 $html = $this->normalizeHTML($r->render($b));
231
232 $this->assertCount(1, $ids);
233
234 $id = $ids[0];
235 $css_classes = self::$canonical_css_classes[$factory_method];
236 $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"$id\">" .
237 "label" .
238 "</button>";
239 $this->assertHTMLEquals($expected, $html);
240 }
241
243 {
244 $f = $this->getButtonFactory();
245 $r = $this->getDefaultRenderer();
246 $ids = array();
247 $b = $f->close()
248 ->withOnLoadCode(function ($id) use (&$ids) {
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 $expected = "<button type=\"button\" class=\"close\" data-dismiss=\"modal\" id=\"$id\">" .
259 " <span aria-hidden=\"true\">&times;</span>" .
260 " <span class=\"sr-only\">Close</span>" .
261 "</button>";
262 $this->assertEquals($expected, $html);
263 }
264
265 public function test_btn_tag_relevance()
266 {
267 $f = $this->getButtonFactory();
268 $b = $f->tag('tag', '#');
269 try {
270 $b->withRelevance(0);
271 $this->assertFalse("This should not happen");
272 } catch (\InvalidArgumentException $e) {
273 $this->assertTrue(true);
274 }
275 try {
276 $b->withRelevance('notsoimportant');
277 $this->assertFalse("This should not happen");
278 } catch (\InvalidArgumentException $e) {
279 $this->assertTrue(true);
280 }
281 }
282
284 {
285 $expectations = array(
286 '<button class="btn btn-tag btn-tag-relevance-verylow" data-action="#" id="id_1">tag</button>',
287 '<button class="btn btn-tag btn-tag-relevance-low" data-action="#" id="id_2">tag</button>',
288 '<button class="btn btn-tag btn-tag-relevance-middle" data-action="#" id="id_3">tag</button>',
289 '<button class="btn btn-tag btn-tag-relevance-high" data-action="#" id="id_4">tag</button>',
290 '<button class="btn btn-tag btn-tag-relevance-veryhigh" data-action="#" id="id_5">tag</button>'
291 );
292
293 $f = $this->getButtonFactory();
294 $r = $this->getDefaultRenderer();
295 $t = $f->tag('tag', '#');
296 $possible_relevances = array(
297 $t::REL_VERYLOW,
298 $t::REL_LOW,
299 $t::REL_MID,
300 $t::REL_HIGH,
301 $t::REL_VERYHIGH
302 );
303 foreach ($possible_relevances as $w) {
304 $html = $this->normalizeHTML(
305 $r->render($t->withRelevance($w))
306 );
307 $expected = $expectations[array_search($w, $possible_relevances)];
308 $this->assertEquals($expected, $html);
309 }
310 }
311
313 {
314 $f = $this->getButtonFactory();
315 $r = $this->getDefaultRenderer();
316 $df = new \ILIAS\Data\Factory;
317
318 $bgcol = $df->color('#00ff00');
319
320 $b = $f->tag('tag', '#')
321 ->withBackgroundColor($bgcol);
322 $html = $this->normalizeHTML($r->render($b));
323 $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #000000;" data-action="#" id="id_1">tag</button>';
324 $this->assertEquals($expected, $html);
325
326 $fcol = $df->color('#ddd');
327 $b = $b->withForegroundColor($fcol);
328 $html = $this->normalizeHTML($r->render($b));
329 $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #dddddd;" data-action="#" id="id_2">tag</button>';
330 $this->assertEquals($expected, $html);
331 }
332
334 {
335 $f = $this->getButtonFactory();
336 $r = $this->getDefaultRenderer();
337 $df = new \ILIAS\Data\Factory;
338
339 $classes = array('cl1', 'cl2');
340 $b = $f->tag('tag', '#')
341 ->withClasses($classes);
342 $this->assertEquals($classes, $b->getClasses());
343
344 $html = $this->normalizeHTML($r->render($b));
345 $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh cl1 cl2" data-action="#" id="id_1">tag</button>';
346 $this->assertEquals($expected, $html);
347 }
351 public function test_button_with_aria_label($factory_method)
352 {
353 $f = $this->getButtonFactory();
354 $b = $f->$factory_method("label", "http://www.ilias.de")->withAriaLabel("ariatext");
355 $this->assertEquals("ariatext", $b->getAriaLabel());
356 }
357
361 public function test_button_with_aria_checked($factory_method)
362 {
363 $f = $this->getButtonFactory();
364 $b = $f->$factory_method("label", "http://www.ilias.de");
365 $this->assertEquals(false, $b->isAriaChecked());
366 $b2 = $f->$factory_method("label", "http://www.ilias.de")->withAriaChecked();
367 $this->assertEquals(true, $b2->isAriaChecked());
368 }
369
373 public function test_render_button_with_aria_label($factory_method)
374 {
375 //only standard buttons have aria labels in the template. Should the others accept aria stuff?
376 //if yes, remove this conditional
377 if ($factory_method == "standard") {
378 $ln = "http://www.ilias.de";
379 $f = $this->getButtonFactory();
380 $r = $this->getDefaultRenderer();
381 $b = $f->$factory_method("label", $ln)->withAriaLabel("aria label text");
382 $aria_label = $b->getAriaLabel();
383
384 $html = $this->normalizeHTML($r->render($b));
385 $css_classes = self::$canonical_css_classes[$factory_method];
386 $expected = "<button class=\"$css_classes\" aria-label=\"$aria_label\" data-action=\"$ln\" id=\"id_1\">" .
387 "label" .
388 "</button>";
389 $this->assertHTMLEquals($expected, $html);
390 }
391 }
392
396 public function test_render_button_with_aria_checked($factory_method)
397 {
398 //only standard buttons have aria labels in the template. Should the others accept aria stuff?
399 //if yes, remove this conditional
400 if ($factory_method == "standard") {
401 $ln = "http://www.ilias.de";
402 $f = $this->getButtonFactory();
403 $r = $this->getDefaultRenderer();
404 $b = $f->$factory_method("label", $ln)->withAriaChecked();
405
406 $html = $this->normalizeHTML($r->render($b));
407 $css_classes = self::$canonical_css_classes[$factory_method];
408 $expected = "<button class=\"$css_classes\" aria-checked=\"true\" data-action=\"$ln\" id=\"id_1\">" .
409 "label" .
410 "</button>";
411 $this->assertHTMLEquals($expected, $html);
412 }
413 }
414
418 public function test_withOnClick_removes_action($factory_method)
419 {
420 $f = $this->getButtonFactory();
421 $signal = $this->createMock(C\Signal::class);
422 $button = $f->$factory_method("label", "http://www.example.com");
423 $this->assertEquals("http://www.example.com", $button->getAction());
424
425 $button = $button->withOnClick($signal);
426
427 $this->assertEquals([$signal], $button->getAction());
428 }
429
433 public function test_appendOnClick_appends_to_action($factory_method)
434 {
435 $f = $this->getButtonFactory();
436 $signal1 = $this->createMock(C\Signal::class);
437 $signal2 = $this->createMock(C\Signal::class);
438 $button = $f->$factory_method("label", "http://www.example.com");
439
440 $button = $button->withOnClick($signal1)->appendOnClick($signal2);
441
442 $this->assertEquals([$signal1, $signal2], $button->getAction());
443 }
444
448 public function test_render_button_with_signal($factory_method)
449 {
450 $ln = "http://www.ilias.de";
451 $f = $this->getButtonFactory();
452 $signal = $this->createMock(Signal::class);
453 $signal->method("__toString")
454 ->willReturn("MOCK_SIGNAL");
455
456 $b = $f->$factory_method("label", $ln)
457 ->withOnClick($signal);
458 $r = $this->getDefaultRenderer();
459
460 $html = $this->normalizeHTML($r->render($b));
461
462 $css_classes = self::$canonical_css_classes[$factory_method];
463 $expected = "<button class=\"$css_classes\" id=\"id_1\">" .
464 "label" .
465 "</button>";
466 $this->assertHTMLEquals($expected, $html);
467 }
468
473 {
474 foreach (["primary", "standard"] as $method) {
475 $ln = "http://www.ilias.de";
476 $f = $this->getButtonFactory();
477 $r = $this->getDefaultRenderer();
478 $b = $f->$method("label", $ln)
479 ->withLoadingAnimationOnClick(true);
480
481 $html = $this->normalizeHTML($r->render($b));
482
483 $css_classes = self::$canonical_css_classes[$method];
484 $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"id_1\">" .
485 "label" .
486 "</button>";
487 $this->assertHTMLEquals($expected, $html);
488 }
489 }
490
491
492 // TODO: We are missing a test for the rendering of a button with an signal
493 // here. Does it still render the action js?
494
498 public function test_factory_accepts_signal_as_action($factory_method)
499 {
500 $f = $this->getButtonFactory();
501 $signal = $this->createMock(C\Signal::class);
502
503 $button = $f->$factory_method("label", $signal);
504
505 $this->assertEquals([$signal], $button->getAction());
506 }
507
508 public function button_type_provider()
509 {
510 return array( array("standard")
511 , array("primary")
512 , array("shy")
513 , array("tag")
514 );
515 }
516}
Test on button implementation.
Definition: ButtonTest.php:15
test_withOnClick_removes_action($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:418
test_button_with_aria_checked($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:361
test_button_label_or_glyph_only($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:60
test_button_with_label($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:97
test_____render_close_button_with_on_load_code()
Definition: ButtonTest.php:242
test_render_button_label($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:163
button_type_provider()
Definition: ButtonTest.php:508
static $canonical_css_inactivation_classes
Definition: ButtonTest.php:27
test_render_button_disabled($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:182
getButtonFactory()
Definition: ButtonTest.php:16
test_render_button_with_signal($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:448
test_implements_factory_interface()
Definition: ButtonTest.php:34
test_button_deactivation($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:133
test_btn_tag_relevance()
Definition: ButtonTest.php:265
test_button_string_action_only($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:73
test_render_btn_tag_colors()
Definition: ButtonTest.php:312
test_render_button_with_on_load_code($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:218
test_render_close_button()
Definition: ButtonTest.php:200
test_factory_accepts_signal_as_action($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:498
static $canonical_css_classes
Definition: ButtonTest.php:21
test_button_activated_on_default($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:122
test_render_button_with_aria_label($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:373
test_render_button_with_aria_checked($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:396
test_button_with_aria_label($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:351
test_button_with_loading_animation()
test loading animation
Definition: ButtonTest.php:146
test_render_btn_tag_classes()
Definition: ButtonTest.php:333
test_render_btn_tag_relevance()
Definition: ButtonTest.php:283
test_render_button_with_on_click_animation()
test rendering with on click animation
Definition: ButtonTest.php:472
test_appendOnClick_appends_to_action($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:433
test_button_label($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:86
test_button_action($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:111
An exception for terminatinating execution or to throw for unit testing.
Provides common functionality for UI tests.
Definition: Base.php:192
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:270
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:228
normalizeHTML($html)
Definition: Base.php:261
$html
Definition: example_001.php:87
$w
$r
Definition: example_031.php:79
if(!array_key_exists('StateId', $_REQUEST)) $id