AvatarTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 OCP\Files\File;
  10. use OCP\Files\Folder;
  11. use OCP\IConfig;
  12. use OCP\IL10N;
  13. use OCP\ILogger;
  14. class AvatarTest extends \Test\TestCase {
  15. /** @var Folder | \PHPUnit_Framework_MockObject_MockObject */
  16. private $folder;
  17. /** @var \OC\Avatar */
  18. private $avatar;
  19. /** @var \OC\User\User | \PHPUnit_Framework_MockObject_MockObject $user */
  20. private $user;
  21. /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
  22. private $config;
  23. public function setUp() {
  24. parent::setUp();
  25. $this->folder = $this->createMock(Folder::class);
  26. /** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */
  27. $l = $this->createMock(IL10N::class);
  28. $l->method('t')->will($this->returnArgument(0));
  29. $this->user = $this->getMockBuilder('OC\User\User')->disableOriginalConstructor()->getMock();
  30. $this->config = $this->createMock(IConfig::class);
  31. $this->avatar = new \OC\Avatar(
  32. $this->folder,
  33. $l,
  34. $this->user,
  35. $this->createMock(ILogger::class),
  36. $this->config
  37. );
  38. }
  39. public function testGetNoAvatar() {
  40. $this->assertEquals(false, $this->avatar->get());
  41. }
  42. public function testGetAvatarSizeMatch() {
  43. $this->folder->method('nodeExists')
  44. ->will($this->returnValueMap([
  45. ['avatar.jpg', true],
  46. ['avatar.128.jpg', true],
  47. ]));
  48. $expected = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  49. $file = $this->createMock(File::class);
  50. $file->method('getContent')->willReturn($expected->data());
  51. $this->folder->method('get')->with('avatar.128.jpg')->willReturn($file);
  52. $this->assertEquals($expected->data(), $this->avatar->get(128)->data());
  53. }
  54. public function testGetAvatarSizeMinusOne() {
  55. $this->folder->method('nodeExists')
  56. ->will($this->returnValueMap([
  57. ['avatar.jpg', true],
  58. ]));
  59. $expected = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  60. $file = $this->createMock(File::class);
  61. $file->method('getContent')->willReturn($expected->data());
  62. $this->folder->method('get')->with('avatar.jpg')->willReturn($file);
  63. $this->assertEquals($expected->data(), $this->avatar->get(-1)->data());
  64. }
  65. public function testGetAvatarNoSizeMatch() {
  66. $this->folder->method('nodeExists')
  67. ->will($this->returnValueMap([
  68. ['avatar.png', true],
  69. ['avatar.32.png', false],
  70. ]));
  71. $expected = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  72. $expected2 = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  73. $expected2->resize(32);
  74. $file = $this->createMock(File::class);
  75. $file->method('getContent')->willReturn($expected->data());
  76. $this->folder->method('get')
  77. ->will($this->returnCallback(
  78. function($path) use ($file) {
  79. if ($path === 'avatar.png') {
  80. return $file;
  81. } else {
  82. throw new \OCP\Files\NotFoundException;
  83. }
  84. }
  85. ));
  86. $newFile = $this->createMock(File::class);
  87. $newFile->expects($this->once())
  88. ->method('putContent')
  89. ->with($expected2->data());
  90. $newFile->expects($this->once())
  91. ->method('getContent')
  92. ->willReturn($expected2->data());
  93. $this->folder->expects($this->once())
  94. ->method('newFile')
  95. ->with('avatar.32.png')
  96. ->willReturn($newFile);
  97. $this->assertEquals($expected2->data(), $this->avatar->get(32)->data());
  98. }
  99. public function testExistsNo() {
  100. $this->assertFalse($this->avatar->exists());
  101. }
  102. public function testExiststJPG() {
  103. $this->folder->method('nodeExists')
  104. ->will($this->returnValueMap([
  105. ['avatar.jpg', true],
  106. ['avatar.png', false],
  107. ]));
  108. $this->assertTrue($this->avatar->exists());
  109. }
  110. public function testExistsPNG() {
  111. $this->folder->method('nodeExists')
  112. ->will($this->returnValueMap([
  113. ['avatar.jpg', false],
  114. ['avatar.png', true],
  115. ]));
  116. $this->assertTrue($this->avatar->exists());
  117. }
  118. public function testSetAvatar() {
  119. $avatarFileJPG = $this->createMock(File::class);
  120. $avatarFileJPG->method('getName')
  121. ->willReturn('avatar.jpg');
  122. $avatarFileJPG->expects($this->once())->method('delete');
  123. $avatarFilePNG = $this->createMock(File::class);
  124. $avatarFilePNG->method('getName')
  125. ->willReturn('avatar.png');
  126. $avatarFilePNG->expects($this->once())->method('delete');
  127. $resizedAvatarFile = $this->createMock(File::class);
  128. $resizedAvatarFile->method('getName')
  129. ->willReturn('avatar.32.jpg');
  130. $resizedAvatarFile->expects($this->once())->method('delete');
  131. $nonAvatarFile = $this->createMock(File::class);
  132. $nonAvatarFile->method('getName')
  133. ->willReturn('avatarX');
  134. $nonAvatarFile->expects($this->never())->method('delete');
  135. $this->folder->method('getDirectoryListing')
  136. ->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile, $nonAvatarFile]);
  137. $newFile = $this->createMock(File::class);
  138. $this->folder->expects($this->once())
  139. ->method('newFile')
  140. ->with('avatar.png')
  141. ->willReturn($newFile);
  142. $image = new \OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  143. $newFile->expects($this->once())
  144. ->method('putContent')
  145. ->with($image->data());
  146. $this->config->expects($this->once())
  147. ->method('setUserValue');
  148. $this->config->expects($this->once())
  149. ->method('getUserValue');
  150. // One on remove and once on setting the new avatar
  151. $this->user->expects($this->exactly(2))->method('triggerChange');
  152. $this->avatar->set($image->data());
  153. }
  154. }