ApiControllerTest.php 6.8 KB

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