ApiControllerTest.php 7.8 KB

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