ILIAS  release_7 Revision v7.30-3-g800a261c036
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 testAlternativeText() : void
121  {
122  $f = $this->getAvatarFactory();
123  $this->assertEquals("", $f->picture('', 'ro')->getAlternativeText());
124  $this->assertEquals("", $f->letter('', 'ro')->getAlternativeText());
125  $this->assertEquals("alternative", $f->picture('', 'ro')
126  ->withAlternativeText("alternative")
127  ->getAlternativeText());
128  $this->assertEquals("alternative", $f->letter('', 'ro')
129  ->withAlternativeText("alternative")
130  ->getAlternativeText());
131  }
132 
133  public function testRenderingLetter()
134  {
135  $f = $this->getAvatarFactory();
136  $r = $this->getDefaultRenderer();
137 
138  $letter = $f->letter('ro');
139  $html = $this->brutallyTrimHTML($r->render($letter));
140  $expected = $this->brutallyTrimHTML('
141 <span class="il-avatar il-avatar-letter il-avatar-size-large il-avatar-letter-color-1" aria-label="user_avatar" role="img">
142  <span class="abbreviation">ro</span>
143 </span>');
144  $this->assertEquals($expected, $html);
145  }
146 
147  public function testRenderingPicture()
148  {
149  $f = $this->getAvatarFactory();
150  $r = $this->getDefaultRenderer();
151 
152  $str = '/path/to/picture.jpg';
153  $letter = $f->picture($str, 'ro');
154  $html = $this->brutallyTrimHTML($r->render($letter));
155  $expected = $this->brutallyTrimHTML('
156 <span class="il-avatar il-avatar-picture il-avatar-size-large">
157  <img src="/path/to/picture.jpg" alt="user_avatar"/>
158 </span>');
159  $this->assertEquals($expected, $html);
160  }
161 
163  {
164  $f = $this->getAvatarFactory();
165  $r = $this->getDefaultRenderer();
166 
167  $str = '/path/to/picture.jpg';
168  $letter = $f->picture($str, 'ro')->withAlternativeText("alternative");
169  $html = $this->brutallyTrimHTML($r->render($letter));
170  $expected = $this->brutallyTrimHTML('
171 <span class="il-avatar il-avatar-picture il-avatar-size-large">
172  <img src="/path/to/picture.jpg" alt="alternative"/>
173 </span>');
174  $this->assertEquals($expected, $html);
175  }
181  public function getRandom26StringsForAllColorVariants(int $color_variants = 26, int $length = 2) : Generator
182  {
183  $sh = static function ($length = 10) {
184  return substr(str_shuffle(str_repeat($x = 'abcdefghijklmnopqrstuvwxyz', (int) ceil($length / strlen($x)))), 1, $length);
185  };
186 
187  $strings = [];
188  $running = true;
189  while ($running) {
190  $str = $sh($length);
191  $probe = crc32($str);
192  $i = ($probe % $color_variants) + 1;
193  if (!in_array($i, $strings, true)) {
194  $strings[$i] = $str;
195  yield $i => $str;
196  }
197  if (count($strings) === $color_variants) {
198  $running = false;
199  }
200  }
201  }
202 }
const ICON_PATH
Definition: AvatarTest.php:14
testRenderingPicture()
Definition: AvatarTest.php:147
testPicturePath()
Definition: AvatarTest.php:57
testRenderingLetter()
Definition: AvatarTest.php:133
testConstruction()
Definition: AvatarTest.php:21
getAvatarFactory()
Definition: AvatarTest.php:16
Provides common functionality for UI tests.
Definition: Base.php:262
testAbbreviation()
Definition: AvatarTest.php:33
getRandom26StringsForAllColorVariants(int $color_variants=26, int $length=2)
Definition: AvatarTest.php:181
testColorVariant()
Definition: AvatarTest.php:65
brutallyTrimHTML($html)
A more radical version of normalizeHTML.
Definition: Base.php:392
testAlternativeText()
Definition: AvatarTest.php:120
Test on avatar implementation.
Definition: AvatarTest.php:12
testRenderingPictureWithSomeAlternativeText()
Definition: AvatarTest.php:162
$i
Definition: metadata.php:24
getDefaultRenderer(JavaScriptBinding $js_binding=null, $with_stub_renderings=[])
Definition: Base.php:311