1
0

FileSearchBackendTest.php 12 KB

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