1
0

PreviewControllerTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_Trashbin\Tests\Controller;
  7. use OCA\Files_Trashbin\Controller\PreviewController;
  8. use OCA\Files_Trashbin\Trash\ITrashManager;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\DataResponse;
  11. use OCP\AppFramework\Http\FileDisplayResponse;
  12. use OCP\AppFramework\Utility\ITimeFactory;
  13. use OCP\Files\File;
  14. use OCP\Files\Folder;
  15. use OCP\Files\IMimeTypeDetector;
  16. use OCP\Files\IRootFolder;
  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 ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  33. private $time;
  34. /** @var PreviewController */
  35. private $controller;
  36. /** @var ITrashManager|\PHPUnit\Framework\MockObject\MockObject */
  37. private $trashManager;
  38. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  39. private $userSession;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->rootFolder = $this->createMock(IRootFolder::class);
  43. $this->userId = 'user';
  44. $this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class);
  45. $this->previewManager = $this->createMock(IPreview::class);
  46. $this->time = $this->createMock(ITimeFactory::class);
  47. $this->trashManager = $this->createMock(ITrashManager::class);
  48. $this->userSession = $this->createMock(IUserSession::class);
  49. $user = $this->createMock(IUser::class);
  50. $user->expects($this->any())
  51. ->method('getUID')
  52. ->willReturn($this->userId);
  53. $this->userSession->expects($this->any())
  54. ->method('getUser')
  55. ->willReturn($user);
  56. $this->controller = new PreviewController(
  57. 'files_versions',
  58. $this->createMock(IRequest::class),
  59. $this->rootFolder,
  60. $this->trashManager,
  61. $this->userSession,
  62. $this->mimeTypeDetector,
  63. $this->previewManager,
  64. $this->time
  65. );
  66. }
  67. public function testInvalidWidth(): void {
  68. $res = $this->controller->getPreview(42, 0);
  69. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  70. $this->assertEquals($expected, $res);
  71. }
  72. public function testInvalidHeight(): void {
  73. $res = $this->controller->getPreview(42, 10, 0);
  74. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  75. $this->assertEquals($expected, $res);
  76. }
  77. public function testValidPreview(): void {
  78. $userFolder = $this->createMock(Folder::class);
  79. $userRoot = $this->createMock(Folder::class);
  80. $trash = $this->createMock(Folder::class);
  81. $this->rootFolder->method('getUserFolder')
  82. ->with($this->userId)
  83. ->willReturn($userFolder);
  84. $userFolder->method('getParent')
  85. ->willReturn($userRoot);
  86. $userRoot->method('get')
  87. ->with('files_trashbin/files')
  88. ->willReturn($trash);
  89. $this->mimeTypeDetector->method('detectPath')
  90. ->with($this->equalTo('file'))
  91. ->willReturn('myMime');
  92. $file = $this->createMock(File::class);
  93. $trash->method('getById')
  94. ->with($this->equalTo(42))
  95. ->willReturn([$file]);
  96. $file->method('getName')
  97. ->willReturn('file.d1234');
  98. $file->method('getParent')
  99. ->willReturn($trash);
  100. $this->trashManager->expects($this->any())
  101. ->method('getTrashNodeById')
  102. ->willReturn($file);
  103. $preview = $this->createMock(ISimpleFile::class);
  104. $preview->method('getName')->willReturn('name');
  105. $preview->method('getMTime')->willReturn(42);
  106. $this->previewManager->method('getPreview')
  107. ->with($this->equalTo($file), 10, 10, true, IPreview::MODE_FILL, 'myMime')
  108. ->willReturn($preview);
  109. $preview->method('getMimeType')
  110. ->willReturn('previewMime');
  111. $this->time->method('getTime')
  112. ->willReturn(1337);
  113. $this->overwriteService(ITimeFactory::class, $this->time);
  114. $res = $this->controller->getPreview(42, 10, 10, false);
  115. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']);
  116. $expected->cacheFor(3600 * 24);
  117. $this->assertEquals($expected, $res);
  118. }
  119. public function testTrashFileNotFound(): void {
  120. $userFolder = $this->createMock(Folder::class);
  121. $userRoot = $this->createMock(Folder::class);
  122. $trash = $this->createMock(Folder::class);
  123. $this->rootFolder->method('getUserFolder')
  124. ->with($this->userId)
  125. ->willReturn($userFolder);
  126. $userFolder->method('getParent')
  127. ->willReturn($userRoot);
  128. $userRoot->method('get')
  129. ->with('files_trashbin/files')
  130. ->willReturn($trash);
  131. $trash->method('getById')
  132. ->with($this->equalTo(42))
  133. ->willReturn([]);
  134. $res = $this->controller->getPreview(42, 10, 10);
  135. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  136. $this->assertEquals($expected, $res);
  137. }
  138. public function testTrashFolder(): void {
  139. $userFolder = $this->createMock(Folder::class);
  140. $userRoot = $this->createMock(Folder::class);
  141. $trash = $this->createMock(Folder::class);
  142. $this->rootFolder->method('getUserFolder')
  143. ->with($this->userId)
  144. ->willReturn($userFolder);
  145. $userFolder->method('getParent')
  146. ->willReturn($userRoot);
  147. $userRoot->method('get')
  148. ->with('files_trashbin/files')
  149. ->willReturn($trash);
  150. $folder = $this->createMock(Folder::class);
  151. $this->trashManager->expects($this->any())
  152. ->method('getTrashNodeById')
  153. ->willReturn($folder);
  154. $trash->method('getById')
  155. ->with($this->equalTo(43))
  156. ->willReturn([$folder]);
  157. $res = $this->controller->getPreview(43, 10, 10);
  158. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  159. $this->assertEquals($expected, $res);
  160. }
  161. }