Skip to content
Snippets Groups Projects
Commit acc0df1d authored by Sergey Shliakhov's avatar Sergey Shliakhov
Browse files

Change avatar placeholder from single letter to 2 letters

parent aa950ffc
No related branches found
No related tags found
No related merge requests found
...@@ -89,13 +89,15 @@ abstract class Avatar implements IAvatar { ...@@ -89,13 +89,15 @@ abstract class Avatar implements IAvatar {
* *
* @return string * @return string
*/ */
private function getAvatarLetter(): string { private function getAvatarText(): string {
$displayName = $this->getDisplayName(); $displayName = $this->getDisplayName();
if (empty($displayName) === true) { if (empty($displayName) === true) {
return '?'; return '?';
} else {
return mb_strtoupper(mb_substr($displayName, 0, 1), 'UTF-8');
} }
$firstTwoLetters = array_map(function ($namePart) {
return mb_strtoupper(mb_substr($namePart, 0, 1), 'UTF-8');
}, explode(' ', $displayName, 2));
return implode('', $firstTwoLetters);
} }
/** /**
...@@ -130,9 +132,9 @@ abstract class Avatar implements IAvatar { ...@@ -130,9 +132,9 @@ abstract class Avatar implements IAvatar {
$userDisplayName = $this->getDisplayName(); $userDisplayName = $this->getDisplayName();
$bgRGB = $this->avatarBackgroundColor($userDisplayName); $bgRGB = $this->avatarBackgroundColor($userDisplayName);
$bgHEX = sprintf("%02x%02x%02x", $bgRGB->r, $bgRGB->g, $bgRGB->b); $bgHEX = sprintf("%02x%02x%02x", $bgRGB->r, $bgRGB->g, $bgRGB->b);
$letter = $this->getAvatarLetter(); $text = $this->getAvatarText();
$toReplace = ['{size}', '{fill}', '{letter}']; $toReplace = ['{size}', '{fill}', '{text}'];
return str_replace($toReplace, [$size, $bgHEX, $letter], $this->svgTemplate); return str_replace($toReplace, [$size, $bgHEX, $text], $this->svgTemplate);
} }
/** /**
...@@ -168,7 +170,7 @@ abstract class Avatar implements IAvatar { ...@@ -168,7 +170,7 @@ abstract class Avatar implements IAvatar {
* @return string * @return string
*/ */
protected function generateAvatar($userDisplayName, $size) { protected function generateAvatar($userDisplayName, $size) {
$letter = $this->getAvatarLetter(); $text = $this->getAvatarText();
$backgroundColor = $this->avatarBackgroundColor($userDisplayName); $backgroundColor = $this->avatarBackgroundColor($userDisplayName);
$im = imagecreatetruecolor($size, $size); $im = imagecreatetruecolor($size, $size);
...@@ -185,10 +187,10 @@ abstract class Avatar implements IAvatar { ...@@ -185,10 +187,10 @@ abstract class Avatar implements IAvatar {
$fontSize = $size * 0.4; $fontSize = $size * 0.4;
list($x, $y) = $this->imageTTFCenter( list($x, $y) = $this->imageTTFCenter(
$im, $letter, $font, (int)$fontSize $im, $text, $font, (int)$fontSize
); );
imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $letter); imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text);
ob_start(); ob_start();
imagepng($im); imagepng($im);
......
...@@ -35,22 +35,20 @@ class UserAvatarTest extends \Test\TestCase { ...@@ -35,22 +35,20 @@ class UserAvatarTest extends \Test\TestCase {
parent::setUp(); parent::setUp();
$this->folder = $this->createMock(SimpleFolder::class); $this->folder = $this->createMock(SimpleFolder::class);
/** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */ // abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9
$l = $this->createMock(IL10N::class); $this->user = $this->getUserWithDisplayName('abcdefghi');
$l->method('t')->will($this->returnArgument(0));
$this->user = $this->createMock(User::class);
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->avatar = new \OC\Avatar\UserAvatar( $this->avatar = $this->getUserAvatar($this->user);
$this->folder, }
$l,
$this->user,
$this->createMock(ILogger::class),
$this->config
);
// abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9 public function avatarTextData() {
$this->user->method('getDisplayName')->willReturn('abcdefghi'); return [
['', '?'],
['matchish', 'M'],
['Firstname Lastname', 'FL'],
['Firstname Lastname Rest', 'FL'],
];
} }
public function testGetNoAvatar() { public function testGetNoAvatar() {
...@@ -239,6 +237,18 @@ class UserAvatarTest extends \Test\TestCase { ...@@ -239,6 +237,18 @@ class UserAvatarTest extends \Test\TestCase {
$this->assertEquals($avatar, $svg); $this->assertEquals($avatar, $svg);
} }
/**
* @dataProvider avatarTextData
*/
public function testGetAvatarText($displayName, $expectedAvatarText) {
$user = $this->getUserWithDisplayName($displayName);
$avatar = $this->getUserAvatar($user);
$avatarText = $this->invokePrivate($avatar, 'getAvatarText');
$this->assertEquals($expectedAvatarText, $avatarText);
}
public function testHashToInt() { public function testHashToInt() {
$hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]); $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]);
$this->assertTrue(gettype($hashToInt) === 'integer'); $this->assertTrue(gettype($hashToInt) === 'integer');
...@@ -261,4 +271,26 @@ class UserAvatarTest extends \Test\TestCase { ...@@ -261,4 +271,26 @@ class UserAvatarTest extends \Test\TestCase {
$this->assertTrue(gettype($hashToInt) === 'integer'); $this->assertTrue(gettype($hashToInt) === 'integer');
} }
private function getUserWithDisplayName($name)
{
$user = $this->createMock(User::class);
$user->method('getDisplayName')->willReturn($name);
return $user;
}
private function getUserAvatar($user)
{
/** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */
$l = $this->createMock(IL10N::class);
$l->method('t')->will($this->returnArgument(0));
return new \OC\Avatar\UserAvatar(
$this->folder,
$l,
$user,
$this->createMock(ILogger::class),
$this->config
);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment