FileSearchBackendTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\Files;
  7. use OC\Files\Search\SearchComparison;
  8. use OC\Files\Search\SearchQuery;
  9. use OC\Files\View;
  10. use OCA\DAV\Connector\Sabre\Directory;
  11. use OCA\DAV\Connector\Sabre\File;
  12. use OCA\DAV\Connector\Sabre\FilesPlugin;
  13. use OCA\DAV\Connector\Sabre\ObjectTree;
  14. use OCA\DAV\Files\FileSearchBackend;
  15. use OCP\Files\FileInfo;
  16. use OCP\Files\Folder;
  17. use OCP\Files\IRootFolder;
  18. use OCP\Files\Search\ISearchBinaryOperator;
  19. use OCP\Files\Search\ISearchComparison;
  20. use OCP\Files\Search\ISearchQuery;
  21. use OCP\FilesMetadata\IFilesMetadataManager;
  22. use OCP\IUser;
  23. use OCP\Share\IManager;
  24. use SearchDAV\Backend\SearchPropertyDefinition;
  25. use SearchDAV\Query\Limit;
  26. use SearchDAV\Query\Operator;
  27. use SearchDAV\Query\Query;
  28. use Test\TestCase;
  29. class FileSearchBackendTest extends TestCase {
  30. /** @var ObjectTree|\PHPUnit\Framework\MockObject\MockObject */
  31. private $tree;
  32. /** @var IUser */
  33. private $user;
  34. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  35. private $rootFolder;
  36. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  37. private $shareManager;
  38. /** @var View|\PHPUnit\Framework\MockObject\MockObject */
  39. private $view;
  40. /** @var Folder|\PHPUnit\Framework\MockObject\MockObject */
  41. private $searchFolder;
  42. /** @var FileSearchBackend */
  43. private $search;
  44. /** @var Directory|\PHPUnit\Framework\MockObject\MockObject */
  45. private $davFolder;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->user = $this->createMock(IUser::class);
  49. $this->user->expects($this->any())
  50. ->method('getUID')
  51. ->willReturn('test');
  52. $this->tree = $this->getMockBuilder(ObjectTree::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->view = $this->createMock(View::class);
  56. $this->view->expects($this->any())
  57. ->method('getRoot')
  58. ->willReturn('');
  59. $this->view->expects($this->any())
  60. ->method('getRelativePath')
  61. ->willReturnArgument(0);
  62. $this->rootFolder = $this->createMock(IRootFolder::class);
  63. $this->shareManager = $this->createMock(IManager::class);
  64. $this->searchFolder = $this->createMock(Folder::class);
  65. $fileInfo = $this->createMock(FileInfo::class);
  66. $this->davFolder = $this->createMock(Directory::class);
  67. $this->davFolder->expects($this->any())
  68. ->method('getFileInfo')
  69. ->willReturn($fileInfo);
  70. $this->rootFolder->expects($this->any())
  71. ->method('get')
  72. ->willReturn($this->searchFolder);
  73. $filesMetadataManager = $this->createMock(IFilesMetadataManager::class);
  74. $this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager);
  75. }
  76. public function testSearchFilename(): void {
  77. $this->tree->expects($this->any())
  78. ->method('getNodeForPath')
  79. ->willReturn($this->davFolder);
  80. $this->searchFolder->expects($this->once())
  81. ->method('search')
  82. ->with(new SearchQuery(
  83. new SearchComparison(
  84. ISearchComparison::COMPARE_EQUAL,
  85. 'name',
  86. 'foo'
  87. ),
  88. 0,
  89. 0,
  90. [],
  91. $this->user
  92. ))
  93. ->willReturn([
  94. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
  95. ]);
  96. $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
  97. $result = $this->search->search($query);
  98. $this->assertCount(1, $result);
  99. $this->assertEquals('/files/test/test/path', $result[0]->href);
  100. }
  101. public function testSearchMimetype(): void {
  102. $this->tree->expects($this->any())
  103. ->method('getNodeForPath')
  104. ->willReturn($this->davFolder);
  105. $this->searchFolder->expects($this->once())
  106. ->method('search')
  107. ->with(new SearchQuery(
  108. new SearchComparison(
  109. ISearchComparison::COMPARE_EQUAL,
  110. 'mimetype',
  111. 'foo'
  112. ),
  113. 0,
  114. 0,
  115. [],
  116. $this->user
  117. ))
  118. ->willReturn([
  119. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
  120. ]);
  121. $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}getcontenttype', 'foo');
  122. $result = $this->search->search($query);
  123. $this->assertCount(1, $result);
  124. $this->assertEquals('/files/test/test/path', $result[0]->href);
  125. }
  126. public function testSearchSize(): void {
  127. $this->tree->expects($this->any())
  128. ->method('getNodeForPath')
  129. ->willReturn($this->davFolder);
  130. $this->searchFolder->expects($this->once())
  131. ->method('search')
  132. ->with(new SearchQuery(
  133. new SearchComparison(
  134. ISearchComparison::COMPARE_GREATER_THAN,
  135. 'size',
  136. 10
  137. ),
  138. 0,
  139. 0,
  140. [],
  141. $this->user
  142. ))
  143. ->willReturn([
  144. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
  145. ]);
  146. $query = $this->getBasicQuery(Operator::OPERATION_GREATER_THAN, FilesPlugin::SIZE_PROPERTYNAME, 10);
  147. $result = $this->search->search($query);
  148. $this->assertCount(1, $result);
  149. $this->assertEquals('/files/test/test/path', $result[0]->href);
  150. }
  151. public function testSearchMtime(): void {
  152. $this->tree->expects($this->any())
  153. ->method('getNodeForPath')
  154. ->willReturn($this->davFolder);
  155. $this->searchFolder->expects($this->once())
  156. ->method('search')
  157. ->with(new SearchQuery(
  158. new SearchComparison(
  159. ISearchComparison::COMPARE_GREATER_THAN,
  160. 'mtime',
  161. 10
  162. ),
  163. 0,
  164. 0,
  165. [],
  166. $this->user
  167. ))
  168. ->willReturn([
  169. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
  170. ]);
  171. $query = $this->getBasicQuery(Operator::OPERATION_GREATER_THAN, '{DAV:}getlastmodified', 10);
  172. $result = $this->search->search($query);
  173. $this->assertCount(1, $result);
  174. $this->assertEquals('/files/test/test/path', $result[0]->href);
  175. }
  176. public function testSearchIsCollection(): void {
  177. $this->tree->expects($this->any())
  178. ->method('getNodeForPath')
  179. ->willReturn($this->davFolder);
  180. $this->searchFolder->expects($this->once())
  181. ->method('search')
  182. ->with(new SearchQuery(
  183. new SearchComparison(
  184. ISearchComparison::COMPARE_EQUAL,
  185. 'mimetype',
  186. FileInfo::MIMETYPE_FOLDER
  187. ),
  188. 0,
  189. 0,
  190. [],
  191. $this->user
  192. ))
  193. ->willReturn([
  194. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
  195. ]);
  196. $query = $this->getBasicQuery(Operator::OPERATION_IS_COLLECTION, 'yes');
  197. $result = $this->search->search($query);
  198. $this->assertCount(1, $result);
  199. $this->assertEquals('/files/test/test/path', $result[0]->href);
  200. }
  201. public function testSearchInvalidProp(): void {
  202. $this->expectException(\InvalidArgumentException::class);
  203. $this->tree->expects($this->any())
  204. ->method('getNodeForPath')
  205. ->willReturn($this->davFolder);
  206. $this->searchFolder->expects($this->never())
  207. ->method('search');
  208. $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}getetag', 'foo');
  209. $this->search->search($query);
  210. }
  211. private function getBasicQuery($type, $property, $value = null) {
  212. $scope = new \SearchDAV\Query\Scope('/', 'infinite');
  213. $scope->path = '/';
  214. $from = [$scope];
  215. $orderBy = [];
  216. $select = [];
  217. if (is_null($value)) {
  218. $where = new Operator(
  219. $type,
  220. [new \SearchDAV\Query\Literal($property)]
  221. );
  222. } else {
  223. $where = new Operator(
  224. $type,
  225. [new SearchPropertyDefinition($property, true, true, true), new \SearchDAV\Query\Literal($value)]
  226. );
  227. }
  228. $limit = new Limit();
  229. return new Query($select, $from, $where, $orderBy, $limit);
  230. }
  231. public function testSearchNonFolder(): void {
  232. $this->expectException(\InvalidArgumentException::class);
  233. $davNode = $this->createMock(File::class);
  234. $this->tree->expects($this->any())
  235. ->method('getNodeForPath')
  236. ->willReturn($davNode);
  237. $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
  238. $this->search->search($query);
  239. }
  240. public function testSearchLimitOwnerBasic(): void {
  241. $this->tree->expects($this->any())
  242. ->method('getNodeForPath')
  243. ->willReturn($this->davFolder);
  244. /** @var ISearchQuery|null $receivedQuery */
  245. $receivedQuery = null;
  246. $this->searchFolder
  247. ->method('search')
  248. ->willReturnCallback(function ($query) use (&$receivedQuery) {
  249. $receivedQuery = $query;
  250. return [
  251. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
  252. ];
  253. });
  254. $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID());
  255. $this->search->search($query);
  256. $this->assertNotNull($receivedQuery);
  257. $this->assertTrue($receivedQuery->limitToHome());
  258. /** @var ISearchBinaryOperator $operator */
  259. $operator = $receivedQuery->getSearchOperation();
  260. $this->assertInstanceOf(ISearchBinaryOperator::class, $operator);
  261. $this->assertEquals(ISearchBinaryOperator::OPERATOR_AND, $operator->getType());
  262. $this->assertEmpty($operator->getArguments());
  263. }
  264. public function testSearchLimitOwnerNested(): void {
  265. $this->tree->expects($this->any())
  266. ->method('getNodeForPath')
  267. ->willReturn($this->davFolder);
  268. /** @var ISearchQuery|null $receivedQuery */
  269. $receivedQuery = null;
  270. $this->searchFolder
  271. ->method('search')
  272. ->willReturnCallback(function ($query) use (&$receivedQuery) {
  273. $receivedQuery = $query;
  274. return [
  275. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path'),
  276. ];
  277. });
  278. $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID());
  279. $query->where = new Operator(
  280. Operator::OPERATION_AND,
  281. [
  282. new Operator(
  283. Operator::OPERATION_EQUAL,
  284. [new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), new \SearchDAV\Query\Literal('image/png')]
  285. ),
  286. new Operator(
  287. Operator::OPERATION_EQUAL,
  288. [new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, true, true, true), new \SearchDAV\Query\Literal($this->user->getUID())]
  289. ),
  290. ]
  291. );
  292. $this->search->search($query);
  293. $this->assertNotNull($receivedQuery);
  294. $this->assertTrue($receivedQuery->limitToHome());
  295. /** @var ISearchBinaryOperator $operator */
  296. $operator = $receivedQuery->getSearchOperation();
  297. $this->assertInstanceOf(ISearchBinaryOperator::class, $operator);
  298. $this->assertEquals(ISearchBinaryOperator::OPERATOR_AND, $operator->getType());
  299. $this->assertCount(2, $operator->getArguments());
  300. /** @var ISearchBinaryOperator $operator */
  301. $operator = $operator->getArguments()[1];
  302. $this->assertInstanceOf(ISearchBinaryOperator::class, $operator);
  303. $this->assertEquals(ISearchBinaryOperator::OPERATOR_AND, $operator->getType());
  304. $this->assertEmpty($operator->getArguments());
  305. }
  306. public function testSearchOperatorLimit(): void {
  307. $this->tree->expects($this->any())
  308. ->method('getNodeForPath')
  309. ->willReturn($this->davFolder);
  310. $innerOperator = new Operator(
  311. Operator::OPERATION_EQUAL,
  312. [new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), new \SearchDAV\Query\Literal('image/png')]
  313. );
  314. // 5 child operators
  315. $level1Operator = new Operator(
  316. Operator::OPERATION_AND,
  317. [
  318. $innerOperator,
  319. $innerOperator,
  320. $innerOperator,
  321. $innerOperator,
  322. $innerOperator,
  323. ]
  324. );
  325. // 5^2 = 25 child operators
  326. $level2Operator = new Operator(
  327. Operator::OPERATION_AND,
  328. [
  329. $level1Operator,
  330. $level1Operator,
  331. $level1Operator,
  332. $level1Operator,
  333. $level1Operator,
  334. ]
  335. );
  336. // 5^3 = 125 child operators
  337. $level3Operator = new Operator(
  338. Operator::OPERATION_AND,
  339. [
  340. $level2Operator,
  341. $level2Operator,
  342. $level2Operator,
  343. $level2Operator,
  344. $level2Operator,
  345. ]
  346. );
  347. $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID());
  348. $query->where = $level3Operator;
  349. $this->expectException(\InvalidArgumentException::class);
  350. $this->search->search($query);
  351. }
  352. }