1
0

ApiControllerTest.php 7.9 KB

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