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.

UserAvatarTest.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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\Avatar;
  9. use OC\Files\SimpleFS\SimpleFolder;
  10. use OC\User\User;
  11. use OCP\Files\File;
  12. use OCP\Files\Folder;
  13. use OCP\Files\NotFoundException;
  14. use OCP\Files\SimpleFS\ISimpleFile;
  15. use OCP\IConfig;
  16. use OCP\IL10N;
  17. use Psr\Log\LoggerInterface;
  18. class UserAvatarTest extends \Test\TestCase {
  19. /** @var Folder | \PHPUnit\Framework\MockObject\MockObject */
  20. private $folder;
  21. /** @var \OC\Avatar\UserAvatar */
  22. private $avatar;
  23. /** @var \OC\User\User | \PHPUnit\Framework\MockObject\MockObject $user */
  24. private $user;
  25. /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
  26. private $config;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->folder = $this->createMock(SimpleFolder::class);
  30. // abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9
  31. $this->user = $this->getUserWithDisplayName('abcdefghi');
  32. $this->config = $this->createMock(IConfig::class);
  33. $this->avatar = $this->getUserAvatar($this->user);
  34. }
  35. public function avatarTextData() {
  36. return [
  37. ['', '?'],
  38. ['matchish', 'M'],
  39. ['Firstname Lastname', 'FL'],
  40. ['Firstname Lastname Rest', 'FL'],
  41. ];
  42. }
  43. public function testGetNoAvatar() {
  44. $file = $this->createMock(ISimpleFile::class);
  45. $this->folder->method('newFile')
  46. ->willReturn($file);
  47. $this->folder->method('getFile')
  48. ->willReturnCallback(function ($path) {
  49. if ($path === 'avatar.64.png') {
  50. throw new NotFoundException();
  51. }
  52. });
  53. $this->folder->method('fileExists')
  54. ->willReturnCallback(function ($path) {
  55. if ($path === 'generated') {
  56. return true;
  57. }
  58. return false;
  59. });
  60. $data = null;
  61. $file->method('putContent')
  62. ->with($this->callback(function ($d) use (&$data) {
  63. $data = $d;
  64. return true;
  65. }));
  66. $file->method('getContent')
  67. ->willReturnCallback(function () use (&$data) {
  68. return $data;
  69. });
  70. $result = $this->avatar->get();
  71. $this->assertTrue($result->valid());
  72. }
  73. public function testGetAvatarSizeMatch() {
  74. $this->folder->method('fileExists')
  75. ->willReturnMap([
  76. ['avatar.jpg', true],
  77. ['avatar.128.jpg', true],
  78. ]);
  79. $expected = new \OC_Image();
  80. $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  81. $file = $this->createMock(File::class);
  82. $file->method('getContent')->willReturn($expected->data());
  83. $this->folder->method('getFile')->with('avatar.128.jpg')->willReturn($file);
  84. $this->assertEquals($expected->data(), $this->avatar->get(128)->data());
  85. }
  86. public function testGetAvatarSizeMinusOne() {
  87. $this->folder->method('fileExists')
  88. ->willReturnMap([
  89. ['avatar.jpg', true],
  90. ]);
  91. $expected = new \OC_Image();
  92. $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  93. $file = $this->createMock(File::class);
  94. $file->method('getContent')->willReturn($expected->data());
  95. $this->folder->method('getFile')->with('avatar.jpg')->willReturn($file);
  96. $this->assertEquals($expected->data(), $this->avatar->get(-1)->data());
  97. }
  98. public function testGetAvatarNoSizeMatch() {
  99. $this->folder->method('fileExists')
  100. ->willReturnMap([
  101. ['avatar.png', true],
  102. ['avatar.32.png', false],
  103. ]);
  104. $expected = new \OC_Image();
  105. $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  106. $expected2 = new \OC_Image();
  107. $expected2->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  108. $expected2->resize(32);
  109. $file = $this->createMock(File::class);
  110. $file->method('getContent')->willReturn($expected->data());
  111. $this->folder->method('getFile')
  112. ->willReturnCallback(
  113. function ($path) use ($file) {
  114. if ($path === 'avatar.png') {
  115. return $file;
  116. } else {
  117. throw new \OCP\Files\NotFoundException;
  118. }
  119. }
  120. );
  121. $newFile = $this->createMock(File::class);
  122. $newFile->expects($this->once())
  123. ->method('putContent')
  124. ->with($expected2->data());
  125. $newFile->expects($this->once())
  126. ->method('getContent')
  127. ->willReturn($expected2->data());
  128. $this->folder->expects($this->once())
  129. ->method('newFile')
  130. ->with('avatar.32.png')
  131. ->willReturn($newFile);
  132. $this->assertEquals($expected2->data(), $this->avatar->get(32)->data());
  133. }
  134. public function testExistsNo() {
  135. $this->assertFalse($this->avatar->exists());
  136. }
  137. public function testExiststJPG() {
  138. $this->folder->method('fileExists')
  139. ->willReturnMap([
  140. ['avatar.jpg', true],
  141. ['avatar.png', false],
  142. ]);
  143. $this->assertTrue($this->avatar->exists());
  144. }
  145. public function testExistsPNG() {
  146. $this->folder->method('fileExists')
  147. ->willReturnMap([
  148. ['avatar.jpg', false],
  149. ['avatar.png', true],
  150. ]);
  151. $this->assertTrue($this->avatar->exists());
  152. }
  153. public function testSetAvatar() {
  154. $avatarFileJPG = $this->createMock(File::class);
  155. $avatarFileJPG->method('getName')
  156. ->willReturn('avatar.jpg');
  157. $avatarFileJPG->expects($this->once())->method('delete');
  158. $avatarFilePNG = $this->createMock(File::class);
  159. $avatarFilePNG->method('getName')
  160. ->willReturn('avatar.png');
  161. $avatarFilePNG->expects($this->once())->method('delete');
  162. $resizedAvatarFile = $this->createMock(File::class);
  163. $resizedAvatarFile->method('getName')
  164. ->willReturn('avatar.32.jpg');
  165. $resizedAvatarFile->expects($this->once())->method('delete');
  166. $this->folder->method('getDirectoryListing')
  167. ->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile]);
  168. $generated = $this->createMock(File::class);
  169. $this->folder->method('getFile')
  170. ->with('generated')
  171. ->willReturn($generated);
  172. $newFile = $this->createMock(File::class);
  173. $this->folder->expects($this->once())
  174. ->method('newFile')
  175. ->with('avatar.png')
  176. ->willReturn($newFile);
  177. $image = new \OC_Image();
  178. $image->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  179. $newFile->expects($this->once())
  180. ->method('putContent')
  181. ->with($image->data());
  182. $this->config->expects($this->exactly(3))
  183. ->method('setUserValue');
  184. $this->config->expects($this->once())
  185. ->method('getUserValue');
  186. $this->user->expects($this->exactly(1))->method('triggerChange');
  187. $this->avatar->set($image->data());
  188. }
  189. public function testGenerateSvgAvatar() {
  190. $avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [64]);
  191. $svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  192. <svg width="64" height="64" version="1.1" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
  193. <rect width="100%" height="100%" fill="#0082c9"></rect>
  194. <text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#fff">A</text>
  195. </svg>';
  196. $this->assertEquals($avatar, $svg);
  197. }
  198. /**
  199. * @dataProvider avatarTextData
  200. */
  201. public function testGetAvatarText($displayName, $expectedAvatarText) {
  202. $user = $this->getUserWithDisplayName($displayName);
  203. $avatar = $this->getUserAvatar($user);
  204. $avatarText = $this->invokePrivate($avatar, 'getAvatarText');
  205. $this->assertEquals($expectedAvatarText, $avatarText);
  206. }
  207. public function testHashToInt() {
  208. $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]);
  209. $this->assertTrue(gettype($hashToInt) === 'integer');
  210. }
  211. public function testMixPalette() {
  212. $colorFrom = new \OC\Color(0, 0, 0);
  213. $colorTo = new \OC\Color(6, 12, 18);
  214. $steps = 6;
  215. $palette = $this->invokePrivate($this->avatar, 'mixPalette', [$steps, $colorFrom, $colorTo]);
  216. foreach ($palette as $j => $color) {
  217. // calc increment
  218. $incR = $colorTo->r / $steps * $j;
  219. $incG = $colorTo->g / $steps * $j;
  220. $incB = $colorTo->b / $steps * $j;
  221. // ensure everything is equal
  222. $this->assertEquals($color, new \OC\Color($incR, $incG, $incB));
  223. }
  224. $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]);
  225. $this->assertTrue(gettype($hashToInt) === 'integer');
  226. }
  227. private function getUserWithDisplayName($name) {
  228. $user = $this->createMock(User::class);
  229. $user->method('getDisplayName')->willReturn($name);
  230. return $user;
  231. }
  232. private function getUserAvatar($user) {
  233. /** @var \OCP\IL10N | \PHPUnit\Framework\MockObject\MockObject $l */
  234. $l = $this->createMock(IL10N::class);
  235. $l->method('t')->willReturnArgument(0);
  236. return new \OC\Avatar\UserAvatar(
  237. $this->folder,
  238. $l,
  239. $user,
  240. $this->createMock(LoggerInterface::class),
  241. $this->config
  242. );
  243. }
  244. }