PreviewControllerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. $preview->method('getName')->willReturn('name');
  124. $preview->method('getMTime')->willReturn(42);
  125. $this->previewManager->method('getPreview')
  126. ->with($this->equalTo($file), 10, 10, true, IPreview::MODE_FILL, 'myMime')
  127. ->willReturn($preview);
  128. $preview->method('getMimeType')
  129. ->willReturn('previewMime');
  130. $this->time->method('getTime')
  131. ->willReturn(1337);
  132. $this->overwriteService(ITimeFactory::class, $this->time);
  133. $res = $this->controller->getPreview(42, 10, 10, false);
  134. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']);
  135. $expected->cacheFor(3600 * 24);
  136. $this->assertEquals($expected, $res);
  137. }
  138. public function testTrashFileNotFound() {
  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. $trash->method('getById')
  151. ->with($this->equalTo(42))
  152. ->willReturn([]);
  153. $res = $this->controller->getPreview(42, 10, 10);
  154. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  155. $this->assertEquals($expected, $res);
  156. }
  157. public function testTrashFolder() {
  158. $userFolder = $this->createMock(Folder::class);
  159. $userRoot = $this->createMock(Folder::class);
  160. $trash = $this->createMock(Folder::class);
  161. $this->rootFolder->method('getUserFolder')
  162. ->with($this->userId)
  163. ->willReturn($userFolder);
  164. $userFolder->method('getParent')
  165. ->willReturn($userRoot);
  166. $userRoot->method('get')
  167. ->with('files_trashbin/files')
  168. ->willReturn($trash);
  169. $folder = $this->createMock(Folder::class);
  170. $this->trashManager->expects($this->any())
  171. ->method('getTrashNodeById')
  172. ->willReturn($folder);
  173. $trash->method('getById')
  174. ->with($this->equalTo(43))
  175. ->willReturn([$folder]);
  176. $res = $this->controller->getPreview(43, 10, 10);
  177. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  178. $this->assertEquals($expected, $res);
  179. }
  180. }