2015-12-02 00:08:42 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2019-01-20 13:13:41 +03:00
|
|
|
namespace Test\Avatar;
|
2016-05-19 10:27:21 +03:00
|
|
|
|
2016-09-07 17:09:22 +03:00
|
|
|
use OC\Files\SimpleFS\SimpleFolder;
|
|
|
|
use OC\User\User;
|
2016-09-02 12:09:42 +03:00
|
|
|
use OCP\Files\File;
|
2015-12-02 00:08:42 +03:00
|
|
|
use OCP\Files\Folder;
|
2017-11-09 13:34:50 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
2016-09-02 12:09:42 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ILogger;
|
2015-12-02 00:08:42 +03:00
|
|
|
|
2019-01-20 13:13:41 +03:00
|
|
|
class UserAvatarTest extends \Test\TestCase {
|
2016-05-19 10:27:21 +03:00
|
|
|
/** @var Folder | \PHPUnit_Framework_MockObject_MockObject */
|
2015-12-02 00:08:42 +03:00
|
|
|
private $folder;
|
|
|
|
|
2019-01-20 13:13:41 +03:00
|
|
|
/** @var \OC\Avatar\UserAvatar */
|
2015-12-02 00:08:42 +03:00
|
|
|
private $avatar;
|
|
|
|
|
2016-05-19 10:27:21 +03:00
|
|
|
/** @var \OC\User\User | \PHPUnit_Framework_MockObject_MockObject $user */
|
2016-01-22 14:56:53 +03:00
|
|
|
private $user;
|
|
|
|
|
2016-08-29 16:17:59 +03:00
|
|
|
/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $config;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-12-02 00:08:42 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2016-09-07 17:09:22 +03:00
|
|
|
$this->folder = $this->createMock(SimpleFolder::class);
|
2020-01-08 15:21:50 +03:00
|
|
|
// abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9
|
|
|
|
$this->user = $this->getUserWithDisplayName('abcdefghi');
|
2016-09-02 12:09:42 +03:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2016-08-29 16:17:59 +03:00
|
|
|
|
2020-01-08 15:21:50 +03:00
|
|
|
$this->avatar = $this->getUserAvatar($this->user);
|
|
|
|
}
|
2018-10-23 14:13:35 +03:00
|
|
|
|
2020-01-08 15:21:50 +03:00
|
|
|
public function avatarTextData() {
|
|
|
|
return [
|
|
|
|
['', '?'],
|
|
|
|
['matchish', 'M'],
|
|
|
|
['Firstname Lastname', 'FL'],
|
|
|
|
['Firstname Lastname Rest', 'FL'],
|
|
|
|
];
|
2015-12-02 00:08:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNoAvatar() {
|
2017-11-09 13:34:50 +03:00
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->folder->method('newFile')
|
|
|
|
->willReturn($file);
|
|
|
|
|
|
|
|
$this->folder->method('getFile')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function($path) {
|
2017-11-09 13:34:50 +03:00
|
|
|
if ($path === 'avatar.64.png') {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2017-11-09 13:34:50 +03:00
|
|
|
$this->folder->method('fileExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function($path) {
|
2017-11-09 13:34:50 +03:00
|
|
|
if ($path === 'generated') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2017-11-09 13:34:50 +03:00
|
|
|
|
2020-03-25 23:53:04 +03:00
|
|
|
$data = null;
|
2017-11-09 13:34:50 +03:00
|
|
|
$file->method('putContent')
|
|
|
|
->with($this->callback(function ($d) use (&$data) {
|
|
|
|
$data = $d;
|
|
|
|
return true;
|
|
|
|
}));
|
|
|
|
|
|
|
|
$file->method('getContent')
|
|
|
|
->willReturn($data);
|
|
|
|
|
|
|
|
$this->assertEquals($data, $this->avatar->get()->data());
|
2015-12-02 00:08:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAvatarSizeMatch() {
|
2016-09-07 17:09:22 +03:00
|
|
|
$this->folder->method('fileExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2015-12-02 00:08:42 +03:00
|
|
|
['avatar.jpg', true],
|
|
|
|
['avatar.128.jpg', true],
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2015-12-02 00:08:42 +03:00
|
|
|
|
2018-01-17 13:46:30 +03:00
|
|
|
$expected = new \OC_Image();
|
|
|
|
$expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
2015-12-02 00:08:42 +03:00
|
|
|
|
2016-09-02 12:09:42 +03:00
|
|
|
$file = $this->createMock(File::class);
|
2015-12-02 00:08:42 +03:00
|
|
|
$file->method('getContent')->willReturn($expected->data());
|
2016-09-07 17:09:22 +03:00
|
|
|
$this->folder->method('getFile')->with('avatar.128.jpg')->willReturn($file);
|
2015-12-02 00:08:42 +03:00
|
|
|
|
|
|
|
$this->assertEquals($expected->data(), $this->avatar->get(128)->data());
|
|
|
|
}
|
|
|
|
|
2016-01-25 15:24:56 +03:00
|
|
|
public function testGetAvatarSizeMinusOne() {
|
2016-09-07 17:09:22 +03:00
|
|
|
$this->folder->method('fileExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2016-01-25 15:24:56 +03:00
|
|
|
['avatar.jpg', true],
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2016-01-25 15:24:56 +03:00
|
|
|
|
2018-01-17 13:46:30 +03:00
|
|
|
$expected = new \OC_Image();
|
|
|
|
$expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
2016-01-25 15:24:56 +03:00
|
|
|
|
2016-09-02 12:09:42 +03:00
|
|
|
$file = $this->createMock(File::class);
|
2016-01-25 15:24:56 +03:00
|
|
|
$file->method('getContent')->willReturn($expected->data());
|
2016-09-07 17:09:22 +03:00
|
|
|
$this->folder->method('getFile')->with('avatar.jpg')->willReturn($file);
|
2016-01-25 15:24:56 +03:00
|
|
|
|
|
|
|
$this->assertEquals($expected->data(), $this->avatar->get(-1)->data());
|
|
|
|
}
|
|
|
|
|
2015-12-02 00:08:42 +03:00
|
|
|
public function testGetAvatarNoSizeMatch() {
|
2016-09-07 17:09:22 +03:00
|
|
|
$this->folder->method('fileExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2015-12-02 00:08:42 +03:00
|
|
|
['avatar.png', true],
|
|
|
|
['avatar.32.png', false],
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2015-12-02 00:08:42 +03:00
|
|
|
|
2018-01-17 13:46:30 +03:00
|
|
|
$expected = new \OC_Image();
|
|
|
|
$expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
|
|
|
$expected2 = new \OC_Image();
|
|
|
|
$expected2->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
2015-12-02 00:08:42 +03:00
|
|
|
$expected2->resize(32);
|
|
|
|
|
2016-09-02 12:09:42 +03:00
|
|
|
$file = $this->createMock(File::class);
|
2015-12-02 00:08:42 +03:00
|
|
|
$file->method('getContent')->willReturn($expected->data());
|
2015-12-16 22:26:00 +03:00
|
|
|
|
2016-09-07 17:09:22 +03:00
|
|
|
$this->folder->method('getFile')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(
|
2015-12-16 22:26:00 +03:00
|
|
|
function($path) use ($file) {
|
|
|
|
if ($path === 'avatar.png') {
|
|
|
|
return $file;
|
|
|
|
} else {
|
|
|
|
throw new \OCP\Files\NotFoundException;
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
);
|
2015-12-02 00:08:42 +03:00
|
|
|
|
2016-09-02 12:09:42 +03:00
|
|
|
$newFile = $this->createMock(File::class);
|
2015-12-02 00:08:42 +03:00
|
|
|
$newFile->expects($this->once())
|
|
|
|
->method('putContent')
|
|
|
|
->with($expected2->data());
|
2015-12-16 22:26:00 +03:00
|
|
|
$newFile->expects($this->once())
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn($expected2->data());
|
2015-12-02 00:08:42 +03:00
|
|
|
$this->folder->expects($this->once())
|
|
|
|
->method('newFile')
|
|
|
|
->with('avatar.32.png')
|
|
|
|
->willReturn($newFile);
|
|
|
|
|
|
|
|
$this->assertEquals($expected2->data(), $this->avatar->get(32)->data());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExistsNo() {
|
|
|
|
$this->assertFalse($this->avatar->exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExiststJPG() {
|
2016-09-07 17:09:22 +03:00
|
|
|
$this->folder->method('fileExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2015-12-02 00:08:42 +03:00
|
|
|
['avatar.jpg', true],
|
|
|
|
['avatar.png', false],
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2015-12-02 00:08:42 +03:00
|
|
|
$this->assertTrue($this->avatar->exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExistsPNG() {
|
2016-09-07 17:09:22 +03:00
|
|
|
$this->folder->method('fileExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2015-12-02 00:08:42 +03:00
|
|
|
['avatar.jpg', false],
|
|
|
|
['avatar.png', true],
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2015-12-02 00:08:42 +03:00
|
|
|
$this->assertTrue($this->avatar->exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetAvatar() {
|
2016-09-02 12:09:42 +03:00
|
|
|
$avatarFileJPG = $this->createMock(File::class);
|
2016-01-07 22:51:18 +03:00
|
|
|
$avatarFileJPG->method('getName')
|
|
|
|
->willReturn('avatar.jpg');
|
|
|
|
$avatarFileJPG->expects($this->once())->method('delete');
|
|
|
|
|
2016-09-02 12:09:42 +03:00
|
|
|
$avatarFilePNG = $this->createMock(File::class);
|
2016-01-07 22:51:18 +03:00
|
|
|
$avatarFilePNG->method('getName')
|
|
|
|
->willReturn('avatar.png');
|
|
|
|
$avatarFilePNG->expects($this->once())->method('delete');
|
|
|
|
|
2016-09-02 12:09:42 +03:00
|
|
|
$resizedAvatarFile = $this->createMock(File::class);
|
2016-01-07 22:51:18 +03:00
|
|
|
$resizedAvatarFile->method('getName')
|
|
|
|
->willReturn('avatar.32.jpg');
|
|
|
|
$resizedAvatarFile->expects($this->once())->method('delete');
|
|
|
|
|
2016-03-29 19:52:17 +03:00
|
|
|
$this->folder->method('getDirectoryListing')
|
2017-11-09 13:34:50 +03:00
|
|
|
->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile]);
|
|
|
|
|
|
|
|
$generated = $this->createMock(File::class);
|
|
|
|
$this->folder->method('getFile')
|
|
|
|
->with('generated')
|
|
|
|
->willReturn($generated);
|
2015-12-02 00:08:42 +03:00
|
|
|
|
2016-09-02 12:09:42 +03:00
|
|
|
$newFile = $this->createMock(File::class);
|
2015-12-02 00:08:42 +03:00
|
|
|
$this->folder->expects($this->once())
|
|
|
|
->method('newFile')
|
|
|
|
->with('avatar.png')
|
|
|
|
->willReturn($newFile);
|
|
|
|
|
2018-01-17 13:46:30 +03:00
|
|
|
$image = new \OC_Image();
|
|
|
|
$image->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
2015-12-02 00:08:42 +03:00
|
|
|
$newFile->expects($this->once())
|
|
|
|
->method('putContent')
|
|
|
|
->with($image->data());
|
|
|
|
|
2017-12-19 20:49:03 +03:00
|
|
|
$this->config->expects($this->exactly(3))
|
2016-08-29 16:17:59 +03:00
|
|
|
->method('setUserValue');
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getUserValue');
|
|
|
|
|
2019-09-04 14:36:36 +03:00
|
|
|
$this->user->expects($this->exactly(1))->method('triggerChange');
|
2016-01-22 14:56:53 +03:00
|
|
|
|
2015-12-02 00:08:42 +03:00
|
|
|
$this->avatar->set($image->data());
|
|
|
|
}
|
|
|
|
|
2018-05-07 15:09:31 +03:00
|
|
|
public function testGenerateSvgAvatar() {
|
2018-05-28 10:44:10 +03:00
|
|
|
$avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [64]);
|
2018-10-23 14:13:35 +03:00
|
|
|
|
2018-05-28 12:06:33 +03:00
|
|
|
$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
2018-05-07 15:09:31 +03:00
|
|
|
<svg width="64" height="64" version="1.1" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
|
|
|
|
<rect width="100%" height="100%" fill="#0082c9"></rect>
|
2019-06-21 23:35:30 +03:00
|
|
|
<text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#fff">A</text>
|
2018-05-07 15:09:31 +03:00
|
|
|
</svg>';
|
|
|
|
$this->assertEquals($avatar, $svg);
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:21:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider avatarTextData
|
|
|
|
*/
|
|
|
|
public function testGetAvatarText($displayName, $expectedAvatarText) {
|
|
|
|
$user = $this->getUserWithDisplayName($displayName);
|
|
|
|
$avatar = $this->getUserAvatar($user);
|
|
|
|
|
|
|
|
$avatarText = $this->invokePrivate($avatar, 'getAvatarText');
|
|
|
|
$this->assertEquals($expectedAvatarText, $avatarText);
|
|
|
|
}
|
|
|
|
|
2018-05-07 15:09:31 +03:00
|
|
|
public function testHashToInt() {
|
|
|
|
$hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]);
|
|
|
|
$this->assertTrue(gettype($hashToInt) === 'integer');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMixPalette() {
|
|
|
|
$colorFrom = new \OC\Color(0,0,0);
|
|
|
|
$colorTo = new \OC\Color(6,12,18);
|
|
|
|
$steps = 6;
|
|
|
|
$palette = $this->invokePrivate($this->avatar, 'mixPalette', [$steps, $colorFrom, $colorTo]);
|
|
|
|
foreach($palette as $j => $color) {
|
|
|
|
// calc increment
|
|
|
|
$incR = $colorTo->r / $steps * $j;
|
|
|
|
$incG = $colorTo->g / $steps * $j;
|
|
|
|
$incB = $colorTo->b / $steps * $j;
|
|
|
|
// ensure everything is equal
|
|
|
|
$this->assertEquals($color, new \OC\Color($incR, $incG,$incB));
|
|
|
|
}
|
|
|
|
$hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]);
|
|
|
|
$this->assertTrue(gettype($hashToInt) === 'integer');
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:21:50 +03:00
|
|
|
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);
|
2020-03-26 00:21:27 +03:00
|
|
|
$l->method('t')->willReturnArgument(0);
|
2020-01-08 15:21:50 +03:00
|
|
|
|
|
|
|
return new \OC\Avatar\UserAvatar(
|
|
|
|
$this->folder,
|
|
|
|
$l,
|
|
|
|
$user,
|
|
|
|
$this->createMock(ILogger::class),
|
|
|
|
$this->config
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-02 00:08:42 +03:00
|
|
|
}
|