ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
14 {
15  public function getButtonFactory()
16  {
17  return new \ILIAS\UI\Implementation\Component\Button\Factory();
18  }
19 
20  public static $canonical_css_classes = array( "standard" => "btn btn-default"
21  , "primary" => "btn btn-default btn-primary"
22  , "shy" => "btn btn-link"
23  , "tag" => "btn btn-tag btn-tag-relevance-veryhigh"
24  );
25 
26  public static $canonical_css_inactivation_classes = array( "standard" => "ilSubmitInactive disabled"
27  , "primary" => "ilSubmitInactive disabled"
28  , "shy" => "ilSubmitInactive disabled"
29  , "tag" => "btn-tag-inactive"
30  );
31 
32 
34  {
35  $f = $this->getButtonFactory();
36 
37  $this->assertInstanceOf("ILIAS\\UI\\Component\\Button\\Factory", $f);
38  $this->assertInstanceOf(
39  "ILIAS\\UI\\Component\\Button\\Standard",
40  $f->standard("label", "http://www.ilias.de")
41  );
42  $this->assertInstanceOf(
43  "ILIAS\\UI\\Component\\Button\\Primary",
44  $f->primary("label", "http://www.ilias.de")
45  );
46  $this->assertInstanceOf(
47  "ILIAS\\UI\\Component\\Button\\Close",
48  $f->close()
49  );
50  $this->assertInstanceOf(
51  "ILIAS\\UI\\Component\\Button\\Shy",
52  $f->shy("label", "http://www.ilias.de")
53  );
54  }
55 
59  public function test_button_label_or_glyph_only($factory_method)
60  {
61  $f = $this->getButtonFactory();
62  try {
63  $f->$factory_method($this, "http://www.ilias.de");
64  $this->assertFalse("This should not happen");
65  } catch (\InvalidArgumentException $e) {
66  }
67  }
68 
72  public function test_button_string_action_only($factory_method)
73  {
74  $f = $this->getButtonFactory();
75  try {
76  $f->$factory_method("label", $this);
77  $this->assertFalse("This should not happen");
78  } catch (\InvalidArgumentException $e) {
79  }
80  }
81 
85  public function test_button_label($factory_method)
86  {
87  $f = $this->getButtonFactory();
88  $b = $f->$factory_method("label", "http://www.ilias.de");
89 
90  $this->assertEquals("label", $b->getLabel());
91  }
92 
96  public function test_button_with_label($factory_method)
97  {
98  $f = $this->getButtonFactory();
99  $b = $f->$factory_method("label", "http://www.ilias.de");
100 
101  $b2 = $b->withLabel("label2");
102 
103  $this->assertEquals("label", $b->getLabel());
104  $this->assertEquals("label2", $b2->getLabel());
105  }
106 
110  public function test_button_action($factory_method)
111  {
112  $f = $this->getButtonFactory();
113  $b = $f->$factory_method("label", "http://www.ilias.de");
114 
115  $this->assertEquals("http://www.ilias.de", $b->getAction());
116  }
117 
121  public function test_button_activated_on_default($factory_method)
122  {
123  $f = $this->getButtonFactory();
124  $b = $f->$factory_method("label", "http://www.ilias.de");
125 
126  $this->assertTrue($b->isActive());
127  }
128 
132  public function test_button_deactivation($factory_method)
133  {
134  $f = $this->getButtonFactory();
135  $b = $f->$factory_method("label", "http://www.ilias.de")
136  ->withUnavailableAction();
137 
138  $this->assertFalse($b->isActive());
139  $this->assertEquals("http://www.ilias.de", $b->getAction());
140  }
141 
145  public function test_render_button_label($factory_method)
146  {
147  $ln = "http://www.ilias.de";
148  $f = $this->getButtonFactory();
149  $b = $f->$factory_method("label", $ln);
150  $r = $this->getDefaultRenderer();
151 
152  $html = $this->normalizeHTML($r->render($b));
153 
154  $css_classes = self::$canonical_css_classes[$factory_method];
155  $expected = "<a class=\"$css_classes\" href=\"$ln\" data-action=\"$ln\">" .
156  "label" .
157  "</a>";
158  $this->assertHTMLEquals($expected, $html);
159  }
160 
164  public function test_render_button_disabled($factory_method)
165  {
166  $ln = "http://www.ilias.de";
167  $f = $this->getButtonFactory();
168  $b = $f->$factory_method("label", $ln)
169  ->withUnavailableAction();
170  $r = $this->getDefaultRenderer();
171 
172  $html = $this->normalizeHTML($r->render($b));
173 
174  $css_classes = self::$canonical_css_classes[$factory_method];
175  $css_class_inactive = self::$canonical_css_inactivation_classes[$factory_method];
176  $expected = "<a class=\"$css_classes $css_class_inactive\" data-action=\"$ln\">" .
177  "label" .
178  "</a>";
179  $this->assertHTMLEquals($expected, $html);
180  }
181 
182  public function test_render_close_button()
183  {
184  $f = $this->getButtonFactory();
185  $r = $this->getDefaultRenderer();
186  $b = $f->close();
187 
188  $html = $this->normalizeHTML($r->render($b));
189 
190  $expected = "<button type=\"button\" class=\"close\" data-dismiss=\"modal\">" .
191  " <span aria-hidden=\"true\">&times;</span>" .
192  " <span class=\"sr-only\">Close</span>" .
193  "</button>";
194  $this->assertEquals($expected, $html);
195  }
196 
200  public function test_render_button_with_on_load_code($factory_method)
201  {
202  $ln = "http://www.ilias.de";
203  $f = $this->getButtonFactory();
204  $r = $this->getDefaultRenderer();
205  $ids = array();
206  $b = $f->$factory_method("label", $ln)
207  ->withOnLoadCode(function ($id) use (&$ids) {
208  $ids[] = $id;
209  return "";
210  });
211 
212  $html = $this->normalizeHTML($r->render($b));
213 
214  $this->assertCount(1, $ids);
215 
216  $id = $ids[0];
217  $css_classes = self::$canonical_css_classes[$factory_method];
218  $expected = "<a class=\"$css_classes\" href=\"$ln\" data-action=\"$ln\" id=\"$id\">" .
219  "label" .
220  "</a>";
221  $this->assertHTMLEquals($expected, $html);
222  }
223 
225  {
226  $f = $this->getButtonFactory();
227  $r = $this->getDefaultRenderer();
228  $ids = array();
229  $b = $f->close()
230  ->withOnLoadCode(function ($id) use (&$ids) {
231  $ids[] = $id;
232  return "";
233  });
234 
235  $html = $this->normalizeHTML($r->render($b));
236 
237  $this->assertCount(1, $ids);
238 
239  $id = $ids[0];
240  $expected = "<button type=\"button\" class=\"close\" data-dismiss=\"modal\" id=\"$id\">" .
241  " <span aria-hidden=\"true\">&times;</span>" .
242  " <span class=\"sr-only\">Close</span>" .
243  "</button>";
244  $this->assertEquals($expected, $html);
245  }
246 
247  public function test_btn_tag_relevance()
248  {
249  $f = $this->getButtonFactory();
250  $b = $f->tag('tag', '#');
251  try {
252  $b->withRelevance(0);
253  $this->assertFalse("This should not happen");
254  } catch (\InvalidArgumentException $e) {
255  $this->assertTrue(true);
256  }
257  try {
258  $b->withRelevance('notsoimportant');
259  $this->assertFalse("This should not happen");
260  } catch (\InvalidArgumentException $e) {
261  $this->assertTrue(true);
262  }
263  }
264 
266  {
267  $expectations = array(
268  '<a class="btn btn-tag btn-tag-relevance-verylow" href="#" data-action="#">tag</a>',
269  '<a class="btn btn-tag btn-tag-relevance-low" href="#" data-action="#">tag</a>',
270  '<a class="btn btn-tag btn-tag-relevance-middle" href="#" data-action="#">tag</a>',
271  '<a class="btn btn-tag btn-tag-relevance-high" href="#" data-action="#">tag</a>',
272  '<a class="btn btn-tag btn-tag-relevance-veryhigh" href="#" data-action="#">tag</a>'
273  );
274 
275  $f = $this->getButtonFactory();
276  $r = $this->getDefaultRenderer();
277  $t = $f->tag('tag', '#');
278  $possible_relevances = array(
279  $t::REL_VERYLOW,
280  $t::REL_LOW,
281  $t::REL_MID,
282  $t::REL_HIGH,
283  $t::REL_VERYHIGH
284  );
285  foreach ($possible_relevances as $w) {
286  $html = $this->normalizeHTML(
287  $r->render($t->withRelevance($w))
288  );
289  $expected = $expectations[array_search($w, $possible_relevances)];
290  $this->assertEquals($expected, $html);
291  }
292  }
293 
294  public function test_render_btn_tag_colors()
295  {
296  $f = $this->getButtonFactory();
297  $r = $this->getDefaultRenderer();
298  $df = new \ILIAS\Data\Factory;
299 
300  $bgcol = $df->color('#00ff00');
301 
302  $b = $f->tag('tag', '#')
303  ->withBackgroundColor($bgcol);
304  $html = $this->normalizeHTML($r->render($b));
305  $expected = '<a class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #000000;" href="#" data-action="#">tag</a>';
306  $this->assertEquals($expected, $html);
307 
308  $fcol = $df->color('#ddd');
309  $b = $b->withForegroundColor($fcol);
310  $html = $this->normalizeHTML($r->render($b));
311  $expected = '<a class="btn btn-tag btn-tag-relevance-veryhigh" style="background-color: #00ff00; color: #dddddd;" href="#" data-action="#">tag</a>';
312  $this->assertEquals($expected, $html);
313  }
314 
315  public function test_render_btn_tag_classes()
316  {
317  $f = $this->getButtonFactory();
318  $r = $this->getDefaultRenderer();
319  $df = new \ILIAS\Data\Factory;
320 
321  $classes = array('cl1', 'cl2');
322  $b = $f->tag('tag', '#')
323  ->withClasses($classes);
324  $this->assertEquals($classes, $b->getClasses());
325 
326  $html = $this->normalizeHTML($r->render($b));
327  $expected = '<a class="btn btn-tag btn-tag-relevance-veryhigh cl1 cl2" href="#" data-action="#">tag</a>';
328  $this->assertEquals($expected, $html);
329  }
333  public function test_button_with_aria_label($factory_method)
334  {
335  $f = $this->getButtonFactory();
336  $b = $f->$factory_method("label", "http://www.ilias.de")->withAriaLabel("ariatext");
337  $this->assertEquals("ariatext", $b->getAriaLabel());
338  }
339 
343  public function test_button_with_aria_checked($factory_method)
344  {
345  $f = $this->getButtonFactory();
346  $b = $f->$factory_method("label", "http://www.ilias.de");
347  $this->assertEquals(false, $b->isAriaChecked());
348  $b2 = $f->$factory_method("label", "http://www.ilias.de")->withAriaChecked();
349  $this->assertEquals(true, $b2->isAriaChecked());
350  }
351 
355  public function test_render_button_with_aria_label($factory_method)
356  {
357  //only standard buttons have aria labels in the template. Should the others accept aria stuff?
358  //if yes, remove this conditional
359  if ($factory_method == "standard") {
360  $ln = "http://www.ilias.de";
361  $f = $this->getButtonFactory();
362  $r = $this->getDefaultRenderer();
363  $b = $f->$factory_method("label", $ln)->withAriaLabel("aria label text");
364  $aria_label = $b->getAriaLabel();
365 
366  $html = $this->normalizeHTML($r->render($b));
367  $css_classes = self::$canonical_css_classes[$factory_method];
368  $expected = "<a class=\"$css_classes\" href=\"$ln\" aria-label=\"$aria_label\" data-action=\"$ln\">" .
369  "label" .
370  "</a>";
371  $this->assertHTMLEquals($expected, $html);
372  }
373  }
374 
378  public function test_render_button_with_aria_checked($factory_method)
379  {
380  //only standard buttons have aria labels in the template. Should the others accept aria stuff?
381  //if yes, remove this conditional
382  if ($factory_method == "standard") {
383  $ln = "http://www.ilias.de";
384  $f = $this->getButtonFactory();
385  $r = $this->getDefaultRenderer();
386  $b = $f->$factory_method("label", $ln)->withAriaChecked();
387 
388  $html = $this->normalizeHTML($r->render($b));
389  $css_classes = self::$canonical_css_classes[$factory_method];
390  $expected = "<a class=\"$css_classes\" href=\"$ln\" aria-checked=\"true\" data-action=\"$ln\">" .
391  "label" .
392  "</a>";
393  $this->assertHTMLEquals($expected, $html);
394  }
395  }
396 
397 
398  public function button_type_provider()
399  {
400  return array( array("standard")
401  , array("primary")
402  , array("shy")
403  , array("tag")
404  );
405  }
406 }
static $canonical_css_classes
Definition: ButtonTest.php:20
test_render_button_label($factory_method)
button_type_provider
Definition: ButtonTest.php:145
test_render_btn_tag_colors()
Definition: ButtonTest.php:294
test_btn_tag_relevance()
Definition: ButtonTest.php:247
test_render_button_with_aria_label($factory_method)
button_type_provider
Definition: ButtonTest.php:355
test_button_with_label($factory_method)
button_type_provider
Definition: ButtonTest.php:96
test_render_btn_tag_relevance()
Definition: ButtonTest.php:265
test_____render_close_button_with_on_load_code()
Definition: ButtonTest.php:224
test_button_string_action_only($factory_method)
button_type_provider
Definition: ButtonTest.php:72
if(!array_key_exists('StateId', $_REQUEST)) $id
button_type_provider()
Definition: ButtonTest.php:398
static $canonical_css_inactivation_classes
Definition: ButtonTest.php:26
Test on button implementation.
Definition: ButtonTest.php:13
test_button_with_aria_label($factory_method)
button_type_provider
Definition: ButtonTest.php:333
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:216
normalizeHTML($html)
Definition: Base.php:243
$w
test_button_label_or_glyph_only($factory_method)
button_type_provider
Definition: ButtonTest.php:59
test_render_close_button()
Definition: ButtonTest.php:182
getButtonFactory()
Definition: ButtonTest.php:15
test_render_btn_tag_classes()
Definition: ButtonTest.php:315
$r
Definition: example_031.php:79
test_button_with_aria_checked($factory_method)
button_type_provider
Definition: ButtonTest.php:343
Provides common functionality for UI tests.
Definition: Base.php:177
test_render_button_disabled($factory_method)
button_type_provider
Definition: ButtonTest.php:164
test_render_button_with_aria_checked($factory_method)
button_type_provider
Definition: ButtonTest.php:378
test_button_label($factory_method)
button_type_provider
Definition: ButtonTest.php:85
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:252
Create styles array
The data for the language used.
test_button_deactivation($factory_method)
button_type_provider
Definition: ButtonTest.php:132
test_button_activated_on_default($factory_method)
button_type_provider
Definition: ButtonTest.php:121
test_render_button_with_on_load_code($factory_method)
button_type_provider
Definition: ButtonTest.php:200
$html
Definition: example_001.php:87
test_implements_factory_interface()
Definition: ButtonTest.php:33
test_button_action($factory_method)
button_type_provider
Definition: ButtonTest.php:110