You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AvatarTest.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test;
  9. use OC\Files\SimpleFS\SimpleFolder;
  10. use OC\User\User;
  11. use OCP\Files\File;
  12. use OCP\Files\Folder;
  13. use OCP\IConfig;
  14. use OCP\IL10N;
  15. use OCP\ILogger;
  16. class AvatarTest extends \Test\TestCase {
  17. /** @var Folder | \PHPUnit_Framework_MockObject_MockObject */
  18. private $folder;
  19. /** @var \OC\Avatar */
  20. private $avatar;
  21. /** @var \OC\User\User | \PHPUnit_Framework_MockObject_MockObject $user */
  22. private $user;
  23. /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
  24. private $config;
  25. public function setUp() {
  26. parent::setUp();
  27. $this->folder = $this->createMock(SimpleFolder::class);
  28. /** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */
  29. $l = $this->createMock(IL10N::class);
  30. $l->method('t')->will($this->returnArgument(0));
  31. $this->user = $this->createMock(User::class);
  32. $this->config = $this->createMock(IConfig::class);
  33. $this->avatar = new \OC\Avatar(
  34. $this->folder,
  35. $l,
  36. $this->user,
  37. $this->createMock(ILogger::class),
  38. $this->config
  39. );
  40. }
  41. public function testGetNoAvatar() {
  42. $this->assertEquals(false, $this->avatar->get());
  43. }
  44. public function testGetAvatarSizeMatch() {
  45. $this->folder->method('fileExists')
  46. ->will($this->returnValueMap([
  47. ['avatar.jpg', true],
  48. ['avatar.128.jpg', true],
  49. ]));
  50. $expected = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  51. $file = $this->createMock(File::class);
  52. $file->method('getContent')->willReturn($expected->data());
  53. $this->folder->method('getFile')->with('avatar.128.jpg')->willReturn($file);
  54. $this->assertEquals($expected->data(), $this->avatar->get(128)->data());
  55. }
  56. public function testGetAvatarSizeMinusOne() {
  57. $this->folder->method('fileExists')
  58. ->will($this->returnValueMap([
  59. ['avatar.jpg', true],
  60. ]));
  61. $expected = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  62. $file = $this->createMock(File::class);
  63. $file->method('getContent')->willReturn($expected->data());
  64. $this->folder->method('getFile')->with('avatar.jpg')->willReturn($file);
  65. $this->assertEquals($expected->data(), $this->avatar->get(-1)->data());
  66. }
  67. public function testGetAvatarNoSizeMatch() {
  68. $this->folder->method('fileExists')
  69. ->will($this->returnValueMap([
  70. ['avatar.png', true],
  71. ['avatar.32.png', false],
  72. ]));
  73. $expected = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  74. $expected2 = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  75. $expected2->resize(32);
  76. $file = $this->createMock(File::class);
  77. $file->method('getContent')->willReturn($expected->data());
  78. $this->folder->method('getFile')
  79. ->will($this->returnCallback(
  80. function($path) use ($file) {
  81. if ($path === 'avatar.png') {
  82. return $file;
  83. } else {
  84. throw new \OCP\Files\NotFoundException;
  85. }
  86. }
  87. ));
  88. $newFile = $this->createMock(File::class);
  89. $newFile->expects($this->once())
  90. ->method('putContent')
  91. ->with($expected2->data());
  92. $newFile->expects($this->once())
  93. ->method('getContent')
  94. ->willReturn($expected2->data());
  95. $this->folder->expects($this->once())
  96. ->method('newFile')
  97. ->with('avatar.32.png')
  98. ->willReturn($newFile);
  99. $this->assertEquals($expected2->data(), $this->avatar->get(32)->data());
  100. }
  101. public function testExistsNo() {
  102. $this->assertFalse($this->avatar->exists());
  103. }
  104. public function testExiststJPG() {
  105. $this->folder->method('fileExists')
  106. ->will($this->returnValueMap([
  107. ['avatar.jpg', true],
  108. ['avatar.png', false],
  109. ]));
  110. $this->assertTrue($this->avatar->exists());
  111. }
  112. public function testExistsPNG() {
  113. $this->folder->method('fileExists')
  114. ->will($this->returnValueMap([
  115. ['avatar.jpg', false],
  116. ['avatar.png', true],
  117. ]));
  118. $this->assertTrue($this->avatar->exists());
  119. }
  120. public function testSetAvatar() {
  121. $avatarFileJPG = $this->createMock(File::class);
  122. $avatarFileJPG->method('getName')
  123. ->willReturn('avatar.jpg');
  124. $avatarFileJPG->expects($this->once())->method('delete');
  125. $avatarFilePNG = $this->createMock(File::class);
  126. $avatarFilePNG->method('getName')
  127. ->willReturn('avatar.png');
  128. $avatarFilePNG->expects($this->once())->method('delete');
  129. $resizedAvatarFile = $this->createMock(File::class);
  130. $resizedAvatarFile->method('getName')
  131. ->willReturn('avatar.32.jpg');
  132. $resizedAvatarFile->expects($this->once())->method('delete');
  133. $nonAvatarFile = $this->createMock(File::class);
  134. $nonAvatarFile->method('getName')
  135. ->willReturn('avatarX');
  136. $nonAvatarFile->expects($this->never())->method('delete');
  137. $this->folder->method('getDirectoryListing')
  138. ->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile, $nonAvatarFile]);
  139. $newFile = $this->createMock(File::class);
  140. $this->folder->expects($this->once())
  141. ->method('newFile')
  142. ->with('avatar.png')
  143. ->willReturn($newFile);
  144. $image = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  145. $newFile->expects($this->once())
  146. ->method('putContent')
  147. ->with($image->data());
  148. $this->config->expects($this->once())
  149. ->method('setUserValue');
  150. $this->config->expects($this->once())
  151. ->method('getUserValue');
  152. // One on remove and once on setting the new avatar
  153. $this->user->expects($this->exactly(2))->method('triggerChange');
  154. $this->avatar->set($image->data());
  155. }
  156. }