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.

ManagerTest.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\Contacts\ContactsMenu\IEntry;
  29. use OCP\Contacts\ContactsMenu\IProvider;
  30. use OCP\IConfig;
  31. use OCP\IUser;
  32. use PHPUnit_Framework_MockObject_MockObject;
  33. use Test\TestCase;
  34. class ManagerTest extends TestCase {
  35. /** @var ContactsStore|PHPUnit_Framework_MockObject_MockObject */
  36. private $contactsStore;
  37. /** @var IAppManager|PHPUnit_Framework_MockObject_MockObject */
  38. private $appManager;
  39. /** @var IConfig|PHPUnit_Framework_MockObject_MockObject */
  40. private $config;
  41. /** @var ActionProviderStore|PHPUnit_Framework_MockObject_MockObject */
  42. private $actionProviderStore;
  43. /** @var Manager */
  44. private $manager;
  45. protected function setUp() {
  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() {
  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->at(0))
  70. ->method('getSystemValueInt')
  71. ->with('sharing.maxAutocompleteResults', 25)
  72. ->willReturn(25);
  73. $this->config->expects($this->at(1))
  74. ->method('getSystemValueInt')
  75. ->with('sharing.minSearchStringLength', 0)
  76. ->willReturn(0);
  77. $this->contactsStore->expects($this->once())
  78. ->method('getContacts')
  79. ->with($user, $filter)
  80. ->willReturn($entries);
  81. $this->actionProviderStore->expects($this->once())
  82. ->method('getProviders')
  83. ->with($user)
  84. ->willReturn([$provider]);
  85. $provider->expects($this->exactly(25))
  86. ->method('process');
  87. $this->appManager->expects($this->once())
  88. ->method('isEnabledForUser')
  89. ->with($this->equalTo('contacts'), $user)
  90. ->willReturn(false);
  91. $expected = [
  92. 'contacts' => array_slice($entries, 0, 25),
  93. 'contactsAppEnabled' => false,
  94. ];
  95. $data = $this->manager->getEntries($user, $filter);
  96. $this->assertEquals($expected, $data);
  97. }
  98. public function testGetFilteredEntriesLimit() {
  99. $filter = 'con';
  100. $user = $this->createMock(IUser::class);
  101. $entries = $this->generateTestEntries();
  102. $provider = $this->createMock(IProvider::class);
  103. $this->config->expects($this->at(0))
  104. ->method('getSystemValueInt')
  105. ->with('sharing.maxAutocompleteResults', 25)
  106. ->willReturn(3);
  107. $this->config->expects($this->at(1))
  108. ->method('getSystemValueInt')
  109. ->with('sharing.minSearchStringLength', 0)
  110. ->willReturn(0);
  111. $this->contactsStore->expects($this->once())
  112. ->method('getContacts')
  113. ->with($user, $filter)
  114. ->willReturn($entries);
  115. $this->actionProviderStore->expects($this->once())
  116. ->method('getProviders')
  117. ->with($user)
  118. ->willReturn([$provider]);
  119. $provider->expects($this->exactly(3))
  120. ->method('process');
  121. $this->appManager->expects($this->once())
  122. ->method('isEnabledForUser')
  123. ->with($this->equalTo('contacts'), $user)
  124. ->willReturn(false);
  125. $expected = [
  126. 'contacts' => array_slice($entries, 0, 3),
  127. 'contactsAppEnabled' => false,
  128. ];
  129. $data = $this->manager->getEntries($user, $filter);
  130. $this->assertEquals($expected, $data);
  131. }
  132. public function testGetFilteredEntriesMinSearchStringLength() {
  133. $filter = 'con';
  134. $user = $this->createMock(IUser::class);
  135. $provider = $this->createMock(IProvider::class);
  136. $this->config->expects($this->at(0))
  137. ->method('getSystemValueInt')
  138. ->with('sharing.maxAutocompleteResults', 25)
  139. ->willReturn(3);
  140. $this->config->expects($this->at(1))
  141. ->method('getSystemValueInt')
  142. ->with('sharing.minSearchStringLength', 0)
  143. ->willReturn(4);
  144. $this->appManager->expects($this->once())
  145. ->method('isEnabledForUser')
  146. ->with($this->equalTo('contacts'), $user)
  147. ->willReturn(false);
  148. $expected = [
  149. 'contacts' => [],
  150. 'contactsAppEnabled' => false,
  151. ];
  152. $data = $this->manager->getEntries($user, $filter);
  153. $this->assertEquals($expected, $data);
  154. }
  155. public function testFindOne() {
  156. $shareTypeFilter = 42;
  157. $shareWithFilter = 'foobar';
  158. $user = $this->createMock(IUser::class);
  159. $entry = current($this->generateTestEntries());
  160. $provider = $this->createMock(IProvider::class);
  161. $this->contactsStore->expects($this->once())
  162. ->method('findOne')
  163. ->with($user, $shareTypeFilter, $shareWithFilter)
  164. ->willReturn($entry);
  165. $this->actionProviderStore->expects($this->once())
  166. ->method('getProviders')
  167. ->with($user)
  168. ->willReturn([$provider]);
  169. $provider->expects($this->once())
  170. ->method('process');
  171. $data = $this->manager->findOne($user, $shareTypeFilter, $shareWithFilter);
  172. $this->assertEquals($entry, $data);
  173. }
  174. public function testFindOne404() {
  175. $shareTypeFilter = 42;
  176. $shareWithFilter = 'foobar';
  177. $user = $this->createMock(IUser::class);
  178. $provider = $this->createMock(IProvider::class);
  179. $this->contactsStore->expects($this->once())
  180. ->method('findOne')
  181. ->with($user, $shareTypeFilter, $shareWithFilter)
  182. ->willReturn(null);
  183. $this->actionProviderStore->expects($this->never())
  184. ->method('getProviders')
  185. ->with($user)
  186. ->willReturn([$provider]);
  187. $provider->expects($this->never())
  188. ->method('process');
  189. $data = $this->manager->findOne($user, $shareTypeFilter, $shareWithFilter);
  190. $this->assertEquals(null, $data);
  191. }
  192. }