ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
GlyphTest.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("libs/composer/vendor/autoload.php");
6 require_once(__DIR__ . "/../../../Base.php");
7 
8 use \ILIAS\UI\Component\Symbol\Glyph as G;
9 
14 {
15  public function getGlyphFactory()
16  {
17  return new \ILIAS\UI\Implementation\Component\Symbol\Glyph\Factory();
18  }
19 
20  public function getCounterFactory()
21  {
22  return new \ILIAS\UI\Implementation\Component\Counter\Factory();
23  }
24 
25  public static $canonical_css_classes = array( G\Glyph::SETTINGS => "glyphicon glyphicon-cog"
26  , G\Glyph::EXPAND => "glyphicon glyphicon-triangle-right"
27  , G\Glyph::COLLAPSE => "glyphicon glyphicon-triangle-bottom"
28  , G\Glyph::ADD => "glyphicon glyphicon-plus-sign"
29  , G\Glyph::REMOVE => "glyphicon glyphicon-minus-sign"
30  , G\Glyph::UP => "glyphicon glyphicon-circle-arrow-up"
31  , G\Glyph::DOWN => "glyphicon glyphicon-circle-arrow-down"
32  , G\Glyph::BACK => "glyphicon glyphicon-chevron-left"
33  , G\Glyph::NEXT => "glyphicon glyphicon-chevron-right"
34  , G\Glyph::SORT_ASCENDING => "glyphicon glyphicon-arrow-up"
35  , G\Glyph::SORT_DESCENDING => "glyphicon glyphicon-arrow-down"
36  , G\Glyph::USER => "glyphicon glyphicon-user"
37  , G\Glyph::MAIL => "glyphicon glyphicon-envelope"
38  , G\Glyph::NOTIFICATION => "glyphicon glyphicon-bell"
39  , G\Glyph::TAG => "glyphicon glyphicon-tag"
40  , G\Glyph::NOTE => "glyphicon glyphicon-pushpin"
41  , G\Glyph::COMMENT => "glyphicon glyphicon-comment"
42  , G\Glyph::LIKE => "glyphicon il-glyphicon-like"
43  , G\Glyph::LOVE => "glyphicon il-glyphicon-love"
44  , G\Glyph::DISLIKE => "glyphicon il-glyphicon-dislike"
45  , G\Glyph::LAUGH => "glyphicon il-glyphicon-laugh"
46  , G\Glyph::ASTOUNDED => "glyphicon il-glyphicon-astounded"
47  , G\Glyph::SAD => "glyphicon il-glyphicon-sad"
48  , G\Glyph::ANGRY => "glyphicon il-glyphicon-angry"
49  , G\Glyph::ATTACHMENT => "glyphicon glyphicon-paperclip"
50  , G\Glyph::RESET => "glyphicon glyphicon-repeat"
51  , G\Glyph::APPLY => "glyphicon glyphicon-ok"
52  , G\Glyph::SEARCH => "glyphicon glyphicon-search"
53  , G\Glyph::HELP => "glyphicon glyphicon-question-sign"
54  , G\Glyph::CALENDAR => "glyphicon glyphicon-calendar"
55  , G\Glyph::TIME => "glyphicon glyphicon-time"
56  , G\Glyph::CLOSE => "glyphicon glyphicon-remove"
57  , G\Glyph::MORE => "glyphicon glyphicon-option-horizontal"
58  , G\Glyph::DISCLOSURE => "glyphicon glyphicon-option-vertical"
59  , G\Glyph::LANGUAGE => "glyphicon glyphicon-lang"
60  , G\Glyph::LOGIN => "glyphicon glyphicon-login"
61  , G\Glyph::LOGOUT => "glyphicon glyphicon-logout"
62  );
63 
64  public static $aria_labels = array(
65  G\Glyph::SETTINGS => "settings"
66  , G\Glyph::EXPAND => "expand_content"
67  , G\Glyph::COLLAPSE => "collapse_content"
68  , G\Glyph::ADD => "add"
69  , G\Glyph::REMOVE => "remove"
70  , G\Glyph::UP => "up"
71  , G\Glyph::DOWN => "down"
72  , G\Glyph::BACK => "back"
73  , G\Glyph::NEXT => "next"
74  , G\Glyph::SORT_ASCENDING => "sort_ascending"
75  , G\Glyph::SORT_DESCENDING => "sort_descending"
76  , G\Glyph::USER => "show_who_is_online"
77  , G\Glyph::MAIL => "mail"
78  , G\Glyph::NOTIFICATION => "notifications"
79  , G\Glyph::TAG => "tags"
80  , G\Glyph::NOTE => "notes"
81  , G\Glyph::COMMENT => "comments"
82  , G\Glyph::LIKE => "like"
83  , G\Glyph::LOVE => "love"
84  , G\Glyph::DISLIKE => "dislike"
85  , G\Glyph::LAUGH => "laugh"
86  , G\Glyph::ASTOUNDED => "astounded"
87  , G\Glyph::SAD => "sad"
88  , G\Glyph::ANGRY => "angry"
89  , G\Glyph::ATTACHMENT => "attachment"
90  , G\Glyph::RESET => "reset"
91  , G\Glyph::APPLY => "apply"
92  , G\Glyph::SEARCH => "search"
93  , G\Glyph::HELP => "help"
94  , G\Glyph::CALENDAR => "calendar"
95  , G\Glyph::TIME => "time"
96  , G\Glyph::CLOSE => "close"
97  , G\Glyph::MORE => "show_more"
98  , G\Glyph::DISCLOSURE => "disclose"
99  , G\Glyph::LANGUAGE => "switch_language"
100  , G\Glyph::LOGIN => "log_in"
101  , G\Glyph::LOGOUT => "log_out"
102  );
103 
107  public function test_implements_factory_interface($factory_method)
108  {
109  $f = $this->getGlyphFactory();
110 
111  $this->assertInstanceOf("ILIAS\\UI\\Component\\Symbol\\Glyph\\Factory", $f);
112  $this->assertInstanceOf("ILIAS\\UI\\Component\\Symbol\\Glyph\\Glyph", $f->$factory_method("http://www.ilias.de"));
113  }
114 
118  public function test_glyph_types($factory_method)
119  {
120  $f = $this->getGlyphFactory();
121  $g = $f->$factory_method();
122 
123  $this->assertNotNull($g);
124  $this->assertEquals($factory_method, $g->getType());
125  }
126 
130  public function test_glyph_action($factory_method)
131  {
132  $f = $this->getGlyphFactory();
133  $g = $f->$factory_method("http://www.ilias.de");
134 
135  $this->assertNotNull($g);
136  $this->assertEquals("http://www.ilias.de", $g->getAction());
137  }
138 
142  public function test_glyph_no_action($factory_method)
143  {
144  $f = $this->getGlyphFactory();
145  $g = $f->$factory_method();
146 
147  $this->assertNotNull($g);
148  $this->assertEquals(null, $g->getAction());
149  }
150 
154  public function test_with_unavailable_action($factory_method)
155  {
156  $f = $this->getGlyphFactory();
157  $g = $f->$factory_method();
158  $g2 = $f->$factory_method()->withUnavailableAction();
159 
160  $this->assertTrue($g->isActive());
161  $this->assertFalse($g2->isActive());
162  }
163 
167  public function test_with_highlight($counter_type)
168  {
169  $gf = $this->getGlyphFactory();
170 
171  $g = $gf
172  ->mail()
173  ;
174  $g2 = $g->withHighlight();
175 
176  $this->assertFalse($g->isHighlighted());
177  $this->assertTrue($g2->isHighlighted());
178  }
179 
183  public function test_no_counter($factory_method)
184  {
185  $f = $this->getGlyphFactory();
186  $g = $f->$factory_method();
187 
188  $this->assertCount(0, $g->getCounters());
189  }
190 
194  public function test_one_counter($counter_type)
195  {
196  $gf = $this->getGlyphFactory();
197  $cf = $this->getCounterFactory();
198  $number = 1;
199 
200  $g = $gf
201  ->mail()
202  ->withCounter(
203  $cf->$counter_type($number)
204  );
205 
206  $counters = $g->getCounters();
207  $this->assertCount(1, $counters);
208  $c = $counters[0];
209  $this->assertEquals($counter_type, $c->getType());
210  $this->assertEquals($number, $c->getNumber());
211  }
212 
213  public function test_two_counters()
214  {
215  $gf = $this->getGlyphFactory();
216  $cf = $this->getCounterFactory();
217  $number_s = 1;
218  $number_n = 2;
219 
220  $g = $gf
221  ->mail()
222  ->withCounter(
223  $cf->status($number_s)
224  )
225  ->withCounter(
226  $cf->novelty($number_n)
227  );
228 
229  $counters = $g->getCounters();
230  $this->assertCount(2, $counters);
231  $vals = array_map(function ($c) {
232  return array($c->getType(), $c->getNumber());
233  }, $counters);
234  $this->assertContains(array("status", $number_s), $vals);
235  $this->assertContains(array("novelty", $number_n), $vals);
236  }
237 
238  public function test_only_two_counters()
239  {
240  $gf = $this->getGlyphFactory();
241  $cf = $this->getCounterFactory();
242  $number_s = 1;
243  $number_n1 = 2;
244  $number_n2 = 2;
245 
246  $g = $gf
247  ->mail()
248  ->withCounter(
249  $cf->status($number_s)
250  )
251  ->withCounter(
252  $cf->novelty($number_n1)
253  )
254  ->withCounter(
255  $cf->novelty($number_n2)
256  );
257 
258  $counters = $g->getCounters();
259  $this->assertCount(2, $counters);
260  $vals = array_map(function ($c) {
261  return array($c->getType(), $c->getNumber());
262  }, $counters);
263  $this->assertContains(array("status", $number_s), $vals);
264  $this->assertContains(array("novelty", $number_n2), $vals);
265  }
266 
268  {
269  $gf = $this->getGlyphFactory();
270  $cf = $this->getCounterFactory();
271 
272  $g = $gf->mail();
273  $g2 = $g
274  ->withCounter(
275  $cf->novelty(0)
276  );
277 
278  $counters = $g->getCounters();
279  $this->assertCount(0, $counters);
280  }
281 
282  public function test_known_glyphs_only()
283  {
284  $this->expectException(\InvalidArgumentException::class);
285  new \ILIAS\UI\Implementation\Component\Symbol\Glyph\Glyph("FOO", "http://www.ilias.de");
286  }
287 
288  public function glyph_type_provider()
289  {
290  return array( array(G\Glyph::SETTINGS)
291  , array(G\Glyph::EXPAND)
292  , array(G\Glyph::COLLAPSE)
293  , array(G\Glyph::ADD)
294  , array(G\Glyph::REMOVE)
295  , array(G\Glyph::UP)
296  , array(G\Glyph::DOWN)
297  , array(G\Glyph::BACK)
298  , array(G\Glyph::NEXT)
299  , array(G\Glyph::SORT_ASCENDING)
300  , array(G\Glyph::SORT_DESCENDING)
301  , array(G\Glyph::USER)
302  , array(G\Glyph::MAIL)
303  , array(G\Glyph::NOTIFICATION)
304  , array(G\Glyph::TAG)
305  , array(G\Glyph::NOTE)
306  , array(G\Glyph::COMMENT)
307  , array(G\Glyph::LIKE)
308  , array(G\Glyph::LOVE)
309  , array(G\Glyph::DISLIKE)
310  , array(G\Glyph::LAUGH)
311  , array(G\Glyph::ASTOUNDED)
312  , array(G\Glyph::SAD)
313  , array(G\Glyph::ANGRY)
314  , array(G\Glyph::ATTACHMENT)
315  , array(G\Glyph::RESET)
316  , array(G\Glyph::APPLY)
317  , array(G\Glyph::SEARCH)
318  , array(G\Glyph::HELP)
319  , array(G\Glyph::CALENDAR)
320  , array(G\Glyph::TIME)
321  , array(G\Glyph::CLOSE)
322  , array(G\Glyph::MORE)
323  , array(G\Glyph::DISCLOSURE)
324  , array(G\Glyph::LANGUAGE)
325  , array(G\Glyph::LOGIN)
326  , array(G\Glyph::LOGOUT)
327  );
328  }
329 
330  public function counter_type_provider()
331  {
332  return array( array("status")
333  , array("novelty")
334  );
335  }
336 
340  public function test_render_simple($type)
341  {
342  $f = $this->getGlyphFactory();
343  $r = $this->getDefaultRenderer();
344  $c = $f->$type("http://www.ilias.de");
345 
346  $html = $this->normalizeHTML($r->render($c));
347 
348  $css_classes = self::$canonical_css_classes[$type];
349  $aria_label = self::$aria_labels[$type];
350 
351  $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
352  $this->assertEquals($expected, $html);
353  }
354 
359  {
360  $f = $this->getGlyphFactory();
361  $r = $this->getDefaultRenderer();
362  $c = $f->$type("http://www.ilias.de")->withUnavailableAction();
363 
364  $html = $this->normalizeHTML($r->render($c));
365 
366  $css_classes = self::$canonical_css_classes[$type];
367  $aria_label = self::$aria_labels[$type];
368 
369  $expected = "<a class=\"glyph disabled\" aria-label=\"$aria_label\" " .
370  "aria-disabled=\"true\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
371  $this->assertEquals($expected, $html);
372  }
373 
378  {
379  $fg = $this->getGlyphFactory();
380  $fc = $this->getCounterFactory();
381  $r = $this->getDefaultRenderer();
382  $c = $fg->mail("http://www.ilias.de")->withCounter($fc->$type(42));
383 
384  $html = $this->normalizeHTML($r->render($c));
385 
386  $css_classes = self::$canonical_css_classes[G\Glyph::MAIL];
387  $aria_label = self::$aria_labels[G\Glyph::MAIL];
388 
389  $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\">" .
390  "<span class=\"$css_classes\" aria-hidden=\"true\"></span>" .
391  "<span class=\"il-counter\"><span class=\"badge badge-notify il-counter-$type\">42</span></span>" .
392  "<span class=\"il-counter-spacer\">42</span>" .
393  "</a>";
394  $this->assertHTMLEquals($expected, $html);
395  }
396 
397  public function test_render_withTwoCounters()
398  {
399  $fg = $this->getGlyphFactory();
400  $fc = $this->getCounterFactory();
401  $r = $this->getDefaultRenderer();
402  $c = $fg->mail("http://www.ilias.de")
403  ->withCounter($fc->novelty(42))
404  ->withCounter($fc->status(7));
405 
406  $html = $this->normalizeHTML($r->render($c));
407 
408  $css_classes = self::$canonical_css_classes[G\Glyph::MAIL];
409  $aria_label = self::$aria_labels[G\Glyph::MAIL];
410  $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\">" .
411  "<span class=\"$css_classes\" aria-hidden=\"true\"></span>" .
412  "<span class=\"il-counter\"><span class=\"badge badge-notify il-counter-status\">7</span></span>" .
413  "<span class=\"il-counter\"><span class=\"badge badge-notify il-counter-novelty\">42</span></span>" .
414  "<span class=\"il-counter-spacer\">42</span>" .
415  "</a>";
416  $this->assertHTMLEquals($expected, $html);
417  }
418 
419  public function test_dont_render_counter()
420  {
421  $this->expectException(\LogicException::class);
422  $r = new \ILIAS\UI\Implementation\Component\Symbol\Glyph\Renderer(
423  $this->getUIFactory(),
424  $this->getTemplateFactory(),
425  $this->getLanguage(),
426  $this->getJavaScriptBinding(),
427  $this->getRefinery()
428  );
429  $f = $this->getCounterFactory();
430 
431  $r->render($f->status(0), $this->getDefaultRenderer());
432  }
433 
438  {
439  $f = $this->getGlyphFactory();
440  $r = $this->getDefaultRenderer();
441  $ids = array();
442  $c = $f->$type("http://www.ilias.de")
443  ->withOnLoadCode(function ($id) use (&$ids) {
444  $ids[] = $id;
445  return "";
446  });
447 
448  $html = $this->normalizeHTML($r->render($c));
449 
450  $this->assertCount(1, $ids);
451 
452  $css_classes = self::$canonical_css_classes[$type];
453  $aria_label = self::$aria_labels[$type];
454 
455  $id = $ids[0];
456  $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\" id=\"$id\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
457  $this->assertEquals($expected, $html);
458  }
459 
464  {
465  $f = $this->getGlyphFactory();
466  $r = $this->getDefaultRenderer();
467  $c = $f->$type("http://www.ilias.de");
468  $c = $c->withAction("http://www.ilias.de/open-source-lms-ilias/");
469 
470  $html = $this->normalizeHTML($r->render($c));
471 
472  $css_classes = self::$canonical_css_classes[$type];
473  $aria_label = self::$aria_labels[$type];
474 
475  $expected = "<a class=\"glyph\" href=\"http://www.ilias.de/open-source-lms-ilias/\" aria-label=\"$aria_label\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
476  $this->assertEquals($expected, $html);
477  }
478 }
test_glyph_no_action($factory_method)
glyph_type_provider
Definition: GlyphTest.php:142
$type
test_render_simple($type)
glyph_type_provider
Definition: GlyphTest.php:340
counter_type_provider()
Definition: GlyphTest.php:330
test_dont_render_counter()
Definition: GlyphTest.php:419
glyph_type_provider()
Definition: GlyphTest.php:288
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:268
normalizeHTML($html)
Definition: Base.php:317
test_with_highlight($counter_type)
counter_type_provider
Definition: GlyphTest.php:167
test_render_withCounter($type)
counter_type_provider
Definition: GlyphTest.php:377
test_two_counters()
Definition: GlyphTest.php:213
test_implements_factory_interface($factory_method)
glyph_type_provider
Definition: GlyphTest.php:107
getJavaScriptBinding()
Definition: Base.php:256
test_known_glyphs_only()
Definition: GlyphTest.php:282
Provides common functionality for UI tests.
Definition: Base.php:224
test_one_counter($counter_type)
counter_type_provider
Definition: GlyphTest.php:194
test_glyph_types($factory_method)
glyph_type_provider
Definition: GlyphTest.php:118
getCounterFactory()
Definition: GlyphTest.php:20
test_render_with_action($type)
glyph_type_provider
Definition: GlyphTest.php:463
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:326
test_no_counter($factory_method)
glyph_type_provider
Definition: GlyphTest.php:183
test_with_unavailable_action($factory_method)
glyph_type_provider
Definition: GlyphTest.php:154
static $canonical_css_classes
Definition: GlyphTest.php:25
test_render_with_on_load_code($type)
glyph_type_provider
Definition: GlyphTest.php:437
getTemplateFactory()
Definition: Base.php:241
test_glyph_action($factory_method)
glyph_type_provider
Definition: GlyphTest.php:130
test_render_with_unavailable_action($type)
glyph_type_provider
Definition: GlyphTest.php:358
test_render_withTwoCounters()
Definition: GlyphTest.php:397
test_immutability_withCounter()
Definition: GlyphTest.php:267
test_only_two_counters()
Definition: GlyphTest.php:238
static $aria_labels
Definition: GlyphTest.php:64
Test on glyph implementation.
Definition: GlyphTest.php:13
getGlyphFactory()
Definition: GlyphTest.php:15