GuestAvatarControllerTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Core\Controller;
  7. use OC\Core\Controller\GuestAvatarController;
  8. use OCP\AppFramework\Http\FileDisplayResponse;
  9. use OCP\Files\SimpleFS\ISimpleFile;
  10. use OCP\IAvatar;
  11. use OCP\IAvatarManager;
  12. use OCP\IRequest;
  13. use Psr\Log\LoggerInterface;
  14. /**
  15. * This class provides tests for the guest avatar controller.
  16. */
  17. class GuestAvatarControllerTest extends \Test\TestCase {
  18. /**
  19. * @var GuestAvatarController
  20. */
  21. private $guestAvatarController;
  22. /**
  23. * @var IRequest|\PHPUnit\Framework\MockObject\MockObject
  24. */
  25. private $request;
  26. /**
  27. * @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject
  28. */
  29. private $avatarManager;
  30. /**
  31. * @var IAvatar|\PHPUnit\Framework\MockObject\MockObject
  32. */
  33. private $avatar;
  34. /**
  35. * @var \OCP\Files\File|\PHPUnit\Framework\MockObject\MockObject
  36. */
  37. private $file;
  38. /**
  39. * @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject
  40. */
  41. private $logger;
  42. /**
  43. * Sets up the test environment.
  44. */
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  48. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  49. $this->avatar = $this->getMockBuilder(IAvatar::class)->getMock();
  50. $this->avatarManager = $this->getMockBuilder(IAvatarManager::class)->getMock();
  51. $this->file = $this->getMockBuilder(ISimpleFile::class)->getMock();
  52. $this->file->method('getName')->willReturn('my name');
  53. $this->file->method('getMTime')->willReturn(42);
  54. $this->guestAvatarController = new GuestAvatarController(
  55. 'core',
  56. $this->request,
  57. $this->avatarManager,
  58. $this->logger
  59. );
  60. }
  61. /**
  62. * Tests getAvatar returns the guest avatar.
  63. */
  64. public function testGetAvatar(): void {
  65. $this->avatarManager->expects($this->once())
  66. ->method('getGuestAvatar')
  67. ->with('Peter')
  68. ->willReturn($this->avatar);
  69. $this->avatar->expects($this->once())
  70. ->method('getFile')
  71. ->with(512)
  72. ->willReturn($this->file);
  73. $this->file->method('getMimeType')
  74. ->willReturn('image/svg+xml');
  75. $response = $this->guestAvatarController->getAvatar('Peter', 128);
  76. $this->assertGreaterThanOrEqual(201, $response->getStatus());
  77. $this->assertInstanceOf(FileDisplayResponse::class, $response);
  78. }
  79. }