FileSearchBackendTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Tests\Files;
  25. use OC\Files\Search\SearchComparison;
  26. use OC\Files\Search\SearchQuery;
  27. use OC\Files\View;
  28. use OCA\DAV\Connector\Sabre\CachingTree;
  29. use OCA\DAV\Connector\Sabre\Directory;
  30. use OCA\DAV\Connector\Sabre\File;
  31. use OCA\DAV\Connector\Sabre\FilesPlugin;
  32. use OCA\DAV\Files\FileSearchBackend;
  33. use OCP\Files\FileInfo;
  34. use OCP\Files\Folder;
  35. use OCP\Files\IRootFolder;
  36. use OCP\Files\Search\ISearchBinaryOperator;
  37. use OCP\Files\Search\ISearchComparison;
  38. use OCP\Files\Search\ISearchQuery;
  39. use OCP\IUser;
  40. use OCP\Share\IManager;
  41. use SearchDAV\Backend\SearchPropertyDefinition;
  42. use SearchDAV\Query\Limit;
  43. use SearchDAV\Query\Query;
  44. use SearchDAV\XML\BasicSearch;
  45. use SearchDAV\XML\Literal;
  46. use SearchDAV\XML\Operator;
  47. use SearchDAV\XML\Scope;
  48. use Test\TestCase;
  49. class FileSearchBackendTest extends TestCase {
  50. /** @var CachingTree|\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(CachingTree::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->view = $this->getMockBuilder(View::class)
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->view->expects($this->any())
  79. ->method('getRelativePath')
  80. ->willReturnArgument(0);
  81. $this->rootFolder = $this->createMock(IRootFolder::class);
  82. $this->shareManager = $this->createMock(IManager::class);
  83. $this->searchFolder = $this->createMock(Folder::class);
  84. $fileInfo = $this->createMock(FileInfo::class);
  85. $this->davFolder = $this->createMock(Directory::class);
  86. $this->davFolder->expects($this->any())
  87. ->method('getFileInfo')
  88. ->willReturn($fileInfo);
  89. $this->rootFolder->expects($this->any())
  90. ->method('get')
  91. ->willReturn($this->searchFolder);
  92. $this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view);
  93. }
  94. public function testSearchFilename() {
  95. $this->tree->expects($this->any())
  96. ->method('getNodeForPath')
  97. ->willReturn($this->davFolder);
  98. $this->searchFolder->expects($this->once())
  99. ->method('search')
  100. ->with(new SearchQuery(
  101. new SearchComparison(
  102. ISearchComparison::COMPARE_EQUAL,
  103. 'name',
  104. 'foo'
  105. ),
  106. 0,
  107. 0,
  108. [],
  109. $this->user
  110. ))
  111. ->willReturn([
  112. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  113. ]);
  114. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
  115. $result = $this->search->search($query);
  116. $this->assertCount(1, $result);
  117. $this->assertEquals('/files/test/test/path', $result[0]->href);
  118. }
  119. public function testSearchMimetype() {
  120. $this->tree->expects($this->any())
  121. ->method('getNodeForPath')
  122. ->willReturn($this->davFolder);
  123. $this->searchFolder->expects($this->once())
  124. ->method('search')
  125. ->with(new SearchQuery(
  126. new SearchComparison(
  127. ISearchComparison::COMPARE_EQUAL,
  128. 'mimetype',
  129. 'foo'
  130. ),
  131. 0,
  132. 0,
  133. [],
  134. $this->user
  135. ))
  136. ->willReturn([
  137. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  138. ]);
  139. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}getcontenttype', 'foo');
  140. $result = $this->search->search($query);
  141. $this->assertCount(1, $result);
  142. $this->assertEquals('/files/test/test/path', $result[0]->href);
  143. }
  144. public function testSearchSize() {
  145. $this->tree->expects($this->any())
  146. ->method('getNodeForPath')
  147. ->willReturn($this->davFolder);
  148. $this->searchFolder->expects($this->once())
  149. ->method('search')
  150. ->with(new SearchQuery(
  151. new SearchComparison(
  152. ISearchComparison::COMPARE_GREATER_THAN,
  153. 'size',
  154. 10
  155. ),
  156. 0,
  157. 0,
  158. [],
  159. $this->user
  160. ))
  161. ->willReturn([
  162. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  163. ]);
  164. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_GREATER_THAN, FilesPlugin::SIZE_PROPERTYNAME, 10);
  165. $result = $this->search->search($query);
  166. $this->assertCount(1, $result);
  167. $this->assertEquals('/files/test/test/path', $result[0]->href);
  168. }
  169. public function testSearchMtime() {
  170. $this->tree->expects($this->any())
  171. ->method('getNodeForPath')
  172. ->willReturn($this->davFolder);
  173. $this->searchFolder->expects($this->once())
  174. ->method('search')
  175. ->with(new SearchQuery(
  176. new SearchComparison(
  177. ISearchComparison::COMPARE_GREATER_THAN,
  178. 'mtime',
  179. 10
  180. ),
  181. 0,
  182. 0,
  183. [],
  184. $this->user
  185. ))
  186. ->willReturn([
  187. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  188. ]);
  189. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_GREATER_THAN, '{DAV:}getlastmodified', 10);
  190. $result = $this->search->search($query);
  191. $this->assertCount(1, $result);
  192. $this->assertEquals('/files/test/test/path', $result[0]->href);
  193. }
  194. public function testSearchIsCollection() {
  195. $this->tree->expects($this->any())
  196. ->method('getNodeForPath')
  197. ->willReturn($this->davFolder);
  198. $this->searchFolder->expects($this->once())
  199. ->method('search')
  200. ->with(new SearchQuery(
  201. new SearchComparison(
  202. ISearchComparison::COMPARE_EQUAL,
  203. 'mimetype',
  204. FileInfo::MIMETYPE_FOLDER
  205. ),
  206. 0,
  207. 0,
  208. [],
  209. $this->user
  210. ))
  211. ->willReturn([
  212. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  213. ]);
  214. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_IS_COLLECTION, 'yes');
  215. $result = $this->search->search($query);
  216. $this->assertCount(1, $result);
  217. $this->assertEquals('/files/test/test/path', $result[0]->href);
  218. }
  219. public function testSearchInvalidProp() {
  220. $this->expectException(\InvalidArgumentException::class);
  221. $this->tree->expects($this->any())
  222. ->method('getNodeForPath')
  223. ->willReturn($this->davFolder);
  224. $this->searchFolder->expects($this->never())
  225. ->method('search');
  226. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}getetag', 'foo');
  227. $this->search->search($query);
  228. }
  229. private function getBasicQuery($type, $property, $value = null) {
  230. $scope = new \SearchDAV\Query\Scope('/', 'infinite');
  231. $scope->path = '/';
  232. $from = [$scope];
  233. $orderBy = [];
  234. $select = [];
  235. if (is_null($value)) {
  236. $where = new \SearchDAV\Query\Operator(
  237. $type,
  238. [new \SearchDAV\Query\Literal($property)]
  239. );
  240. } else {
  241. $where = new \SearchDAV\Query\Operator(
  242. $type,
  243. [new SearchPropertyDefinition($property, true, true, true), new \SearchDAV\Query\Literal($value)]
  244. );
  245. }
  246. $limit = new Limit();
  247. return new Query($select, $from, $where, $orderBy, $limit);
  248. }
  249. public function testSearchNonFolder() {
  250. $this->expectException(\InvalidArgumentException::class);
  251. $davNode = $this->createMock(File::class);
  252. $this->tree->expects($this->any())
  253. ->method('getNodeForPath')
  254. ->willReturn($davNode);
  255. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
  256. $this->search->search($query);
  257. }
  258. public function testSearchLimitOwnerBasic() {
  259. $this->tree->expects($this->any())
  260. ->method('getNodeForPath')
  261. ->willReturn($this->davFolder);
  262. /** @var ISearchQuery|null $receivedQuery */
  263. $receivedQuery = null;
  264. $this->searchFolder
  265. ->method('search')
  266. ->willReturnCallback(function ($query) use (&$receivedQuery) {
  267. $receivedQuery = $query;
  268. return [
  269. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  270. ];
  271. });
  272. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID());
  273. $this->search->search($query);
  274. $this->assertNotNull($receivedQuery);
  275. $this->assertTrue($receivedQuery->limitToHome());
  276. /** @var ISearchBinaryOperator $operator */
  277. $operator = $receivedQuery->getSearchOperation();
  278. $this->assertInstanceOf(ISearchBinaryOperator::class, $operator);
  279. $this->assertEquals(ISearchBinaryOperator::OPERATOR_AND, $operator->getType());
  280. $this->assertEmpty($operator->getArguments());
  281. }
  282. public function testSearchLimitOwnerNested() {
  283. $this->tree->expects($this->any())
  284. ->method('getNodeForPath')
  285. ->willReturn($this->davFolder);
  286. /** @var ISearchQuery|null $receivedQuery */
  287. $receivedQuery = null;
  288. $this->searchFolder
  289. ->method('search')
  290. ->willReturnCallback(function ($query) use (&$receivedQuery) {
  291. $receivedQuery = $query;
  292. return [
  293. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  294. ];
  295. });
  296. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, FilesPlugin::OWNER_ID_PROPERTYNAME, $this->user->getUID());
  297. $query->where = new \SearchDAV\Query\Operator(
  298. \SearchDAV\Query\Operator::OPERATION_AND,
  299. [
  300. new \SearchDAV\Query\Operator(
  301. \SearchDAV\Query\Operator::OPERATION_EQUAL,
  302. [new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), new \SearchDAV\Query\Literal('image/png')]
  303. ),
  304. new \SearchDAV\Query\Operator(
  305. \SearchDAV\Query\Operator::OPERATION_EQUAL,
  306. [new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, true, true, true), new \SearchDAV\Query\Literal($this->user->getUID())]
  307. )
  308. ]
  309. );
  310. $this->search->search($query);
  311. $this->assertNotNull($receivedQuery);
  312. $this->assertTrue($receivedQuery->limitToHome());
  313. /** @var ISearchBinaryOperator $operator */
  314. $operator = $receivedQuery->getSearchOperation();
  315. $this->assertInstanceOf(ISearchBinaryOperator::class, $operator);
  316. $this->assertEquals(ISearchBinaryOperator::OPERATOR_AND, $operator->getType());
  317. $this->assertCount(2, $operator->getArguments());
  318. /** @var ISearchBinaryOperator $operator */
  319. $operator = $operator->getArguments()[1];
  320. $this->assertInstanceOf(ISearchBinaryOperator::class, $operator);
  321. $this->assertEquals(ISearchBinaryOperator::OPERATOR_AND, $operator->getType());
  322. $this->assertEmpty($operator->getArguments());
  323. }
  324. }