ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AvatarTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once("vendor/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../../Base.php");
23
26
31{
32 protected const ICON_PATH = __DIR__ . "/../../../../../../../assets/images/";
33
34 private function getAvatarFactory(): Factory
35 {
36 return new I\Component\Symbol\Avatar\Factory();
37 }
38
39 public function testConstruction(): void
40 {
41 $f = $this->getAvatarFactory();
42 $this->assertInstanceOf("ILIAS\\UI\\Component\\Symbol\\Avatar\\Factory", $f);
43
44 $le = $f->letter('ru');
45 $this->assertInstanceOf("ILIAS\\UI\\Component\\Symbol\\Avatar\\Letter", $le);
46
47 $ci = $f->picture(self::ICON_PATH . 'placeholder/no_photo_xsmall.jpg', 'ru');
48 $this->assertInstanceOf("ILIAS\\UI\\Component\\Symbol\\Avatar\\Picture", $ci);
49 }
50
51 public function testAbbreviation(): void
52 {
53 $f = $this->getAvatarFactory();
54
55 $this->assertEquals('ro', $f->letter('ro')->getAbbreviation());
56 $this->assertEquals('ro', $f->letter('root')->getAbbreviation());
57 $this->assertEquals('Ro', $f->letter('Root')->getAbbreviation());
58 $this->assertEquals('RO', $f->letter('ROOT')->getAbbreviation());
59 }
60
61 public function testUsername(): void
62 {
63 $f = $this->getAvatarFactory();
64
65 $this->assertEquals('ro', $f->letter('ro')->getUsername());
66 $this->assertEquals('ro', $f->picture('', 'ro')->getUsername());
67 $this->assertEquals('root', $f->letter('root')->getUsername());
68 $this->assertEquals('root', $f->picture('', 'root')->getUsername());
69 $this->assertEquals('Root', $f->letter('Root')->getUsername());
70 $this->assertEquals('Root', $f->picture('', 'Root')->getUsername());
71 $this->assertEquals('ROOT', $f->letter('ROOT')->getUsername());
72 $this->assertEquals('ROOT', $f->picture('', 'ROOT')->getUsername());
73 }
74
75 public function testPicturePath(): void
76 {
77 $f = $this->getAvatarFactory();
78
79 $str = '/path/to/picture.jpg';
80 $this->assertEquals($str, $f->picture($str, 'ro')->getPicturePath());
81 }
82
83 public function testColorVariant(): void
84 {
85 $f = $this->getAvatarFactory();
86
87 // Test all 26 colors
88 $variants = array(
89 1 => 'om',
90 2 => 'gk',
91 3 => 'bj',
92 4 => 'ea',
93 5 => 'mf',
94 6 => 'ob',
95 7 => 'bi',
96 8 => 'hu',
97 9 => 'fa',
98 10 => 'so',
99 11 => 'il',
100 12 => 'ut',
101 13 => 'ur',
102 14 => 'lt',
103 15 => 'kg',
104 16 => 'jl',
105 17 => 'qb',
106 18 => 'rq',
107 19 => 'ot',
108 20 => 'cq',
109 21 => 'rm',
110 22 => 'aj',
111 23 => 'li',
112 24 => 'er',
113 25 => 'ui',
114 26 => 'mi',
115 );
116 foreach ($variants as $color => $variant) {
117 $this->assertEquals($color, $f->letter($variant)->getBackgroundColorVariant());
118 }
119 }
120
121 public function testCrc32(): void
122 {
123 // test mechanism (crc32)
124 $f = $this->getAvatarFactory();
125 $number_of_colors = 26;
126 $abb = 'ru';
127
128 $calculated_color_variant = (crc32($abb) % $number_of_colors) + 1; // plus 1 since colors start with 1
129 $this->assertEquals($calculated_color_variant, $f->letter($abb)->getBackgroundColorVariant());
130
131 // test with random abbreviations (dynamically generated)
132
133 foreach ($this->getRandom26StringsForAllColorVariants() as $color => $variant) {
134 $this->assertEquals($color, $f->letter($variant)->getBackgroundColorVariant());
135 }
136 }
137
138 public function testAlternativeText(): void
139 {
140 $f = $this->getAvatarFactory();
141 $this->assertEquals("", $f->picture('', 'ro')->getLabel());
142 $this->assertEquals("", $f->letter('', 'ro')->getLabel());
143 $this->assertEquals("alternative", $f->picture('', 'ro')
144 ->withLabel("alternative")
145 ->getLabel());
146 $this->assertEquals("alternative", $f->letter('', 'ro')
147 ->withLabel("alternative")
148 ->getLabel());
149 }
150
151 public function testRenderingLetter(): void
152 {
153 $f = $this->getAvatarFactory();
154 $r = $this->getDefaultRenderer();
155
156 $letter = $f->letter('ro');
157 $html = $this->brutallyTrimHTML($r->render($letter));
158 $expected = $this->brutallyTrimHTML('
159<span class="il-avatar il-avatar-letter il-avatar-size-large il-avatar-letter-color-1" aria-label="user_avatar" role="img">
160 <span class="abbreviation">ro</span>
161</span>');
162 $this->assertEquals($expected, $html);
163 }
164
165 public function testRenderingPicture(): void
166 {
167 $f = $this->getAvatarFactory();
168 $r = $this->getDefaultRenderer();
169
170 $str = '/path/to/picture.jpg';
171 $letter = $f->picture($str, 'ro');
172 $html = $this->brutallyTrimHTML($r->render($letter));
173 $expected = $this->brutallyTrimHTML('
174<span class="il-avatar il-avatar-picture il-avatar-size-large">
175 <img src="/path/to/picture.jpg" alt="user_avatar"/>
176</span>');
177 $this->assertEquals($expected, $html);
178 }
179
181 {
182 $f = $this->getAvatarFactory();
183 $r = $this->getDefaultRenderer();
184
185 $str = '/path/to/picture.jpg';
186 $letter = $f->picture($str, 'ro')->withLabel("alternative");
187 $html = $this->brutallyTrimHTML($r->render($letter));
188 $expected = $this->brutallyTrimHTML('
189<span class="il-avatar il-avatar-picture il-avatar-size-large">
190 <img src="/path/to/picture.jpg" alt="alternative"/>
191</span>');
192 $this->assertEquals($expected, $html);
193 }
199 public function getRandom26StringsForAllColorVariants(int $color_variants = 26, int $length = 2): Generator
200 {
201 $sh = static function ($length = 10) {
202 return substr(
203 str_shuffle(str_repeat(
204 $x = 'abcdefghijklmnopqrstuvwxyz',
205 (int) ceil($length / strlen($x))
206 )),
207 1,
208 $length
209 );
210 };
211
212 $strings = [];
213 $running = true;
214 while ($running) {
215 $str = $sh($length);
216 $probe = crc32($str);
217 $i = ($probe % $color_variants) + 1;
218 if (!in_array($i, $strings, true)) {
219 $strings[$i] = $str;
220 yield $i => $str;
221 }
222 if (count($strings) === $color_variants) {
223 $running = false;
224 }
225 }
226 }
227
228 public function testHTMLInLabel(): void
229 {
230 $f = $this->getAvatarFactory();
231 $r = $this->getDefaultRenderer();
232
233 $avatar = $f->letter('user')->withLabel('<h1>label</h1>');
234 $html = $this->brutallyTrimHTML($r->render($avatar));
235 $expected = $this->brutallyTrimHTML('
236<span class="il-avatar il-avatar-letter il-avatar-size-large il-avatar-letter-color-11" aria-label="&lt;h1&gt;label&lt;/h1&gt;" role="img">
237 <span class="abbreviation">us</span>
238</span>');
239 $this->assertEquals($expected, $html);
240 }
241
242 public function testHTMLInCustomImage(): void
243 {
244 $f = $this->getAvatarFactory();
245 $r = $this->getDefaultRenderer();
246
247 $avatar = $f->picture('<h1>path</h1>', 'user');
248 $html = $this->brutallyTrimHTML($r->render($avatar));
249 $expected = $this->brutallyTrimHTML('
250<span class="il-avatar il-avatar-picture il-avatar-size-large">
251 <img src="&lt;h1&gt;path&lt;/h1&gt;" alt="user_avatar"/>
252</span>');
253 $this->assertEquals($expected, $html);
254 }
255}
Test on avatar implementation.
Definition: AvatarTest.php:31
testRenderingPicture()
Definition: AvatarTest.php:165
getAvatarFactory()
Definition: AvatarTest.php:34
testRenderingLetter()
Definition: AvatarTest.php:151
testAbbreviation()
Definition: AvatarTest.php:51
testColorVariant()
Definition: AvatarTest.php:83
testAlternativeText()
Definition: AvatarTest.php:138
testConstruction()
Definition: AvatarTest.php:39
testHTMLInCustomImage()
Definition: AvatarTest.php:242
testRenderingPictureWithSomeAlternativeText()
Definition: AvatarTest.php:180
testPicturePath()
Definition: AvatarTest.php:75
const ICON_PATH
Definition: AvatarTest.php:32
getRandom26StringsForAllColorVariants(int $color_variants=26, int $length=2)
Definition: AvatarTest.php:199
Provides common functionality for UI tests.
Definition: Base.php:337