FileSearchBackendTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\DAV\Tests\Files;
  22. use OC\Files\Search\SearchComparison;
  23. use OC\Files\Search\SearchQuery;
  24. use OC\Files\View;
  25. use OCA\DAV\Connector\Sabre\Directory;
  26. use OCA\DAV\Connector\Sabre\File;
  27. use OCA\DAV\Connector\Sabre\FilesPlugin;
  28. use OCA\DAV\Files\FileSearchBackend;
  29. use OCP\Files\FileInfo;
  30. use OCP\Files\Folder;
  31. use OCP\Files\IRootFolder;
  32. use OCP\Files\Search\ISearchComparison;
  33. use OCP\IUser;
  34. use OCP\Share\IManager;
  35. use Sabre\DAV\Tree;
  36. use SearchDAV\XML\BasicSearch;
  37. use SearchDAV\XML\Literal;
  38. use SearchDAV\XML\Operator;
  39. use SearchDAV\XML\Scope;
  40. use Test\TestCase;
  41. class FileSearchBackendTest extends TestCase {
  42. /** @var Tree|\PHPUnit_Framework_MockObject_MockObject */
  43. private $tree;
  44. /** @var IUser */
  45. private $user;
  46. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  47. private $rootFolder;
  48. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  49. private $shareManager;
  50. /** @var View|\PHPUnit_Framework_MockObject_MockObject */
  51. private $view;
  52. /** @var Folder|\PHPUnit_Framework_MockObject_MockObject */
  53. private $searchFolder;
  54. /** @var FileSearchBackend */
  55. private $search;
  56. /** @var Directory|\PHPUnit_Framework_MockObject_MockObject */
  57. private $davFolder;
  58. protected function setUp() {
  59. parent::setUp();
  60. $this->user = $this->createMock(IUser::class);
  61. $this->user->expects($this->any())
  62. ->method('getUID')
  63. ->willReturn('test');
  64. $this->tree = $this->getMockBuilder(Tree::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->view = $this->getMockBuilder(View::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->view->expects($this->any())
  71. ->method('getRelativePath')
  72. ->willReturnArgument(0);
  73. $this->rootFolder = $this->createMock(IRootFolder::class);
  74. $this->shareManager = $this->createMock(IManager::class);
  75. $this->searchFolder = $this->createMock(Folder::class);
  76. $fileInfo = $this->createMock(FileInfo::class);
  77. $this->davFolder = $this->createMock(Directory::class);
  78. $this->davFolder->expects($this->any())
  79. ->method('getFileInfo')
  80. ->willReturn($fileInfo);
  81. $this->rootFolder->expects($this->any())
  82. ->method('get')
  83. ->willReturn($this->searchFolder);
  84. $this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view);
  85. }
  86. public function testSearchFilename() {
  87. $this->tree->expects($this->any())
  88. ->method('getNodeForPath')
  89. ->willReturn($this->davFolder);
  90. $this->searchFolder->expects($this->once())
  91. ->method('search')
  92. ->with(new SearchQuery(
  93. new SearchComparison(
  94. ISearchComparison::COMPARE_EQUAL,
  95. 'name',
  96. 'foo'
  97. ),
  98. 0,
  99. 0,
  100. []
  101. ))
  102. ->will($this->returnValue([
  103. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  104. ]));
  105. $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
  106. $result = $this->search->search($query);
  107. $this->assertCount(1, $result);
  108. $this->assertEquals('/files/test/test/path', $result[0]->href);
  109. }
  110. public function testSearchMimetype() {
  111. $this->tree->expects($this->any())
  112. ->method('getNodeForPath')
  113. ->willReturn($this->davFolder);
  114. $this->searchFolder->expects($this->once())
  115. ->method('search')
  116. ->with(new SearchQuery(
  117. new SearchComparison(
  118. ISearchComparison::COMPARE_EQUAL,
  119. 'mimetype',
  120. 'foo'
  121. ),
  122. 0,
  123. 0,
  124. []
  125. ))
  126. ->will($this->returnValue([
  127. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  128. ]));
  129. $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}getcontenttype', 'foo');
  130. $result = $this->search->search($query);
  131. $this->assertCount(1, $result);
  132. $this->assertEquals('/files/test/test/path', $result[0]->href);
  133. }
  134. public function testSearchSize() {
  135. $this->tree->expects($this->any())
  136. ->method('getNodeForPath')
  137. ->willReturn($this->davFolder);
  138. $this->searchFolder->expects($this->once())
  139. ->method('search')
  140. ->with(new SearchQuery(
  141. new SearchComparison(
  142. ISearchComparison::COMPARE_GREATER_THAN,
  143. 'size',
  144. 10
  145. ),
  146. 0,
  147. 0,
  148. []
  149. ))
  150. ->will($this->returnValue([
  151. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  152. ]));
  153. $query = $this->getBasicQuery(Operator::OPERATION_GREATER_THAN, FilesPlugin::SIZE_PROPERTYNAME, 10);
  154. $result = $this->search->search($query);
  155. $this->assertCount(1, $result);
  156. $this->assertEquals('/files/test/test/path', $result[0]->href);
  157. }
  158. public function testSearchMtime() {
  159. $this->tree->expects($this->any())
  160. ->method('getNodeForPath')
  161. ->willReturn($this->davFolder);
  162. $this->searchFolder->expects($this->once())
  163. ->method('search')
  164. ->with(new SearchQuery(
  165. new SearchComparison(
  166. ISearchComparison::COMPARE_GREATER_THAN,
  167. 'mtime',
  168. 10
  169. ),
  170. 0,
  171. 0,
  172. []
  173. ))
  174. ->will($this->returnValue([
  175. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  176. ]));
  177. $query = $this->getBasicQuery(Operator::OPERATION_GREATER_THAN, '{DAV:}getlastmodifed', 10);
  178. $result = $this->search->search($query);
  179. $this->assertCount(1, $result);
  180. $this->assertEquals('/files/test/test/path', $result[0]->href);
  181. }
  182. public function testSearchIsCollection() {
  183. $this->tree->expects($this->any())
  184. ->method('getNodeForPath')
  185. ->willReturn($this->davFolder);
  186. $this->searchFolder->expects($this->once())
  187. ->method('search')
  188. ->with(new SearchQuery(
  189. new SearchComparison(
  190. ISearchComparison::COMPARE_EQUAL,
  191. 'mimetype',
  192. FileInfo::MIMETYPE_FOLDER
  193. ),
  194. 0,
  195. 0,
  196. []
  197. ))
  198. ->will($this->returnValue([
  199. new \OC\Files\Node\Folder($this->rootFolder, $this->view, '/test/path')
  200. ]));
  201. $query = $this->getBasicQuery(Operator::OPERATION_IS_COLLECTION, 'yes');
  202. $result = $this->search->search($query);
  203. $this->assertCount(1, $result);
  204. $this->assertEquals('/files/test/test/path', $result[0]->href);
  205. }
  206. /**
  207. * @expectedException \InvalidArgumentException
  208. */
  209. public function testSearchInvalidProp() {
  210. $this->tree->expects($this->any())
  211. ->method('getNodeForPath')
  212. ->willReturn($this->davFolder);
  213. $this->searchFolder->expects($this->never())
  214. ->method('search');
  215. $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}getetag', 'foo');
  216. $this->search->search($query);
  217. }
  218. private function getBasicQuery($type, $property, $value = null) {
  219. $query = new BasicSearch();
  220. $scope = new Scope('/', 'infinite');
  221. $scope->path = '/';
  222. $query->from = [$scope];
  223. $query->orderBy = [];
  224. $query->select = [];
  225. if (is_null($value)) {
  226. $query->where = new Operator(
  227. $type,
  228. [new Literal($property)]
  229. );
  230. } else {
  231. $query->where = new Operator(
  232. $type,
  233. [$property, new Literal($value)]
  234. );
  235. }
  236. return $query;
  237. }
  238. /**
  239. * @expectedException \InvalidArgumentException
  240. */
  241. public function testSearchNonFolder() {
  242. $davNode = $this->createMock(File::class);
  243. $this->tree->expects($this->any())
  244. ->method('getNodeForPath')
  245. ->willReturn($davNode);
  246. $query = $this->getBasicQuery(Operator::OPERATION_EQUAL, '{DAV:}displayname', 'foo');
  247. $this->search->search($query);
  248. }
  249. }