ContactsManagerTest.php 5.3 KB

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