UserAvatarTest.php 8.7 KB

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