ILIAS  release_8 Revision v8.24
GlyphTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21require_once("libs/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../../Base.php");
23
29
34{
35 public function getGlyphFactory(): G\Factory
36 {
37 return new I\Symbol\Glyph\Factory();
38 }
39
40 public function getCounterFactory(): C\Factory
41 {
42 return new I\Counter\Factory();
43 }
44
45 public static array $canonical_css_classes = array(
46 G\Glyph::SETTINGS => "glyphicon glyphicon-cog",
47 G\Glyph::EXPAND => "glyphicon glyphicon-triangle-right",
48 G\Glyph::COLLAPSE => "glyphicon glyphicon-triangle-bottom",
49 G\Glyph::ADD => "glyphicon glyphicon-plus-sign",
50 G\Glyph::REMOVE => "glyphicon glyphicon-minus-sign",
51 G\Glyph::UP => "glyphicon glyphicon-circle-arrow-up",
52 G\Glyph::DOWN => "glyphicon glyphicon-circle-arrow-down",
53 G\Glyph::BACK => "glyphicon glyphicon-chevron-left",
54 G\Glyph::NEXT => "glyphicon glyphicon-chevron-right",
55 G\Glyph::SORT_ASCENDING => "glyphicon glyphicon-arrow-up",
56 G\Glyph::SORT_DESCENDING => "glyphicon glyphicon-arrow-down",
57 G\Glyph::USER => "glyphicon glyphicon-user",
58 G\Glyph::MAIL => "glyphicon glyphicon-envelope",
59 G\Glyph::NOTIFICATION => "glyphicon glyphicon-bell",
60 G\Glyph::TAG => "glyphicon glyphicon-tag",
61 G\Glyph::NOTE => "glyphicon glyphicon-pushpin",
62 G\Glyph::COMMENT => "glyphicon glyphicon-comment",
63 G\Glyph::BRIEFCASE => "glyphicon glyphicon-briefcase",
64 G\Glyph::LIKE => "glyphicon il-glyphicon-like",
65 G\Glyph::LOVE => "glyphicon il-glyphicon-love",
66 G\Glyph::DISLIKE => "glyphicon il-glyphicon-dislike",
67 G\Glyph::LAUGH => "glyphicon il-glyphicon-laugh",
68 G\Glyph::ASTOUNDED => "glyphicon il-glyphicon-astounded",
69 G\Glyph::SAD => "glyphicon il-glyphicon-sad",
70 G\Glyph::ANGRY => "glyphicon il-glyphicon-angry",
71 G\Glyph::EYEOPEN => "glyphicon glyphicon-eye-open",
72 G\Glyph::EYECLOSED => "glyphicon glyphicon-eye-close",
73 G\Glyph::ATTACHMENT => "glyphicon glyphicon-paperclip",
74 G\Glyph::RESET => "glyphicon glyphicon-repeat",
75 G\Glyph::APPLY => "glyphicon glyphicon-ok",
76 G\Glyph::SEARCH => "glyphicon glyphicon-search",
77 G\Glyph::HELP => "glyphicon glyphicon-question-sign",
78 G\Glyph::CALENDAR => "glyphicon glyphicon-calendar",
79 G\Glyph::TIME => "glyphicon glyphicon-time",
80 G\Glyph::CLOSE => "glyphicon glyphicon-remove",
81 G\Glyph::MORE => "glyphicon glyphicon-option-horizontal",
82 G\Glyph::DISCLOSURE => "glyphicon glyphicon-option-vertical",
83 G\Glyph::LANGUAGE => "glyphicon glyphicon-lang",
84 G\Glyph::LOGIN => "glyphicon glyphicon-login",
85 G\Glyph::LOGOUT => "glyphicon glyphicon-logout",
86 G\Glyph::BULLETLIST => "glyphicon glyphicon-bulletlist",
87 G\Glyph::NUMBEREDLIST => "glyphicon glyphicon-numberedlist",
88 G\Glyph::LISTINDENT => "glyphicon glyphicon-listindent",
89 G\Glyph::LISTOUTDENT => "glyphicon glyphicon-listoutdent",
90 G\Glyph::FILTER => "glyphicon glyphicon-filter",
91 G\Glyph::COLLAPSE_HORIZONTAL => "glyphicon glyphicon-triangle-left"
92 );
93
94 public static array $aria_labels = array(
95 G\Glyph::SETTINGS => "settings",
96 G\Glyph::EXPAND => "expand_content",
97 G\Glyph::COLLAPSE => "collapse_content",
98 G\Glyph::ADD => "add",
99 G\Glyph::REMOVE => "remove",
100 G\Glyph::UP => "up",
101 G\Glyph::DOWN => "down",
102 G\Glyph::BACK => "back",
103 G\Glyph::NEXT => "next",
104 G\Glyph::SORT_ASCENDING => "sort_ascending",
105 G\Glyph::SORT_DESCENDING => "sort_descending",
106 G\Glyph::USER => "show_who_is_online",
107 G\Glyph::MAIL => "mail",
108 G\Glyph::NOTIFICATION => "notifications",
109 G\Glyph::TAG => "tags",
110 G\Glyph::NOTE => "notes",
111 G\Glyph::COMMENT => "comments",
112 G\Glyph::BRIEFCASE => "briefcase",
113 G\Glyph::LIKE => "like",
114 G\Glyph::LOVE => "love",
115 G\Glyph::DISLIKE => "dislike",
116 G\Glyph::LAUGH => "laugh",
117 G\Glyph::ASTOUNDED => "astounded",
118 G\Glyph::SAD => "sad",
119 G\Glyph::ANGRY => "angry",
120 G\Glyph::EYEOPEN => "eyeopened",
121 G\Glyph::EYECLOSED => "eyeclosed",
122 G\Glyph::ATTACHMENT => "attachment",
123 G\Glyph::RESET => "reset",
124 G\Glyph::APPLY => "apply",
125 G\Glyph::SEARCH => "search",
126 G\Glyph::HELP => "help",
127 G\Glyph::CALENDAR => "calendar",
128 G\Glyph::TIME => "time",
129 G\Glyph::CLOSE => "close",
130 G\Glyph::MORE => "show_more",
131 G\Glyph::DISCLOSURE => "disclose",
132 G\Glyph::LANGUAGE => "switch_language",
133 G\Glyph::LOGIN => "log_in",
134 G\Glyph::LOGOUT => "log_out",
135 G\Glyph::BULLETLIST => "bulletlist",
136 G\Glyph::NUMBEREDLIST => "numberedlist",
137 G\Glyph::LISTINDENT => "listindent",
138 G\Glyph::LISTOUTDENT => "listoutdent",
139 G\Glyph::FILTER => "filter",
140 G\Glyph::COLLAPSE_HORIZONTAL => "collapse/back"
141 );
142
146 public function test_implements_factory_interface(string $factory_method): void
147 {
148 $f = $this->getGlyphFactory();
149
150 $this->assertInstanceOf("ILIAS\\UI\\Component\\Symbol\\Glyph\\Factory", $f);
151 $this->assertInstanceOf("ILIAS\\UI\\Component\\Symbol\\Glyph\\Glyph", $f->$factory_method("http://www.ilias.de"));
152 }
153
157 public function test_glyph_types(string $factory_method): void
158 {
159 $f = $this->getGlyphFactory();
160 $g = $f->$factory_method();
161
162 $this->assertNotNull($g);
163 $this->assertEquals($factory_method, $g->getType());
164 }
165
169 public function test_glyph_action(string $factory_method): void
170 {
171 $f = $this->getGlyphFactory();
172 $g = $f->$factory_method("http://www.ilias.de");
173
174 $this->assertNotNull($g);
175 $this->assertEquals("http://www.ilias.de", $g->getAction());
176 }
177
181 public function test_glyph_no_action(string $factory_method): void
182 {
183 $f = $this->getGlyphFactory();
184 $g = $f->$factory_method();
185
186 $this->assertNotNull($g);
187 $this->assertEquals(null, $g->getAction());
188 }
189
193 public function test_with_unavailable_action(string $factory_method): void
194 {
195 $f = $this->getGlyphFactory();
196 $g = $f->$factory_method();
197 $g2 = $f->$factory_method()->withUnavailableAction();
198
199 $this->assertTrue($g->isActive());
200 $this->assertFalse($g2->isActive());
201 }
202
203 public function test_with_highlight(): void
204 {
205 $gf = $this->getGlyphFactory();
206
207 $g = $gf->mail();
208 $g2 = $g->withHighlight();
209
210 $this->assertFalse($g->isHighlighted());
211 $this->assertTrue($g2->isHighlighted());
212 }
213
217 public function test_no_counter(string $factory_method): void
218 {
219 $f = $this->getGlyphFactory();
220 $g = $f->$factory_method();
221
222 $this->assertCount(0, $g->getCounters());
223 }
224
228 public function test_one_counter(string $counter_type): void
229 {
230 $gf = $this->getGlyphFactory();
231 $cf = $this->getCounterFactory();
232 $number = 1;
233
234 $g = $gf
235 ->mail()
236 ->withCounter(
237 $cf->$counter_type($number)
238 );
239
240 $counters = $g->getCounters();
241 $this->assertCount(1, $counters);
242 $c = $counters[0];
243 $this->assertEquals($counter_type, $c->getType());
244 $this->assertEquals($number, $c->getNumber());
245 }
246
247 public function test_two_counters(): void
248 {
249 $gf = $this->getGlyphFactory();
250 $cf = $this->getCounterFactory();
251 $number_s = 1;
252 $number_n = 2;
253
254 $g = $gf
255 ->mail()
256 ->withCounter(
257 $cf->status($number_s)
258 )
259 ->withCounter(
260 $cf->novelty($number_n)
261 );
262
263 $counters = $g->getCounters();
264 $this->assertCount(2, $counters);
265 $vals = array_map(function ($c) {
266 return array($c->getType(), $c->getNumber());
267 }, $counters);
268 $this->assertContains(array("status", $number_s), $vals);
269 $this->assertContains(array("novelty", $number_n), $vals);
270 }
271
272 public function test_only_two_counters(): void
273 {
274 $gf = $this->getGlyphFactory();
275 $cf = $this->getCounterFactory();
276 $number_s = 1;
277 $number_n1 = 2;
278 $number_n2 = 2;
279
280 $g = $gf
281 ->mail()
282 ->withCounter(
283 $cf->status($number_s)
284 )
285 ->withCounter(
286 $cf->novelty($number_n1)
287 )
288 ->withCounter(
289 $cf->novelty($number_n2)
290 );
291
292 $counters = $g->getCounters();
293 $this->assertCount(2, $counters);
294 $vals = array_map(function ($c) {
295 return array($c->getType(), $c->getNumber());
296 }, $counters);
297 $this->assertContains(array("status", $number_s), $vals);
298 $this->assertContains(array("novelty", $number_n2), $vals);
299 }
300
301 public function test_immutability_withCounter(): void
302 {
303 $gf = $this->getGlyphFactory();
304 $cf = $this->getCounterFactory();
305
306 $g = $gf->mail();
307 $g
308 ->withCounter(
309 $cf->novelty(0)
310 );
311
312 $counters = $g->getCounters();
313 $this->assertCount(0, $counters);
314 }
315
316 public function test_known_glyphs_only(): void
317 {
318 $this->expectException(InvalidArgumentException::class);
319 new Glyph("FOO", "http://www.ilias.de");
320 }
321
322 public function glyph_type_provider(): array
323 {
324 $glyph_reflection = new ReflectionClass(G\Glyph::class);
325 $constant_values = array_values($glyph_reflection->getConstants());
326 return array_map(function ($val) {
327 return [$val];
328 }, $constant_values);
329 }
330
331 public function counter_type_provider(): array
332 {
333 return [
334 ["status"],
335 ["novelty"]
336 ];
337 }
338
342 public function test_render_simple(string $type): void
343 {
344 $f = $this->getGlyphFactory();
345 $r = $this->getDefaultRenderer();
346 $c = $f->$type("http://www.ilias.de");
347
348 $html = $this->normalizeHTML($r->render($c));
349
350 $css_classes = self::$canonical_css_classes[$type];
351 $aria_label = self::$aria_labels[$type];
352
353 $expected = '<a tabindex="0" class="glyph" href="http://www.ilias.de" aria-label="' . $aria_label . '"><span class="' . $css_classes . '" aria-hidden="true"></span></a>';
354 $this->assertEquals($expected, $html);
355 }
356
360 public function test_render_with_unavailable_action(string $type): void
361 {
362 $f = $this->getGlyphFactory();
363 $r = $this->getDefaultRenderer();
364 $c = $f->$type("http://www.ilias.de")->withUnavailableAction();
365
366 $html = $this->normalizeHTML($r->render($c));
367
368 $css_classes = self::$canonical_css_classes[$type];
369 $aria_label = self::$aria_labels[$type];
370
371 $expected = '
372 <a class="glyph disabled" aria-label="' . $aria_label . '" aria-disabled="true">
373 <span class="' . $css_classes . '" aria-hidden="true"></span>
374 </a>';
375 $this->assertEquals($this->brutallyTrimHTML($expected), $this->brutallyTrimHTML($html));
376 }
377
381 public function test_render_withCounter(string $type): void
382 {
383 $fg = $this->getGlyphFactory();
384 $fc = $this->getCounterFactory();
385 $r = $this->getDefaultRenderer();
386 $c = $fg->mail("http://www.ilias.de")->withCounter($fc->$type(42));
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
393 $expected = '
394 <a tabindex="0" class="glyph" href="http://www.ilias.de" aria-label="' . $aria_label . '">
395 <span class="' . $css_classes . '" aria-hidden="true"></span>
396 <span class="il-counter"><span class="badge badge-notify il-counter-' . $type . '">42</span></span>
397 <span class="il-counter-spacer">42</span>
398 </a>';
399 $this->assertHTMLEquals($expected, $html);
400 }
401
402 public function test_render_withTwoCounters(): void
403 {
404 $fg = $this->getGlyphFactory();
405 $fc = $this->getCounterFactory();
406 $r = $this->getDefaultRenderer();
407 $c = $fg->mail("http://www.ilias.de")
408 ->withCounter($fc->novelty(42))
409 ->withCounter($fc->status(7));
410
411 $html = $this->normalizeHTML($r->render($c));
412
413 $css_classes = self::$canonical_css_classes[G\Glyph::MAIL];
414 $aria_label = self::$aria_labels[G\Glyph::MAIL];
415 $expected = "<a tabindex=\"0\" class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\">" .
416 "<span class=\"$css_classes\" aria-hidden=\"true\"></span>" .
417 "<span class=\"il-counter\"><span class=\"badge badge-notify il-counter-status\">7</span></span>" .
418 "<span class=\"il-counter\"><span class=\"badge badge-notify il-counter-novelty\">42</span></span>" .
419 "<span class=\"il-counter-spacer\">42</span>" .
420 "</a>";
421 $this->assertHTMLEquals($expected, $html);
422 }
423
424 public function test_dont_render_counter(): void
425 {
426 $this->expectException(LogicException::class);
427 $r = new Renderer(
428 $this->getUIFactory(),
429 $this->getTemplateFactory(),
430 $this->getLanguage(),
431 $this->getJavaScriptBinding(),
432 $this->getRefinery(),
434 $this->getDataFactory()
435 );
436 $f = $this->getCounterFactory();
437
438 $r->render($f->status(0), $this->getDefaultRenderer());
439 }
440
444 public function test_render_with_on_load_code(string $type): void
445 {
446 $f = $this->getGlyphFactory();
447 $r = $this->getDefaultRenderer();
448 $ids = array();
449 $c = $f->$type("http://www.ilias.de")
450 ->withOnLoadCode(function ($id) use (&$ids): string {
451 $ids[] = $id;
452 return "";
453 });
454
455 $html = $this->normalizeHTML($r->render($c));
456
457 $this->assertCount(1, $ids);
458
459 $css_classes = self::$canonical_css_classes[$type];
460 $aria_label = self::$aria_labels[$type];
461
462 $id = $ids[0];
463 $expected = "<a tabindex=\"0\" class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\" id=\"$id\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
464 $this->assertEquals($expected, $html);
465 }
466
470 public function test_render_with_action(string $type): void
471 {
472 $f = $this->getGlyphFactory();
473 $r = $this->getDefaultRenderer();
474 $c = $f->$type("http://www.ilias.de");
475 $c = $c->withAction("http://www.ilias.de/open-source-lms-ilias/");
476
477 $html = $this->normalizeHTML($r->render($c));
478
479 $css_classes = self::$canonical_css_classes[$type];
480 $aria_label = self::$aria_labels[$type];
481
482 $expected = "<a tabindex=\"0\" class=\"glyph\" href=\"http://www.ilias.de/open-source-lms-ilias/\" aria-label=\"$aria_label\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
483 $this->assertEquals($expected, $html);
484 }
485
486 public function testIsTabbable(): void
487 {
488 $f = $this->getGlyphFactory();
489 $r = $this->getDefaultRenderer();
490
491 // Glyph without Action or Signal
492 $c = $f->user();
493 $this->assertFalse($c->isTabbable());
494
495 // Glyph with Action
496 $c = $f->user()->withAction("#");
497 $this->assertTrue($c->isTabbable());
498
499 // Glyph with Signal
500 $c = $f->user()->withOnClick(new I\Signal("id_1", "click"));
501 $this->assertTrue($c->isTabbable());
502
503 // Glyph with Action and Signal
504 $c = $f->user()->withAction("#")->withOnClick(new I\Signal("id_1", "click"));
505 $this->assertTrue($c->isTabbable());
506
507 // Glyph with Action and Signal but Inactive
508 $c = $f->user()->withAction("#")->withOnClick(new I\Signal("id_1", "click"))->withUnavailableAction();
509 $this->assertFalse($c->isTabbable());
510 }
511
512 public function testTabbableGlyphRender(): void
513 {
514 $f = $this->getGlyphFactory();
515 $r = $this->getDefaultRenderer();
516
517 // Glyph without Action or Signal (not Tabbable)
518 $c = $f->user();
519 $expected = '<a class="glyph" aria-label="show_who_is_online"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></a>';
520 $html = $this->normalizeHTML($r->render($c));
521 $this->assertEquals($expected, $html);
522
523 // Glyph with Action (Tabbable)
524 $c = $f->user()->withAction("#");
525 $expected = '<a tabindex="0" class="glyph" href="#" aria-label="show_who_is_online"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></a>';
526 $html = $this->normalizeHTML($r->render($c));
527 $this->assertEquals($expected, $html);
528 }
529}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Test on glyph implementation.
Definition: GlyphTest.php:34
test_dont_render_counter()
Definition: GlyphTest.php:424
test_glyph_no_action(string $factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:181
test_render_with_unavailable_action(string $type)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:360
test_known_glyphs_only()
Definition: GlyphTest.php:316
testTabbableGlyphRender()
Definition: GlyphTest.php:512
test_render_withTwoCounters()
Definition: GlyphTest.php:402
glyph_type_provider()
Definition: GlyphTest.php:322
test_glyph_action(string $factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:169
test_render_with_on_load_code(string $type)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:444
getCounterFactory()
Definition: GlyphTest.php:40
test_with_highlight()
Definition: GlyphTest.php:203
test_only_two_counters()
Definition: GlyphTest.php:272
static array $canonical_css_classes
Definition: GlyphTest.php:45
counter_type_provider()
Definition: GlyphTest.php:331
test_render_simple(string $type)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:342
test_immutability_withCounter()
Definition: GlyphTest.php:301
getGlyphFactory()
Definition: GlyphTest.php:35
test_render_with_action(string $type)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:470
test_one_counter(string $counter_type)
@dataProvider counter_type_provider
Definition: GlyphTest.php:228
test_with_unavailable_action(string $factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:193
testIsTabbable()
Definition: GlyphTest.php:486
test_no_counter(string $factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:217
test_render_withCounter(string $type)
@dataProvider counter_type_provider
Definition: GlyphTest.php:381
test_two_counters()
Definition: GlyphTest.php:247
test_implements_factory_interface(string $factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:146
test_glyph_types(string $factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:157
static array $aria_labels
Definition: GlyphTest.php:94
Provides common functionality for UI tests.
Definition: Base.php:299
getJavaScriptBinding()
Definition: Base.php:330
normalizeHTML(string $html)
Definition: Base.php:422
getTemplateFactory()
Definition: Base.php:315
assertHTMLEquals(string $expected_html_as_string, string $html_as_string)
Definition: Base.php:427
brutallyTrimHTML(string $html)
A more radical version of normalizeHTML.
Definition: Base.php:444
getDefaultRenderer(JavaScriptBinding $js_binding=null, array $with_stub_renderings=[])
Definition: Base.php:355
$c
Definition: cli.php:38
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Counter.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Factory.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$type