ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
5 require_once(__DIR__ . "/../../../../libs/composer/vendor/autoload.php");
6 require_once(__DIR__ . "/../../Base.php");
7 
8 use \ILIAS\UI\Component as C;
9 use \ILIAS\UI\Implementation\Component\Signal;
10 
15 {
16  const NOT_APPLICABLE = true;
17 
18  public function getButtonFactory()
19  {
20  return new \ILIAS\UI\Implementation\Component\Button\Factory();
21  }
22 
23  public static $canonical_css_classes = array( "standard" => "btn btn-default"
24  , "primary" => "btn btn-default btn-primary"
25  , "shy" => "btn btn-link"
26  , "tag" => "btn btn-tag btn-tag-relevance-veryhigh"
27  );
28 
30  {
31  $f = $this->getButtonFactory();
32 
33  $this->assertInstanceOf("ILIAS\\UI\\Component\\Button\\Factory", $f);
34  $this->assertInstanceOf(
35  "ILIAS\\UI\\Component\\Button\\Standard",
36  $f->standard("label", "http://www.ilias.de")
37  );
38  $this->assertInstanceOf(
39  "ILIAS\\UI\\Component\\Button\\Primary",
40  $f->primary("label", "http://www.ilias.de")
41  );
42  $this->assertInstanceOf(
43  "ILIAS\\UI\\Component\\Button\\Close",
44  $f->close()
45  );
46  $this->assertInstanceOf(
47  "ILIAS\\UI\\Component\\Button\\Shy",
48  $f->shy("label", "http://www.ilias.de")
49  );
50  }
51 
55  public function test_button_label_or_glyph_only($factory_method)
56  {
57  $this->expectException(\InvalidArgumentException::class);
58  $f = $this->getButtonFactory();
59  $f->$factory_method($this, "http://www.ilias.de");
60  }
61 
65  public function test_button_string_action_only($factory_method)
66  {
67  $this->expectException(\InvalidArgumentException::class);
68  $f = $this->getButtonFactory();
69  $f->$factory_method("label", $this);
70  }
71 
75  public function test_button_label($factory_method)
76  {
77  $f = $this->getButtonFactory();
78  $b = $f->$factory_method("label", "http://www.ilias.de");
79 
80  $this->assertEquals("label", $b->getLabel());
81  }
82 
86  public function test_button_with_label($factory_method)
87  {
88  $f = $this->getButtonFactory();
89  $b = $f->$factory_method("label", "http://www.ilias.de");
90 
91  $b2 = $b->withLabel("label2");
92 
93  $this->assertEquals("label", $b->getLabel());
94  $this->assertEquals("label2", $b2->getLabel());
95  }
96 
100  public function test_button_action($factory_method)
101  {
102  $f = $this->getButtonFactory();
103  $b = $f->$factory_method("label", "http://www.ilias.de");
104 
105  $this->assertEquals("http://www.ilias.de", $b->getAction());
106  }
107 
111  public function test_button_activated_on_default($factory_method)
112  {
113  $f = $this->getButtonFactory();
114  $b = $f->$factory_method("label", "http://www.ilias.de");
115 
116  $this->assertTrue($b->isActive());
117  }
118 
122  public function test_button_deactivation($factory_method)
123  {
124  $f = $this->getButtonFactory();
125  $b = $f->$factory_method("label", "http://www.ilias.de")
126  ->withUnavailableAction();
127 
128  $this->assertFalse($b->isActive());
129  $this->assertEquals("http://www.ilias.de", $b->getAction());
130  }
131 
136  {
137  $f = $this->getButtonFactory();
138  foreach (["standard", "primary"] as $method) {
139  $b = $f->$method("label", "http://www.ilias.de");
140 
141  $this->assertFalse($b->hasLoadingAnimationOnClick());
142 
143  $b = $b->withLoadingAnimationOnClick(true);
144 
145  $this->assertTrue($b->hasLoadingAnimationOnClick());
146  }
147  }
148 
152  public function test_render_button_label($factory_method)
153  {
154  $ln = "http://www.ilias.de";
155  $f = $this->getButtonFactory();
156  $b = $f->$factory_method("label", $ln);
157  $r = $this->getDefaultRenderer();
158 
159  $html = $this->normalizeHTML($r->render($b));
160 
161  $css_classes = self::$canonical_css_classes[$factory_method];
162  $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"id_1\">" .
163  "label" .
164  "</button>";
165  $this->assertHTMLEquals($expected, $html);
166  }
167 
171  public function test_render_button_disabled($factory_method)
172  {
173  $ln = "http://www.ilias.de";
174  $f = $this->getButtonFactory();
175  $b = $f->$factory_method("label", $ln)
176  ->withUnavailableAction();
177  $r = $this->getDefaultRenderer();
178 
179  $html = $this->normalizeHTML($r->render($b));
180 
181  $css_classes = self::$canonical_css_classes[$factory_method];
182  $expected = "<button class=\"$css_classes\" data-action=\"$ln\" disabled=\"disabled\">" .
183  "label" .
184  "</button>";
185  $this->assertHTMLEquals($expected, $html);
186  }
187 
188  public function test_render_close_button()
189  {
190  $f = $this->getButtonFactory();
191  $r = $this->getDefaultRenderer();
192  $b = $f->close();
193 
194  $html = $this->normalizeHTML($r->render($b));
195 
196  $expected = "<button type=\"button\" class=\"close\" data-dismiss=\"modal\">" .
197  " <span aria-hidden=\"true\">&times;</span>" .
198  " <span class=\"sr-only\">Close</span>" .
199  "</button>";
200  $this->assertEquals($expected, $html);
201  }
202 
206  public function test_render_button_with_on_load_code($factory_method)
207  {
208  $ln = "http://www.ilias.de";
209  $f = $this->getButtonFactory();
210  $r = $this->getDefaultRenderer();
211  $ids = array();
212  $b = $f->$factory_method("label", $ln)
213  ->withOnLoadCode(function ($id) use (&$ids) {
214  $ids[] = $id;
215  return "";
216  });
217 
218  $html = $this->normalizeHTML($r->render($b));
219 
220  $this->assertCount(1, $ids);
221 
222  $id = $ids[0];
223  $css_classes = self::$canonical_css_classes[$factory_method];
224  $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"$id\">" .
225  "label" .
226  "</button>";
227  $this->assertHTMLEquals($expected, $html);
228  }
229 
231  {
232  $f = $this->getButtonFactory();
233  $r = $this->getDefaultRenderer();
234  $ids = array();
235  $b = $f->close()
236  ->withOnLoadCode(function ($id) use (&$ids) {
237  $ids[] = $id;
238  return "";
239  });
240 
241  $html = $this->normalizeHTML($r->render($b));
242 
243  $this->assertCount(1, $ids);
244 
245  $id = $ids[0];
246  $expected = "<button type=\"button\" class=\"close\" data-dismiss=\"modal\" id=\"$id\">" .
247  " <span aria-hidden=\"true\">&times;</span>" .
248  " <span class=\"sr-only\">Close</span>" .
249  "</button>";
250  $this->assertEquals($expected, $html);
251  }
252 
253  public function test_btn_tag_relevance()
254  {
255  $f = $this->getButtonFactory();
256  $b = $f->tag('tag', '#');
257  try {
258  $b->withRelevance(0);
259  $this->assertFalse("This should not happen");
260  } catch (\InvalidArgumentException $e) {
261  $this->assertTrue(true);
262  }
263  try {
264  $b->withRelevance('notsoimportant');
265  $this->assertFalse("This should not happen");
266  } catch (\InvalidArgumentException $e) {
267  $this->assertTrue(true);
268  }
269  }
270 
272  {
273  $expectations = array(
274  '<button class="btn btn-tag btn-tag-relevance-verylow" data-action="#" id="id_1">tag</button>',
275  '<button class="btn btn-tag btn-tag-relevance-low" data-action="#" id="id_2">tag</button>',
276  '<button class="btn btn-tag btn-tag-relevance-middle" data-action="#" id="id_3">tag</button>',
277  '<button class="btn btn-tag btn-tag-relevance-high" data-action="#" id="id_4">tag</button>',
278  '<button class="btn btn-tag btn-tag-relevance-veryhigh" data-action="#" id="id_5">tag</button>'
279  );
280 
281  $f = $this->getButtonFactory();
282  $r = $this->getDefaultRenderer();
283  $t = $f->tag('tag', '#');
284  $possible_relevances = array(
285  $t::REL_VERYLOW,
286  $t::REL_LOW,
287  $t::REL_MID,
288  $t::REL_HIGH,
289  $t::REL_VERYHIGH
290  );
291  foreach ($possible_relevances as $w) {
292  $html = $this->normalizeHTML(
293  $r->render($t->withRelevance($w))
294  );
295  $expected = $expectations[array_search($w, $possible_relevances)];
296  $this->assertEquals($expected, $html);
297  }
298  }
299 
300  public function test_render_btn_tag_colors()
301  {
302  $f = $this->getButtonFactory();
303  $r = $this->getDefaultRenderer();
304  $df = new \ILIAS\Data\Factory;
305 
306  $bgcol = $df->color('#00ff00');
307 
308  $b = $f->tag('tag', '#')
309  ->withBackgroundColor($bgcol);
310  $html = $this->normalizeHTML($r->render($b));
311  $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #000000;" data-action="#" id="id_1">tag</button>';
312  $this->assertEquals($expected, $html);
313 
314  $fcol = $df->color('#ddd');
315  $b = $b->withForegroundColor($fcol);
316  $html = $this->normalizeHTML($r->render($b));
317  $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #dddddd;" data-action="#" id="id_2">tag</button>';
318  $this->assertEquals($expected, $html);
319  }
320 
321  public function test_render_btn_tag_classes()
322  {
323  $f = $this->getButtonFactory();
324  $r = $this->getDefaultRenderer();
325  $df = new \ILIAS\Data\Factory;
326 
327  $classes = array('cl1', 'cl2');
328  $b = $f->tag('tag', '#')
329  ->withClasses($classes);
330  $this->assertEquals($classes, $b->getClasses());
331 
332  $html = $this->normalizeHTML($r->render($b));
333  $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh cl1 cl2" data-action="#" id="id_1">tag</button>';
334  $this->assertEquals($expected, $html);
335  }
339  public function test_button_with_aria_label($factory_method)
340  {
341  $f = $this->getButtonFactory();
342  $b = $f->$factory_method("label", "http://www.ilias.de")->withAriaLabel("ariatext");
343  $this->assertEquals("ariatext", $b->getAriaLabel());
344  }
345 
349  public function test_button_with_engageable($factory_method)
350  {
351  $f = $this->getButtonFactory();
352  $b = $f->$factory_method("label", "http://www.ilias.de");
353  if ($b instanceof C\Button\Engageable) {
354  $this->assertEquals(false, $b->isEngageable());
355  $b2 = $f->$factory_method("label", "http://www.ilias.de")->withEngagedState(false);
356  $this->assertEquals(true, $b2->isEngageable());
357  } else {
358  $this->assertTrue(self::NOT_APPLICABLE);
359  }
360  }
361 
365  public function test_button_with_engaged($factory_method)
366  {
367  $f = $this->getButtonFactory();
368  $b = $f->$factory_method("label", "http://www.ilias.de");
369  if ($b instanceof C\Button\Engageable) {
370  $b = $b->withEngagedState(false);
371  $this->assertEquals(false, $b->isEngaged());
372  $b2 = $f->$factory_method("label", "http://www.ilias.de")->withEngagedState(true);
373  $this->assertEquals(true, $b2->isEngaged());
374  } else {
375  $this->assertTrue(self::NOT_APPLICABLE);
376  }
377  }
378 
382  public function test_render_button_with_aria_label($factory_method)
383  {
384  $ln = "http://www.ilias.de";
385  $f = $this->getButtonFactory();
386  $r = $this->getDefaultRenderer();
387  $b = $f->$factory_method("label", $ln)->withAriaLabel("aria label text");
388  $aria_label = $b->getAriaLabel();
389 
390  $html = $this->normalizeHTML($r->render($b));
391  $css_classes = self::$canonical_css_classes[$factory_method];
392  $expected = "<button class=\"$css_classes\" aria-label=\"$aria_label\" data-action=\"$ln\" id=\"id_1\">" .
393  "label" .
394  "</button>";
395  $this->assertHTMLEquals($expected, $html);
396  }
397 
401  public function test_render_button_with_aria_pressed($factory_method)
402  {
403  $ln = "http://www.ilias.de";
404  $f = $this->getButtonFactory();
405  $r = $this->getDefaultRenderer();
406  $b = $f->$factory_method("label", $ln);
407  if ($b instanceof C\Button\Engageable) {
408  $b = $b->withEngagedState(true);
409 
410  $html = $this->normalizeHTML($r->render($b));
411  $css_classes = self::$canonical_css_classes[$factory_method];
412  $css_classes .= ' engaged';
413  $expected = "<button class=\"$css_classes\" aria-pressed=\"true\" data-action=\"$ln\" id=\"id_1\">" .
414  "label" .
415  "</button>";
416  $this->assertHTMLEquals($expected, $html);
417  } else {
418  $this->assertTrue(self::NOT_APPLICABLE);
419  }
420  }
421 
425  public function test_withOnClick_removes_action($factory_method)
426  {
427  $f = $this->getButtonFactory();
428  $signal = $this->createMock(C\Signal::class);
429  $button = $f->$factory_method("label", "http://www.example.com");
430  $this->assertEquals("http://www.example.com", $button->getAction());
431 
432  $button = $button->withOnClick($signal);
433 
434  $this->assertEquals([$signal], $button->getAction());
435  }
436 
440  public function test_appendOnClick_appends_to_action($factory_method)
441  {
442  $f = $this->getButtonFactory();
443  $signal1 = $this->createMock(C\Signal::class);
444  $signal2 = $this->createMock(C\Signal::class);
445  $button = $f->$factory_method("label", "http://www.example.com");
446 
447  $button = $button->withOnClick($signal1)->appendOnClick($signal2);
448 
449  $this->assertEquals([$signal1, $signal2], $button->getAction());
450  }
451 
455  public function test_render_button_with_signal($factory_method)
456  {
457  $ln = "http://www.ilias.de";
458  $f = $this->getButtonFactory();
459  $signal = $this->createMock(Signal::class);
460  $signal->method("__toString")
461  ->willReturn("MOCK_SIGNAL");
462 
463  $b = $f->$factory_method("label", $ln)
464  ->withOnClick($signal);
465  $r = $this->getDefaultRenderer();
466 
467  $html = $this->normalizeHTML($r->render($b));
468 
469  $css_classes = self::$canonical_css_classes[$factory_method];
470  $expected = "<button class=\"$css_classes\" id=\"id_1\">" .
471  "label" .
472  "</button>";
473  $this->assertHTMLEquals($expected, $html);
474  }
475 
480  {
481  foreach (["primary", "standard"] as $method) {
482  $ln = "http://www.ilias.de";
483  $f = $this->getButtonFactory();
484  $r = $this->getDefaultRenderer();
485  $b = $f->$method("label", $ln)
486  ->withLoadingAnimationOnClick(true);
487 
488  $html = $this->normalizeHTML($r->render($b));
489 
490  $css_classes = self::$canonical_css_classes[$method];
491  $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"id_1\">" .
492  "label" .
493  "</button>";
494  $this->assertHTMLEquals($expected, $html);
495  }
496  }
497 
498 
499  // TODO: We are missing a test for the rendering of a button with an signal
500  // here. Does it still render the action js?
501 
505  public function test_factory_accepts_signal_as_action($factory_method)
506  {
507  $f = $this->getButtonFactory();
508  $signal = $this->createMock(C\Signal::class);
509 
510  $button = $f->$factory_method("label", $signal);
511 
512  $this->assertEquals([$signal], $button->getAction());
513  }
514 
515  public function button_type_provider()
516  {
517  return array( array("standard")
518  , array("primary")
519  , array("shy")
520  , array("tag")
521  );
522  }
523 }
static $canonical_css_classes
Definition: ButtonTest.php:23
test_render_button_label($factory_method)
button_type_provider
Definition: ButtonTest.php:152
test_render_btn_tag_colors()
Definition: ButtonTest.php:300
test_btn_tag_relevance()
Definition: ButtonTest.php:253
test_render_button_with_aria_label($factory_method)
button_type_provider
Definition: ButtonTest.php:382
test_factory_accepts_signal_as_action($factory_method)
button_type_provider
Definition: ButtonTest.php:505
test_button_with_label($factory_method)
button_type_provider
Definition: ButtonTest.php:86
test_render_btn_tag_relevance()
Definition: ButtonTest.php:271
test_____render_close_button_with_on_load_code()
Definition: ButtonTest.php:230
test_button_string_action_only($factory_method)
button_type_provider
Definition: ButtonTest.php:65
button_type_provider()
Definition: ButtonTest.php:515
Test on button implementation.
Definition: ButtonTest.php:14
test_button_with_aria_label($factory_method)
button_type_provider
Definition: ButtonTest.php:339
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:268
normalizeHTML($html)
Definition: Base.php:317
test_render_button_with_aria_pressed($factory_method)
button_type_provider
Definition: ButtonTest.php:401
test_button_label_or_glyph_only($factory_method)
button_type_provider
Definition: ButtonTest.php:55
test_render_close_button()
Definition: ButtonTest.php:188
getButtonFactory()
Definition: ButtonTest.php:18
test_render_btn_tag_classes()
Definition: ButtonTest.php:321
Provides common functionality for UI tests.
Definition: Base.php:224
test_render_button_disabled($factory_method)
button_type_provider
Definition: ButtonTest.php:171
test_render_button_with_signal($factory_method)
button_type_provider
Definition: ButtonTest.php:455
test_button_label($factory_method)
button_type_provider
Definition: ButtonTest.php:75
test_button_with_engaged($factory_method)
button_type_provider
Definition: ButtonTest.php:365
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:326
test_render_button_with_on_click_animation()
test rendering with on click animation
Definition: ButtonTest.php:479
const NOT_APPLICABLE
Definition: ButtonTest.php:16
test_button_deactivation($factory_method)
button_type_provider
Definition: ButtonTest.php:122
test_appendOnClick_appends_to_action($factory_method)
button_type_provider
Definition: ButtonTest.php:440
test_button_activated_on_default($factory_method)
button_type_provider
Definition: ButtonTest.php:111
test_button_with_engageable($factory_method)
button_type_provider
Definition: ButtonTest.php:349
test_button_with_loading_animation()
test loading animation
Definition: ButtonTest.php:135
test_withOnClick_removes_action($factory_method)
button_type_provider
Definition: ButtonTest.php:425
test_render_button_with_on_load_code($factory_method)
button_type_provider
Definition: ButtonTest.php:206
test_implements_factory_interface()
Definition: ButtonTest.php:29
test_button_action($factory_method)
button_type_provider
Definition: ButtonTest.php:100