AutoCompleteControllerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 Tests\Core\Controller;
  24. use OC\Core\Controller\AutoCompleteController;
  25. use OCP\Collaboration\AutoComplete\IManager;
  26. use OCP\Collaboration\Collaborators\ISearch;
  27. use OCP\EventDispatcher\IEventDispatcher;
  28. use OCP\IRequest;
  29. use PHPUnit\Framework\MockObject\MockObject;
  30. use Test\TestCase;
  31. class AutoCompleteControllerTest extends TestCase {
  32. /** @var ISearch|MockObject */
  33. protected $collaboratorSearch;
  34. /** @var IManager|MockObject */
  35. protected $autoCompleteManager;
  36. /** @var IEventDispatcher|MockObject */
  37. protected $dispatcher;
  38. /** @var AutoCompleteController */
  39. protected $controller;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. /** @var IRequest $request */
  43. $request = $this->createMock(IRequest::class);
  44. $this->collaboratorSearch = $this->createMock(ISearch::class);
  45. $this->autoCompleteManager = $this->createMock(IManager::class);
  46. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  47. $this->controller = new AutoCompleteController(
  48. 'core',
  49. $request,
  50. $this->collaboratorSearch,
  51. $this->autoCompleteManager,
  52. $this->dispatcher
  53. );
  54. }
  55. public function searchDataProvider() {
  56. return [
  57. [ #0 – regular search
  58. // searchResults
  59. [
  60. 'exact' => [
  61. 'users' => [],
  62. 'robots' => [],
  63. ],
  64. 'users' => [
  65. ['label' => 'Alice A.', 'value' => ['shareWith' => 'alice']],
  66. ['label' => 'Bob Y.', 'value' => ['shareWith' => 'bob']],
  67. ],
  68. ],
  69. // expected
  70. [
  71. [ 'id' => 'alice', 'label' => 'Alice A.', 'icon' => '', 'source' => 'users', 'status' => '', 'subline' => '', 'shareWithDisplayNameUnique' => ''],
  72. [ 'id' => 'bob', 'label' => 'Bob Y.', 'icon' => '', 'source' => 'users', 'status' => '', 'subline' => '', 'shareWithDisplayNameUnique' => ''],
  73. ],
  74. '',
  75. 'files',
  76. '42',
  77. null
  78. ],
  79. [ #1 – missing itemtype and id
  80. [
  81. 'exact' => [
  82. 'users' => [],
  83. 'robots' => [],
  84. ],
  85. 'users' => [
  86. ['label' => 'Alice A.', 'value' => ['shareWith' => 'alice']],
  87. ['label' => 'Bob Y.', 'value' => ['shareWith' => 'bob']],
  88. ],
  89. ],
  90. // expected
  91. [
  92. [ 'id' => 'alice', 'label' => 'Alice A.', 'icon' => '', 'source' => 'users', 'status' => '', 'subline' => '', 'shareWithDisplayNameUnique' => ''],
  93. [ 'id' => 'bob', 'label' => 'Bob Y.', 'icon' => '', 'source' => 'users', 'status' => '', 'subline' => '', 'shareWithDisplayNameUnique' => ''],
  94. ],
  95. '',
  96. null,
  97. null,
  98. null
  99. ],
  100. [ #2 – with sorter
  101. [
  102. 'exact' => [
  103. 'users' => [],
  104. 'robots' => [],
  105. ],
  106. 'users' => [
  107. ['label' => 'Alice A.', 'value' => ['shareWith' => 'alice']],
  108. ['label' => 'Bob Y.', 'value' => ['shareWith' => 'bob']],
  109. ],
  110. ],
  111. // expected
  112. [
  113. [ 'id' => 'alice', 'label' => 'Alice A.', 'icon' => '', 'source' => 'users', 'status' => '', 'subline' => '', 'shareWithDisplayNameUnique' => ''],
  114. [ 'id' => 'bob', 'label' => 'Bob Y.', 'icon' => '', 'source' => 'users', 'status' => '', 'subline' => '', 'shareWithDisplayNameUnique' => ''],
  115. ],
  116. '',
  117. 'files',
  118. '42',
  119. 'karma|bus-factor'
  120. ],
  121. [ #3 – exact Match
  122. [
  123. 'exact' => [
  124. 'users' => [
  125. ['label' => 'Bob Y.', 'value' => ['shareWith' => 'bob']],
  126. ],
  127. 'robots' => [],
  128. ],
  129. 'users' => [
  130. ['label' => 'Robert R.', 'value' => ['shareWith' => 'bobby']],
  131. ],
  132. ],
  133. [
  134. [ 'id' => 'bob', 'label' => 'Bob Y.', 'icon' => '', 'source' => 'users', 'status' => '', 'subline' => '', 'shareWithDisplayNameUnique' => ''],
  135. [ 'id' => 'bobby', 'label' => 'Robert R.', 'icon' => '', 'source' => 'users', 'status' => '', 'subline' => '', 'shareWithDisplayNameUnique' => ''],
  136. ],
  137. 'bob',
  138. 'files',
  139. '42',
  140. null
  141. ],
  142. [ #4 – with unique name
  143. [
  144. 'exact' => [
  145. 'users' => [],
  146. 'robots' => [],
  147. ],
  148. 'users' => [
  149. ['label' => 'Alice A.', 'value' => ['shareWith' => 'alice'], 'shareWithDisplayNameUnique' => 'alica@nextcloud.com'],
  150. ['label' => 'Alice A.', 'value' => ['shareWith' => 'alicea'], 'shareWithDisplayNameUnique' => 'alicaa@nextcloud.com'],
  151. ],
  152. ],
  153. // expected
  154. [
  155. [ 'id' => 'alice', 'label' => 'Alice A.', 'icon' => '', 'source' => 'users', 'status' => '', 'subline' => '', 'shareWithDisplayNameUnique' => 'alica@nextcloud.com'],
  156. [ 'id' => 'alicea', 'label' => 'Alice A.', 'icon' => '', 'source' => 'users', 'status' => '', 'subline' => '', 'shareWithDisplayNameUnique' => 'alicaa@nextcloud.com'],
  157. ],
  158. '',
  159. 'files',
  160. '42',
  161. 'karma|bus-factor'
  162. ],
  163. ];
  164. }
  165. /**
  166. * @dataProvider searchDataProvider
  167. */
  168. public function testGet(array $searchResults, array $expected, string $searchTerm, ?string $itemType, ?string $itemId, ?string $sorter) {
  169. $this->collaboratorSearch->expects($this->once())
  170. ->method('search')
  171. ->willReturn([$searchResults, false]);
  172. $runSorterFrequency = $sorter === null ? $this->never() : $this->once();
  173. $this->autoCompleteManager->expects($runSorterFrequency)
  174. ->method('runSorters');
  175. $response = $this->controller->get($searchTerm, $itemType, $itemId, $sorter);
  176. $list = $response->getData();
  177. $this->assertEquals($expected, $list); // has better error output…
  178. $this->assertSame($expected, $list);
  179. }
  180. }