ApiControllerTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 OCA\Files\Service\UserConfig;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\Files\File;
  34. use OCP\Files\Folder;
  35. use OCP\Files\NotFoundException;
  36. use OCP\Files\SimpleFS\ISimpleFile;
  37. use OCP\Files\StorageNotAvailableException;
  38. use OCP\IConfig;
  39. use OCP\IPreview;
  40. use OCP\IRequest;
  41. use OCP\IUser;
  42. use OCP\IUserSession;
  43. use OCP\Share\IManager;
  44. use Test\TestCase;
  45. /**
  46. * Class ApiController
  47. *
  48. * @package OCA\Files\Controller
  49. */
  50. class ApiControllerTest extends TestCase {
  51. /** @var string */
  52. private $appName = 'files';
  53. /** @var \OCP\IUser */
  54. private $user;
  55. /** @var IRequest */
  56. private $request;
  57. /** @var TagService */
  58. private $tagService;
  59. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  60. private $preview;
  61. /** @var ApiController */
  62. private $apiController;
  63. /** @var \OCP\Share\IManager */
  64. private $shareManager;
  65. /** @var \OCP\IConfig */
  66. private $config;
  67. /** @var Folder|\PHPUnit\Framework\MockObject\MockObject */
  68. private $userFolder;
  69. /** @var UserConfig|\PHPUnit\Framework\MockObject\MockObject */
  70. private $userConfig;
  71. protected function setUp(): void {
  72. parent::setUp();
  73. $this->request = $this->getMockBuilder(IRequest::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->user = $this->createMock(IUser::class);
  77. $this->user->expects($this->any())
  78. ->method('getUID')
  79. ->willReturn('user1');
  80. $userSession = $this->createMock(IUserSession::class);
  81. $userSession->expects($this->any())
  82. ->method('getUser')
  83. ->willReturn($this->user);
  84. $this->tagService = $this->getMockBuilder(TagService::class)
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $this->shareManager = $this->getMockBuilder(IManager::class)
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $this->preview = $this->getMockBuilder(IPreview::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $this->config = $this->createMock(IConfig::class);
  94. $this->userFolder = $this->getMockBuilder(Folder::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $this->userConfig = $this->createMock(UserConfig::class);
  98. $this->apiController = new ApiController(
  99. $this->appName,
  100. $this->request,
  101. $userSession,
  102. $this->tagService,
  103. $this->preview,
  104. $this->shareManager,
  105. $this->config,
  106. $this->userFolder,
  107. $this->userConfig
  108. );
  109. }
  110. public function testUpdateFileTagsEmpty() {
  111. $expected = new DataResponse([]);
  112. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt'));
  113. }
  114. public function testUpdateFileTagsWorking() {
  115. $this->tagService->expects($this->once())
  116. ->method('updateFileTags')
  117. ->with('/path.txt', ['Tag1', 'Tag2']);
  118. $expected = new DataResponse([
  119. 'tags' => [
  120. 'Tag1',
  121. 'Tag2'
  122. ],
  123. ]);
  124. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  125. }
  126. public function testUpdateFileTagsNotFoundException() {
  127. $this->tagService->expects($this->once())
  128. ->method('updateFileTags')
  129. ->with('/path.txt', ['Tag1', 'Tag2'])
  130. ->will($this->throwException(new NotFoundException('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 testUpdateFileTagsStorageNotAvailableException() {
  135. $this->tagService->expects($this->once())
  136. ->method('updateFileTags')
  137. ->with('/path.txt', ['Tag1', 'Tag2'])
  138. ->will($this->throwException(new StorageNotAvailableException('My error message')));
  139. $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_SERVICE_UNAVAILABLE);
  140. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  141. }
  142. public function testUpdateFileTagsStorageGenericException() {
  143. $this->tagService->expects($this->once())
  144. ->method('updateFileTags')
  145. ->with('/path.txt', ['Tag1', 'Tag2'])
  146. ->will($this->throwException(new \Exception('My error message')));
  147. $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND);
  148. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  149. }
  150. public function testGetThumbnailInvalidSize() {
  151. $this->userFolder->method('get')
  152. ->with($this->equalTo(''))
  153. ->willThrowException(new NotFoundException());
  154. $expected = new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST);
  155. $this->assertEquals($expected, $this->apiController->getThumbnail(0, 0, ''));
  156. }
  157. public function testGetThumbnailInvalidImage() {
  158. $file = $this->createMock(File::class);
  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 testGetThumbnail() {
  170. $file = $this->createMock(File::class);
  171. $this->userFolder->method('get')
  172. ->with($this->equalTo('known.jpg'))
  173. ->willReturn($file);
  174. $preview = $this->createMock(ISimpleFile::class);
  175. $preview->method('getName')->willReturn('my name');
  176. $preview->method('getMTime')->willReturn(42);
  177. $this->preview->expects($this->once())
  178. ->method('getPreview')
  179. ->with($this->equalTo($file), 10, 10, true)
  180. ->willReturn($preview);
  181. $ret = $this->apiController->getThumbnail(10, 10, 'known.jpg');
  182. $this->assertEquals(Http::STATUS_OK, $ret->getStatus());
  183. $this->assertInstanceOf(Http\FileDisplayResponse::class, $ret);
  184. }
  185. public function testUpdateFileSorting() {
  186. $mode = 'mtime';
  187. $direction = 'desc';
  188. $this->config->expects($this->exactly(2))
  189. ->method('setUserValue')
  190. ->withConsecutive(
  191. [$this->user->getUID(), 'files', 'file_sorting', $mode],
  192. [$this->user->getUID(), 'files', 'file_sorting_direction', $direction],
  193. );
  194. $expected = new HTTP\Response();
  195. $actual = $this->apiController->updateFileSorting($mode, $direction);
  196. $this->assertEquals($expected, $actual);
  197. }
  198. public function invalidSortingModeData() {
  199. return [
  200. ['color', 'asc'],
  201. ['name', 'size'],
  202. ['foo', 'bar']
  203. ];
  204. }
  205. /**
  206. * @dataProvider invalidSortingModeData
  207. */
  208. public function testUpdateInvalidFileSorting($mode, $direction) {
  209. $this->config->expects($this->never())
  210. ->method('setUserValue');
  211. $expected = new Http\Response(null);
  212. $expected->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
  213. $result = $this->apiController->updateFileSorting($mode, $direction);
  214. $this->assertEquals($expected, $result);
  215. }
  216. public function testShowHiddenFiles() {
  217. $show = false;
  218. $this->config->expects($this->once())
  219. ->method('setUserValue')
  220. ->with($this->user->getUID(), 'files', 'show_hidden', '0');
  221. $expected = new Http\Response();
  222. $actual = $this->apiController->showHiddenFiles($show);
  223. $this->assertEquals($expected, $actual);
  224. }
  225. public function testCropImagePreviews() {
  226. $crop = true;
  227. $this->config->expects($this->once())
  228. ->method('setUserValue')
  229. ->with($this->user->getUID(), 'files', 'crop_image_previews', '1');
  230. $expected = new Http\Response();
  231. $actual = $this->apiController->cropImagePreviews($crop);
  232. $this->assertEquals($expected, $actual);
  233. }
  234. }