AutoCompleteControllerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\IRequest;
  28. use PHPUnit\Framework\MockObject\MockObject;
  29. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  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 EventDispatcherInterface|MockObject */
  37. protected $dispatcher;
  38. /** @var AutoCompleteController */
  39. protected $controller;
  40. protected function setUp() {
  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(EventDispatcherInterface::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.', 'source' => 'users'],
  72. [ 'id' => 'bob', 'label' => 'Bob Y.', 'source' => 'users'],
  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.', 'source' => 'users'],
  93. [ 'id' => 'bob', 'label' => 'Bob Y.', 'source' => 'users'],
  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.', 'source' => 'users'],
  114. [ 'id' => 'bob', 'label' => 'Bob Y.', 'source' => 'users'],
  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.', 'source' => 'users'],
  135. [ 'id' => 'bobby', 'label' => 'Robert R.', 'source' => 'users'],
  136. ],
  137. 'bob',
  138. 'files',
  139. '42',
  140. null
  141. ]
  142. ];
  143. }
  144. /**
  145. * @param $searchResults
  146. * @param $expected
  147. * @param $searchTerm
  148. * @param $itemType
  149. * @param $itemId
  150. * @param $sorter
  151. * @dataProvider searchDataProvider
  152. */
  153. public function testGet($searchResults, $expected, $searchTerm, $itemType, $itemId, $sorter) {
  154. $this->collaboratorSearch->expects($this->once())
  155. ->method('search')
  156. ->willReturn([$searchResults, false]);
  157. $runSorterFrequency = $sorter === null ? $this->never() : $this->once();
  158. $this->autoCompleteManager->expects($runSorterFrequency)
  159. ->method('runSorters');
  160. $response = $this->controller->get($searchTerm, $itemType, $itemId, $sorter);
  161. $list = $response->getData();
  162. $this->assertEquals($expected, $list); // has better error output…
  163. $this->assertSame($expected, $list);
  164. }
  165. }