ILIAS  release_7 Revision v7.30-3-g800a261c036
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\" aria-label=\"close\">" .
197  " <span aria-hidden=\"true\">&times;</span>" .
198  "</button>";
199  $this->assertEquals($expected, $html);
200  }
201 
205  public function test_render_button_with_on_load_code($factory_method)
206  {
207  $ln = "http://www.ilias.de";
208  $f = $this->getButtonFactory();
209  $r = $this->getDefaultRenderer();
210  $ids = array();
211  $b = $f->$factory_method("label", $ln)
212  ->withOnLoadCode(function ($id) use (&$ids) {
213  $ids[] = $id;
214  return "";
215  });
216 
217  $html = $this->normalizeHTML($r->render($b));
218 
219  $this->assertCount(1, $ids);
220 
221  $id = $ids[0];
222  $css_classes = self::$canonical_css_classes[$factory_method];
223  $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"$id\">" .
224  "label" .
225  "</button>";
226  $this->assertHTMLEquals($expected, $html);
227  }
228 
230  {
231  $f = $this->getButtonFactory();
232  $r = $this->getDefaultRenderer();
233  $ids = array();
234  $b = $f->close()
235  ->withOnLoadCode(function ($id) use (&$ids) {
236  $ids[] = $id;
237  return "";
238  });
239 
240  $html = $this->normalizeHTML($r->render($b));
241 
242  $this->assertCount(1, $ids);
243 
244  $id = $ids[0];
245  $expected = "<button type=\"button\" class=\"close\" aria-label=\"close\" id=\"$id\">" .
246  " <span aria-hidden=\"true\">&times;</span>" .
247  "</button>";
248  $this->assertEquals($expected, $html);
249  }
250 
251  public function test_btn_tag_relevance()
252  {
253  $f = $this->getButtonFactory();
254  $b = $f->tag('tag', '#');
255  try {
256  $b->withRelevance(0);
257  $this->assertFalse("This should not happen");
258  } catch (\InvalidArgumentException $e) {
259  $this->assertTrue(true);
260  }
261  try {
262  $b->withRelevance('notsoimportant');
263  $this->assertFalse("This should not happen");
264  } catch (\InvalidArgumentException $e) {
265  $this->assertTrue(true);
266  }
267  }
268 
270  {
271  $expectations = array(
272  '<button class="btn btn-tag btn-tag-relevance-verylow" data-action="#" id="id_1">tag</button>',
273  '<button class="btn btn-tag btn-tag-relevance-low" data-action="#" id="id_2">tag</button>',
274  '<button class="btn btn-tag btn-tag-relevance-middle" data-action="#" id="id_3">tag</button>',
275  '<button class="btn btn-tag btn-tag-relevance-high" data-action="#" id="id_4">tag</button>',
276  '<button class="btn btn-tag btn-tag-relevance-veryhigh" data-action="#" id="id_5">tag</button>'
277  );
278 
279  $f = $this->getButtonFactory();
280  $r = $this->getDefaultRenderer();
281  $t = $f->tag('tag', '#');
282  $possible_relevances = array(
283  $t::REL_VERYLOW,
284  $t::REL_LOW,
285  $t::REL_MID,
286  $t::REL_HIGH,
287  $t::REL_VERYHIGH
288  );
289  foreach ($possible_relevances as $w) {
290  $html = $this->normalizeHTML(
291  $r->render($t->withRelevance($w))
292  );
293  $expected = $expectations[array_search($w, $possible_relevances)];
294  $this->assertEquals($expected, $html);
295  }
296  }
297 
298  public function test_render_btn_tag_colors()
299  {
300  $f = $this->getButtonFactory();
301  $r = $this->getDefaultRenderer();
302  $df = new \ILIAS\Data\Factory;
303 
304  $bgcol = $df->color('#00ff00');
305 
306  $b = $f->tag('tag', '#')
307  ->withBackgroundColor($bgcol);
308  $html = $this->normalizeHTML($r->render($b));
309  $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #000000;" data-action="#" id="id_1">tag</button>';
310  $this->assertEquals($expected, $html);
311 
312  $fcol = $df->color('#ddd');
313  $b = $b->withForegroundColor($fcol);
314  $html = $this->normalizeHTML($r->render($b));
315  $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #dddddd;" data-action="#" id="id_2">tag</button>';
316  $this->assertEquals($expected, $html);
317  }
318 
319  public function test_render_btn_tag_classes()
320  {
321  $f = $this->getButtonFactory();
322  $r = $this->getDefaultRenderer();
323  $df = new \ILIAS\Data\Factory;
324 
325  $classes = array('cl1', 'cl2');
326  $b = $f->tag('tag', '#')
327  ->withClasses($classes);
328  $this->assertEquals($classes, $b->getClasses());
329 
330  $html = $this->normalizeHTML($r->render($b));
331  $expected = '<button class="btn btn-tag btn-tag-relevance-veryhigh cl1 cl2" data-action="#" id="id_1">tag</button>';
332  $this->assertEquals($expected, $html);
333  }
337  public function test_button_with_aria_label($factory_method)
338  {
339  $f = $this->getButtonFactory();
340  $b = $f->$factory_method("label", "http://www.ilias.de")->withAriaLabel("ariatext");
341  $this->assertEquals("ariatext", $b->getAriaLabel());
342  }
343 
347  public function test_button_with_engageable($factory_method)
348  {
349  $f = $this->getButtonFactory();
350  $b = $f->$factory_method("label", "http://www.ilias.de");
351  if ($b instanceof C\Button\Engageable) {
352  $this->assertEquals(false, $b->isEngageable());
353  $b2 = $f->$factory_method("label", "http://www.ilias.de")->withEngagedState(false);
354  $this->assertEquals(true, $b2->isEngageable());
355  } else {
356  $this->assertTrue(self::NOT_APPLICABLE);
357  }
358  }
359 
363  public function test_button_with_engaged($factory_method)
364  {
365  $f = $this->getButtonFactory();
366  $b = $f->$factory_method("label", "http://www.ilias.de");
367  if ($b instanceof C\Button\Engageable) {
368  $b = $b->withEngagedState(false);
369  $this->assertEquals(false, $b->isEngaged());
370  $b2 = $f->$factory_method("label", "http://www.ilias.de")->withEngagedState(true);
371  $this->assertEquals(true, $b2->isEngaged());
372  } else {
373  $this->assertTrue(self::NOT_APPLICABLE);
374  }
375  }
376 
380  public function test_render_button_with_aria_label($factory_method)
381  {
382  $ln = "http://www.ilias.de";
383  $f = $this->getButtonFactory();
384  $r = $this->getDefaultRenderer();
385  $b = $f->$factory_method("label", $ln)->withAriaLabel("aria label text");
386  $aria_label = $b->getAriaLabel();
387 
388  $html = $this->normalizeHTML($r->render($b));
389  $css_classes = self::$canonical_css_classes[$factory_method];
390  $expected = "<button class=\"$css_classes\" aria-label=\"$aria_label\" data-action=\"$ln\" id=\"id_1\">" .
391  "label" .
392  "</button>";
393  $this->assertHTMLEquals($expected, $html);
394  }
395 
399  public function test_render_button_with_aria_pressed($factory_method)
400  {
401  $ln = "http://www.ilias.de";
402  $f = $this->getButtonFactory();
403  $r = $this->getDefaultRenderer();
404  $b = $f->$factory_method("label", $ln);
405  if ($b instanceof C\Button\Engageable) {
406  $b = $b->withEngagedState(true);
407 
408  $html = $this->normalizeHTML($r->render($b));
409  $css_classes = self::$canonical_css_classes[$factory_method];
410  $css_classes .= ' engaged';
411  $expected = "<button class=\"$css_classes\" aria-pressed=\"true\" data-action=\"$ln\" id=\"id_1\">" .
412  "label" .
413  "</button>";
414  $this->assertHTMLEquals($expected, $html);
415  } else {
416  $this->assertTrue(self::NOT_APPLICABLE);
417  }
418  }
419 
423  public function test_withOnClick_removes_action($factory_method)
424  {
425  $f = $this->getButtonFactory();
426  $signal = $this->createMock(C\Signal::class);
427  $button = $f->$factory_method("label", "http://www.example.com");
428  $this->assertEquals("http://www.example.com", $button->getAction());
429 
430  $button = $button->withOnClick($signal);
431 
432  $this->assertEquals([$signal], $button->getAction());
433  }
434 
438  public function test_appendOnClick_appends_to_action($factory_method)
439  {
440  $f = $this->getButtonFactory();
441  $signal1 = $this->createMock(C\Signal::class);
442  $signal2 = $this->createMock(C\Signal::class);
443  $button = $f->$factory_method("label", "http://www.example.com");
444 
445  $button = $button->withOnClick($signal1)->appendOnClick($signal2);
446 
447  $this->assertEquals([$signal1, $signal2], $button->getAction());
448  }
449 
453  public function test_render_button_with_signal($factory_method)
454  {
455  $ln = "http://www.ilias.de";
456  $f = $this->getButtonFactory();
457  $signal = $this->createMock(Signal::class);
458  $signal->method("__toString")
459  ->willReturn("MOCK_SIGNAL");
460 
461  $b = $f->$factory_method("label", $ln)
462  ->withOnClick($signal);
463  $r = $this->getDefaultRenderer();
464 
465  $html = $this->normalizeHTML($r->render($b));
466 
467  $css_classes = self::$canonical_css_classes[$factory_method];
468  $expected = "<button class=\"$css_classes\" id=\"id_1\">" .
469  "label" .
470  "</button>";
471  $this->assertHTMLEquals($expected, $html);
472  }
473 
478  {
479  foreach (["primary", "standard"] as $method) {
480  $ln = "http://www.ilias.de";
481  $f = $this->getButtonFactory();
482  $r = $this->getDefaultRenderer();
483  $b = $f->$method("label", $ln)
484  ->withLoadingAnimationOnClick(true);
485 
486  $html = $this->normalizeHTML($r->render($b));
487 
488  $css_classes = self::$canonical_css_classes[$method];
489  $expected = "<button class=\"$css_classes\" data-action=\"$ln\" id=\"id_1\">" .
490  "label" .
491  "</button>";
492  $this->assertHTMLEquals($expected, $html);
493  }
494  }
495 
496 
497  // TODO: We are missing a test for the rendering of a button with an signal
498  // here. Does it still render the action js?
499 
503  public function test_factory_accepts_signal_as_action($factory_method)
504  {
505  $f = $this->getButtonFactory();
506  $signal = $this->createMock(C\Signal::class);
507 
508  $button = $f->$factory_method("label", $signal);
509 
510  $this->assertEquals([$signal], $button->getAction());
511  }
512 
513  public function button_type_provider()
514  {
515  return array( array("standard")
516  , array("primary")
517  , array("shy")
518  , array("tag")
519  );
520  }
521 }
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:298
test_btn_tag_relevance()
Definition: ButtonTest.php:251
test_render_button_with_aria_label($factory_method)
button_type_provider
Definition: ButtonTest.php:380
test_factory_accepts_signal_as_action($factory_method)
button_type_provider
Definition: ButtonTest.php:503
test_button_with_label($factory_method)
button_type_provider
Definition: ButtonTest.php:86
test_render_btn_tag_relevance()
Definition: ButtonTest.php:269
test_____render_close_button_with_on_load_code()
Definition: ButtonTest.php:229
test_button_string_action_only($factory_method)
button_type_provider
Definition: ButtonTest.php:65
button_type_provider()
Definition: ButtonTest.php:513
Test on button implementation.
Definition: ButtonTest.php:14
test_button_with_aria_label($factory_method)
button_type_provider
Definition: ButtonTest.php:337
normalizeHTML($html)
Definition: Base.php:363
test_render_button_with_aria_pressed($factory_method)
button_type_provider
Definition: ButtonTest.php:399
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:319
Provides common functionality for UI tests.
Definition: Base.php:262
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:453
test_button_label($factory_method)
button_type_provider
Definition: ButtonTest.php:75
test_button_with_engaged($factory_method)
button_type_provider
Definition: ButtonTest.php:363
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:372
test_render_button_with_on_click_animation()
test rendering with on click animation
Definition: ButtonTest.php:477
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:438
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:347
test_button_with_loading_animation()
test loading animation
Definition: ButtonTest.php:135
test_withOnClick_removes_action($factory_method)
button_type_provider
Definition: ButtonTest.php:423
test_render_button_with_on_load_code($factory_method)
button_type_provider
Definition: ButtonTest.php:205
test_implements_factory_interface()
Definition: ButtonTest.php:29
test_button_action($factory_method)
button_type_provider
Definition: ButtonTest.php:100
getDefaultRenderer(JavaScriptBinding $js_binding=null, $with_stub_renderings=[])
Definition: Base.php:311