ContactsManagerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test;
  8. use OCP\IAddressBook;
  9. class ContactsManagerTest extends \Test\TestCase {
  10. /** @var \OC\ContactsManager */
  11. private $cm;
  12. protected function setUp(): void {
  13. parent::setUp();
  14. $this->cm = new \OC\ContactsManager();
  15. }
  16. public function searchProvider() {
  17. $search1 = [
  18. 0 => [
  19. 'N' => [0 => '', 1 => 'Jan', 2 => 'Jansen', 3 => '', 4 => '',],
  20. 'UID' => '04ada7f5-01f9-4309-9c82-6b555b2170ed',
  21. 'FN' => 'Jan Jansen',
  22. 'id' => '1',
  23. 'addressbook-key' => 'simple:1',
  24. ],
  25. 0 => [
  26. 'N' => [0 => '', 1 => 'Tom', 2 => 'Peeters', 3 => '', 4 => '',],
  27. 'UID' => '04ada7f5-01f9-4309-9c82-2345-2345--6b555b2170ed',
  28. 'FN' => 'Tom Peeters',
  29. 'id' => '2',
  30. 'addressbook-key' => 'simple:1',
  31. ],
  32. ];
  33. $search2 = [
  34. 0 => [
  35. 'N' => [0 => '', 1 => 'fg', 2 => '', 3 => '', 4 => '',],
  36. 'UID' => '04ada234h5jh357f5-01f9-4309-9c82-6b555b2170ed',
  37. 'FN' => 'Jan Rompuy',
  38. 'id' => '1',
  39. 'addressbook-key' => 'simple:2',
  40. ],
  41. 0 => [
  42. 'N' => [0 => '', 1 => 'fg', 2 => '', 3 => '', 4 => '',],
  43. 'UID' => '04ada7f5-01f9-4309-345kj345j9c82-2345-2345--6b555b2170ed',
  44. 'FN' => 'Tim Peeters',
  45. 'id' => '2',
  46. 'addressbook-key' => 'simple:2',
  47. ],
  48. ];
  49. $expectedResult = array_merge($search1, $search2);
  50. return [
  51. [
  52. $search1,
  53. $search2,
  54. $expectedResult
  55. ]
  56. ];
  57. }
  58. /**
  59. * @dataProvider searchProvider
  60. */
  61. public function testSearch($search1, $search2, $expectedResult) {
  62. /** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
  63. $addressbook1 = $this->getMockBuilder('\OCP\IAddressBook')
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $addressbook1->expects($this->once())
  67. ->method('search')
  68. ->willReturn($search1);
  69. $addressbook1->expects($this->any())
  70. ->method('getKey')
  71. ->willReturn('simple:1');
  72. $addressbook2 = $this->getMockBuilder('\OCP\IAddressBook')
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $addressbook2->expects($this->once())
  76. ->method('search')
  77. ->willReturn($search2);
  78. $addressbook2->expects($this->any())
  79. ->method('getKey')
  80. ->willReturn('simple:2');
  81. $this->cm->registerAddressBook($addressbook1);
  82. $this->cm->registerAddressBook($addressbook2);
  83. $result = $this->cm->search('');
  84. $this->assertEquals($expectedResult, $result);
  85. }
  86. public function testDeleteHavePermission() {
  87. /** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
  88. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $addressbook->expects($this->any())
  92. ->method('getPermissions')
  93. ->willReturn(\OCP\Constants::PERMISSION_ALL);
  94. $addressbook->expects($this->once())
  95. ->method('delete')
  96. ->willReturn('returnMe');
  97. $addressbook->expects($this->any())
  98. ->method('getKey')
  99. ->willReturn('addressbookKey');
  100. $this->cm->registerAddressBook($addressbook);
  101. $result = $this->cm->delete(1, $addressbook->getKey());
  102. $this->assertEquals($result, 'returnMe');
  103. }
  104. public function testDeleteNoPermission() {
  105. /** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
  106. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $addressbook->expects($this->any())
  110. ->method('getPermissions')
  111. ->willReturn(\OCP\Constants::PERMISSION_READ);
  112. $addressbook->expects($this->never())
  113. ->method('delete');
  114. $addressbook->expects($this->any())
  115. ->method('getKey')
  116. ->willReturn('addressbookKey');
  117. $this->cm->registerAddressBook($addressbook);
  118. $result = $this->cm->delete(1, $addressbook->getKey());
  119. $this->assertEquals($result, null);
  120. }
  121. public function testDeleteNoAddressbook() {
  122. /** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
  123. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126. $addressbook->expects($this->never())
  127. ->method('delete');
  128. $addressbook->expects($this->any())
  129. ->method('getKey')
  130. ->willReturn('addressbookKey');
  131. $this->cm->registerAddressBook($addressbook);
  132. $result = $this->cm->delete(1, 'noaddressbook');
  133. $this->assertEquals($result, null);
  134. }
  135. public function testCreateOrUpdateHavePermission() {
  136. /** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
  137. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  138. ->disableOriginalConstructor()
  139. ->getMock();
  140. $addressbook->expects($this->any())
  141. ->method('getPermissions')
  142. ->willReturn(\OCP\Constants::PERMISSION_ALL);
  143. $addressbook->expects($this->once())
  144. ->method('createOrUpdate')
  145. ->willReturn('returnMe');
  146. $addressbook->expects($this->any())
  147. ->method('getKey')
  148. ->willReturn('addressbookKey');
  149. $this->cm->registerAddressBook($addressbook);
  150. $result = $this->cm->createOrUpdate([], $addressbook->getKey());
  151. $this->assertEquals($result, 'returnMe');
  152. }
  153. public function testCreateOrUpdateNoPermission() {
  154. /** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
  155. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  156. ->disableOriginalConstructor()
  157. ->getMock();
  158. $addressbook->expects($this->any())
  159. ->method('getPermissions')
  160. ->willReturn(\OCP\Constants::PERMISSION_READ);
  161. $addressbook->expects($this->never())
  162. ->method('createOrUpdate');
  163. $addressbook->expects($this->any())
  164. ->method('getKey')
  165. ->willReturn('addressbookKey');
  166. $this->cm->registerAddressBook($addressbook);
  167. $result = $this->cm->createOrUpdate([], $addressbook->getKey());
  168. $this->assertEquals($result, null);
  169. }
  170. public function testCreateOrUpdateNOAdressbook() {
  171. /** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
  172. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  173. ->disableOriginalConstructor()
  174. ->getMock();
  175. $addressbook->expects($this->never())
  176. ->method('createOrUpdate');
  177. $addressbook->expects($this->any())
  178. ->method('getKey')
  179. ->willReturn('addressbookKey');
  180. $this->cm->registerAddressBook($addressbook);
  181. $result = $this->cm->createOrUpdate([], 'noaddressbook');
  182. $this->assertEquals($result, null);
  183. }
  184. public function testIsEnabledIfNot() {
  185. $result = $this->cm->isEnabled();
  186. $this->assertFalse($result);
  187. }
  188. public function testIsEnabledIfSo() {
  189. /** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
  190. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  191. ->disableOriginalConstructor()
  192. ->getMock();
  193. $addressbook->expects($this->any())
  194. ->method('getKey')
  195. ->willReturn('addressbookKey');
  196. $this->cm->registerAddressBook($addressbook);
  197. $result = $this->cm->isEnabled();
  198. $this->assertTrue($result);
  199. }
  200. public function testAddressBookEnumeration() {
  201. // create mock for the addressbook
  202. /** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
  203. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  204. ->disableOriginalConstructor()
  205. ->getMock();
  206. // setup return for method calls
  207. $addressbook->expects($this->any())
  208. ->method('getKey')
  209. ->willReturn('SIMPLE_ADDRESS_BOOK');
  210. $addressbook->expects($this->any())
  211. ->method('getDisplayName')
  212. ->willReturn('A very simple Addressbook');
  213. // register the address book
  214. $this->cm->registerAddressBook($addressbook);
  215. $all_books = $this->cm->getUserAddressBooks();
  216. $this->assertEquals(1, count($all_books));
  217. $this->assertEquals($addressbook, $all_books['SIMPLE_ADDRESS_BOOK']);
  218. }
  219. }