AutoCompleteControllerTest.php 5.3 KB

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