You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

avatartest.php 4.1KB

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