1
0

FileSearchBackendTest.php 11 KB

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