Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions lib/private/Avatar/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ abstract public function getDisplayName(): string;
*
* @return string
*/
private function getAvatarLetter(): string {
private function getAvatarText(): string {
$displayName = $this->getDisplayName();
if (empty($displayName) === true) {
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);
}

/**
Expand Down Expand Up @@ -130,9 +132,9 @@ protected function getAvatarVector(int $size): string {
$userDisplayName = $this->getDisplayName();
$bgRGB = $this->avatarBackgroundColor($userDisplayName);
$bgHEX = sprintf("%02x%02x%02x", $bgRGB->r, $bgRGB->g, $bgRGB->b);
$letter = $this->getAvatarLetter();
$toReplace = ['{size}', '{fill}', '{letter}'];
return str_replace($toReplace, [$size, $bgHEX, $letter], $this->svgTemplate);
$text = $this->getAvatarText();
$toReplace = ['{size}', '{fill}', '{text}'];
return str_replace($toReplace, [$size, $bgHEX, $text], $this->svgTemplate);
}

/**
Expand Down Expand Up @@ -168,7 +170,7 @@ protected function generateAvatarFromSvg(int $size) {
* @return string
*/
protected function generateAvatar($userDisplayName, $size) {
$letter = $this->getAvatarLetter();
$text = $this->getAvatarText();
$backgroundColor = $this->avatarBackgroundColor($userDisplayName);

$im = imagecreatetruecolor($size, $size);
Expand All @@ -185,10 +187,10 @@ protected function generateAvatar($userDisplayName, $size) {

$fontSize = $size * 0.4;
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();
imagepng($im);
Expand Down
58 changes: 45 additions & 13 deletions tests/lib/Avatar/UserAvatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,20 @@ protected function setUp(): void {
parent::setUp();

$this->folder = $this->createMock(SimpleFolder::class);
/** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */
$l = $this->createMock(IL10N::class);
$l->method('t')->will($this->returnArgument(0));
$this->user = $this->createMock(User::class);
// abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9
$this->user = $this->getUserWithDisplayName('abcdefghi');
$this->config = $this->createMock(IConfig::class);

$this->avatar = new \OC\Avatar\UserAvatar(
$this->folder,
$l,
$this->user,
$this->createMock(ILogger::class),
$this->config
);
$this->avatar = $this->getUserAvatar($this->user);
}

// abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9
$this->user->method('getDisplayName')->willReturn('abcdefghi');
public function avatarTextData() {
return [
['', '?'],
['matchish', 'M'],
['Firstname Lastname', 'FL'],
['Firstname Lastname Rest', 'FL'],
];
}

public function testGetNoAvatar() {
Expand Down Expand Up @@ -239,6 +237,18 @@ public function testGenerateSvgAvatar() {
$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() {
$hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]);
$this->assertTrue(gettype($hashToInt) === 'integer');
Expand All @@ -261,4 +271,26 @@ public function testMixPalette() {
$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
);
}

}