You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ApiControllerTest.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Nina Pypchenko <22447785+nina-py@users.noreply.github.com>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files\Controller;
  29. use OCA\Files\Service\TagService;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\Files\File;
  33. use OCP\Files\Folder;
  34. use OCP\Files\NotFoundException;
  35. use OCP\Files\SimpleFS\ISimpleFile;
  36. use OCP\Files\StorageNotAvailableException;
  37. use OCP\IConfig;
  38. use OCP\IPreview;
  39. use OCP\IRequest;
  40. use OCP\IUser;
  41. use OCP\IUserSession;
  42. use OCP\Share\IManager;
  43. use Test\TestCase;
  44. /**
  45. * Class ApiController
  46. *
  47. * @package OCA\Files\Controller
  48. */
  49. class ApiControllerTest extends TestCase {
  50. /** @var string */
  51. private $appName = 'files';
  52. /** @var \OCP\IUser */
  53. private $user;
  54. /** @var IRequest */
  55. private $request;
  56. /** @var TagService */
  57. private $tagService;
  58. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  59. private $preview;
  60. /** @var ApiController */
  61. private $apiController;
  62. /** @var \OCP\Share\IManager */
  63. private $shareManager;
  64. /** @var \OCP\IConfig */
  65. private $config;
  66. /** @var Folder|\PHPUnit\Framework\MockObject\MockObject */
  67. private $userFolder;
  68. protected function setUp(): void {
  69. parent::setUp();
  70. $this->request = $this->getMockBuilder(IRequest::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->user = $this->createMock(IUser::class);
  74. $this->user->expects($this->any())
  75. ->method('getUID')
  76. ->willReturn('user1');
  77. $userSession = $this->createMock(IUserSession::class);
  78. $userSession->expects($this->any())
  79. ->method('getUser')
  80. ->willReturn($this->user);
  81. $this->tagService = $this->getMockBuilder(TagService::class)
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $this->shareManager = $this->getMockBuilder(IManager::class)
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $this->preview = $this->getMockBuilder(IPreview::class)
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $this->config = $this->createMock(IConfig::class);
  91. $this->userFolder = $this->getMockBuilder(Folder::class)
  92. ->disableOriginalConstructor()
  93. ->getMock();
  94. $this->apiController = new ApiController(
  95. $this->appName,
  96. $this->request,
  97. $userSession,
  98. $this->tagService,
  99. $this->preview,
  100. $this->shareManager,
  101. $this->config,
  102. $this->userFolder
  103. );
  104. }
  105. public function testUpdateFileTagsEmpty() {
  106. $expected = new DataResponse([]);
  107. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt'));
  108. }
  109. public function testUpdateFileTagsWorking() {
  110. $this->tagService->expects($this->once())
  111. ->method('updateFileTags')
  112. ->with('/path.txt', ['Tag1', 'Tag2']);
  113. $expected = new DataResponse([
  114. 'tags' => [
  115. 'Tag1',
  116. 'Tag2'
  117. ],
  118. ]);
  119. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  120. }
  121. public function testUpdateFileTagsNotFoundException() {
  122. $this->tagService->expects($this->once())
  123. ->method('updateFileTags')
  124. ->with('/path.txt', ['Tag1', 'Tag2'])
  125. ->will($this->throwException(new NotFoundException('My error message')));
  126. $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND);
  127. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  128. }
  129. public function testUpdateFileTagsStorageNotAvailableException() {
  130. $this->tagService->expects($this->once())
  131. ->method('updateFileTags')
  132. ->with('/path.txt', ['Tag1', 'Tag2'])
  133. ->will($this->throwException(new StorageNotAvailableException('My error message')));
  134. $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_SERVICE_UNAVAILABLE);
  135. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  136. }
  137. public function testUpdateFileTagsStorageGenericException() {
  138. $this->tagService->expects($this->once())
  139. ->method('updateFileTags')
  140. ->with('/path.txt', ['Tag1', 'Tag2'])
  141. ->will($this->throwException(new \Exception('My error message')));
  142. $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND);
  143. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  144. }
  145. public function testGetThumbnailInvalidSize() {
  146. $this->userFolder->method('get')
  147. ->with($this->equalTo(''))
  148. ->willThrowException(new NotFoundException());
  149. $expected = new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST);
  150. $this->assertEquals($expected, $this->apiController->getThumbnail(0, 0, ''));
  151. }
  152. public function testGetThumbnailInvalidImage() {
  153. $file = $this->createMock(File::class);
  154. $this->userFolder->method('get')
  155. ->with($this->equalTo('unknown.jpg'))
  156. ->willReturn($file);
  157. $this->preview->expects($this->once())
  158. ->method('getPreview')
  159. ->with($file, 10, 10, true)
  160. ->willThrowException(new NotFoundException());
  161. $expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
  162. $this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
  163. }
  164. public function testGetThumbnail() {
  165. $file = $this->createMock(File::class);
  166. $this->userFolder->method('get')
  167. ->with($this->equalTo('known.jpg'))
  168. ->willReturn($file);
  169. $preview = $this->createMock(ISimpleFile::class);
  170. $preview->method('getName')->willReturn('my name');
  171. $preview->method('getMTime')->willReturn(42);
  172. $this->preview->expects($this->once())
  173. ->method('getPreview')
  174. ->with($this->equalTo($file), 10, 10, true)
  175. ->willReturn($preview);
  176. $ret = $this->apiController->getThumbnail(10, 10, 'known.jpg');
  177. $this->assertEquals(Http::STATUS_OK, $ret->getStatus());
  178. $this->assertInstanceOf(Http\FileDisplayResponse::class, $ret);
  179. }
  180. public function testUpdateFileSorting() {
  181. $mode = 'mtime';
  182. $direction = 'desc';
  183. $this->config->expects($this->exactly(2))
  184. ->method('setUserValue')
  185. ->withConsecutive(
  186. [$this->user->getUID(), 'files', 'file_sorting', $mode],
  187. [$this->user->getUID(), 'files', 'file_sorting_direction', $direction],
  188. );
  189. $expected = new HTTP\Response();
  190. $actual = $this->apiController->updateFileSorting($mode, $direction);
  191. $this->assertEquals($expected, $actual);
  192. }
  193. public function invalidSortingModeData() {
  194. return [
  195. ['color', 'asc'],
  196. ['name', 'size'],
  197. ['foo', 'bar']
  198. ];
  199. }
  200. /**
  201. * @dataProvider invalidSortingModeData
  202. */
  203. public function testUpdateInvalidFileSorting($mode, $direction) {
  204. $this->config->expects($this->never())
  205. ->method('setUserValue');
  206. $expected = new Http\Response(null);
  207. $expected->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
  208. $result = $this->apiController->updateFileSorting($mode, $direction);
  209. $this->assertEquals($expected, $result);
  210. }
  211. public function testShowHiddenFiles() {
  212. $show = false;
  213. $this->config->expects($this->once())
  214. ->method('setUserValue')
  215. ->with($this->user->getUID(), 'files', 'show_hidden', '0');
  216. $expected = new Http\Response();
  217. $actual = $this->apiController->showHiddenFiles($show);
  218. $this->assertEquals($expected, $actual);
  219. }
  220. public function testCropImagePreviews() {
  221. $crop = true;
  222. $this->config->expects($this->once())
  223. ->method('setUserValue')
  224. ->with($this->user->getUID(), 'files', 'crop_image_previews', '1');
  225. $expected = new Http\Response();
  226. $actual = $this->apiController->cropImagePreviews($crop);
  227. $this->assertEquals($expected, $actual);
  228. }
  229. }