1
0

ManagerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\Contacts\ContactsMenu;
  7. use OC\Contacts\ContactsMenu\ActionProviderStore;
  8. use OC\Contacts\ContactsMenu\ContactsStore;
  9. use OC\Contacts\ContactsMenu\Entry;
  10. use OC\Contacts\ContactsMenu\Manager;
  11. use OCP\App\IAppManager;
  12. use OCP\Constants;
  13. use OCP\Contacts\ContactsMenu\IProvider;
  14. use OCP\IConfig;
  15. use OCP\IUser;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Test\TestCase;
  18. class ManagerTest extends TestCase {
  19. /** @var ContactsStore|MockObject */
  20. private $contactsStore;
  21. /** @var IAppManager|MockObject */
  22. private $appManager;
  23. /** @var IConfig|MockObject */
  24. private $config;
  25. /** @var ActionProviderStore|MockObject */
  26. private $actionProviderStore;
  27. private Manager $manager;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->contactsStore = $this->createMock(ContactsStore::class);
  31. $this->actionProviderStore = $this->createMock(ActionProviderStore::class);
  32. $this->appManager = $this->createMock(IAppManager::class);
  33. $this->config = $this->createMock(IConfig::class);
  34. $this->manager = new Manager($this->contactsStore, $this->actionProviderStore, $this->appManager, $this->config);
  35. }
  36. private function generateTestEntries(): array {
  37. $entries = [];
  38. foreach (range('Z', 'A') as $char) {
  39. $entry = $this->createMock(Entry::class);
  40. $entry->expects($this->any())
  41. ->method('getFullName')
  42. ->willReturn('Contact ' . $char);
  43. $entries[] = $entry;
  44. }
  45. return $entries;
  46. }
  47. public function testGetFilteredEntries() {
  48. $filter = 'con';
  49. $user = $this->createMock(IUser::class);
  50. $entries = $this->generateTestEntries();
  51. $provider = $this->createMock(IProvider::class);
  52. $this->config->expects($this->exactly(2))
  53. ->method('getSystemValueInt')
  54. ->withConsecutive(
  55. ['sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT],
  56. ['sharing.minSearchStringLength', 0]
  57. )
  58. ->willReturnOnConsecutiveCalls(25, 0);
  59. $this->contactsStore->expects($this->once())
  60. ->method('getContacts')
  61. ->with($user, $filter)
  62. ->willReturn($entries);
  63. $this->actionProviderStore->expects($this->once())
  64. ->method('getProviders')
  65. ->with($user)
  66. ->willReturn([$provider]);
  67. $provider->expects($this->exactly(25))
  68. ->method('process');
  69. $this->appManager->expects($this->once())
  70. ->method('isEnabledForUser')
  71. ->with($this->equalTo('contacts'), $user)
  72. ->willReturn(false);
  73. $expected = [
  74. 'contacts' => array_slice($entries, 0, 25),
  75. 'contactsAppEnabled' => false,
  76. ];
  77. $data = $this->manager->getEntries($user, $filter);
  78. $this->assertEquals($expected, $data);
  79. }
  80. public function testGetFilteredEntriesLimit() {
  81. $filter = 'con';
  82. $user = $this->createMock(IUser::class);
  83. $entries = $this->generateTestEntries();
  84. $provider = $this->createMock(IProvider::class);
  85. $this->config->expects($this->exactly(2))
  86. ->method('getSystemValueInt')
  87. ->withConsecutive(
  88. ['sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT],
  89. ['sharing.minSearchStringLength', 0]
  90. )
  91. ->willReturnOnConsecutiveCalls(3, 0);
  92. $this->contactsStore->expects($this->once())
  93. ->method('getContacts')
  94. ->with($user, $filter)
  95. ->willReturn($entries);
  96. $this->actionProviderStore->expects($this->once())
  97. ->method('getProviders')
  98. ->with($user)
  99. ->willReturn([$provider]);
  100. $provider->expects($this->exactly(3))
  101. ->method('process');
  102. $this->appManager->expects($this->once())
  103. ->method('isEnabledForUser')
  104. ->with($this->equalTo('contacts'), $user)
  105. ->willReturn(false);
  106. $expected = [
  107. 'contacts' => array_slice($entries, 0, 3),
  108. 'contactsAppEnabled' => false,
  109. ];
  110. $data = $this->manager->getEntries($user, $filter);
  111. $this->assertEquals($expected, $data);
  112. }
  113. public function testGetFilteredEntriesMinSearchStringLength() {
  114. $filter = 'con';
  115. $user = $this->createMock(IUser::class);
  116. $provider = $this->createMock(IProvider::class);
  117. $this->config->expects($this->exactly(2))
  118. ->method('getSystemValueInt')
  119. ->withConsecutive(
  120. ['sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT],
  121. ['sharing.minSearchStringLength', 0]
  122. )
  123. ->willReturnOnConsecutiveCalls(3, 4);
  124. $this->appManager->expects($this->once())
  125. ->method('isEnabledForUser')
  126. ->with($this->equalTo('contacts'), $user)
  127. ->willReturn(false);
  128. $expected = [
  129. 'contacts' => [],
  130. 'contactsAppEnabled' => false,
  131. ];
  132. $data = $this->manager->getEntries($user, $filter);
  133. $this->assertEquals($expected, $data);
  134. }
  135. public function testFindOne() {
  136. $shareTypeFilter = 42;
  137. $shareWithFilter = 'foobar';
  138. $user = $this->createMock(IUser::class);
  139. $entry = current($this->generateTestEntries());
  140. $provider = $this->createMock(IProvider::class);
  141. $this->contactsStore->expects($this->once())
  142. ->method('findOne')
  143. ->with($user, $shareTypeFilter, $shareWithFilter)
  144. ->willReturn($entry);
  145. $this->actionProviderStore->expects($this->once())
  146. ->method('getProviders')
  147. ->with($user)
  148. ->willReturn([$provider]);
  149. $provider->expects($this->once())
  150. ->method('process');
  151. $data = $this->manager->findOne($user, $shareTypeFilter, $shareWithFilter);
  152. $this->assertEquals($entry, $data);
  153. }
  154. public function testFindOne404() {
  155. $shareTypeFilter = 42;
  156. $shareWithFilter = 'foobar';
  157. $user = $this->createMock(IUser::class);
  158. $provider = $this->createMock(IProvider::class);
  159. $this->contactsStore->expects($this->once())
  160. ->method('findOne')
  161. ->with($user, $shareTypeFilter, $shareWithFilter)
  162. ->willReturn(null);
  163. $this->actionProviderStore->expects($this->never())
  164. ->method('getProviders')
  165. ->with($user)
  166. ->willReturn([$provider]);
  167. $provider->expects($this->never())
  168. ->method('process');
  169. $data = $this->manager->findOne($user, $shareTypeFilter, $shareWithFilter);
  170. $this->assertEquals(null, $data);
  171. }
  172. }