AvatarTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\Files\NotFoundException;
  14. use OCP\Files\SimpleFS\ISimpleFile;
  15. use OCP\IConfig;
  16. use OCP\IL10N;
  17. use OCP\ILogger;
  18. class AvatarTest extends \Test\TestCase {
  19. /** @var Folder | \PHPUnit_Framework_MockObject_MockObject */
  20. private $folder;
  21. /** @var \OC\Avatar */
  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. public function setUp() {
  28. parent::setUp();
  29. $this->folder = $this->createMock(SimpleFolder::class);
  30. /** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */
  31. $l = $this->createMock(IL10N::class);
  32. $l->method('t')->will($this->returnArgument(0));
  33. $this->user = $this->createMock(User::class);
  34. $this->config = $this->createMock(IConfig::class);
  35. $this->avatar = new \OC\Avatar(
  36. $this->folder,
  37. $l,
  38. $this->user,
  39. $this->createMock(ILogger::class),
  40. $this->config
  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. ->will($this->returnCallback(function($path) {
  49. if ($path === 'avatar.64.png') {
  50. throw new NotFoundException();
  51. }
  52. }));
  53. $this->folder->method('fileExists')
  54. ->will($this->returnCallback(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. ->willReturn($data);
  68. $this->assertEquals($data, $this->avatar->get()->data());
  69. }
  70. public function testGetAvatarSizeMatch() {
  71. $this->folder->method('fileExists')
  72. ->will($this->returnValueMap([
  73. ['avatar.jpg', true],
  74. ['avatar.128.jpg', true],
  75. ]));
  76. $expected = new \OC_Image();
  77. $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  78. $file = $this->createMock(File::class);
  79. $file->method('getContent')->willReturn($expected->data());
  80. $this->folder->method('getFile')->with('avatar.128.jpg')->willReturn($file);
  81. $this->assertEquals($expected->data(), $this->avatar->get(128)->data());
  82. }
  83. public function testGetAvatarSizeMinusOne() {
  84. $this->folder->method('fileExists')
  85. ->will($this->returnValueMap([
  86. ['avatar.jpg', true],
  87. ]));
  88. $expected = new \OC_Image();
  89. $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  90. $file = $this->createMock(File::class);
  91. $file->method('getContent')->willReturn($expected->data());
  92. $this->folder->method('getFile')->with('avatar.jpg')->willReturn($file);
  93. $this->assertEquals($expected->data(), $this->avatar->get(-1)->data());
  94. }
  95. public function testGetAvatarNoSizeMatch() {
  96. $this->folder->method('fileExists')
  97. ->will($this->returnValueMap([
  98. ['avatar.png', true],
  99. ['avatar.32.png', false],
  100. ]));
  101. $expected = new \OC_Image();
  102. $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  103. $expected2 = new \OC_Image();
  104. $expected2->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  105. $expected2->resize(32);
  106. $file = $this->createMock(File::class);
  107. $file->method('getContent')->willReturn($expected->data());
  108. $this->folder->method('getFile')
  109. ->will($this->returnCallback(
  110. function($path) use ($file) {
  111. if ($path === 'avatar.png') {
  112. return $file;
  113. } else {
  114. throw new \OCP\Files\NotFoundException;
  115. }
  116. }
  117. ));
  118. $newFile = $this->createMock(File::class);
  119. $newFile->expects($this->once())
  120. ->method('putContent')
  121. ->with($expected2->data());
  122. $newFile->expects($this->once())
  123. ->method('getContent')
  124. ->willReturn($expected2->data());
  125. $this->folder->expects($this->once())
  126. ->method('newFile')
  127. ->with('avatar.32.png')
  128. ->willReturn($newFile);
  129. $this->assertEquals($expected2->data(), $this->avatar->get(32)->data());
  130. }
  131. public function testExistsNo() {
  132. $this->assertFalse($this->avatar->exists());
  133. }
  134. public function testExiststJPG() {
  135. $this->folder->method('fileExists')
  136. ->will($this->returnValueMap([
  137. ['avatar.jpg', true],
  138. ['avatar.png', false],
  139. ]));
  140. $this->assertTrue($this->avatar->exists());
  141. }
  142. public function testExistsPNG() {
  143. $this->folder->method('fileExists')
  144. ->will($this->returnValueMap([
  145. ['avatar.jpg', false],
  146. ['avatar.png', true],
  147. ]));
  148. $this->assertTrue($this->avatar->exists());
  149. }
  150. public function testSetAvatar() {
  151. $avatarFileJPG = $this->createMock(File::class);
  152. $avatarFileJPG->method('getName')
  153. ->willReturn('avatar.jpg');
  154. $avatarFileJPG->expects($this->once())->method('delete');
  155. $avatarFilePNG = $this->createMock(File::class);
  156. $avatarFilePNG->method('getName')
  157. ->willReturn('avatar.png');
  158. $avatarFilePNG->expects($this->once())->method('delete');
  159. $resizedAvatarFile = $this->createMock(File::class);
  160. $resizedAvatarFile->method('getName')
  161. ->willReturn('avatar.32.jpg');
  162. $resizedAvatarFile->expects($this->once())->method('delete');
  163. $this->folder->method('getDirectoryListing')
  164. ->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile]);
  165. $generated = $this->createMock(File::class);
  166. $this->folder->method('getFile')
  167. ->with('generated')
  168. ->willReturn($generated);
  169. $newFile = $this->createMock(File::class);
  170. $this->folder->expects($this->once())
  171. ->method('newFile')
  172. ->with('avatar.png')
  173. ->willReturn($newFile);
  174. $image = new \OC_Image();
  175. $image->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  176. $newFile->expects($this->once())
  177. ->method('putContent')
  178. ->with($image->data());
  179. $this->config->expects($this->exactly(3))
  180. ->method('setUserValue');
  181. $this->config->expects($this->once())
  182. ->method('getUserValue');
  183. // One on remove and once on setting the new avatar
  184. $this->user->expects($this->exactly(2))->method('triggerChange');
  185. $this->avatar->set($image->data());
  186. }
  187. }