FileSearchBackendTest.php 12 KB

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