1
0

FileSearchBackendTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <robin@icewind.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\ISearchComparison;
  37. use OCP\IUser;
  38. use OCP\Share\IManager;
  39. use SearchDAV\Backend\SearchPropertyDefinition;
  40. use SearchDAV\Query\Limit;
  41. use SearchDAV\Query\Query;
  42. use SearchDAV\XML\BasicSearch;
  43. use SearchDAV\XML\Literal;
  44. use SearchDAV\XML\Operator;
  45. use SearchDAV\XML\Scope;
  46. use Test\TestCase;
  47. class FileSearchBackendTest extends TestCase {
  48. /** @var CachingTree|\PHPUnit_Framework_MockObject_MockObject */
  49. private $tree;
  50. /** @var IUser */
  51. private $user;
  52. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  53. private $rootFolder;
  54. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  55. private $shareManager;
  56. /** @var View|\PHPUnit_Framework_MockObject_MockObject */
  57. private $view;
  58. /** @var Folder|\PHPUnit_Framework_MockObject_MockObject */
  59. private $searchFolder;
  60. /** @var FileSearchBackend */
  61. private $search;
  62. /** @var Directory|\PHPUnit_Framework_MockObject_MockObject */
  63. private $davFolder;
  64. protected function setUp() {
  65. parent::setUp();
  66. $this->user = $this->createMock(IUser::class);
  67. $this->user->expects($this->any())
  68. ->method('getUID')
  69. ->willReturn('test');
  70. $this->tree = $this->getMockBuilder(CachingTree::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->view = $this->getMockBuilder(View::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->view->expects($this->any())
  77. ->method('getRelativePath')
  78. ->willReturnArgument(0);
  79. $this->rootFolder = $this->createMock(IRootFolder::class);
  80. $this->shareManager = $this->createMock(IManager::class);
  81. $this->searchFolder = $this->createMock(Folder::class);
  82. $fileInfo = $this->createMock(FileInfo::class);
  83. $this->davFolder = $this->createMock(Directory::class);
  84. $this->davFolder->expects($this->any())
  85. ->method('getFileInfo')
  86. ->willReturn($fileInfo);
  87. $this->rootFolder->expects($this->any())
  88. ->method('get')
  89. ->willReturn($this->searchFolder);
  90. $this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view);
  91. }
  92. public function testSearchFilename() {
  93. $this->tree->expects($this->any())
  94. ->method('getNodeForPath')
  95. ->willReturn($this->davFolder);
  96. $this->searchFolder->expects($this->once())
  97. ->method('search')
  98. ->with(new SearchQuery(
  99. new SearchComparison(
  100. ISearchComparison::COMPARE_EQUAL,
  101. 'name',
  102. 'foo'
  103. ),
  104. 0,
  105. 0,
  106. [],
  107. $this->user
  108. ))
  109. ->will($this->returnValue([
  110. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  111. ]));
  112. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
  113. $result = $this->search->search($query);
  114. $this->assertCount(1, $result);
  115. $this->assertEquals('/files/test/test/path', $result[0]->href);
  116. }
  117. public function testSearchMimetype() {
  118. $this->tree->expects($this->any())
  119. ->method('getNodeForPath')
  120. ->willReturn($this->davFolder);
  121. $this->searchFolder->expects($this->once())
  122. ->method('search')
  123. ->with(new SearchQuery(
  124. new SearchComparison(
  125. ISearchComparison::COMPARE_EQUAL,
  126. 'mimetype',
  127. 'foo'
  128. ),
  129. 0,
  130. 0,
  131. [],
  132. $this->user
  133. ))
  134. ->will($this->returnValue([
  135. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  136. ]));
  137. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}getcontenttype', 'foo');
  138. $result = $this->search->search($query);
  139. $this->assertCount(1, $result);
  140. $this->assertEquals('/files/test/test/path', $result[0]->href);
  141. }
  142. public function testSearchSize() {
  143. $this->tree->expects($this->any())
  144. ->method('getNodeForPath')
  145. ->willReturn($this->davFolder);
  146. $this->searchFolder->expects($this->once())
  147. ->method('search')
  148. ->with(new SearchQuery(
  149. new SearchComparison(
  150. ISearchComparison::COMPARE_GREATER_THAN,
  151. 'size',
  152. 10
  153. ),
  154. 0,
  155. 0,
  156. [],
  157. $this->user
  158. ))
  159. ->will($this->returnValue([
  160. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  161. ]));
  162. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_GREATER_THAN, FilesPlugin::SIZE_PROPERTYNAME, 10);
  163. $result = $this->search->search($query);
  164. $this->assertCount(1, $result);
  165. $this->assertEquals('/files/test/test/path', $result[0]->href);
  166. }
  167. public function testSearchMtime() {
  168. $this->tree->expects($this->any())
  169. ->method('getNodeForPath')
  170. ->willReturn($this->davFolder);
  171. $this->searchFolder->expects($this->once())
  172. ->method('search')
  173. ->with(new SearchQuery(
  174. new SearchComparison(
  175. ISearchComparison::COMPARE_GREATER_THAN,
  176. 'mtime',
  177. 10
  178. ),
  179. 0,
  180. 0,
  181. [],
  182. $this->user
  183. ))
  184. ->will($this->returnValue([
  185. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  186. ]));
  187. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_GREATER_THAN, '{DAV:}getlastmodified', 10);
  188. $result = $this->search->search($query);
  189. $this->assertCount(1, $result);
  190. $this->assertEquals('/files/test/test/path', $result[0]->href);
  191. }
  192. public function testSearchIsCollection() {
  193. $this->tree->expects($this->any())
  194. ->method('getNodeForPath')
  195. ->willReturn($this->davFolder);
  196. $this->searchFolder->expects($this->once())
  197. ->method('search')
  198. ->with(new SearchQuery(
  199. new SearchComparison(
  200. ISearchComparison::COMPARE_EQUAL,
  201. 'mimetype',
  202. FileInfo::MIMETYPE_FOLDER
  203. ),
  204. 0,
  205. 0,
  206. [],
  207. $this->user
  208. ))
  209. ->will($this->returnValue([
  210. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  211. ]));
  212. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_IS_COLLECTION, 'yes');
  213. $result = $this->search->search($query);
  214. $this->assertCount(1, $result);
  215. $this->assertEquals('/files/test/test/path', $result[0]->href);
  216. }
  217. /**
  218. * @expectedException \InvalidArgumentException
  219. */
  220. public function testSearchInvalidProp() {
  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. /**
  250. * @expectedException \InvalidArgumentException
  251. */
  252. public function testSearchNonFolder() {
  253. $davNode = $this->createMock(File::class);
  254. $this->tree->expects($this->any())
  255. ->method('getNodeForPath')
  256. ->willReturn($davNode);
  257. $query = $this->getBasicQuery(\SearchDAV\Query\Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
  258. $this->search->search($query);
  259. }
  260. }