1
0

ManagerTest.php 6.6 KB

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