SearchTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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;
  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. public function setUp() {
  38. parent::setUp();
  39. $this->container = $this->createMock(IContainer::class);
  40. $this->search = new Search($this->container);
  41. }
  42. /**
  43. * @dataProvider dataSearchSharees
  44. *
  45. * @param string $searchTerm
  46. * @param array $shareTypes
  47. * @param int $page
  48. * @param int $perPage
  49. * @param array $mockedUserResult
  50. * @param array $mockedGroupsResult
  51. * @param array $mockedRemotesResult
  52. * @param array $expected
  53. * @param bool $expectedMoreResults
  54. */
  55. public function testSearch(
  56. $searchTerm,
  57. array $shareTypes,
  58. $page,
  59. $perPage,
  60. array $mockedUserResult,
  61. array $mockedGroupsResult,
  62. array $mockedRemotesResult,
  63. array $expected,
  64. $expectedMoreResults
  65. ) {
  66. $searchResult = new SearchResult();
  67. $userPlugin = $this->createMock(ISearchPlugin::class);
  68. $userPlugin->expects($this->any())
  69. ->method('search')
  70. ->willReturnCallback(function() use ($searchResult, $mockedUserResult, $expectedMoreResults) {
  71. $type = new SearchResultType('users');
  72. $searchResult->addResultSet($type, $mockedUserResult);
  73. return $expectedMoreResults;
  74. });
  75. $groupPlugin = $this->createMock(ISearchPlugin::class);
  76. $groupPlugin->expects($this->any())
  77. ->method('search')
  78. ->willReturnCallback(function() use ($searchResult, $mockedGroupsResult, $expectedMoreResults) {
  79. $type = new SearchResultType('groups');
  80. $searchResult->addResultSet($type, $mockedGroupsResult);
  81. return $expectedMoreResults;
  82. });
  83. $remotePlugin = $this->createMock(ISearchPlugin::class);
  84. $remotePlugin->expects($this->any())
  85. ->method('search')
  86. ->willReturnCallback(function() use ($searchResult, $mockedRemotesResult, $expectedMoreResults) {
  87. if($mockedRemotesResult !== null) {
  88. $type = new SearchResultType('remotes');
  89. $searchResult->addResultSet($type, $mockedRemotesResult['results'], $mockedRemotesResult['exact']);
  90. if($mockedRemotesResult['exactIdMatch'] === true) {
  91. $searchResult->markExactIdMatch($type);
  92. }
  93. }
  94. return $expectedMoreResults;
  95. });
  96. $this->container->expects($this->any())
  97. ->method('resolve')
  98. ->willReturnCallback(function($class) use ($searchResult, $userPlugin, $groupPlugin, $remotePlugin) {
  99. if($class === SearchResult::class) {
  100. return $searchResult;
  101. } elseif ($class === $userPlugin) {
  102. return $userPlugin;
  103. } elseif ($class === $groupPlugin) {
  104. return $groupPlugin;
  105. } elseif ($class === $remotePlugin) {
  106. return $remotePlugin;
  107. }
  108. return null;
  109. });
  110. $this->search->registerPlugin(['shareType' => 'SHARE_TYPE_USER', 'class' => $userPlugin]);
  111. $this->search->registerPlugin(['shareType' => 'SHARE_TYPE_GROUP', 'class' => $groupPlugin]);
  112. $this->search->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE', 'class' => $remotePlugin]);
  113. list($results, $moreResults) = $this->search->search($searchTerm, $shareTypes, false, $perPage, $perPage * ($page - 1));
  114. $this->assertEquals($expected, $results);
  115. $this->assertSame($expectedMoreResults, $moreResults);
  116. }
  117. public function dataSearchSharees() {
  118. return [
  119. [
  120. 'test', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE], 1, 2, [], [], ['results' => [], 'exact' => [], 'exactIdMatch' => false],
  121. [
  122. 'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
  123. 'users' => [],
  124. 'groups' => [],
  125. 'remotes' => [],
  126. ], false
  127. ],
  128. [
  129. 'test', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE], 1, 2, [], [], ['results' => [], 'exact' => [], 'exactIdMatch' => false],
  130. [
  131. 'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
  132. 'users' => [],
  133. 'groups' => [],
  134. 'remotes' => [],
  135. ], false
  136. ],
  137. [
  138. 'test', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE], 1, 2, [
  139. ['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
  140. ], [
  141. ['label' => 'testgroup1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'testgroup1']],
  142. ], [
  143. 'results' => [['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']]], 'exact' => [], 'exactIdMatch' => false,
  144. ],
  145. [
  146. 'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
  147. 'users' => [
  148. ['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
  149. ],
  150. 'groups' => [
  151. ['label' => 'testgroup1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'testgroup1']],
  152. ],
  153. 'remotes' => [
  154. ['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']],
  155. ],
  156. ], true,
  157. ],
  158. // No groups requested
  159. [
  160. 'test', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_REMOTE], 1, 2, [
  161. ['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
  162. ], [], [
  163. 'results' => [['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']]], 'exact' => [], 'exactIdMatch' => false
  164. ],
  165. [
  166. 'exact' => ['users' => [], 'remotes' => []],
  167. 'users' => [
  168. ['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
  169. ],
  170. 'remotes' => [
  171. ['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']],
  172. ],
  173. ], false,
  174. ],
  175. // Share type restricted to user - Only one user
  176. [
  177. 'test', [Share::SHARE_TYPE_USER], 1, 2, [
  178. ['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
  179. ], [], [],
  180. [
  181. 'exact' => ['users' => []],
  182. 'users' => [
  183. ['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
  184. ],
  185. ], false,
  186. ],
  187. // Share type restricted to user - Multipage result
  188. [
  189. 'test', [Share::SHARE_TYPE_USER], 1, 2, [
  190. ['label' => 'test 1', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
  191. ['label' => 'test 2', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']],
  192. ], [], [],
  193. [
  194. 'exact' => ['users' => []],
  195. 'users' => [
  196. ['label' => 'test 1', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
  197. ['label' => 'test 2', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']],
  198. ],
  199. ], true,
  200. ],
  201. ];
  202. }
  203. }