ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 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  );
43 
44  public static $aria_labels = array(
45  C\Glyph\Glyph::SETTINGS => "settings"
46  , C\Glyph\Glyph::EXPAND => "expand_content"
47  , C\Glyph\Glyph::COLLAPSE => "collapse_content"
48  , C\Glyph\Glyph::ADD => "add"
49  , C\Glyph\Glyph::REMOVE => "remove"
50  , C\Glyph\Glyph::UP => "up"
51  , C\Glyph\Glyph::DOWN => "down"
52  , C\Glyph\Glyph::BACK => "back"
53  , C\Glyph\Glyph::NEXT => "next"
54  , C\Glyph\Glyph::SORT_ASCENDING => "sort_ascending"
55  , C\Glyph\Glyph::SORT_DESCENDING => "sort_descending"
56  , C\Glyph\Glyph::USER => "show_who_is_online"
57  , C\Glyph\Glyph::MAIL => "mail"
58  , C\Glyph\Glyph::NOTIFICATION => "notifications"
59  , C\Glyph\Glyph::TAG => "tags"
60  , C\Glyph\Glyph::NOTE => "notes"
61  , C\Glyph\Glyph::COMMENT => "comments"
62  );
63 
67  public function test_implements_factory_interface($factory_method)
68  {
69  $f = $this->getGlyphFactory();
70 
71  $this->assertInstanceOf("ILIAS\\UI\\Component\\Glyph\\Factory", $f);
72  $this->assertInstanceOf("ILIAS\\UI\\Component\\Glyph\\Glyph", $f->$factory_method("http://www.ilias.de"));
73  }
74 
78  public function test_glyph_types($factory_method)
79  {
80  $f = $this->getGlyphFactory();
81  $g = $f->$factory_method();
82 
83  $this->assertNotNull($g);
84  $this->assertEquals($factory_method, $g->getType());
85  }
86 
90  public function test_glyph_action($factory_method)
91  {
92  $f = $this->getGlyphFactory();
93  $g = $f->$factory_method("http://www.ilias.de");
94 
95  $this->assertNotNull($g);
96  $this->assertEquals("http://www.ilias.de", $g->getAction());
97  }
98 
102  public function test_glyph_no_action($factory_method)
103  {
104  $f = $this->getGlyphFactory();
105  $g = $f->$factory_method();
106 
107  $this->assertNotNull($g);
108  $this->assertEquals(null, $g->getAction());
109  }
110 
114  public function test_with_highlight($counter_type)
115  {
116  $gf = $this->getGlyphFactory();
117 
118  $g = $gf
119  ->mail()
120  ;
121  $g2 = $g->withHighlight();
122 
123  $this->assertFalse($g->isHighlighted());
124  $this->assertTrue($g2->isHighlighted());
125  }
126 
130  public function test_no_counter($factory_method)
131  {
132  $f = $this->getGlyphFactory();
133  $g = $f->$factory_method();
134 
135  $this->assertCount(0, $g->getCounters());
136  }
137 
141  public function test_one_counter($counter_type)
142  {
143  $gf = $this->getGlyphFactory();
144  $cf = $this->getCounterFactory();
145  $number = 1;
146 
147  $g = $gf
148  ->mail()
149  ->withCounter(
150  $cf->$counter_type($number)
151  );
152 
153  $counters = $g->getCounters();
154  $this->assertCount(1, $counters);
155  $c = $counters[0];
156  $this->assertEquals($counter_type, $c->getType());
157  $this->assertEquals($number, $c->getNumber());
158  }
159 
160  public function test_two_counters()
161  {
162  $gf = $this->getGlyphFactory();
163  $cf = $this->getCounterFactory();
164  $number_s = 1;
165  $number_n = 2;
166 
167  $g = $gf
168  ->mail()
169  ->withCounter(
170  $cf->status($number_s)
171  )
172  ->withCounter(
173  $cf->novelty($number_n)
174  );
175 
176  $counters = $g->getCounters();
177  $this->assertCount(2, $counters);
178  $vals = array_map(function ($c) {
179  return array($c->getType(), $c->getNumber());
180  }, $counters);
181  $this->assertContains(array("status", $number_s), $vals);
182  $this->assertContains(array("novelty", $number_n), $vals);
183  }
184 
185  public function test_only_two_counters()
186  {
187  $gf = $this->getGlyphFactory();
188  $cf = $this->getCounterFactory();
189  $number_s = 1;
190  $number_n1 = 2;
191  $number_n2 = 2;
192 
193  $g = $gf
194  ->mail()
195  ->withCounter(
196  $cf->status($number_s)
197  )
198  ->withCounter(
199  $cf->novelty($number_n1)
200  )
201  ->withCounter(
202  $cf->novelty($number_n2)
203  );
204 
205  $counters = $g->getCounters();
206  $this->assertCount(2, $counters);
207  $vals = array_map(function ($c) {
208  return array($c->getType(), $c->getNumber());
209  }, $counters);
210  $this->assertContains(array("status", $number_s), $vals);
211  $this->assertContains(array("novelty", $number_n2), $vals);
212  }
213 
215  {
216  $gf = $this->getGlyphFactory();
217  $cf = $this->getCounterFactory();
218 
219  $g = $gf->mail();
220  $g2 = $g
221  ->withCounter(
222  $cf->novelty(0)
223  );
224 
225  $counters = $g->getCounters();
226  $this->assertCount(0, $counters);
227  }
228 
229  public function test_known_glyphs_only()
230  {
231  try {
232  new \ILIAS\UI\Implementation\Component\Glyph\Glyph("FOO", "http://www.ilias.de");
233  $this->assertFalse("We should not get here");
234  } catch (\InvalidArgumentException $e) {
235  }
236  }
237 
238  public function glyph_type_provider()
239  {
240  return array( array(C\Glyph\Glyph::SETTINGS)
241  , array(C\Glyph\Glyph::EXPAND)
242  , array(C\Glyph\Glyph::COLLAPSE)
243  , array(C\Glyph\Glyph::ADD)
244  , array(C\Glyph\Glyph::REMOVE)
245  , array(C\Glyph\Glyph::UP)
246  , array(C\Glyph\Glyph::DOWN)
247  , array(C\Glyph\Glyph::BACK)
248  , array(C\Glyph\Glyph::NEXT)
249  , array(C\Glyph\Glyph::SORT_ASCENDING)
250  , array(C\Glyph\Glyph::SORT_DESCENDING)
251  , array(C\Glyph\Glyph::USER)
252  , array(C\Glyph\Glyph::MAIL)
253  , array(C\Glyph\Glyph::NOTIFICATION)
254  , array(C\Glyph\Glyph::TAG)
255  , array(C\Glyph\Glyph::NOTE)
256  , array(C\Glyph\Glyph::COMMENT)
257  );
258  }
259 
260  public function counter_type_provider()
261  {
262  return array( array("status")
263  , array("novelty")
264  );
265  }
266 
270  public function test_render_simple($type)
271  {
272  $f = $this->getGlyphFactory();
273  $r = $this->getDefaultRenderer();
274  $c = $f->$type("http://www.ilias.de");
275 
276  $html = $this->normalizeHTML($r->render($c));
277 
278  $css_classes = self::$canonical_css_classes[$type];
279  $aria_label = self::$aria_labels[$type];
280 
281  $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
282  $this->assertEquals($expected, $html);
283  }
284 
289  {
290  $fg = $this->getGlyphFactory();
291  $fc = $this->getCounterFactory();
292  $r = $this->getDefaultRenderer();
293  $c = $fg->mail("http://www.ilias.de")->withCounter($fc->$type(42));
294 
295  $html = $this->normalizeHTML($r->render($c));
296 
297  $css_classes = self::$canonical_css_classes[C\Glyph\Glyph::MAIL];
298  $aria_label = self::$aria_labels[C\Glyph\Glyph::MAIL];
299 
300  $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\">" .
301  "<span class=\"$css_classes\" aria-hidden=\"true\"></span>" .
302  "<span class=\"badge badge-notify il-counter-$type\">42</span>" .
303  "<span class=\"il-counter-spacer\">42</span>" .
304  "</a>";
305  $this->assertEquals($expected, $html);
306  }
307 
308  public function test_render_withTwoCounters()
309  {
310  $fg = $this->getGlyphFactory();
311  $fc = $this->getCounterFactory();
312  $r = $this->getDefaultRenderer();
313  $c = $fg->mail("http://www.ilias.de")
314  ->withCounter($fc->novelty(42))
315  ->withCounter($fc->status(7));
316 
317  $html = $this->normalizeHTML($r->render($c));
318 
319  $css_classes = self::$canonical_css_classes[C\Glyph\Glyph::MAIL];
320  $aria_label = self::$aria_labels[C\Glyph\Glyph::MAIL];
321  $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\">" .
322  "<span class=\"$css_classes\" aria-hidden=\"true\"></span>" .
323  "<span class=\"badge badge-notify il-counter-status\">7</span>" .
324  "<span class=\"badge badge-notify il-counter-novelty\">42</span>" .
325  "<span class=\"il-counter-spacer\">42</span>" .
326  "</a>";
327  $this->assertEquals($expected, $html);
328  }
329 
330  public function test_dont_render_counter()
331  {
332  $r = new \ILIAS\UI\Implementation\Component\Glyph\Renderer($this->getUIFactory(), $this->getTemplateFactory(), $this->getLanguage(), $this->getJavaScriptBinding());
333  $f = $this->getCounterFactory();
334 
335  try {
336  $r->render($f->status(0), $this->getDefaultRenderer());
337  $this->assertFalse("This should not happen!");
338  } catch (\LogicException $e) {
339  }
340  }
341 
346  {
347  $f = $this->getGlyphFactory();
348  $r = $this->getDefaultRenderer();
349  $ids = array();
350  $c = $f->$type("http://www.ilias.de")
351  ->withOnLoadCode(function ($id) use (&$ids) {
352  $ids[] = $id;
353  return "";
354  });
355 
356  $html = $this->normalizeHTML($r->render($c));
357 
358  $this->assertCount(1, $ids);
359 
360  $css_classes = self::$canonical_css_classes[$type];
361  $aria_label = self::$aria_labels[$type];
362 
363  $id = $ids[0];
364  $expected = "<a class=\"glyph\" href=\"http://www.ilias.de\" aria-label=\"$aria_label\" id=\"$id\"><span class=\"$css_classes\" aria-hidden=\"true\"></span></a>";
365  $this->assertEquals($expected, $html);
366  }
367 }
test_glyph_no_action($factory_method)
glyph_type_provider
Definition: GlyphTest.php:102
$type
test_render_simple($type)
glyph_type_provider
Definition: GlyphTest.php:270
counter_type_provider()
Definition: GlyphTest.php:260
test_dont_render_counter()
Definition: GlyphTest.php:330
glyph_type_provider()
Definition: GlyphTest.php:238
if(!array_key_exists('StateId', $_REQUEST)) $id
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:216
normalizeHTML($html)
Definition: Base.php:243
test_with_highlight($counter_type)
counter_type_provider
Definition: GlyphTest.php:114
test_render_withCounter($type)
counter_type_provider
Definition: GlyphTest.php:288
test_two_counters()
Definition: GlyphTest.php:160
test_implements_factory_interface($factory_method)
glyph_type_provider
Definition: GlyphTest.php:67
getJavaScriptBinding()
Definition: Base.php:211
test_known_glyphs_only()
Definition: GlyphTest.php:229
$r
Definition: example_031.php:79
Provides common functionality for UI tests.
Definition: Base.php:177
test_one_counter($counter_type)
counter_type_provider
Definition: GlyphTest.php:141
test_glyph_types($factory_method)
glyph_type_provider
Definition: GlyphTest.php:78
getCounterFactory()
Definition: GlyphTest.php:20
Create styles array
The data for the language used.
test_no_counter($factory_method)
glyph_type_provider
Definition: GlyphTest.php:130
static $canonical_css_classes
Definition: GlyphTest.php:25
test_render_with_on_load_code($type)
glyph_type_provider
Definition: GlyphTest.php:345
getTemplateFactory()
Definition: Base.php:196
test_glyph_action($factory_method)
glyph_type_provider
Definition: GlyphTest.php:90
test_render_withTwoCounters()
Definition: GlyphTest.php:308
test_immutability_withCounter()
Definition: GlyphTest.php:214
test_only_two_counters()
Definition: GlyphTest.php:185
static $aria_labels
Definition: GlyphTest.php:44
Test on glyph implementation.
Definition: GlyphTest.php:13
$html
Definition: example_001.php:87
getGlyphFactory()
Definition: GlyphTest.php:15