contacts.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 \Test\TestCase {
  22. protected function setUp() {
  23. parent::setUp();
  24. OCP\Contacts::clear();
  25. }
  26. public function testDisabledIfEmpty() {
  27. // pretty simple
  28. $this->assertFalse(OCP\Contacts::isEnabled());
  29. }
  30. public function testEnabledAfterRegister() {
  31. // create mock for the addressbook
  32. $stub = $this->getMockForAbstractClass("OCP\IAddressBook", array('getKey'));
  33. // we expect getKey to be called twice:
  34. // first time on register
  35. // second time on un-register
  36. $stub->expects($this->exactly(2))
  37. ->method('getKey');
  38. // not enabled before register
  39. $this->assertFalse(OCP\Contacts::isEnabled());
  40. // register the address book
  41. OCP\Contacts::registerAddressBook($stub);
  42. // contacts api shall be enabled
  43. $this->assertTrue(OCP\Contacts::isEnabled());
  44. // unregister the address book
  45. OCP\Contacts::unregisterAddressBook($stub);
  46. // not enabled after register
  47. $this->assertFalse(OCP\Contacts::isEnabled());
  48. }
  49. public function testAddressBookEnumeration() {
  50. // create mock for the addressbook
  51. $stub = $this->getMockForAbstractClass("OCP\IAddressBook", array('getKey', 'getDisplayName'));
  52. // setup return for method calls
  53. $stub->expects($this->any())
  54. ->method('getKey')
  55. ->will($this->returnValue('SIMPLE_ADDRESS_BOOK'));
  56. $stub->expects($this->any())
  57. ->method('getDisplayName')
  58. ->will($this->returnValue('A very simple Addressbook'));
  59. // register the address book
  60. OCP\Contacts::registerAddressBook($stub);
  61. $all_books = OCP\Contacts::getAddressBooks();
  62. $this->assertEquals(1, count($all_books));
  63. $this->assertEquals('A very simple Addressbook', $all_books['SIMPLE_ADDRESS_BOOK']);
  64. }
  65. public function testSearchInAddressBook() {
  66. // create mock for the addressbook
  67. $stub1 = $this->getMockForAbstractClass("OCP\IAddressBook", array('getKey', 'getDisplayName', 'search'));
  68. $stub2 = $this->getMockForAbstractClass("OCP\IAddressBook", array('getKey', 'getDisplayName', 'search'));
  69. $searchResult1 = array(
  70. array('id' => 0, 'FN' => 'Frank Karlitschek', 'EMAIL' => 'a@b.c', 'GEO' => '37.386013;-122.082932'),
  71. array('id' => 5, 'FN' => 'Klaas Freitag', 'EMAIL' => array('d@e.f', 'g@h.i')),
  72. );
  73. $searchResult2 = array(
  74. array('id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => 'a@b.c'),
  75. array('id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => array('d@e.f', 'g@h.i')),
  76. );
  77. // setup return for method calls for $stub1
  78. $stub1->expects($this->any())->method('getKey')->will($this->returnValue('SIMPLE_ADDRESS_BOOK1'));
  79. $stub1->expects($this->any())->method('getDisplayName')->will($this->returnValue('Address book ownCloud Inc'));
  80. $stub1->expects($this->any())->method('search')->will($this->returnValue($searchResult1));
  81. // setup return for method calls for $stub2
  82. $stub2->expects($this->any())->method('getKey')->will($this->returnValue('SIMPLE_ADDRESS_BOOK2'));
  83. $stub2->expects($this->any())->method('getDisplayName')->will($this->returnValue('Address book ownCloud Community'));
  84. $stub2->expects($this->any())->method('search')->will($this->returnValue($searchResult2));
  85. // register the address books
  86. OCP\Contacts::registerAddressBook($stub1);
  87. OCP\Contacts::registerAddressBook($stub2);
  88. $all_books = OCP\Contacts::getAddressBooks();
  89. // assert the count - doesn't hurt
  90. $this->assertEquals(2, count($all_books));
  91. // perform the search
  92. $result = OCP\Contacts::search('x', array());
  93. // we expect 4 hits
  94. $this->assertEquals(4, count($result));
  95. }
  96. }