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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
|
|
|
|
class AvatarTest extends \Test\TestCase {
|
2016-01-25 15:24:56 +03:00
|
|
|
/** @var Folder | PHPUnit_Framework_MockObject_MockObject */
|
2015-12-02 00:08:42 +03:00
|
|
|
private $folder;
|
|
|
|
|
2016-01-25 15:24:56 +03:00
|
|
|
/** @var \OC\Avatar */
|
2015-12-02 00:08:42 +03:00
|
|
|
private $avatar;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->folder = $this->getMock('\OCP\Files\Folder');
|
|
|
|
$l = $this->getMock('\OCP\IL10N');
|
|
|
|
$l->method('t')->will($this->returnArgument(0));
|
|
|
|
$this->avatar = new \OC\Avatar($this->folder, $l);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNoAvatar() {
|
|
|
|
$this->assertEquals(false, $this->avatar->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAvatarSizeMatch() {
|
|
|
|
$this->folder->method('nodeExists')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
['avatar.jpg', true],
|
|
|
|
['avatar.128.jpg', true],
|
|
|
|
]));
|
|
|
|
|
|
|
|
$expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
|
|
|
|
|
|
|
$file = $this->getMock('\OCP\Files\File');
|
|
|
|
$file->method('getContent')->willReturn($expected->data());
|
|
|
|
$this->folder->method('get')->with('avatar.128.jpg')->willReturn($file);
|
|
|
|
|
|
|
|
$this->assertEquals($expected->data(), $this->avatar->get(128)->data());
|
|
|
|
}
|
|
|
|
|
2016-01-25 15:24:56 +03:00
|
|
|
public function testGetAvatarSizeMinusOne() {
|
|
|
|
$this->folder->method('nodeExists')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
['avatar.jpg', true],
|
|
|
|
]));
|
|
|
|
|
|
|
|
$expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
|
|
|
|
|
|
|
$file = $this->getMock('\OCP\Files\File');
|
|
|
|
$file->method('getContent')->willReturn($expected->data());
|
|
|
|
$this->folder->method('get')->with('avatar.jpg')->willReturn($file);
|
|
|
|
|
|
|
|
$this->assertEquals($expected->data(), $this->avatar->get(-1)->data());
|
|
|
|
}
|
|
|
|
|
2015-12-02 00:08:42 +03:00
|
|
|
public function testGetAvatarNoSizeMatch() {
|
|
|
|
$this->folder->method('nodeExists')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
['avatar.png', true],
|
|
|
|
['avatar.32.png', false],
|
|
|
|
]));
|
|
|
|
|
|
|
|
$expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
|
|
|
$expected2 = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
|
|
|
$expected2->resize(32);
|
|
|
|
|
|
|
|
$file = $this->getMock('\OCP\Files\File');
|
|
|
|
$file->method('getContent')->willReturn($expected->data());
|
2015-12-16 22:26:00 +03:00
|
|
|
|
|
|
|
$this->folder->method('get')
|
|
|
|
->will($this->returnCallback(
|
|
|
|
function($path) use ($file) {
|
|
|
|
if ($path === 'avatar.png') {
|
|
|
|
return $file;
|
|
|
|
} else {
|
|
|
|
throw new \OCP\Files\NotFoundException;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
));
|
2015-12-02 00:08:42 +03:00
|
|
|
|
|
|
|
$newFile = $this->getMock('\OCP\Files\File');
|
|
|
|
$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() {
|
|
|
|
$this->folder->method('nodeExists')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
['avatar.jpg', true],
|
|
|
|
['avatar.png', false],
|
|
|
|
]));
|
|
|
|
$this->assertTrue($this->avatar->exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExistsPNG() {
|
|
|
|
$this->folder->method('nodeExists')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
['avatar.jpg', false],
|
|
|
|
['avatar.png', true],
|
|
|
|
]));
|
|
|
|
$this->assertTrue($this->avatar->exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetAvatar() {
|
2016-01-07 22:51:18 +03:00
|
|
|
$avatarFileJPG = $this->getMock('\OCP\Files\File');
|
|
|
|
$avatarFileJPG->method('getName')
|
|
|
|
->willReturn('avatar.jpg');
|
|
|
|
$avatarFileJPG->expects($this->once())->method('delete');
|
|
|
|
|
|
|
|
$avatarFilePNG = $this->getMock('\OCP\Files\File');
|
|
|
|
$avatarFilePNG->method('getName')
|
|
|
|
->willReturn('avatar.png');
|
|
|
|
$avatarFilePNG->expects($this->once())->method('delete');
|
|
|
|
|
|
|
|
$resizedAvatarFile = $this->getMock('\OCP\Files\File');
|
|
|
|
$resizedAvatarFile->method('getName')
|
|
|
|
->willReturn('avatar.32.jpg');
|
|
|
|
$resizedAvatarFile->expects($this->once())->method('delete');
|
|
|
|
|
|
|
|
$nonAvatarFile = $this->getMock('\OCP\Files\File');
|
|
|
|
$nonAvatarFile->method('getName')
|
|
|
|
->willReturn('avatarX');
|
|
|
|
$nonAvatarFile->expects($this->never())->method('delete');
|
|
|
|
|
|
|
|
$this->folder->method('search')
|
|
|
|
->with('avatar')
|
|
|
|
->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile, $nonAvatarFile]);
|
2015-12-02 00:08:42 +03:00
|
|
|
|
|
|
|
$newFile = $this->getMock('\OCP\Files\File');
|
|
|
|
$this->folder->expects($this->once())
|
|
|
|
->method('newFile')
|
|
|
|
->with('avatar.png')
|
|
|
|
->willReturn($newFile);
|
|
|
|
|
|
|
|
$image = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
|
|
|
$newFile->expects($this->once())
|
|
|
|
->method('putContent')
|
|
|
|
->with($image->data());
|
|
|
|
|
|
|
|
$this->avatar->set($image->data());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|