FileSearchBackendTest.php 12 KB

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