PreviewControllerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_Versions\Tests\Controller;
  7. use OCA\Files_Versions\Controller\PreviewController;
  8. use OCA\Files_Versions\Versions\IVersionManager;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\DataResponse;
  11. use OCP\AppFramework\Http\FileDisplayResponse;
  12. use OCP\Files\File;
  13. use OCP\Files\Folder;
  14. use OCP\Files\IMimeTypeDetector;
  15. use OCP\Files\IRootFolder;
  16. use OCP\Files\NotFoundException;
  17. use OCP\Files\SimpleFS\ISimpleFile;
  18. use OCP\IPreview;
  19. use OCP\IRequest;
  20. use OCP\IUser;
  21. use OCP\IUserSession;
  22. use Test\TestCase;
  23. class PreviewControllerTest extends TestCase {
  24. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  25. private $rootFolder;
  26. /** @var string */
  27. private $userId;
  28. /** @var IMimeTypeDetector|\PHPUnit\Framework\MockObject\MockObject */
  29. private $mimeTypeDetector;
  30. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  31. private $previewManager;
  32. /** @var PreviewController|\PHPUnit\Framework\MockObject\MockObject */
  33. private $controller;
  34. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  35. private $userSession;
  36. /** @var IVersionManager|\PHPUnit\Framework\MockObject\MockObject */
  37. private $versionManager;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->rootFolder = $this->createMock(IRootFolder::class);
  41. $this->userId = 'user';
  42. $user = $this->createMock(IUser::class);
  43. $user->expects($this->any())
  44. ->method('getUID')
  45. ->willReturn($this->userId);
  46. $this->previewManager = $this->createMock(IPreview::class);
  47. $this->userSession = $this->createMock(IUserSession::class);
  48. $this->userSession->expects($this->any())
  49. ->method('getUser')
  50. ->willReturn($user);
  51. $this->versionManager = $this->createMock(IVersionManager::class);
  52. $this->controller = new PreviewController(
  53. 'files_versions',
  54. $this->createMock(IRequest::class),
  55. $this->rootFolder,
  56. $this->userSession,
  57. $this->versionManager,
  58. $this->previewManager
  59. );
  60. }
  61. public function testInvalidFile(): void {
  62. $res = $this->controller->getPreview('');
  63. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  64. $this->assertEquals($expected, $res);
  65. }
  66. public function testInvalidWidth(): void {
  67. $res = $this->controller->getPreview('file', 0);
  68. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  69. $this->assertEquals($expected, $res);
  70. }
  71. public function testInvalidHeight(): void {
  72. $res = $this->controller->getPreview('file', 10, 0);
  73. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  74. $this->assertEquals($expected, $res);
  75. }
  76. public function testInvalidVersion(): void {
  77. $res = $this->controller->getPreview('file', 10, 0);
  78. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  79. $this->assertEquals($expected, $res);
  80. }
  81. public function testValidPreview(): void {
  82. $userFolder = $this->createMock(Folder::class);
  83. $userRoot = $this->createMock(Folder::class);
  84. $this->rootFolder->method('getUserFolder')
  85. ->with($this->userId)
  86. ->willReturn($userFolder);
  87. $userFolder->method('getParent')
  88. ->willReturn($userRoot);
  89. $sourceFile = $this->createMock(File::class);
  90. $userFolder->method('get')
  91. ->with('file')
  92. ->willReturn($sourceFile);
  93. $file = $this->createMock(File::class);
  94. $file->method('getMimetype')
  95. ->willReturn('myMime');
  96. $this->versionManager->method('getVersionFile')
  97. ->willReturn($file);
  98. $preview = $this->createMock(ISimpleFile::class);
  99. $preview->method('getName')->willReturn('name');
  100. $preview->method('getMTime')->willReturn(42);
  101. $this->previewManager->method('getPreview')
  102. ->with($this->equalTo($file), 10, 10, true, IPreview::MODE_FILL, 'myMime')
  103. ->willReturn($preview);
  104. $preview->method('getMimeType')
  105. ->willReturn('previewMime');
  106. $res = $this->controller->getPreview('file', 10, 10, '42');
  107. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']);
  108. $this->assertEquals($expected, $res);
  109. }
  110. public function testVersionNotFound(): void {
  111. $userFolder = $this->createMock(Folder::class);
  112. $userRoot = $this->createMock(Folder::class);
  113. $this->rootFolder->method('getUserFolder')
  114. ->with($this->userId)
  115. ->willReturn($userFolder);
  116. $userFolder->method('getParent')
  117. ->willReturn($userRoot);
  118. $sourceFile = $this->createMock(File::class);
  119. $userFolder->method('get')
  120. ->with('file')
  121. ->willReturn($sourceFile);
  122. $this->versionManager->method('getVersionFile')
  123. ->willThrowException(new NotFoundException());
  124. $res = $this->controller->getPreview('file', 10, 10, '42');
  125. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  126. $this->assertEquals($expected, $res);
  127. }
  128. }