1
0

ApiControllerTest.php 7.6 KB

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