SearchTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Collaboration\Collaborators;
  24. use OC\Collaboration\Collaborators\Search;
  25. use OC\Collaboration\Collaborators\SearchResult;
  26. use OCP\Collaboration\Collaborators\ISearch;
  27. use OCP\Collaboration\Collaborators\ISearchPlugin;
  28. use OCP\Collaboration\Collaborators\SearchResultType;
  29. use OCP\IContainer;
  30. use OCP\Share\IShare;
  31. use Test\TestCase;
  32. class SearchTest extends TestCase {
  33. /** @var IContainer|\PHPUnit\Framework\MockObject\MockObject */
  34. protected $container;
  35. /** @var ISearch */
  36. protected $search;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->container = $this->createMock(IContainer::class);
  40. $this->search = new Search($this->container);
  41. }
  42. /**
  43. * @dataProvider dataSearchSharees
  44. */
  45. public function testSearch(
  46. string $searchTerm,
  47. array $shareTypes,
  48. int $page,
  49. int $perPage,
  50. array $mockedUserResult,
  51. array $mockedGroupsResult,
  52. array $mockedRemotesResult,
  53. array $mockedMailResult,
  54. array $expected,
  55. bool $expectedMoreResults
  56. ) {
  57. $searchResult = new SearchResult();
  58. $userPlugin = $this->createMock(ISearchPlugin::class);
  59. $userPlugin->expects($this->any())
  60. ->method('search')
  61. ->willReturnCallback(function () use ($searchResult, $mockedUserResult, $expectedMoreResults) {
  62. $type = new SearchResultType('users');
  63. $searchResult->addResultSet($type, $mockedUserResult);
  64. return $expectedMoreResults;
  65. });
  66. $groupPlugin = $this->createMock(ISearchPlugin::class);
  67. $groupPlugin->expects($this->any())
  68. ->method('search')
  69. ->willReturnCallback(function () use ($searchResult, $mockedGroupsResult, $expectedMoreResults) {
  70. $type = new SearchResultType('groups');
  71. $searchResult->addResultSet($type, $mockedGroupsResult);
  72. return $expectedMoreResults;
  73. });
  74. $remotePlugin = $this->createMock(ISearchPlugin::class);
  75. $remotePlugin->expects($this->any())
  76. ->method('search')
  77. ->willReturnCallback(function () use ($searchResult, $mockedRemotesResult, $expectedMoreResults) {
  78. if ($mockedRemotesResult !== null) {
  79. $type = new SearchResultType('remotes');
  80. $searchResult->addResultSet($type, $mockedRemotesResult['results'], $mockedRemotesResult['exact']);
  81. if ($mockedRemotesResult['exactIdMatch'] === true) {
  82. $searchResult->markExactIdMatch($type);
  83. }
  84. }
  85. return $expectedMoreResults;
  86. });
  87. $mailPlugin = $this->createMock(ISearchPlugin::class);
  88. $mailPlugin->expects($this->any())
  89. ->method('search')
  90. ->willReturnCallback(function () use ($searchResult, $mockedMailResult, $expectedMoreResults) {
  91. $type = new SearchResultType('emails');
  92. $searchResult->addResultSet($type, $mockedMailResult);
  93. return $expectedMoreResults;
  94. });
  95. $this->container->expects($this->any())
  96. ->method('resolve')
  97. ->willReturnCallback(function ($class) use ($searchResult, $userPlugin, $groupPlugin, $remotePlugin, $mailPlugin) {
  98. if ($class === SearchResult::class) {
  99. return $searchResult;
  100. } elseif ($class === $userPlugin) {
  101. return $userPlugin;
  102. } elseif ($class === $groupPlugin) {
  103. return $groupPlugin;
  104. } elseif ($class === $remotePlugin) {
  105. return $remotePlugin;
  106. } elseif ($class === $mailPlugin) {
  107. return $mailPlugin;
  108. }
  109. return null;
  110. });
  111. $this->search->registerPlugin(['shareType' => 'SHARE_TYPE_USER', 'class' => $userPlugin]);
  112. $this->search->registerPlugin(['shareType' => 'SHARE_TYPE_GROUP', 'class' => $groupPlugin]);
  113. $this->search->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE', 'class' => $remotePlugin]);
  114. $this->search->registerPlugin(['shareType' => 'SHARE_TYPE_EMAIL', 'class' => $mailPlugin]);
  115. [$results, $moreResults] = $this->search->search($searchTerm, $shareTypes, false, $perPage, $perPage * ($page - 1));
  116. $this->assertEquals($expected, $results);
  117. $this->assertSame($expectedMoreResults, $moreResults);
  118. }
  119. public function dataSearchSharees() {
  120. return [
  121. // #0
  122. [
  123. 'test', [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE], 1, 2, [], [], ['results' => [], 'exact' => [], 'exactIdMatch' => false],
  124. [],
  125. [
  126. 'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
  127. 'users' => [],
  128. 'groups' => [],
  129. 'remotes' => [],
  130. ],
  131. false
  132. ],
  133. // #1
  134. [
  135. 'test', [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE], 1, 2, [], [], ['results' => [], 'exact' => [], 'exactIdMatch' => false],
  136. [],
  137. [
  138. 'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
  139. 'users' => [],
  140. 'groups' => [],
  141. 'remotes' => [],
  142. ],
  143. false
  144. ],
  145. // #2
  146. [
  147. 'test', [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE], 1, 2, [
  148. ['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
  149. ], [
  150. ['label' => 'testgroup1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'testgroup1']],
  151. ], [
  152. 'results' => [['label' => 'testz@remote', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote']]], 'exact' => [], 'exactIdMatch' => false,
  153. ],
  154. [],
  155. [
  156. 'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
  157. 'users' => [
  158. ['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
  159. ],
  160. 'groups' => [
  161. ['label' => 'testgroup1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'testgroup1']],
  162. ],
  163. 'remotes' => [
  164. ['label' => 'testz@remote', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote']],
  165. ],
  166. ], true,
  167. ],
  168. // #3 No groups requested
  169. [
  170. 'test', [IShare::TYPE_USER, IShare::TYPE_REMOTE], 1, 2, [
  171. ['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
  172. ], [], [
  173. 'results' => [['label' => 'testz@remote', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote']]], 'exact' => [], 'exactIdMatch' => false
  174. ],
  175. [],
  176. [
  177. 'exact' => ['users' => [], 'remotes' => []],
  178. 'users' => [
  179. ['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
  180. ],
  181. 'remotes' => [
  182. ['label' => 'testz@remote', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote']],
  183. ],
  184. ], false,
  185. ],
  186. // #4 Share type restricted to user - Only one user
  187. [
  188. 'test', [IShare::TYPE_USER], 1, 2, [
  189. ['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
  190. ], [], [], [],
  191. [
  192. 'exact' => ['users' => []],
  193. 'users' => [
  194. ['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
  195. ],
  196. ], false,
  197. ],
  198. // #5 Share type restricted to user - Multipage result
  199. [
  200. 'test', [IShare::TYPE_USER], 1, 2, [
  201. ['label' => 'test 1', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
  202. ['label' => 'test 2', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2']],
  203. ], [], [], [],
  204. [
  205. 'exact' => ['users' => []],
  206. 'users' => [
  207. ['label' => 'test 1', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
  208. ['label' => 'test 2', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2']],
  209. ],
  210. ], true,
  211. ],
  212. // #6 Mail shares filtered out in favor of remote shares
  213. [
  214. 'test', // search term
  215. [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_EMAIL], // plugins
  216. 1, // page
  217. 10, // per page
  218. [ // user result
  219. ['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
  220. ],
  221. [ // group result
  222. ['label' => 'testgroup1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'testgroup1']],
  223. ],
  224. [ // remote result
  225. 'results' => [
  226. ['label' => 'testz@remote.tld', 'uuid' => 'f3d78140-abcc-46df-b58d-c7cc1176aadf','value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote.tld']]
  227. ],
  228. 'exact' => [],
  229. 'exactIdMatch' => false,
  230. ],
  231. [ // mail result
  232. ['label' => 'test Two', 'uuid' => 'b2321e9e-31af-43ac-a406-583fb26d1964', 'value' => ['shareType' => IShare::TYPE_EMAIL, 'shareWith' => 'test2@remote.tld']],
  233. ['label' => 'test One', 'uuid' => 'f3d78140-abcc-46df-b58d-c7cc1176aadf', 'value' => ['shareType' => IShare::TYPE_EMAIL, 'shareWith' => 'testz@remote.tld']],
  234. ],
  235. [ // expected result
  236. 'exact' => ['users' => [], 'groups' => [], 'remotes' => [], 'emails' => []],
  237. 'users' => [
  238. ['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
  239. ],
  240. 'groups' => [
  241. ['label' => 'testgroup1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'testgroup1']],
  242. ],
  243. 'remotes' => [
  244. ['label' => 'testz@remote.tld', 'uuid' => 'f3d78140-abcc-46df-b58d-c7cc1176aadf', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote.tld']],
  245. ],
  246. 'emails' => [
  247. // one passes, another is filtered out
  248. ['label' => 'test Two', 'uuid' => 'b2321e9e-31af-43ac-a406-583fb26d1964', 'value' => ['shareType' => IShare::TYPE_EMAIL, 'shareWith' => 'test2@remote.tld']]
  249. ]
  250. ],
  251. false, // expected more results indicator
  252. ],
  253. ];
  254. }
  255. }