PreviewControllerTest.php 6.3 KB

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