AutoCompleteControllerTest.php 4.5 KB

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