apicontrollertest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Roeland Jago Douma <rullzer@owncloud.com>
  6. * @author Vincent Petry <pvince81@owncloud.com>
  7. *
  8. * @copyright Copyright (c) 2016, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files\Controller;
  25. use OC\Files\FileInfo;
  26. use OCP\AppFramework\Http;
  27. use OCP\Files\NotFoundException;
  28. use OCP\Files\StorageNotAvailableException;
  29. use Test\TestCase;
  30. use OCP\IRequest;
  31. use OCA\Files\Service\TagService;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\IPreview;
  34. use OCP\Image;
  35. /**
  36. * Class ApiController
  37. *
  38. * @package OCA\Files\Controller
  39. */
  40. class ApiControllerTest extends TestCase {
  41. /** @var string */
  42. private $appName = 'files';
  43. /** @var IRequest */
  44. private $request;
  45. /** @var TagService */
  46. private $tagService;
  47. /** @var IPreview */
  48. private $preview;
  49. /** @var ApiController */
  50. private $apiController;
  51. public function setUp() {
  52. $this->request = $this->getMockBuilder('\OCP\IRequest')
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->tagService = $this->getMockBuilder('\OCA\Files\Service\TagService')
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->preview = $this->getMockBuilder('\OCP\IPreview')
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->apiController = new ApiController(
  62. $this->appName,
  63. $this->request,
  64. $this->tagService,
  65. $this->preview
  66. );
  67. }
  68. public function testGetFilesByTagEmpty() {
  69. $tagName = 'MyTagName';
  70. $this->tagService->expects($this->once())
  71. ->method('getFilesByTag')
  72. ->with($this->equalTo([$tagName]))
  73. ->will($this->returnValue([]));
  74. $expected = new DataResponse(['files' => []]);
  75. $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName]));
  76. }
  77. public function testGetFilesByTagSingle() {
  78. $tagName = 'MyTagName';
  79. $fileInfo = new FileInfo(
  80. '/root.txt',
  81. $this->getMockBuilder('\OC\Files\Storage\Storage')
  82. ->disableOriginalConstructor()
  83. ->getMock(),
  84. '/var/www/root.txt',
  85. [
  86. 'mtime' => 55,
  87. 'mimetype' => 'application/pdf',
  88. 'permissions' => 31,
  89. 'size' => 1234,
  90. 'etag' => 'MyEtag',
  91. ],
  92. $this->getMockBuilder('\OCP\Files\Mount\IMountPoint')
  93. ->disableOriginalConstructor()
  94. ->getMock()
  95. );
  96. $this->tagService->expects($this->once())
  97. ->method('getFilesByTag')
  98. ->with($this->equalTo([$tagName]))
  99. ->will($this->returnValue([$fileInfo]));
  100. $expected = new DataResponse([
  101. 'files' => [
  102. [
  103. 'id' => null,
  104. 'parentId' => null,
  105. 'mtime' => 55000,
  106. 'name' => 'root.txt',
  107. 'permissions' => 31,
  108. 'mimetype' => 'application/pdf',
  109. 'size' => 1234,
  110. 'type' => 'file',
  111. 'etag' => 'MyEtag',
  112. 'path' => '/',
  113. 'tags' => [
  114. [
  115. 'MyTagName'
  116. ]
  117. ],
  118. ],
  119. ],
  120. ]);
  121. $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName]));
  122. }
  123. public function testGetFilesByTagMultiple() {
  124. $tagName = 'MyTagName';
  125. $fileInfo1 = new FileInfo(
  126. '/root.txt',
  127. $this->getMockBuilder('\OC\Files\Storage\Storage')
  128. ->disableOriginalConstructor()
  129. ->getMock(),
  130. '/var/www/root.txt',
  131. [
  132. 'mtime' => 55,
  133. 'mimetype' => 'application/pdf',
  134. 'permissions' => 31,
  135. 'size' => 1234,
  136. 'etag' => 'MyEtag',
  137. ],
  138. $this->getMockBuilder('\OCP\Files\Mount\IMountPoint')
  139. ->disableOriginalConstructor()
  140. ->getMock()
  141. );
  142. $fileInfo2 = new FileInfo(
  143. '/root.txt',
  144. $this->getMockBuilder('\OC\Files\Storage\Storage')
  145. ->disableOriginalConstructor()
  146. ->getMock(),
  147. '/var/www/some/sub.txt',
  148. [
  149. 'mtime' => 999,
  150. 'mimetype' => 'application/binary',
  151. 'permissions' => 31,
  152. 'size' => 9876,
  153. 'etag' => 'SubEtag',
  154. ],
  155. $this->getMockBuilder('\OCP\Files\Mount\IMountPoint')
  156. ->disableOriginalConstructor()
  157. ->getMock()
  158. );
  159. $this->tagService->expects($this->once())
  160. ->method('getFilesByTag')
  161. ->with($this->equalTo([$tagName]))
  162. ->will($this->returnValue([$fileInfo1, $fileInfo2]));
  163. $expected = new DataResponse([
  164. 'files' => [
  165. [
  166. 'id' => null,
  167. 'parentId' => null,
  168. 'mtime' => 55000,
  169. 'name' => 'root.txt',
  170. 'permissions' => 31,
  171. 'mimetype' => 'application/pdf',
  172. 'size' => 1234,
  173. 'type' => 'file',
  174. 'etag' => 'MyEtag',
  175. 'path' => '/',
  176. 'tags' => [
  177. [
  178. 'MyTagName'
  179. ]
  180. ],
  181. ],
  182. [
  183. 'id' => null,
  184. 'parentId' => null,
  185. 'mtime' => 999000,
  186. 'name' => 'root.txt',
  187. 'permissions' => 31,
  188. 'mimetype' => 'application/binary',
  189. 'size' => 9876,
  190. 'type' => 'file',
  191. 'etag' => 'SubEtag',
  192. 'path' => '/',
  193. 'tags' => [
  194. [
  195. 'MyTagName'
  196. ]
  197. ],
  198. ]
  199. ],
  200. ]);
  201. $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName]));
  202. }
  203. public function testUpdateFileTagsEmpty() {
  204. $expected = new DataResponse([]);
  205. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt'));
  206. }
  207. public function testUpdateFileTagsWorking() {
  208. $this->tagService->expects($this->once())
  209. ->method('updateFileTags')
  210. ->with('/path.txt', ['Tag1', 'Tag2']);
  211. $expected = new DataResponse([
  212. 'tags' => [
  213. 'Tag1',
  214. 'Tag2'
  215. ],
  216. ]);
  217. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  218. }
  219. public function testUpdateFileTagsNotFoundException() {
  220. $this->tagService->expects($this->once())
  221. ->method('updateFileTags')
  222. ->with('/path.txt', ['Tag1', 'Tag2'])
  223. ->will($this->throwException(new NotFoundException('My error message')));
  224. $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND);
  225. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  226. }
  227. public function testUpdateFileTagsStorageNotAvailableException() {
  228. $this->tagService->expects($this->once())
  229. ->method('updateFileTags')
  230. ->with('/path.txt', ['Tag1', 'Tag2'])
  231. ->will($this->throwException(new StorageNotAvailableException('My error message')));
  232. $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_SERVICE_UNAVAILABLE);
  233. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  234. }
  235. public function testUpdateFileTagsStorageGenericException() {
  236. $this->tagService->expects($this->once())
  237. ->method('updateFileTags')
  238. ->with('/path.txt', ['Tag1', 'Tag2'])
  239. ->will($this->throwException(new \Exception('My error message')));
  240. $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND);
  241. $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
  242. }
  243. public function testGetThumbnailInvalidSize() {
  244. $expected = new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST);
  245. $this->assertEquals($expected, $this->apiController->getThumbnail(0, 0, ''));
  246. }
  247. public function testGetThumbnailInvaidImage() {
  248. $this->preview->expects($this->once())
  249. ->method('createPreview')
  250. ->with('files/unknown.jpg', 10, 10, true)
  251. ->willReturn(new Image);
  252. $expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
  253. $this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
  254. }
  255. public function testGetThumbnail() {
  256. $this->preview->expects($this->once())
  257. ->method('createPreview')
  258. ->with('files/known.jpg', 10, 10, true)
  259. ->willReturn(new Image(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  260. $ret = $this->apiController->getThumbnail(10, 10, 'known.jpg');
  261. $this->assertEquals(Http::STATUS_OK, $ret->getStatus());
  262. }
  263. }