contacts.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Thomas Müller
  6. * @copyright 2012 Thomas Müller thomas.mueller@tmit.eu
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. class Test_Contacts extends PHPUnit_Framework_TestCase
  22. {
  23. public function setUp() {
  24. OCP\Contacts::clear();
  25. }
  26. public function tearDown() {
  27. }
  28. public function testDisabledIfEmpty() {
  29. // pretty simple
  30. $this->assertFalse(OCP\Contacts::isEnabled());
  31. }
  32. public function testEnabledAfterRegister() {
  33. // create mock for the addressbook
  34. $stub = $this->getMockForAbstractClass("OCP\IAddressBook", array('getKey'));
  35. // we expect getKey to be called twice:
  36. // first time on register
  37. // second time on un-register
  38. $stub->expects($this->exactly(2))
  39. ->method('getKey');
  40. // not enabled before register
  41. $this->assertFalse(OCP\Contacts::isEnabled());
  42. // register the address book
  43. OCP\Contacts::registerAddressBook($stub);
  44. // contacts api shall be enabled
  45. $this->assertTrue(OCP\Contacts::isEnabled());
  46. // unregister the address book
  47. OCP\Contacts::unregisterAddressBook($stub);
  48. // not enabled after register
  49. $this->assertFalse(OCP\Contacts::isEnabled());
  50. }
  51. public function testAddressBookEnumeration() {
  52. // create mock for the addressbook
  53. $stub = $this->getMockForAbstractClass("OCP\IAddressBook", array('getKey', 'getDisplayName'));
  54. // setup return for method calls
  55. $stub->expects($this->any())
  56. ->method('getKey')
  57. ->will($this->returnValue('SIMPLE_ADDRESS_BOOK'));
  58. $stub->expects($this->any())
  59. ->method('getDisplayName')
  60. ->will($this->returnValue('A very simple Addressbook'));
  61. // register the address book
  62. OCP\Contacts::registerAddressBook($stub);
  63. $all_books = OCP\Contacts::getAddressBooks();
  64. $this->assertEquals(1, count($all_books));
  65. $this->assertEquals('A very simple Addressbook', $all_books['SIMPLE_ADDRESS_BOOK']);
  66. }
  67. public function testSearchInAddressBook() {
  68. // create mock for the addressbook
  69. $stub1 = $this->getMockForAbstractClass("OCP\IAddressBook", array('getKey', 'getDisplayName', 'search'));
  70. $stub2 = $this->getMockForAbstractClass("OCP\IAddressBook", array('getKey', 'getDisplayName', 'search'));
  71. $searchResult1 = array(
  72. array('id' => 0, 'FN' => 'Frank Karlitschek', 'EMAIL' => 'a@b.c', 'GEO' => '37.386013;-122.082932'),
  73. array('id' => 5, 'FN' => 'Klaas Freitag', 'EMAIL' => array('d@e.f', 'g@h.i')),
  74. );
  75. $searchResult2 = array(
  76. array('id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => 'a@b.c'),
  77. array('id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => array('d@e.f', 'g@h.i')),
  78. );
  79. // setup return for method calls for $stub1
  80. $stub1->expects($this->any())->method('getKey')->will($this->returnValue('SIMPLE_ADDRESS_BOOK1'));
  81. $stub1->expects($this->any())->method('getDisplayName')->will($this->returnValue('Address book ownCloud Inc'));
  82. $stub1->expects($this->any())->method('search')->will($this->returnValue($searchResult1));
  83. // setup return for method calls for $stub2
  84. $stub2->expects($this->any())->method('getKey')->will($this->returnValue('SIMPLE_ADDRESS_BOOK2'));
  85. $stub2->expects($this->any())->method('getDisplayName')->will($this->returnValue('Address book ownCloud Community'));
  86. $stub2->expects($this->any())->method('search')->will($this->returnValue($searchResult2));
  87. // register the address books
  88. OCP\Contacts::registerAddressBook($stub1);
  89. OCP\Contacts::registerAddressBook($stub2);
  90. $all_books = OCP\Contacts::getAddressBooks();
  91. // assert the count - doesn't hurt
  92. $this->assertEquals(2, count($all_books));
  93. // perform the search
  94. $result = OCP\Contacts::search('x', array());
  95. // we expect 4 hits
  96. $this->assertEquals(4, count($result));
  97. }
  98. }