You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
8.3 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. *
  5. * @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com)
  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 OCA\Talk\Tests\php\Collaboration\Collaborators;
  24. use OCA\Talk\Collaboration\Collaborators\RoomPlugin;
  25. use OCA\Talk\Manager;
  26. use OCA\Talk\Model\Attendee;
  27. use OCA\Talk\Participant;
  28. use OCA\Talk\Room;
  29. use OCA\Talk\Service\ParticipantService;
  30. use OCP\Collaboration\Collaborators\ISearchResult;
  31. use OCP\Collaboration\Collaborators\SearchResultType;
  32. use OCP\IUser;
  33. use OCP\IUserSession;
  34. use OCP\Share\IShare;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Test\TestCase;
  37. class RoomPluginTest extends TestCase {
  38. protected ParticipantService&MockObject $participantService;
  39. protected ?Manager $manager = null;
  40. protected ?IUserSession $userSession = null;
  41. protected ?IUser $user = null;
  42. protected ?ISearchResult $searchResult = null;
  43. protected ?RoomPlugin $plugin = null;
  44. public function setUp(): void {
  45. parent::setUp();
  46. $this->manager = $this->createMock(Manager::class);
  47. $this->participantService = $this->createMock(ParticipantService::class);
  48. $this->user = $this->createMock(IUser::class);
  49. $this->user->expects($this->any())
  50. ->method('getUID')
  51. ->willReturn('user0');
  52. $this->userSession = $this->createMock(IUserSession::class);
  53. $this->userSession->expects($this->any())
  54. ->method('getUser')
  55. ->willReturn($this->user);
  56. $this->searchResult = $this->createMock(ISearchResult::class);
  57. $this->plugin = new RoomPlugin(
  58. $this->manager,
  59. $this->participantService,
  60. $this->userSession
  61. );
  62. }
  63. private function newRoom(int $type, string $token, string $name, int $permissions = Attendee::PERMISSIONS_MAX_DEFAULT): Room {
  64. $room = $this->createMock(Room::class);
  65. $participant = $this->createMock(Participant::class);
  66. $room->expects($this->any())
  67. ->method('getType')
  68. ->willReturn($type);
  69. $room->expects($this->any())
  70. ->method('getToken')
  71. ->willReturn($token);
  72. $room->expects($this->any())
  73. ->method('getDisplayName')
  74. ->willReturn($name);
  75. $this->participantService->expects($this->any())
  76. ->method('getParticipant')
  77. ->willReturn($participant);
  78. $participant->expects($this->any())
  79. ->method('getPermissions')
  80. ->willReturn($permissions);
  81. return $room;
  82. }
  83. private static function newResult(string $label, string $shareWith): array {
  84. return [
  85. 'label' => $label,
  86. 'value' => [
  87. 'shareType' => IShare::TYPE_ROOM,
  88. 'shareWith' => $shareWith
  89. ]
  90. ];
  91. }
  92. public static function dataSearch(): array {
  93. return [
  94. // Empty search term with no rooms
  95. ['', 2, 0, [], [], [], false],
  96. // Empty search term with rooms
  97. ['', 2, 0, [
  98. [Room::TYPE_GROUP, 'roomToken', 'Room name'],
  99. ], [], [], false],
  100. // Search term with no matches
  101. ['Unmatched search term', 2, 0, [
  102. [Room::TYPE_GROUP, 'roomToken', 'Unmatched name'],
  103. ], [], [], false],
  104. // Search term with single wide match
  105. ['room', 2, 0, [
  106. [Room::TYPE_GROUP, 'roomToken', 'Room name'],
  107. [Room::TYPE_GROUP, 'roomToken2', 'Unmatched name'],
  108. ], [], [
  109. self::newResult('Room name', 'roomToken'),
  110. ], false],
  111. // Chats without chat permission are not returned
  112. ['room', 2, 0, [
  113. [Room::TYPE_GROUP, 'roomToken', 'Room name', Attendee::PERMISSIONS_MAX_DEFAULT ^ Attendee::PERMISSIONS_CHAT],
  114. ], [], [], false],
  115. // Search term with single exact match
  116. ['room name', 2, 0, [
  117. [Room::TYPE_GROUP, 'roomToken', 'Unmatched name'],
  118. [Room::TYPE_GROUP, 'roomToken2', 'Room name'],
  119. ], [
  120. self::newResult('Room name', 'roomToken2'),
  121. ], [], false],
  122. // Search term with single exact match and single wide match
  123. ['room name', 2, 0, [
  124. [Room::TYPE_GROUP, 'roomToken', 'Room name that also matches'],
  125. [Room::TYPE_GROUP, 'roomToken2', 'Room name'],
  126. ], [
  127. self::newResult('Room name', 'roomToken2'),
  128. ], [
  129. self::newResult('Room name that also matches', 'roomToken'),
  130. ], false],
  131. // Search term matching one-to-one rooms (not possible in practice
  132. // as one-to-one rooms do not have a name, but it would be if they
  133. // had, so it is included here for completeness).
  134. ['room name', 2, 0, [
  135. [Room::TYPE_ONE_TO_ONE, 'roomToken', 'Room name that also matches'],
  136. [Room::TYPE_ONE_TO_ONE, 'roomToken2', 'Room name'],
  137. ], [
  138. self::newResult('Room name', 'roomToken2'),
  139. ], [
  140. self::newResult('Room name that also matches', 'roomToken'),
  141. ], false],
  142. // Search term matching public rooms
  143. ['room name', 2, 0, [
  144. [Room::TYPE_PUBLIC, 'roomToken', 'Room name that also matches'],
  145. [Room::TYPE_PUBLIC, 'roomToken2', 'Room name'],
  146. ], [
  147. self::newResult('Room name', 'roomToken2'),
  148. ], [
  149. self::newResult('Room name that also matches', 'roomToken'),
  150. ], false],
  151. // Search term with several wide matches
  152. ['room', 2, 0, [
  153. [Room::TYPE_GROUP, 'roomToken', 'Room name'],
  154. [Room::TYPE_GROUP, 'roomToken2', 'Another room name'],
  155. [Room::TYPE_GROUP, 'roomToken3', 'Room name'],
  156. [Room::TYPE_GROUP, 'roomToken4', 'Another room name'],
  157. ], [], [
  158. self::newResult('Room name', 'roomToken'),
  159. self::newResult('Another room name', 'roomToken2'),
  160. self::newResult('Room name', 'roomToken3'),
  161. self::newResult('Another room name', 'roomToken4'),
  162. ], false],
  163. // Search term with several exact matches
  164. ['room name', 2, 0, [
  165. [Room::TYPE_GROUP, 'roomToken', 'Room name'],
  166. [Room::TYPE_GROUP, 'roomToken2', 'Room name'],
  167. [Room::TYPE_GROUP, 'roomToken3', 'Room name'],
  168. [Room::TYPE_GROUP, 'roomToken4', 'Room name'],
  169. ], [
  170. self::newResult('Room name', 'roomToken'),
  171. self::newResult('Room name', 'roomToken2'),
  172. self::newResult('Room name', 'roomToken3'),
  173. self::newResult('Room name', 'roomToken4'),
  174. ], [], false],
  175. // Search term with several matches
  176. ['room name', 2, 0, [
  177. [Room::TYPE_GROUP, 'roomToken', 'Room name'],
  178. [Room::TYPE_GROUP, 'roomToken2', 'Unmatched name'],
  179. [Room::TYPE_GROUP, 'roomToken3', 'Another room name'],
  180. [Room::TYPE_GROUP, 'roomToken4', 'Room name'],
  181. [Room::TYPE_ONE_TO_ONE, 'roomToken5', 'Room name'],
  182. [Room::TYPE_PUBLIC, 'roomToken6', 'Room name'],
  183. [Room::TYPE_GROUP, 'roomToken7', 'Another unmatched name'],
  184. [Room::TYPE_ONE_TO_ONE, 'roomToken8', 'Another unmatched name'],
  185. [Room::TYPE_PUBLIC, 'roomToken9', 'Another unmatched name'],
  186. [Room::TYPE_ONE_TO_ONE, 'roomToken10', 'Another room name'],
  187. [Room::TYPE_PUBLIC, 'roomToken11', 'Another room name'],
  188. ], [
  189. self::newResult('Room name', 'roomToken'),
  190. self::newResult('Room name', 'roomToken4'),
  191. self::newResult('Room name', 'roomToken5'),
  192. self::newResult('Room name', 'roomToken6'),
  193. ], [
  194. self::newResult('Another room name', 'roomToken3'),
  195. self::newResult('Another room name', 'roomToken10'),
  196. self::newResult('Another room name', 'roomToken11'),
  197. ], false],
  198. ];
  199. }
  200. /**
  201. * @dataProvider dataSearch
  202. */
  203. public function testSearch(
  204. string $searchTerm,
  205. int $limit,
  206. int $offset,
  207. array $roomsForParticipant,
  208. array $expectedMatchesExact,
  209. array $expectedMatches,
  210. bool $expectedHasMoreResults
  211. ) {
  212. $rooms = [];
  213. foreach ($roomsForParticipant as $roomData) {
  214. $rooms[] = call_user_func_array([$this, 'newRoom'], $roomData);
  215. }
  216. $this->manager->expects($this->any())
  217. ->method('getRoomsForUser')
  218. ->with('user0')
  219. ->willReturn($rooms);
  220. $this->searchResult->expects($this->any())
  221. ->method('addResultSet')
  222. ->with(
  223. $this->callback(
  224. function (SearchResultType $searchResultType) {
  225. return $searchResultType->getLabel() === 'rooms';
  226. }
  227. ),
  228. $expectedMatches,
  229. $expectedMatchesExact
  230. );
  231. $hasMoreResults = $this->plugin->search($searchTerm, $limit, $offset, $this->searchResult);
  232. $this->assertSame($expectedHasMoreResults, $hasMoreResults);
  233. }
  234. }