avatartest.php 5.0 KB

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