UserAvatarTest.php 8.4 KB

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