1
0

ContactsManagerTest.php 7.3 KB

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