ContactsTest.php 4.2 KB

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