ApiControllerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files\Controller;
  8. use OCA\Files\Service\TagService;
  9. use OCA\Files\Service\UserConfig;
  10. use OCA\Files\Service\ViewConfig;
  11. use OCP\AppFramework\Http;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\Files\File;
  14. use OCP\Files\Folder;
  15. use OCP\Files\IRootFolder;
  16. use OCP\Files\NotFoundException;
  17. use OCP\Files\SimpleFS\ISimpleFile;
  18. use OCP\Files\StorageNotAvailableException;
  19. use OCP\IConfig;
  20. use OCP\IL10N;
  21. use OCP\IPreview;
  22. use OCP\IRequest;
  23. use OCP\IUser;
  24. use OCP\IUserSession;
  25. use OCP\Share\IManager;
  26. use Psr\Log\LoggerInterface;
  27. use Test\TestCase;
  28. /**
  29. * Class ApiController
  30. *
  31. * @package OCA\Files\Controller
  32. */
  33. class ApiControllerTest extends TestCase {
  34. /** @var string */
  35. private $appName = 'files';
  36. /** @var \OCP\IUser */
  37. private $user;
  38. /** @var IRequest */
  39. private $request;
  40. /** @var TagService */
  41. private $tagService;
  42. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  43. private $preview;
  44. /** @var ApiController */
  45. private $apiController;
  46. /** @var \OCP\Share\IManager */
  47. private $shareManager;
  48. /** @var \OCP\IConfig */
  49. private $config;
  50. /** @var Folder|\PHPUnit\Framework\MockObject\MockObject */
  51. private $userFolder;
  52. /** @var UserConfig|\PHPUnit\Framework\MockObject\MockObject */
  53. private $userConfig;
  54. /** @var ViewConfig|\PHPUnit\Framework\MockObject\MockObject */
  55. private $viewConfig;
  56. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  57. private $l10n;
  58. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  59. private $rootFolder;
  60. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  61. private $logger;
  62. protected function setUp(): void {
  63. parent::setUp();
  64. $this->request = $this->getMockBuilder(IRequest::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->user = $this->createMock(IUser::class);
  68. $this->user->expects($this->any())
  69. ->method('getUID')
  70. ->willReturn('user1');
  71. $userSession = $this->createMock(IUserSession::class);
  72. $userSession->expects($this->any())
  73. ->method('getUser')
  74. ->willReturn($this->user);
  75. $this->tagService = $this->getMockBuilder(TagService::class)
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->shareManager = $this->getMockBuilder(IManager::class)
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $this->preview = $this->getMockBuilder(IPreview::class)
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $this->config = $this->createMock(IConfig::class);
  85. $this->userFolder = $this->getMockBuilder(Folder::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $this->userConfig = $this->createMock(UserConfig::class);
  89. $this->viewConfig = $this->createMock(ViewConfig::class);
  90. $this->l10n = $this->createMock(IL10N::class);
  91. $this->rootFolder = $this->createMock(IRootFolder::class);
  92. $this->logger = $this->createMock(LoggerInterface::class);
  93. $this->apiController = new ApiController(
  94. $this->appName,
  95. $this->request,
  96. $userSession,
  97. $this->tagService,
  98. $this->preview,
  99. $this->shareManager,
  100. $this->config,
  101. $this->userFolder,
  102. $this->userConfig,
  103. $this->viewConfig,
  104. $this->l10n,
  105. $this->rootFolder,
  106. $this->logger,
  107. );
  108. }
  109. public function testUpdateFileTagsEmpty(): void {
  110. $expected = new DataResponse([]);
  111. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt'));
  112. }
  113. public function testUpdateFileTagsWorking(): void {
  114. $this->tagService->expects($this->once())
  115. ->method('updateFileTags')
  116. ->with('/path.txt', ['Tag1', 'Tag2']);
  117. $expected = new DataResponse([
  118. 'tags' => [
  119. 'Tag1',
  120. 'Tag2'
  121. ],
  122. ]);
  123. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  124. }
  125. public function testUpdateFileTagsNotFoundException(): void {
  126. $this->tagService->expects($this->once())
  127. ->method('updateFileTags')
  128. ->with('/path.txt', ['Tag1', 'Tag2'])
  129. ->will($this->throwException(new NotFoundException('My error message')));
  130. $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND);
  131. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  132. }
  133. public function testUpdateFileTagsStorageNotAvailableException(): void {
  134. $this->tagService->expects($this->once())
  135. ->method('updateFileTags')
  136. ->with('/path.txt', ['Tag1', 'Tag2'])
  137. ->will($this->throwException(new StorageNotAvailableException('My error message')));
  138. $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_SERVICE_UNAVAILABLE);
  139. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  140. }
  141. public function testUpdateFileTagsStorageGenericException(): void {
  142. $this->tagService->expects($this->once())
  143. ->method('updateFileTags')
  144. ->with('/path.txt', ['Tag1', 'Tag2'])
  145. ->will($this->throwException(new \Exception('My error message')));
  146. $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND);
  147. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  148. }
  149. public function testGetThumbnailInvalidSize(): void {
  150. $this->userFolder->method('get')
  151. ->with($this->equalTo(''))
  152. ->willThrowException(new NotFoundException());
  153. $expected = new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST);
  154. $this->assertEquals($expected, $this->apiController->getThumbnail(0, 0, ''));
  155. }
  156. public function testGetThumbnailInvalidImage(): void {
  157. $file = $this->createMock(File::class);
  158. $file->method('getId')->willReturn(123);
  159. $this->userFolder->method('get')
  160. ->with($this->equalTo('unknown.jpg'))
  161. ->willReturn($file);
  162. $this->preview->expects($this->once())
  163. ->method('getPreview')
  164. ->with($file, 10, 10, true)
  165. ->willThrowException(new NotFoundException());
  166. $expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
  167. $this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
  168. }
  169. public function testGetThumbnailInvalidPartFile(): void {
  170. $file = $this->createMock(File::class);
  171. $file->method('getId')->willReturn(0);
  172. $this->userFolder->method('get')
  173. ->with($this->equalTo('unknown.jpg'))
  174. ->willReturn($file);
  175. $expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
  176. $this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
  177. }
  178. public function testGetThumbnail(): void {
  179. $file = $this->createMock(File::class);
  180. $file->method('getId')->willReturn(123);
  181. $this->userFolder->method('get')
  182. ->with($this->equalTo('known.jpg'))
  183. ->willReturn($file);
  184. $preview = $this->createMock(ISimpleFile::class);
  185. $preview->method('getName')->willReturn('my name');
  186. $preview->method('getMTime')->willReturn(42);
  187. $this->preview->expects($this->once())
  188. ->method('getPreview')
  189. ->with($this->equalTo($file), 10, 10, true)
  190. ->willReturn($preview);
  191. $ret = $this->apiController->getThumbnail(10, 10, 'known.jpg');
  192. $this->assertEquals(Http::STATUS_OK, $ret->getStatus());
  193. $this->assertInstanceOf(Http\FileDisplayResponse::class, $ret);
  194. }
  195. public function testShowHiddenFiles(): void {
  196. $show = false;
  197. $this->config->expects($this->once())
  198. ->method('setUserValue')
  199. ->with($this->user->getUID(), 'files', 'show_hidden', '0');
  200. $expected = new Http\Response();
  201. $actual = $this->apiController->showHiddenFiles($show);
  202. $this->assertEquals($expected, $actual);
  203. }
  204. public function testCropImagePreviews(): void {
  205. $crop = true;
  206. $this->config->expects($this->once())
  207. ->method('setUserValue')
  208. ->with($this->user->getUID(), 'files', 'crop_image_previews', '1');
  209. $expected = new Http\Response();
  210. $actual = $this->apiController->cropImagePreviews($crop);
  211. $this->assertEquals($expected, $actual);
  212. }
  213. }