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