2015-12-01 21:08:42 +00: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.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 07:27:21 +00:00
|
|
|
namespace Test;
|
|
|
|
|
2016-09-07 14:09:22 +00:00
|
|
|
use OC\Files\SimpleFS\SimpleFolder;
|
|
|
|
use OC\User\User;
|
2016-09-02 09:09:42 +00:00
|
|
|
use OCP\Files\File;
|
2015-12-01 21:08:42 +00:00
|
|
|
use OCP\Files\Folder;
|
2017-11-09 10:34:50 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
2016-09-02 09:09:42 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ILogger;
|
2015-12-01 21:08:42 +00:00
|
|
|
|
|
|
|
class AvatarTest extends \Test\TestCase {
|
2016-05-19 07:27:21 +00:00
|
|
|
/** @var Folder | \PHPUnit_Framework_MockObject_MockObject */
|
2015-12-01 21:08:42 +00:00
|
|
|
private $folder;
|
|
|
|
|
2016-01-25 12:24:56 +00:00
|
|
|
/** @var \OC\Avatar */
|
2015-12-01 21:08:42 +00:00
|
|
|
private $avatar;
|
|
|
|
|
2016-05-19 07:27:21 +00:00
|
|
|
/** @var \OC\User\User | \PHPUnit_Framework_MockObject_MockObject $user */
|
2016-01-22 11:56:53 +00:00
|
|
|
private $user;
|
|
|
|
|
2016-08-29 13:17:59 +00:00
|
|
|
/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $config;
|
|
|
|
|
2015-12-01 21:08:42 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-09-07 14:09:22 +00:00
|
|
|
$this->folder = $this->createMock(SimpleFolder::class);
|
2016-05-19 07:27:21 +00:00
|
|
|
/** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */
|
2016-09-02 09:09:42 +00:00
|
|
|
$l = $this->createMock(IL10N::class);
|
2015-12-01 21:08:42 +00:00
|
|
|
$l->method('t')->will($this->returnArgument(0));
|
2016-09-07 14:09:22 +00:00
|
|
|
$this->user = $this->createMock(User::class);
|
2016-09-02 09:09:42 +00:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2016-08-29 13:17:59 +00:00
|
|
|
|
|
|
|
$this->avatar = new \OC\Avatar(
|
|
|
|
$this->folder,
|
|
|
|
$l,
|
|
|
|
$this->user,
|
2016-09-02 09:09:42 +00:00
|
|
|
$this->createMock(ILogger::class),
|
2016-08-29 13:17:59 +00:00
|
|
|
$this->config
|
|
|
|
);
|
2015-12-01 21:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNoAvatar() {
|
2017-11-09 10:34:50 +00:00
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->folder->method('newFile')
|
|
|
|
->willReturn($file);
|
|
|
|
|
|
|
|
$this->folder->method('getFile')
|
|
|
|
->will($this->returnCallback(function($path) {
|
|
|
|
if ($path === 'avatar.64.png') {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
$this->folder->method('fileExists')
|
|
|
|
->will($this->returnCallback(function($path) {
|
|
|
|
if ($path === 'generated') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}));
|
|
|
|
|
|
|
|
$data = NULL;
|
|
|
|
$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-01 21:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAvatarSizeMatch() {
|
2016-09-07 14:09:22 +00:00
|
|
|
$this->folder->method('fileExists')
|
2015-12-01 21:08:42 +00:00
|
|
|
->will($this->returnValueMap([
|
|
|
|
['avatar.jpg', true],
|
|
|
|
['avatar.128.jpg', true],
|
|
|
|
]));
|
|
|
|
|
2018-01-17 10:46:30 +00:00
|
|
|
$expected = new \OC_Image();
|
|
|
|
$expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
2015-12-01 21:08:42 +00:00
|
|
|
|
2016-09-02 09:09:42 +00:00
|
|
|
$file = $this->createMock(File::class);
|
2015-12-01 21:08:42 +00:00
|
|
|
$file->method('getContent')->willReturn($expected->data());
|
2016-09-07 14:09:22 +00:00
|
|
|
$this->folder->method('getFile')->with('avatar.128.jpg')->willReturn($file);
|
2015-12-01 21:08:42 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expected->data(), $this->avatar->get(128)->data());
|
|
|
|
}
|
|
|
|
|
2016-01-25 12:24:56 +00:00
|
|
|
public function testGetAvatarSizeMinusOne() {
|
2016-09-07 14:09:22 +00:00
|
|
|
$this->folder->method('fileExists')
|
2016-01-25 12:24:56 +00:00
|
|
|
->will($this->returnValueMap([
|
|
|
|
['avatar.jpg', true],
|
|
|
|
]));
|
|
|
|
|
2018-01-17 10:46:30 +00:00
|
|
|
$expected = new \OC_Image();
|
|
|
|
$expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
2016-01-25 12:24:56 +00:00
|
|
|
|
2016-09-02 09:09:42 +00:00
|
|
|
$file = $this->createMock(File::class);
|
2016-01-25 12:24:56 +00:00
|
|
|
$file->method('getContent')->willReturn($expected->data());
|
2016-09-07 14:09:22 +00:00
|
|
|
$this->folder->method('getFile')->with('avatar.jpg')->willReturn($file);
|
2016-01-25 12:24:56 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expected->data(), $this->avatar->get(-1)->data());
|
|
|
|
}
|
|
|
|
|
2015-12-01 21:08:42 +00:00
|
|
|
public function testGetAvatarNoSizeMatch() {
|
2016-09-07 14:09:22 +00:00
|
|
|
$this->folder->method('fileExists')
|
2015-12-01 21:08:42 +00:00
|
|
|
->will($this->returnValueMap([
|
|
|
|
['avatar.png', true],
|
|
|
|
['avatar.32.png', false],
|
|
|
|
]));
|
|
|
|
|
2018-01-17 10:46:30 +00: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-01 21:08:42 +00:00
|
|
|
$expected2->resize(32);
|
|
|
|
|
2016-09-02 09:09:42 +00:00
|
|
|
$file = $this->createMock(File::class);
|
2015-12-01 21:08:42 +00:00
|
|
|
$file->method('getContent')->willReturn($expected->data());
|
2015-12-16 19:26:00 +00:00
|
|
|
|
2016-09-07 14:09:22 +00:00
|
|
|
$this->folder->method('getFile')
|
2015-12-16 19:26:00 +00:00
|
|
|
->will($this->returnCallback(
|
|
|
|
function($path) use ($file) {
|
|
|
|
if ($path === 'avatar.png') {
|
|
|
|
return $file;
|
|
|
|
} else {
|
|
|
|
throw new \OCP\Files\NotFoundException;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
));
|
2015-12-01 21:08:42 +00:00
|
|
|
|
2016-09-02 09:09:42 +00:00
|
|
|
$newFile = $this->createMock(File::class);
|
2015-12-01 21:08:42 +00:00
|
|
|
$newFile->expects($this->once())
|
|
|
|
->method('putContent')
|
|
|
|
->with($expected2->data());
|
2015-12-16 19:26:00 +00:00
|
|
|
$newFile->expects($this->once())
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn($expected2->data());
|
2015-12-01 21:08:42 +00: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 14:09:22 +00:00
|
|
|
$this->folder->method('fileExists')
|
2015-12-01 21:08:42 +00:00
|
|
|
->will($this->returnValueMap([
|
|
|
|
['avatar.jpg', true],
|
|
|
|
['avatar.png', false],
|
|
|
|
]));
|
|
|
|
$this->assertTrue($this->avatar->exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExistsPNG() {
|
2016-09-07 14:09:22 +00:00
|
|
|
$this->folder->method('fileExists')
|
2015-12-01 21:08:42 +00:00
|
|
|
->will($this->returnValueMap([
|
|
|
|
['avatar.jpg', false],
|
|
|
|
['avatar.png', true],
|
|
|
|
]));
|
|
|
|
$this->assertTrue($this->avatar->exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetAvatar() {
|
2016-09-02 09:09:42 +00:00
|
|
|
$avatarFileJPG = $this->createMock(File::class);
|
2016-01-07 19:51:18 +00:00
|
|
|
$avatarFileJPG->method('getName')
|
|
|
|
->willReturn('avatar.jpg');
|
|
|
|
$avatarFileJPG->expects($this->once())->method('delete');
|
|
|
|
|
2016-09-02 09:09:42 +00:00
|
|
|
$avatarFilePNG = $this->createMock(File::class);
|
2016-01-07 19:51:18 +00:00
|
|
|
$avatarFilePNG->method('getName')
|
|
|
|
->willReturn('avatar.png');
|
|
|
|
$avatarFilePNG->expects($this->once())->method('delete');
|
|
|
|
|
2016-09-02 09:09:42 +00:00
|
|
|
$resizedAvatarFile = $this->createMock(File::class);
|
2016-01-07 19:51:18 +00:00
|
|
|
$resizedAvatarFile->method('getName')
|
|
|
|
->willReturn('avatar.32.jpg');
|
|
|
|
$resizedAvatarFile->expects($this->once())->method('delete');
|
|
|
|
|
2016-03-29 16:52:17 +00:00
|
|
|
$this->folder->method('getDirectoryListing')
|
2017-11-09 10:34:50 +00:00
|
|
|
->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile]);
|
|
|
|
|
|
|
|
$generated = $this->createMock(File::class);
|
|
|
|
$this->folder->method('getFile')
|
|
|
|
->with('generated')
|
|
|
|
->willReturn($generated);
|
2015-12-01 21:08:42 +00:00
|
|
|
|
2016-09-02 09:09:42 +00:00
|
|
|
$newFile = $this->createMock(File::class);
|
2015-12-01 21:08:42 +00:00
|
|
|
$this->folder->expects($this->once())
|
|
|
|
->method('newFile')
|
|
|
|
->with('avatar.png')
|
|
|
|
->willReturn($newFile);
|
|
|
|
|
2018-01-17 10:46:30 +00:00
|
|
|
$image = new \OC_Image();
|
|
|
|
$image->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
2015-12-01 21:08:42 +00:00
|
|
|
$newFile->expects($this->once())
|
|
|
|
->method('putContent')
|
|
|
|
->with($image->data());
|
|
|
|
|
2017-12-19 17:49:03 +00:00
|
|
|
$this->config->expects($this->exactly(3))
|
2016-08-29 13:17:59 +00:00
|
|
|
->method('setUserValue');
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getUserValue');
|
|
|
|
|
2016-01-25 12:06:00 +00:00
|
|
|
// One on remove and once on setting the new avatar
|
|
|
|
$this->user->expects($this->exactly(2))->method('triggerChange');
|
2016-01-22 11:56:53 +00:00
|
|
|
|
2015-12-01 21:08:42 +00:00
|
|
|
$this->avatar->set($image->data());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|