ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 as C;
9
14{
15 public function getGlyphFactory()
16 {
17 return new \ILIAS\UI\Implementation\Component\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( C\Glyph\Glyph::SETTINGS => "glyphicon glyphicon-cog"
26 , C\Glyph\Glyph::EXPAND => "glyphicon glyphicon-triangle-right"
27 , C\Glyph\Glyph::COLLAPSE => "glyphicon glyphicon-triangle-bottom"
28 , C\Glyph\Glyph::ADD => "glyphicon glyphicon-plus-sign"
29 , C\Glyph\Glyph::REMOVE => "glyphicon glyphicon-minus-sign"
30 , C\Glyph\Glyph::UP => "glyphicon glyphicon-circle-arrow-up"
31 , C\Glyph\Glyph::DOWN => "glyphicon glyphicon-circle-arrow-down"
32 , C\Glyph\Glyph::BACK => "glyphicon glyphicon-chevron-left"
33 , C\Glyph\Glyph::NEXT => "glyphicon glyphicon-chevron-right"
34 , C\Glyph\Glyph::SORT_ASCENDING => "glyphicon glyphicon-arrow-up"
35 , C\Glyph\Glyph::SORT_DESCENDING => "glyphicon glyphicon-arrow-down"
36 , C\Glyph\Glyph::USER => "glyphicon glyphicon-user"
37 , C\Glyph\Glyph::MAIL => "glyphicon glyphicon-envelope"
38 , C\Glyph\Glyph::NOTIFICATION => "glyphicon glyphicon-bell"
39 , C\Glyph\Glyph::TAG => "glyphicon glyphicon-tag"
40 , C\Glyph\Glyph::NOTE => "glyphicon glyphicon-pushpin"
41 , C\Glyph\Glyph::COMMENT => "glyphicon glyphicon-comment"
42 , C\Glyph\Glyph::LIKE => "glyphicon il-glyphicon-like"
43 , C\Glyph\Glyph::LOVE => "glyphicon il-glyphicon-love"
44 , C\Glyph\Glyph::DISLIKE => "glyphicon il-glyphicon-dislike"
45 , C\Glyph\Glyph::LAUGH => "glyphicon il-glyphicon-laugh"
46 , C\Glyph\Glyph::ASTOUNDED => "glyphicon il-glyphicon-astounded"
47 , C\Glyph\Glyph::SAD => "glyphicon il-glyphicon-sad"
48 , C\Glyph\Glyph::ANGRY => "glyphicon il-glyphicon-angry"
49 , C\Glyph\Glyph::ATTACHMENT => "glyphicon glyphicon-paperclip"
50 , C\Glyph\Glyph::RESET => "glyphicon glyphicon-repeat"
51 , C\Glyph\Glyph::APPLY => "glyphicon glyphicon-ok"
52 );
53
54 public static $aria_labels = array(
55 C\Glyph\Glyph::SETTINGS => "settings"
56 , C\Glyph\Glyph::EXPAND => "expand_content"
57 , C\Glyph\Glyph::COLLAPSE => "collapse_content"
58 , C\Glyph\Glyph::ADD => "add"
59 , C\Glyph\Glyph::REMOVE => "remove"
60 , C\Glyph\Glyph::UP => "up"
61 , C\Glyph\Glyph::DOWN => "down"
62 , C\Glyph\Glyph::BACK => "back"
63 , C\Glyph\Glyph::NEXT => "next"
64 , C\Glyph\Glyph::SORT_ASCENDING => "sort_ascending"
65 , C\Glyph\Glyph::SORT_DESCENDING => "sort_descending"
66 , C\Glyph\Glyph::USER => "show_who_is_online"
67 , C\Glyph\Glyph::MAIL => "mail"
68 , C\Glyph\Glyph::NOTIFICATION => "notifications"
69 , C\Glyph\Glyph::TAG => "tags"
70 , C\Glyph\Glyph::NOTE => "notes"
71 , C\Glyph\Glyph::COMMENT => "comments"
72 , C\Glyph\Glyph::LIKE => "like"
73 , C\Glyph\Glyph::LOVE => "love"
74 , C\Glyph\Glyph::DISLIKE => "dislike"
75 , C\Glyph\Glyph::LAUGH => "laugh"
76 , C\Glyph\Glyph::ASTOUNDED => "astounded"
77 , C\Glyph\Glyph::SAD => "sad"
78 , C\Glyph\Glyph::ANGRY => "angry"
79 , C\Glyph\Glyph::ATTACHMENT => "attachment"
80 , C\Glyph\Glyph::RESET => "reset"
81 , C\Glyph\Glyph::APPLY => "apply"
82 );
83
87 public function test_implements_factory_interface($factory_method)
88 {
89 $f = $this->getGlyphFactory();
90
91 $this->assertInstanceOf("ILIAS\\UI\\Component\\Glyph\\Factory", $f);
92 $this->assertInstanceOf("ILIAS\\UI\\Component\\Glyph\\Glyph", $f->$factory_method("http://www.ilias.de"));
93 }
94
98 public function test_glyph_types($factory_method)
99 {
100 $f = $this->getGlyphFactory();
101 $g = $f->$factory_method();
102
103 $this->assertNotNull($g);
104 $this->assertEquals($factory_method, $g->getType());
105 }
106
110 public function test_glyph_action($factory_method)
111 {
112 $f = $this->getGlyphFactory();
113 $g = $f->$factory_method("http://www.ilias.de");
114
115 $this->assertNotNull($g);
116 $this->assertEquals("http://www.ilias.de", $g->getAction());
117 }
118
122 public function test_glyph_no_action($factory_method)
123 {
124 $f = $this->getGlyphFactory();
125 $g = $f->$factory_method();
126
127 $this->assertNotNull($g);
128 $this->assertEquals(null, $g->getAction());
129 }
130
134 public function test_with_unavailable_action($factory_method)
135 {
136 $f = $this->getGlyphFactory();
137 $g = $f->$factory_method();
138 $g2 = $f->$factory_method()->withUnavailableAction();
139
140 $this->assertTrue($g->isActive());
141 $this->assertFalse($g2->isActive());
142 }
143
147 public function test_with_highlight($counter_type)
148 {
149 $gf = $this->getGlyphFactory();
150
151 $g = $gf
152 ->mail()
153 ;
154 $g2 = $g->withHighlight();
155
156 $this->assertFalse($g->isHighlighted());
157 $this->assertTrue($g2->isHighlighted());
158 }
159
163 public function test_no_counter($factory_method)
164 {
165 $f = $this->getGlyphFactory();
166 $g = $f->$factory_method();
167
168 $this->assertCount(0, $g->getCounters());
169 }
170
174 public function test_one_counter($counter_type)
175 {
176 $gf = $this->getGlyphFactory();
177 $cf = $this->getCounterFactory();
178 $number = 1;
179
180 $g = $gf
181 ->mail()
182 ->withCounter(
183 $cf->$counter_type($number)
184 );
185
186 $counters = $g->getCounters();
187 $this->assertCount(1, $counters);
188 $c = $counters[0];
189 $this->assertEquals($counter_type, $c->getType());
190 $this->assertEquals($number, $c->getNumber());
191 }
192
193 public function test_two_counters()
194 {
195 $gf = $this->getGlyphFactory();
196 $cf = $this->getCounterFactory();
197 $number_s = 1;
198 $number_n = 2;
199
200 $g = $gf
201 ->mail()
202 ->withCounter(
203 $cf->status($number_s)
204 )
205 ->withCounter(
206 $cf->novelty($number_n)
207 );
208
209 $counters = $g->getCounters();
210 $this->assertCount(2, $counters);
211 $vals = array_map(function ($c) {
212 return array($c->getType(), $c->getNumber());
213 }, $counters);
214 $this->assertContains(array("status", $number_s), $vals);
215 $this->assertContains(array("novelty", $number_n), $vals);
216 }
217
218 public function test_only_two_counters()
219 {
220 $gf = $this->getGlyphFactory();
221 $cf = $this->getCounterFactory();
222 $number_s = 1;
223 $number_n1 = 2;
224 $number_n2 = 2;
225
226 $g = $gf
227 ->mail()
228 ->withCounter(
229 $cf->status($number_s)
230 )
231 ->withCounter(
232 $cf->novelty($number_n1)
233 )
234 ->withCounter(
235 $cf->novelty($number_n2)
236 );
237
238 $counters = $g->getCounters();
239 $this->assertCount(2, $counters);
240 $vals = array_map(function ($c) {
241 return array($c->getType(), $c->getNumber());
242 }, $counters);
243 $this->assertContains(array("status", $number_s), $vals);
244 $this->assertContains(array("novelty", $number_n2), $vals);
245 }
246
248 {
249 $gf = $this->getGlyphFactory();
250 $cf = $this->getCounterFactory();
251
252 $g = $gf->mail();
253 $g2 = $g
254 ->withCounter(
255 $cf->novelty(0)
256 );
257
258 $counters = $g->getCounters();
259 $this->assertCount(0, $counters);
260 }
261
262 public function test_known_glyphs_only()
263 {
264 try {
265 new \ILIAS\UI\Implementation\Component\Glyph\Glyph("FOO", "http://www.ilias.de");
266 $this->assertFalse("We should not get here");
267 } catch (\InvalidArgumentException $e) {
268 }
269 }
270
271 public function glyph_type_provider()
272 {
273 return array( array(C\Glyph\Glyph::SETTINGS)
274 , array(C\Glyph\Glyph::EXPAND)
275 , array(C\Glyph\Glyph::COLLAPSE)
276 , array(C\Glyph\Glyph::ADD)
277 , array(C\Glyph\Glyph::REMOVE)
278 , array(C\Glyph\Glyph::UP)
279 , array(C\Glyph\Glyph::DOWN)
280 , array(C\Glyph\Glyph::BACK)
281 , array(C\Glyph\Glyph::NEXT)
282 , array(C\Glyph\Glyph::SORT_ASCENDING)
283 , array(C\Glyph\Glyph::SORT_DESCENDING)
284 , array(C\Glyph\Glyph::USER)
285 , array(C\Glyph\Glyph::MAIL)
286 , array(C\Glyph\Glyph::NOTIFICATION)
287 , array(C\Glyph\Glyph::TAG)
288 , array(C\Glyph\Glyph::NOTE)
289 , array(C\Glyph\Glyph::COMMENT)
290 , array(C\Glyph\Glyph::LIKE)
291 , array(C\Glyph\Glyph::LOVE)
292 , array(C\Glyph\Glyph::DISLIKE)
293 , array(C\Glyph\Glyph::LAUGH)
294 , array(C\Glyph\Glyph::ASTOUNDED)
295 , array(C\Glyph\Glyph::SAD)
296 , array(C\Glyph\Glyph::ANGRY)
297 , array(C\Glyph\Glyph::ATTACHMENT)
298 , array(C\Glyph\Glyph::RESET)
299 , array(C\Glyph\Glyph::APPLY)
300 );
301 }
302
303 public function counter_type_provider()
304 {
305 return array( array("status")
306 , array("novelty")
307 );
308 }
309
313 public function test_render_simple($type)
314 {
315 $f = $this->getGlyphFactory();
316 $r = $this->getDefaultRenderer();
317 $c = $f->$type("http://www.ilias.de");
318
319 $html = $this->normalizeHTML($r->render($c));
320
321 $css_classes = self::$canonical_css_classes[$type];
322 $aria_label = self::$aria_labels[$type];
323
324 $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
325 $this->assertEquals($expected, $html);
326 }
327
332 {
333 $f = $this->getGlyphFactory();
334 $r = $this->getDefaultRenderer();
335 $c = $f->$type("http://www.ilias.de")->withUnavailableAction();
336
337 $html = $this->normalizeHTML($r->render($c));
338
339 $css_classes = self::$canonical_css_classes[$type];
340 $aria_label = self::$aria_labels[$type];
341
342 $expected = "<a class=\"glyph disabled\" aria-label=\"$aria_label\" " .
343 "aria-disabled=\"true\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
344 $this->assertEquals($expected, $html);
345 }
346
351 {
352 $fg = $this->getGlyphFactory();
353 $fc = $this->getCounterFactory();
354 $r = $this->getDefaultRenderer();
355 $c = $fg->mail("http://www.ilias.de")->withCounter($fc->$type(42));
356
357 $html = $this->normalizeHTML($r->render($c));
358
359 $css_classes = self::$canonical_css_classes[C\Glyph\Glyph::MAIL];
360 $aria_label = self::$aria_labels[C\Glyph\Glyph::MAIL];
361
362 $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\">" .
363 "<span class=\"$css_classes\" aria-hidden=\"true\"></span>" .
364 "<span class=\"badge badge-notify il-counter-$type\">42</span>" .
365 "<span class=\"il-counter-spacer\">42</span>" .
366 "</a>";
367 $this->assertEquals($expected, $html);
368 }
369
371 {
372 $fg = $this->getGlyphFactory();
373 $fc = $this->getCounterFactory();
374 $r = $this->getDefaultRenderer();
375 $c = $fg->mail("http://www.ilias.de")
376 ->withCounter($fc->novelty(42))
377 ->withCounter($fc->status(7));
378
379 $html = $this->normalizeHTML($r->render($c));
380
381 $css_classes = self::$canonical_css_classes[C\Glyph\Glyph::MAIL];
382 $aria_label = self::$aria_labels[C\Glyph\Glyph::MAIL];
383 $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\">" .
384 "<span class=\"$css_classes\" aria-hidden=\"true\"></span>" .
385 "<span class=\"badge badge-notify il-counter-status\">7</span>" .
386 "<span class=\"badge badge-notify il-counter-novelty\">42</span>" .
387 "<span class=\"il-counter-spacer\">42</span>" .
388 "</a>";
389 $this->assertEquals($expected, $html);
390 }
391
392 public function test_dont_render_counter()
393 {
394 $r = new \ILIAS\UI\Implementation\Component\Glyph\Renderer($this->getUIFactory(), $this->getTemplateFactory(), $this->getLanguage(), $this->getJavaScriptBinding());
395 $f = $this->getCounterFactory();
396
397 try {
398 $r->render($f->status(0), $this->getDefaultRenderer());
399 $this->assertFalse("This should not happen!");
400 } catch (\LogicException $e) {
401 }
402 }
403
408 {
409 $f = $this->getGlyphFactory();
410 $r = $this->getDefaultRenderer();
411 $ids = array();
412 $c = $f->$type("http://www.ilias.de")
413 ->withOnLoadCode(function ($id) use (&$ids) {
414 $ids[] = $id;
415 return "";
416 });
417
418 $html = $this->normalizeHTML($r->render($c));
419
420 $this->assertCount(1, $ids);
421
422 $css_classes = self::$canonical_css_classes[$type];
423 $aria_label = self::$aria_labels[$type];
424
425 $id = $ids[0];
426 $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\" id=\"$id\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
427 $this->assertEquals($expected, $html);
428 }
429}
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:392
test_render_with_on_load_code($type)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:407
test_render_simple($type)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:313
test_render_with_unavailable_action($type)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:331
test_known_glyphs_only()
Definition: GlyphTest.php:262
test_with_highlight($counter_type)
@dataProvider counter_type_provider
Definition: GlyphTest.php:147
test_glyph_types($factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:98
static $canonical_css_classes
Definition: GlyphTest.php:25
test_no_counter($factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:163
test_render_withTwoCounters()
Definition: GlyphTest.php:370
test_with_unavailable_action($factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:134
glyph_type_provider()
Definition: GlyphTest.php:271
getCounterFactory()
Definition: GlyphTest.php:20
test_only_two_counters()
Definition: GlyphTest.php:218
test_one_counter($counter_type)
@dataProvider counter_type_provider
Definition: GlyphTest.php:174
counter_type_provider()
Definition: GlyphTest.php:303
test_render_withCounter($type)
@dataProvider counter_type_provider
Definition: GlyphTest.php:350
static $aria_labels
Definition: GlyphTest.php:54
test_immutability_withCounter()
Definition: GlyphTest.php:247
getGlyphFactory()
Definition: GlyphTest.php:15
test_implements_factory_interface($factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:87
test_glyph_no_action($factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:122
test_two_counters()
Definition: GlyphTest.php:193
test_glyph_action($factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:110
Provides common functionality for UI tests.
Definition: Base.php:192
getJavaScriptBinding()
Definition: Base.php:223
getTemplateFactory()
Definition: Base.php:208
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:228
normalizeHTML($html)
Definition: Base.php:261
$html
Definition: example_001.php:87
$r
Definition: example_031.php:79
if(!array_key_exists('StateId', $_REQUEST)) $id
$type