AvatarTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  77. $file = $this->createMock(File::class);
  78. $file->method('getContent')->willReturn($expected->data());
  79. $this->folder->method('getFile')->with('avatar.128.jpg')->willReturn($file);
  80. $this->assertEquals($expected->data(), $this->avatar->get(128)->data());
  81. }
  82. public function testGetAvatarSizeMinusOne() {
  83. $this->folder->method('fileExists')
  84. ->will($this->returnValueMap([
  85. ['avatar.jpg', true],
  86. ]));
  87. $expected = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  88. $file = $this->createMock(File::class);
  89. $file->method('getContent')->willReturn($expected->data());
  90. $this->folder->method('getFile')->with('avatar.jpg')->willReturn($file);
  91. $this->assertEquals($expected->data(), $this->avatar->get(-1)->data());
  92. }
  93. public function testGetAvatarNoSizeMatch() {
  94. $this->folder->method('fileExists')
  95. ->will($this->returnValueMap([
  96. ['avatar.png', true],
  97. ['avatar.32.png', false],
  98. ]));
  99. $expected = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  100. $expected2 = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  101. $expected2->resize(32);
  102. $file = $this->createMock(File::class);
  103. $file->method('getContent')->willReturn($expected->data());
  104. $this->folder->method('getFile')
  105. ->will($this->returnCallback(
  106. function($path) use ($file) {
  107. if ($path === 'avatar.png') {
  108. return $file;
  109. } else {
  110. throw new \OCP\Files\NotFoundException;
  111. }
  112. }
  113. ));
  114. $newFile = $this->createMock(File::class);
  115. $newFile->expects($this->once())
  116. ->method('putContent')
  117. ->with($expected2->data());
  118. $newFile->expects($this->once())
  119. ->method('getContent')
  120. ->willReturn($expected2->data());
  121. $this->folder->expects($this->once())
  122. ->method('newFile')
  123. ->with('avatar.32.png')
  124. ->willReturn($newFile);
  125. $this->assertEquals($expected2->data(), $this->avatar->get(32)->data());
  126. }
  127. public function testExistsNo() {
  128. $this->assertFalse($this->avatar->exists());
  129. }
  130. public function testExiststJPG() {
  131. $this->folder->method('fileExists')
  132. ->will($this->returnValueMap([
  133. ['avatar.jpg', true],
  134. ['avatar.png', false],
  135. ]));
  136. $this->assertTrue($this->avatar->exists());
  137. }
  138. public function testExistsPNG() {
  139. $this->folder->method('fileExists')
  140. ->will($this->returnValueMap([
  141. ['avatar.jpg', false],
  142. ['avatar.png', true],
  143. ]));
  144. $this->assertTrue($this->avatar->exists());
  145. }
  146. public function testSetAvatar() {
  147. $avatarFileJPG = $this->createMock(File::class);
  148. $avatarFileJPG->method('getName')
  149. ->willReturn('avatar.jpg');
  150. $avatarFileJPG->expects($this->once())->method('delete');
  151. $avatarFilePNG = $this->createMock(File::class);
  152. $avatarFilePNG->method('getName')
  153. ->willReturn('avatar.png');
  154. $avatarFilePNG->expects($this->once())->method('delete');
  155. $resizedAvatarFile = $this->createMock(File::class);
  156. $resizedAvatarFile->method('getName')
  157. ->willReturn('avatar.32.jpg');
  158. $resizedAvatarFile->expects($this->once())->method('delete');
  159. $this->folder->method('getDirectoryListing')
  160. ->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile]);
  161. $generated = $this->createMock(File::class);
  162. $this->folder->method('getFile')
  163. ->with('generated')
  164. ->willReturn($generated);
  165. $newFile = $this->createMock(File::class);
  166. $this->folder->expects($this->once())
  167. ->method('newFile')
  168. ->with('avatar.png')
  169. ->willReturn($newFile);
  170. $image = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  171. $newFile->expects($this->once())
  172. ->method('putContent')
  173. ->with($image->data());
  174. $this->config->expects($this->exactly(3))
  175. ->method('setUserValue');
  176. $this->config->expects($this->once())
  177. ->method('getUserValue');
  178. // One on remove and once on setting the new avatar
  179. $this->user->expects($this->exactly(2))->method('triggerChange');
  180. $this->avatar->set($image->data());
  181. }
  182. }