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.

PreviewControllerTest.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_Trashbin\Tests\Controller;
  26. use OCA\Files_Trashbin\Controller\PreviewController;
  27. use OCA\Files_Trashbin\Trash\ITrashManager;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\Http\FileDisplayResponse;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\Files\File;
  33. use OCP\Files\Folder;
  34. use OCP\Files\IMimeTypeDetector;
  35. use OCP\Files\IRootFolder;
  36. use OCP\Files\SimpleFS\ISimpleFile;
  37. use OCP\IPreview;
  38. use OCP\IRequest;
  39. use OCP\IUser;
  40. use OCP\IUserSession;
  41. use Test\TestCase;
  42. class PreviewControllerTest extends TestCase {
  43. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  44. private $rootFolder;
  45. /** @var string */
  46. private $userId;
  47. /** @var IMimeTypeDetector|\PHPUnit\Framework\MockObject\MockObject */
  48. private $mimeTypeDetector;
  49. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  50. private $previewManager;
  51. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  52. private $time;
  53. /** @var PreviewController */
  54. private $controller;
  55. /** @var ITrashManager|\PHPUnit\Framework\MockObject\MockObject */
  56. private $trashManager;
  57. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  58. private $userSession;
  59. protected function setUp(): void {
  60. parent::setUp();
  61. $this->rootFolder = $this->createMock(IRootFolder::class);
  62. $this->userId = 'user';
  63. $this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class);
  64. $this->previewManager = $this->createMock(IPreview::class);
  65. $this->time = $this->createMock(ITimeFactory::class);
  66. $this->trashManager = $this->createMock(ITrashManager::class);
  67. $this->userSession = $this->createMock(IUserSession::class);
  68. $user = $this->createMock(IUser::class);
  69. $user->expects($this->any())
  70. ->method('getUID')
  71. ->willReturn($this->userId);
  72. $this->userSession->expects($this->any())
  73. ->method('getUser')
  74. ->willReturn($user);
  75. $this->controller = new PreviewController(
  76. 'files_versions',
  77. $this->createMock(IRequest::class),
  78. $this->rootFolder,
  79. $this->trashManager,
  80. $this->userSession,
  81. $this->mimeTypeDetector,
  82. $this->previewManager,
  83. $this->time
  84. );
  85. }
  86. public function testInvalidWidth() {
  87. $res = $this->controller->getPreview(42, 0);
  88. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  89. $this->assertEquals($expected, $res);
  90. }
  91. public function testInvalidHeight() {
  92. $res = $this->controller->getPreview(42, 10, 0);
  93. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  94. $this->assertEquals($expected, $res);
  95. }
  96. public function testValidPreview() {
  97. $userFolder = $this->createMock(Folder::class);
  98. $userRoot = $this->createMock(Folder::class);
  99. $trash = $this->createMock(Folder::class);
  100. $this->rootFolder->method('getUserFolder')
  101. ->with($this->userId)
  102. ->willReturn($userFolder);
  103. $userFolder->method('getParent')
  104. ->willReturn($userRoot);
  105. $userRoot->method('get')
  106. ->with('files_trashbin/files')
  107. ->willReturn($trash);
  108. $this->mimeTypeDetector->method('detectPath')
  109. ->with($this->equalTo('file'))
  110. ->willReturn('myMime');
  111. $file = $this->createMock(File::class);
  112. $trash->method('getById')
  113. ->with($this->equalTo(42))
  114. ->willReturn([$file]);
  115. $file->method('getName')
  116. ->willReturn('file.d1234');
  117. $file->method('getParent')
  118. ->willReturn($trash);
  119. $this->trashManager->expects($this->any())
  120. ->method('getTrashNodeById')
  121. ->willReturn($file);
  122. $preview = $this->createMock(ISimpleFile::class);
  123. $this->previewManager->method('getPreview')
  124. ->with($this->equalTo($file), 10, 10, true, IPreview::MODE_FILL, 'myMime')
  125. ->willReturn($preview);
  126. $preview->method('getMimeType')
  127. ->willReturn('previewMime');
  128. $this->time->method('getTime')
  129. ->willReturn(1337);
  130. $this->overwriteService(ITimeFactory::class, $this->time);
  131. $res = $this->controller->getPreview(42, 10, 10);
  132. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']);
  133. $expected->cacheFor(3600 * 24);
  134. $this->assertEquals($expected, $res);
  135. }
  136. public function testTrashFileNotFound() {
  137. $userFolder = $this->createMock(Folder::class);
  138. $userRoot = $this->createMock(Folder::class);
  139. $trash = $this->createMock(Folder::class);
  140. $this->rootFolder->method('getUserFolder')
  141. ->with($this->userId)
  142. ->willReturn($userFolder);
  143. $userFolder->method('getParent')
  144. ->willReturn($userRoot);
  145. $userRoot->method('get')
  146. ->with('files_trashbin/files')
  147. ->willReturn($trash);
  148. $trash->method('getById')
  149. ->with($this->equalTo(42))
  150. ->willReturn([]);
  151. $res = $this->controller->getPreview(42, 10, 10);
  152. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  153. $this->assertEquals($expected, $res);
  154. }
  155. public function testTrashFolder() {
  156. $userFolder = $this->createMock(Folder::class);
  157. $userRoot = $this->createMock(Folder::class);
  158. $trash = $this->createMock(Folder::class);
  159. $this->rootFolder->method('getUserFolder')
  160. ->with($this->userId)
  161. ->willReturn($userFolder);
  162. $userFolder->method('getParent')
  163. ->willReturn($userRoot);
  164. $userRoot->method('get')
  165. ->with('files_trashbin/files')
  166. ->willReturn($trash);
  167. $folder = $this->createMock(Folder::class);
  168. $this->trashManager->expects($this->any())
  169. ->method('getTrashNodeById')
  170. ->willReturn($folder);
  171. $trash->method('getById')
  172. ->with($this->equalTo(43))
  173. ->willReturn([$folder]);
  174. $res = $this->controller->getPreview(43, 10, 10);
  175. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  176. $this->assertEquals($expected, $res);
  177. }
  178. }