contactsmanager.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. class Test_ContactsManager extends \Test\TestCase {
  3. /** @var \OC\ContactsManager */
  4. private $cm;
  5. protected function setUp() {
  6. parent::setUp();
  7. $this->cm = new \OC\ContactsManager();
  8. }
  9. public function searchProvider(){
  10. $search1 = array(
  11. 0 => array(
  12. 'N' => array(0 => '', 1 => 'Jan', 2 => 'Jansen', 3 => '', 4 => '',),
  13. 'UID' => '04ada7f5-01f9-4309-9c82-6b555b2170ed',
  14. 'FN' => 'Jan Jansen',
  15. 'id' => '1',
  16. 'addressbook-key' => 'simple:1',
  17. ),
  18. 0 => array(
  19. 'N' => array(0 => '', 1 => 'Tom', 2 => 'Peeters', 3 => '', 4 => '',),
  20. 'UID' => '04ada7f5-01f9-4309-9c82-2345-2345--6b555b2170ed',
  21. 'FN' => 'Tom Peeters',
  22. 'id' => '2',
  23. 'addressbook-key' => 'simple:1',
  24. ),
  25. );
  26. $search2 = array(
  27. 0 => array(
  28. 'N' => array(0 => '', 1 => 'fg', 2 => '', 3 => '', 4 => '',),
  29. 'UID' => '04ada234h5jh357f5-01f9-4309-9c82-6b555b2170ed',
  30. 'FN' => 'Jan Rompuy',
  31. 'id' => '1',
  32. 'addressbook-key' => 'simple:2',
  33. ),
  34. 0 => array(
  35. 'N' => array(0 => '', 1 => 'fg', 2 => '', 3 => '', 4 => '',),
  36. 'UID' => '04ada7f5-01f9-4309-345kj345j9c82-2345-2345--6b555b2170ed',
  37. 'FN' => 'Tim Peeters',
  38. 'id' => '2',
  39. 'addressbook-key' => 'simple:2',
  40. ),
  41. );
  42. $expectedResult = array_merge($search1, $search2);
  43. return array(
  44. array(
  45. $search1,
  46. $search2,
  47. $expectedResult
  48. )
  49. );
  50. }
  51. /**
  52. * @dataProvider searchProvider
  53. */
  54. public function testSearch($search1, $search2, $expectedResult ){
  55. $addressbook1 = $this->getMockBuilder('\OCP\IAddressBook')
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $addressbook1->expects($this->once())
  59. ->method('search')
  60. ->willReturn($search1);
  61. $addressbook1->expects($this->any())
  62. ->method('getKey')
  63. ->willReturn('simple:1');
  64. $addressbook2 = $this->getMockBuilder('\OCP\IAddressBook')
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $addressbook2->expects($this->once())
  68. ->method('search')
  69. ->willReturn($search2);
  70. $addressbook2->expects($this->any())
  71. ->method('getKey')
  72. ->willReturn('simple:2');
  73. $this->cm->registerAddressBook($addressbook1);
  74. $this->cm->registerAddressBook($addressbook2);
  75. $result = $this->cm->search('');
  76. $this->assertEquals($expectedResult, $result);
  77. }
  78. public function testDeleteHavePermission(){
  79. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $addressbook->expects($this->any())
  83. ->method('getPermissions')
  84. ->willReturn(\OCP\Constants::PERMISSION_ALL);
  85. $addressbook->expects($this->once())
  86. ->method('delete')
  87. ->willReturn('returnMe');
  88. $this->cm->registerAddressBook($addressbook);
  89. $result = $this->cm->delete(1, $addressbook->getKey());
  90. $this->assertEquals($result, 'returnMe');
  91. }
  92. public function testDeleteNoPermission(){
  93. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96. $addressbook->expects($this->any())
  97. ->method('getPermissions')
  98. ->willReturn(\OCP\Constants::PERMISSION_READ);
  99. $addressbook->expects($this->never())
  100. ->method('delete');
  101. $this->cm->registerAddressBook($addressbook);
  102. $result = $this->cm->delete(1, $addressbook->getKey());
  103. $this->assertEquals($result, null);
  104. }
  105. public function testDeleteNoAddressbook(){
  106. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $addressbook->expects($this->never())
  110. ->method('delete');
  111. $this->cm->registerAddressBook($addressbook);
  112. $result = $this->cm->delete(1, 'noaddressbook');
  113. $this->assertEquals($result, null);
  114. }
  115. public function testCreateOrUpdateHavePermission(){
  116. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  117. ->disableOriginalConstructor()
  118. ->getMock();
  119. $addressbook->expects($this->any())
  120. ->method('getPermissions')
  121. ->willReturn(\OCP\Constants::PERMISSION_ALL);
  122. $addressbook->expects($this->once())
  123. ->method('createOrUpdate')
  124. ->willReturn('returnMe');
  125. $this->cm->registerAddressBook($addressbook);
  126. $result = $this->cm->createOrUpdate(array(), $addressbook->getKey());
  127. $this->assertEquals($result, 'returnMe');
  128. }
  129. public function testCreateOrUpdateNoPermission(){
  130. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  131. ->disableOriginalConstructor()
  132. ->getMock();
  133. $addressbook->expects($this->any())
  134. ->method('getPermissions')
  135. ->willReturn(\OCP\Constants::PERMISSION_READ);
  136. $addressbook->expects($this->never())
  137. ->method('createOrUpdate');
  138. $this->cm->registerAddressBook($addressbook);
  139. $result = $this->cm->createOrUpdate(array(), $addressbook->getKey());
  140. $this->assertEquals($result, null);
  141. }
  142. public function testCreateOrUpdateNOAdressbook(){
  143. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  144. ->disableOriginalConstructor()
  145. ->getMock();
  146. $addressbook->expects($this->never())
  147. ->method('createOrUpdate');
  148. $this->cm->registerAddressBook($addressbook);
  149. $result = $this->cm->createOrUpdate(array(), 'noaddressbook');
  150. $this->assertEquals($result, null);
  151. }
  152. public function testIsEnabledIfNot(){
  153. $result = $this->cm->isEnabled();
  154. $this->assertFalse($result);
  155. }
  156. public function testIsEnabledIfSo(){
  157. $addressbook = $this->getMockBuilder('\OCP\IAddressBook')
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. $this->cm->registerAddressBook($addressbook);
  161. $result = $this->cm->isEnabled();
  162. $this->assertTrue($result);
  163. }
  164. }