PreviewControllerTest.php 6.3 KB

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