ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 public function getGlyphFactory() {
15 return new \ILIAS\UI\Implementation\Component\Glyph\Factory();
16 }
17
18 public function getCounterFactory() {
19 return new \ILIAS\UI\Implementation\Component\Counter\Factory();
20 }
21
23 ( C\Glyph\Glyph::SETTINGS => "glyphicon glyphicon-cog"
24 , C\Glyph\Glyph::EXPAND => "glyphicon glyphicon-triangle-right"
25 , C\Glyph\Glyph::COLLAPSE => "glyphicon glyphicon-triangle-bottom"
26 , C\Glyph\Glyph::ADD => "glyphicon glyphicon-plus-sign"
27 , C\Glyph\Glyph::REMOVE => "glyphicon glyphicon-minus-sign"
28 , C\Glyph\Glyph::UP => "glyphicon glyphicon-circle-arrow-up"
29 , C\Glyph\Glyph::DOWN => "glyphicon glyphicon-circle-arrow-down"
30 , C\Glyph\Glyph::BACK => "glyphicon glyphicon-chevron-left"
31 , C\Glyph\Glyph::NEXT => "glyphicon glyphicon-chevron-right"
32 , C\Glyph\Glyph::SORT_ASCENDING => "glyphicon glyphicon-arrow-up"
33 , C\Glyph\Glyph::SORT_DESCENDING => "glyphicon glyphicon-arrow-down"
34 , C\Glyph\Glyph::USER => "glyphicon glyphicon-user"
35 , C\Glyph\Glyph::MAIL => "glyphicon glyphicon-envelope"
36 , C\Glyph\Glyph::NOTIFICATION => "glyphicon glyphicon-bell"
37 , C\Glyph\Glyph::TAG => "glyphicon glyphicon-tag"
38 , C\Glyph\Glyph::NOTE => "glyphicon glyphicon-pushpin"
39 , C\Glyph\Glyph::COMMENT => "glyphicon glyphicon-comment"
40 );
41
42 static $aria_labels = array(
43 C\Glyph\Glyph::SETTINGS => "settings"
44 , C\Glyph\Glyph::EXPAND => "expand_content"
45 , C\Glyph\Glyph::COLLAPSE => "collapse_content"
46 , C\Glyph\Glyph::ADD => "add"
47 , C\Glyph\Glyph::REMOVE => "remove"
48 , C\Glyph\Glyph::UP => "up"
49 , C\Glyph\Glyph::DOWN => "down"
50 , C\Glyph\Glyph::BACK => "back"
51 , C\Glyph\Glyph::NEXT => "next"
52 , C\Glyph\Glyph::SORT_ASCENDING => "sort_ascending"
53 , C\Glyph\Glyph::SORT_DESCENDING => "sort_descending"
54 , C\Glyph\Glyph::USER => "show_who_is_online"
55 , C\Glyph\Glyph::MAIL => "mail"
56 , C\Glyph\Glyph::NOTIFICATION => "notifications"
57 , C\Glyph\Glyph::TAG => "tags"
58 , C\Glyph\Glyph::NOTE => "notes"
59 , C\Glyph\Glyph::COMMENT => "comments"
60 );
61
65 public function test_implements_factory_interface($factory_method) {
66 $f = $this->getGlyphFactory();
67
68 $this->assertInstanceOf("ILIAS\\UI\\Component\\Glyph\\Factory", $f);
69 $this->assertInstanceOf("ILIAS\\UI\\Component\\Glyph\\Glyph", $f->$factory_method("http://www.ilias.de"));
70 }
71
75 public function test_glyph_types($factory_method) {
76 $f = $this->getGlyphFactory();
77 $g = $f->$factory_method();
78
79 $this->assertNotNull($g);
80 $this->assertEquals($factory_method, $g->getType());
81 }
82
86 public function test_glyph_action($factory_method) {
87 $f = $this->getGlyphFactory();
88 $g = $f->$factory_method("http://www.ilias.de");
89
90 $this->assertNotNull($g);
91 $this->assertEquals("http://www.ilias.de", $g->getAction());
92 }
93
97 public function test_glyph_no_action($factory_method) {
98 $f = $this->getGlyphFactory();
99 $g = $f->$factory_method();
100
101 $this->assertNotNull($g);
102 $this->assertEquals(null, $g->getAction());
103 }
104
108 public function test_with_highlight($counter_type) {
109 $gf = $this->getGlyphFactory();
110
111 $g = $gf
112 ->mail()
113 ;
114 $g2 = $g->withHighlight();
115
116 $this->assertFalse($g->isHighlighted());
117 $this->assertTrue($g2->isHighlighted());
118 }
119
123 public function test_no_counter($factory_method) {
124 $f = $this->getGlyphFactory();
125 $g = $f->$factory_method();
126
127 $this->assertCount(0, $g->getCounters());
128 }
129
133 public function test_one_counter($counter_type) {
134 $gf = $this->getGlyphFactory();
135 $cf = $this->getCounterFactory();
136 $number = 1;
137
138 $g = $gf
139 ->mail()
140 ->withCounter(
141 $cf->$counter_type($number)
142 );
143
144 $counters = $g->getCounters();
145 $this->assertCount(1, $counters);
146 $c = $counters[0];
147 $this->assertEquals($counter_type, $c->getType());
148 $this->assertEquals($number, $c->getNumber());
149 }
150
151 public function test_two_counters() {
152 $gf = $this->getGlyphFactory();
153 $cf = $this->getCounterFactory();
154 $number_s = 1;
155 $number_n = 2;
156
157 $g = $gf
158 ->mail()
159 ->withCounter(
160 $cf->status($number_s)
161 )
162 ->withCounter(
163 $cf->novelty($number_n)
164 );
165
166 $counters = $g->getCounters();
167 $this->assertCount(2, $counters);
168 $vals = array_map(function($c) {
169 return array($c->getType(), $c->getNumber());
170 }, $counters);
171 $this->assertContains(array("status", $number_s), $vals);
172 $this->assertContains(array("novelty", $number_n), $vals);
173 }
174
175 public function test_only_two_counters() {
176 $gf = $this->getGlyphFactory();
177 $cf = $this->getCounterFactory();
178 $number_s = 1;
179 $number_n1 = 2;
180 $number_n2 = 2;
181
182 $g = $gf
183 ->mail()
184 ->withCounter(
185 $cf->status($number_s)
186 )
187 ->withCounter(
188 $cf->novelty($number_n1)
189 )
190 ->withCounter(
191 $cf->novelty($number_n2)
192 );
193
194 $counters = $g->getCounters();
195 $this->assertCount(2, $counters);
196 $vals = array_map(function($c) {
197 return array($c->getType(), $c->getNumber());
198 }, $counters);
199 $this->assertContains(array("status", $number_s), $vals);
200 $this->assertContains(array("novelty", $number_n2), $vals);
201 }
202
204 $gf = $this->getGlyphFactory();
205 $cf = $this->getCounterFactory();
206
207 $g = $gf->mail();
208 $g2 = $g
209 ->withCounter(
210 $cf->novelty(0)
211 );
212
213 $counters = $g->getCounters();
214 $this->assertCount(0, $counters);
215 }
216
217 public function test_known_glyphs_only() {
218 try {
219 new \ILIAS\UI\Implementation\Component\Glyph\Glyph("FOO", "http://www.ilias.de");
220 $this->assertFalse("We should not get here");
221 }
222 catch (\InvalidArgumentException $e) {}
223 }
224
225 public function glyph_type_provider() {
226 return array
227 ( array(C\Glyph\Glyph::SETTINGS)
228 , array(C\Glyph\Glyph::EXPAND)
229 , array(C\Glyph\Glyph::COLLAPSE)
230 , array(C\Glyph\Glyph::ADD)
231 , array(C\Glyph\Glyph::REMOVE)
232 , array(C\Glyph\Glyph::UP)
233 , array(C\Glyph\Glyph::DOWN)
234 , array(C\Glyph\Glyph::BACK)
235 , array(C\Glyph\Glyph::NEXT)
236 , array(C\Glyph\Glyph::SORT_ASCENDING)
237 , array(C\Glyph\Glyph::SORT_DESCENDING)
238 , array(C\Glyph\Glyph::USER)
239 , array(C\Glyph\Glyph::MAIL)
240 , array(C\Glyph\Glyph::NOTIFICATION)
241 , array(C\Glyph\Glyph::TAG)
242 , array(C\Glyph\Glyph::NOTE)
243 , array(C\Glyph\Glyph::COMMENT)
244 );
245 }
246
247 public function counter_type_provider() {
248 return array
249 ( array("status")
250 , array("novelty")
251 );
252 }
253
257 public function test_render_simple($type) {
258 $f = $this->getGlyphFactory();
259 $r = $this->getDefaultRenderer();
260 $c = $f->$type("http://www.ilias.de");
261
262 $html = $this->normalizeHTML($r->render($c));
263
264 $css_classes = self::$canonical_css_classes[$type];
265 $aria_label = self::$aria_labels[$type];
266
267 $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
268 $this->assertEquals($expected, $html);
269 }
270
274 public function test_render_withCounter($type) {
275 $fg = $this->getGlyphFactory();
276 $fc = $this->getCounterFactory();
277 $r = $this->getDefaultRenderer();
278 $c = $fg->mail("http://www.ilias.de")->withCounter($fc->$type(42));
279
280 $html = $this->normalizeHTML($r->render($c));
281
282 $css_classes = self::$canonical_css_classes[C\Glyph\Glyph::MAIL];
283 $aria_label = self::$aria_labels[C\Glyph\Glyph::MAIL];
284
285 $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\">".
286 "<span class=\"$css_classes\" aria-hidden=\"true\"></span>".
287 "<span class=\"badge badge-notify il-counter-$type\">42</span>".
288 "<span class=\"il-counter-spacer\">42</span>".
289 "</a>";
290 $this->assertEquals($expected, $html);
291 }
292
293 public function test_render_withTwoCounters() {
294 $fg = $this->getGlyphFactory();
295 $fc = $this->getCounterFactory();
296 $r = $this->getDefaultRenderer();
297 $c = $fg->mail("http://www.ilias.de")
298 ->withCounter($fc->novelty(42))
299 ->withCounter($fc->status(7));
300
301 $html = $this->normalizeHTML($r->render($c));
302
303 $css_classes = self::$canonical_css_classes[C\Glyph\Glyph::MAIL];
304 $aria_label = self::$aria_labels[C\Glyph\Glyph::MAIL];
305 $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\">".
306 "<span class=\"$css_classes\" aria-hidden=\"true\"></span>".
307 "<span class=\"badge badge-notify il-counter-status\">7</span>".
308 "<span class=\"badge badge-notify il-counter-novelty\">42</span>".
309 "<span class=\"il-counter-spacer\">42</span>".
310 "</a>";
311 $this->assertEquals($expected, $html);
312 }
313
314 public function test_dont_render_counter() {
315 $r = new \ILIAS\UI\Implementation\Component\Glyph\Renderer($this->getUIFactory(), $this->getTemplateFactory(),$this->getLanguage(), $this->getJavaScriptBinding());
316 $f = $this->getCounterFactory();
317
318 try {
319 $r->render($f->status(0), $this->getDefaultRenderer());
320 $this->assertFalse("This should not happen!");
321 }
322 catch (\LogicException $e) {}
323 }
324
328 public function test_render_with_on_load_code($type) {
329 $f = $this->getGlyphFactory();
330 $r = $this->getDefaultRenderer();
331 $ids = array();
332 $c = $f->$type("http://www.ilias.de")
333 ->withOnLoadCode(function($id) use (&$ids) {
334 $ids[] = $id;
335 return "";
336 });
337
338 $html = $this->normalizeHTML($r->render($c));
339
340 $this->assertCount(1, $ids);
341
342 $css_classes = self::$canonical_css_classes[$type];
343 $aria_label = self::$aria_labels[$type];
344
345 $id = $ids[0];
346 $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\" id=\"$id\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
347 $this->assertEquals($expected, $html);
348 }
349}
An exception for terminatinating execution or to throw for unit testing.
Test on glyph implementation.
Definition: GlyphTest.php:13
test_dont_render_counter()
Definition: GlyphTest.php:314
test_render_with_on_load_code($type)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:328
test_render_simple($type)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:257
test_known_glyphs_only()
Definition: GlyphTest.php:217
test_with_highlight($counter_type)
@dataProvider counter_type_provider
Definition: GlyphTest.php:108
test_glyph_types($factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:75
static $canonical_css_classes
Definition: GlyphTest.php:22
test_no_counter($factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:123
test_render_withTwoCounters()
Definition: GlyphTest.php:293
glyph_type_provider()
Definition: GlyphTest.php:225
getCounterFactory()
Definition: GlyphTest.php:18
test_only_two_counters()
Definition: GlyphTest.php:175
test_one_counter($counter_type)
@dataProvider counter_type_provider
Definition: GlyphTest.php:133
counter_type_provider()
Definition: GlyphTest.php:247
test_render_withCounter($type)
@dataProvider counter_type_provider
Definition: GlyphTest.php:274
static $aria_labels
Definition: GlyphTest.php:42
test_immutability_withCounter()
Definition: GlyphTest.php:203
getGlyphFactory()
Definition: GlyphTest.php:14
test_implements_factory_interface($factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:65
test_glyph_no_action($factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:97
test_two_counters()
Definition: GlyphTest.php:151
test_glyph_action($factory_method)
@dataProvider glyph_type_provider
Definition: GlyphTest.php:86
Provides common functionality for UI tests.
Definition: Base.php:69
getJavaScriptBinding()
Definition: Base.php:96
getTemplateFactory()
Definition: Base.php:84
normalizeHTML($html)
Definition: Base.php:110
getDefaultRenderer()
Definition: Base.php:100
$html
Definition: example_001.php:87
$r
Definition: example_031.php:79