use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\DataDisplayResponse;
+use OCP\Files\NotFoundException;
use OCP\IAvatarManager;
use OCP\ILogger;
use OCP\IL10N;
$size = 64;
}
- $avatar = $this->avatarManager->getAvatar($userId);
- $image = $avatar->get($size);
-
- if ($image instanceof \OCP\IImage) {
- $resp = new DataDisplayResponse($image->data(),
+ try {
+ $avatar = $this->avatarManager->getAvatar($userId)->getFile($size);
+ $resp = new DataDisplayResponse($avatar->getContent(),
Http::STATUS_OK,
- ['Content-Type' => $image->mimeType()]);
- $resp->setETag(crc32($image->data()));
- } else {
+ ['Content-Type' => $avatar->getMimeType()]);
+ $resp->setETag($avatar->getEtag());
+ } catch (NotFoundException $e) {
$user = $this->userManager->get($userId);
- $userName = $user ? $user->getDisplayName() : '';
$resp = new DataResponse([
'data' => [
- 'displayname' => $userName,
+ 'displayname' => $user->getDisplayName(),
+ ],
+ ]);
+ } catch (\Exception $e) {
+ $resp = new DataResponse([
+ 'data' => [
+ 'displayname' => '',
],
]);
}
use OCP\AppFramework\Http;
use OCP\Files\Folder;
use OCP\Files\File;
+use OCP\Files\NotFoundException;
use OCP\IUser;
use OCP\IAvatar;
+use Punic\Exception;
use Test\Traits\UserTrait;
/**
private $avatarMock;
/** @var IUser */
private $userMock;
+ /** @var File */
+ private $avatarFile;
protected function setUp() {
parent::setUp();
->willReturnMap([['userId', $this->userMock]]);
$this->container['UserSession']->method('getUser')->willReturn($this->userMock);
+ $this->avatarFile = $this->getMock('OCP\Files\File');
+ $this->avatarFile->method('getContnet')->willReturn('image data');
+ $this->avatarFile->method('getMimeType')->willReturn('image type');
+ $this->avatarFile->method('getEtag')->willReturn('my etag');
}
public function tearDown() {
*/
public function testGetAvatarNoAvatar() {
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
+ $this->avatarMock->method('getFile')->will($this->throwException(new NotFoundException()));
$response = $this->avatarController->getAvatar('userId', 32);
//Comment out until JS is fixed
* Fetch the user's avatar
*/
public function testGetAvatar() {
- $image = $this->getMock('OCP\IImage');
- $image->method('data')->willReturn('image data');
- $image->method('mimeType')->willReturn('image type');
-
- $this->avatarMock->method('get')->willReturn($image);
- $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
+ $this->avatarMock->method('getFile')->willReturn($this->avatarFile);
+ $this->container['AvatarManager']->method('getAvatar')->with('userId')->willReturn($this->avatarMock);
$response = $this->avatarController->getAvatar('userId', 32);
$this->assertArrayHasKey('Content-Type', $response->getHeaders());
$this->assertEquals('image type', $response->getHeaders()['Content-Type']);
- $this->assertEquals(crc32('image data'), $response->getEtag());
+ $this->assertEquals('my etag', $response->getEtag());
}
/**
* Fetch the avatar of a non-existing user
*/
public function testGetAvatarNoUser() {
- $this->avatarMock->method('get')->willReturn(null);
- $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
+ $this->container['AvatarManager']
+ ->method('getAvatar')
+ ->with('userDoesNotExist')
+ ->will($this->throwException(new \Exception('user does not exist')));
- $response = $this->avatarController->getAvatar('userDoesnotexist', 32);
+ $response = $this->avatarController->getAvatar('userDoesNotExist', 32);
//Comment out until JS is fixed
//$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
*/
public function testGetAvatarSize() {
$this->avatarMock->expects($this->once())
- ->method('get')
- ->with($this->equalTo(32));
+ ->method('getFile')
+ ->with($this->equalTo(32))
+ ->willReturn($this->avatarFile);
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
*/
public function testGetAvatarSizeMin() {
$this->avatarMock->expects($this->once())
- ->method('get')
- ->with($this->equalTo(64));
+ ->method('getFile')
+ ->with($this->equalTo(64))
+ ->willReturn($this->avatarFile);
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
*/
public function testGetAvatarSizeMax() {
$this->avatarMock->expects($this->once())
- ->method('get')
- ->with($this->equalTo(2048));
+ ->method('getFile')
+ ->with($this->equalTo(2048))
+ ->willReturn($this->avatarFile);
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);