ContactsManagerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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() {
  8. parent::setUp();
  9. $this->cm = new \OC\ContactsManager();
  10. }
  11. public function searchProvider(){
  12. $search1 = array(
  13. 0 => array(
  14. 'N' => array(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 => array(
  21. 'N' => array(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 = array(
  29. 0 => array(
  30. 'N' => array(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 => array(
  37. 'N' => array(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 array(
  46. array(
  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. $this->cm->registerAddressBook($addressbook);
  93. $result = $this->cm->delete(1, $addressbook->getKey());
  94. $this->assertEquals($result, 'returnMe');
  95. }
  96. public function testDeleteNoPermission(){
  97. /** @var \PHPUnit_Framework_MockObject_MockObject|IAddressBook $addressbook */
  98. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $addressbook->expects($this->any())
  102. ->method('getPermissions')
  103. ->willReturn(\OCP\Constants::PERMISSION_READ);
  104. $addressbook->expects($this->never())
  105. ->method('delete');
  106. $this->cm->registerAddressBook($addressbook);
  107. $result = $this->cm->delete(1, $addressbook->getKey());
  108. $this->assertEquals($result, null);
  109. }
  110. public function testDeleteNoAddressbook(){
  111. /** @var \PHPUnit_Framework_MockObject_MockObject|IAddressBook $addressbook */
  112. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $addressbook->expects($this->never())
  116. ->method('delete');
  117. $this->cm->registerAddressBook($addressbook);
  118. $result = $this->cm->delete(1, 'noaddressbook');
  119. $this->assertEquals($result, null);
  120. }
  121. public function testCreateOrUpdateHavePermission(){
  122. /** @var \PHPUnit_Framework_MockObject_MockObject|IAddressBook $addressbook */
  123. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126. $addressbook->expects($this->any())
  127. ->method('getPermissions')
  128. ->willReturn(\OCP\Constants::PERMISSION_ALL);
  129. $addressbook->expects($this->once())
  130. ->method('createOrUpdate')
  131. ->willReturn('returnMe');
  132. $this->cm->registerAddressBook($addressbook);
  133. $result = $this->cm->createOrUpdate(array(), $addressbook->getKey());
  134. $this->assertEquals($result, 'returnMe');
  135. }
  136. public function testCreateOrUpdateNoPermission(){
  137. /** @var \PHPUnit_Framework_MockObject_MockObject|IAddressBook $addressbook */
  138. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  139. ->disableOriginalConstructor()
  140. ->getMock();
  141. $addressbook->expects($this->any())
  142. ->method('getPermissions')
  143. ->willReturn(\OCP\Constants::PERMISSION_READ);
  144. $addressbook->expects($this->never())
  145. ->method('createOrUpdate');
  146. $this->cm->registerAddressBook($addressbook);
  147. $result = $this->cm->createOrUpdate(array(), $addressbook->getKey());
  148. $this->assertEquals($result, null);
  149. }
  150. public function testCreateOrUpdateNOAdressbook(){
  151. /** @var \PHPUnit_Framework_MockObject_MockObject|IAddressBook $addressbook */
  152. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  153. ->disableOriginalConstructor()
  154. ->getMock();
  155. $addressbook->expects($this->never())
  156. ->method('createOrUpdate');
  157. $this->cm->registerAddressBook($addressbook);
  158. $result = $this->cm->createOrUpdate(array(), 'noaddressbook');
  159. $this->assertEquals($result, null);
  160. }
  161. public function testIsEnabledIfNot(){
  162. $result = $this->cm->isEnabled();
  163. $this->assertFalse($result);
  164. }
  165. public function testIsEnabledIfSo(){
  166. /** @var \PHPUnit_Framework_MockObject_MockObject|IAddressBook $addressbook */
  167. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  168. ->disableOriginalConstructor()
  169. ->getMock();
  170. $this->cm->registerAddressBook($addressbook);
  171. $result = $this->cm->isEnabled();
  172. $this->assertTrue($result);
  173. }
  174. public function testAddressBookEnumeration() {
  175. // create mock for the addressbook
  176. /** @var \PHPUnit_Framework_MockObject_MockObject|IAddressBook $addressbook */
  177. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  178. ->disableOriginalConstructor()
  179. ->getMock();
  180. // setup return for method calls
  181. $addressbook->expects($this->any())
  182. ->method('getKey')
  183. ->will($this->returnValue('SIMPLE_ADDRESS_BOOK'));
  184. $addressbook->expects($this->any())
  185. ->method('getDisplayName')
  186. ->will($this->returnValue('A very simple Addressbook'));
  187. // register the address book
  188. $this->cm->registerAddressBook($addressbook);
  189. $all_books = $this->cm->getAddressBooks();
  190. $this->assertEquals(1, count($all_books));
  191. $this->assertEquals('A very simple Addressbook', $all_books['SIMPLE_ADDRESS_BOOK']);
  192. }
  193. }