ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
AvatarTest.php
Go to the documentation of this file.
1 <?php
2 
3 require_once("libs/composer/vendor/autoload.php");
4 require_once(__DIR__ . "/../../../Base.php");
5 
8 
13 {
14  protected const ICON_PATH = __DIR__ . "/../../../../../templates/default/images/";
15 
16  private function getAvatarFactory() : Factory
17  {
18  return new I\Component\Symbol\Avatar\Factory();
19  }
20 
21  public function testConstruction() : void
22  {
23  $f = $this->getAvatarFactory();
24  $this->assertInstanceOf("ILIAS\\UI\\Component\\Symbol\\Avatar\\Factory", $f);
25 
26  $le = $f->letter('ru');
27  $this->assertInstanceOf("ILIAS\\UI\\Component\\Symbol\\Avatar\\Letter", $le);
28 
29  $ci = $f->picture(self::ICON_PATH . 'no_photo_xsmall.jpg', 'ru');
30  $this->assertInstanceOf("ILIAS\\UI\\Component\\Symbol\\Avatar\\Picture", $ci);
31  }
32 
33  public function testAbbreviation() : void
34  {
35  $f = $this->getAvatarFactory();
36 
37  $this->assertEquals('ro', $f->letter('ro')->getAbbreviation());
38  $this->assertEquals('ro', $f->letter('root')->getAbbreviation());
39  $this->assertEquals('Ro', $f->letter('Root')->getAbbreviation());
40  $this->assertEquals('RO', $f->letter('ROOT')->getAbbreviation());
41  }
42 
43  public function testUsername() : void
44  {
45  $f = $this->getAvatarFactory();
46 
47  $this->assertEquals('ro', $f->letter('ro')->getUsername());
48  $this->assertEquals('ro', $f->picture('', 'ro')->getUsername());
49  $this->assertEquals('root', $f->letter('root')->getUsername());
50  $this->assertEquals('root', $f->picture('', 'root')->getUsername());
51  $this->assertEquals('Root', $f->letter('Root')->getUsername());
52  $this->assertEquals('Root', $f->picture('', 'Root')->getUsername());
53  $this->assertEquals('ROOT', $f->letter('ROOT')->getUsername());
54  $this->assertEquals('ROOT', $f->picture('', 'ROOT')->getUsername());
55  }
56 
57  public function testPicturePath() : void
58  {
59  $f = $this->getAvatarFactory();
60 
61  $str = '/path/to/picture.jpg';
62  $this->assertEquals($str, $f->picture($str, 'ro')->getPicturePath());
63  }
64 
65  public function testColorVariant() : void
66  {
67  $f = $this->getAvatarFactory();
68 
69  // Test all 26 colors
70  $variants = array(
71  1 => 'om',
72  2 => 'gk',
73  3 => 'bj',
74  4 => 'ea',
75  5 => 'mf',
76  6 => 'ob',
77  7 => 'bi',
78  8 => 'hu',
79  9 => 'fa',
80  10 => 'so',
81  11 => 'il',
82  12 => 'ut',
83  13 => 'ur',
84  14 => 'lt',
85  15 => 'kg',
86  16 => 'jl',
87  17 => 'qb',
88  18 => 'rq',
89  19 => 'ot',
90  20 => 'cq',
91  21 => 'rm',
92  22 => 'aj',
93  23 => 'li',
94  24 => 'er',
95  25 => 'ui',
96  26 => 'mi',
97  );
98  foreach ($variants as $color => $variant) {
99  $this->assertEquals($color, $f->letter($variant)->getBackgroundColorVariant());
100  }
101  }
102 
103  public function testCrc32()
104  {
105  // test mechanism (crc32)
106  $f = $this->getAvatarFactory();
107  $number_of_colors = 26;
108  $abb = 'ru';
109 
110  $calculated_color_variant = (crc32($abb) % $number_of_colors) + 1; // plus 1 since colors start with 1
111  $this->assertEquals($calculated_color_variant, $f->letter($abb)->getBackgroundColorVariant());
112 
113  // test with random abbreviations (dynamically generated)
114 
115  foreach ($this->getRandom26StringsForAllColorVariants() as $color => $variant) {
116  $this->assertEquals($color, $f->letter($variant)->getBackgroundColorVariant());
117  }
118  }
119 
120  public function testRenderingLetter()
121  {
122  $f = $this->getAvatarFactory();
123  $r = $this->getDefaultRenderer();
124 
125  $letter = $f->letter('ro');
126  $html = $this->normalizeHTML($r->render($letter));
127  $expected = '<div class="il-avatar il-avatar-letter il-avatar-size-large il-avatar-letter-color-1" aria-label="ro"> <span class="abbreviation">ro</span></div>';
128  $this->assertEquals($expected, $html);
129  }
130 
131  public function testRenderingPicture()
132  {
133  $f = $this->getAvatarFactory();
134  $r = $this->getDefaultRenderer();
135 
136  $str = '/path/to/picture.jpg';
137  $letter = $f->picture($str, 'ro');
138  $html = $this->normalizeHTML($r->render($letter));
139  $expected = '<div class="il-avatar il-avatar-picture il-avatar-size-large" aria-label="ro"> <img src="/path/to/picture.jpg"/></div>';
140  $this->assertEquals($expected, $html);
141  }
142 
148  public function getRandom26StringsForAllColorVariants(int $color_variants = 26, int $length = 10) : Generator
149  {
150  $sh = static function ($length = 10) {
151  return substr(str_shuffle(str_repeat($x = 'abcdefghijklmnopqrstuvwxyz', (int) ceil($length / strlen($x)))), 1, $length);
152  };
153 
154  $strings = [];
155  $running = true;
156  while ($running) {
157  $str = $sh($length);
158  $probe = crc32($str);
159  $i = ($probe % $color_variants) + 1;
160  if (!in_array($i, $strings, true)) {
161  $strings[$i] = $str;
162  yield $i => $str;
163  }
164  if (count($strings) === $color_variants) {
165  $running = false;
166  }
167  }
168  }
169 }
const ICON_PATH
Definition: AvatarTest.php:14
testRenderingPicture()
Definition: AvatarTest.php:131
getRandom26StringsForAllColorVariants(int $color_variants=26, int $length=10)
Definition: AvatarTest.php:148
testPicturePath()
Definition: AvatarTest.php:57
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:268
normalizeHTML($html)
Definition: Base.php:317
testRenderingLetter()
Definition: AvatarTest.php:120
testConstruction()
Definition: AvatarTest.php:21
getAvatarFactory()
Definition: AvatarTest.php:16
Provides common functionality for UI tests.
Definition: Base.php:224
testAbbreviation()
Definition: AvatarTest.php:33
testColorVariant()
Definition: AvatarTest.php:65
Test on avatar implementation.
Definition: AvatarTest.php:12
$i
Definition: metadata.php:24