2013-08-26 20:10:29 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Contacts\Backend;
|
|
|
|
|
|
|
|
use Sabre\VObject\Reader;
|
|
|
|
|
|
|
|
require_once __DIR__ .'/mock.php';
|
|
|
|
|
|
|
|
class BackendTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $abinfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $permissions = array(
|
|
|
|
\OCP\PERMISSION_READ,
|
|
|
|
\OCP\PERMISSION_CREATE,
|
|
|
|
\OCP\PERMISSION_UPDATE,
|
|
|
|
\OCP\PERMISSION_DELETE
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2014-08-05 22:20:50 +02:00
|
|
|
* @var \OCA\Contacts\Backend\AbstractBackend
|
2013-08-26 20:10:29 +02:00
|
|
|
*/
|
|
|
|
protected $backend;
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function setUp() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$this->backend = new Mock('foobar');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function tearDown() {
|
2013-08-26 20:10:29 +02:00
|
|
|
unset($this->backend);
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testHasContactMethodFor() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
foreach($this->permissions as $permission) {
|
|
|
|
$this->assertTrue($this->backend->hasContactMethodFor($permission));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testHasAddressBookMethodFor() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
foreach($this->permissions as $permission) {
|
|
|
|
$this->assertTrue($this->backend->hasAddressBookMethodFor($permission));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testgetAddressBooksForUser() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$this->assertEquals(1, count($this->backend->getAddressBooksForUser()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testDeleteAddressBook() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$this->assertTrue($this->backend->deleteAddressBook('foo'));
|
|
|
|
$this->assertEquals(array(), $this->backend->addressBooks);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testCreateAddressBook() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$id = $this->backend->createAddressBook(array('displayname' => 'bar'));
|
|
|
|
|
|
|
|
$this->assertNotEquals(false, $id);
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($this->backend->getAddressBooksForUser()));
|
|
|
|
|
|
|
|
$book = $this->backend->getAddressBook($id);
|
|
|
|
|
|
|
|
$this->assertEquals('bar', $book['displayname']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testCreateAddressBookFail() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
2014-03-10 15:14:36 +01:00
|
|
|
// displayname must be provided.
|
2013-08-26 20:10:29 +02:00
|
|
|
$id = $this->backend->createAddressBook(array('description' => 'foo bar'));
|
|
|
|
|
|
|
|
$this->assertFalse($id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testUpdateAddressBook() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->backend->updateAddressBook('foo', array('displayname' => 'bar'))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('bar', $this->backend->addressBooks[0]['displayname']);
|
|
|
|
|
|
|
|
return $this->backend;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testUpdateAddressBookFail() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->backend->updateAddressBook('foo', array('description' => 'foo bar'))
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testUpdateAddressBook
|
|
|
|
*/
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testGetAddressBook($backend) {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$book = $backend->getAddressBook('foo');
|
|
|
|
$this->assertEquals('bar', $book['displayname']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testGetAddressBookFail() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$this->assertNull($this->backend->getAddressBook('bar'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testGetLastModifiedAddressBook() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$this->assertNull($this->backend->lastModifiedAddressBook('foo'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testGetContact() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$contact = $this->backend->getContact('foo', '123');
|
|
|
|
$this->assertTrue(is_array($contact));
|
|
|
|
$this->assertEquals('Max Mustermann', $contact['displayname']);
|
|
|
|
$this->assertEquals('foobar', $contact['owner']);
|
|
|
|
$this->assertEquals(\OCP\PERMISSION_ALL, $contact['permissions']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testGetContactFail() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$this->assertNull($this->backend->getContact('foo', '1234'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testCreateContact() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$carddata = file_get_contents(__DIR__ . '/../../data/test2.vcf');
|
|
|
|
$vcard = Reader::read($carddata);
|
|
|
|
$id = $this->backend->createContact('foo', $vcard);
|
|
|
|
|
|
|
|
$this->assertNotEquals(false, $id);
|
|
|
|
|
|
|
|
return $this->backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateContact
|
|
|
|
*/
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testGetContacts($backend) {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$contacts = $backend->getContacts('foo');
|
|
|
|
|
|
|
|
$this->assertCount(2, $contacts);
|
|
|
|
|
|
|
|
$this->assertEquals('Max Mustermann', $contacts[0]['displayname']);
|
|
|
|
$this->assertEquals('John Q. Public', $contacts[1]['displayname']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testUpdateContact() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$carddata = file_get_contents(__DIR__ . '/../../data/test2.vcf');
|
|
|
|
$vcard = Reader::read($carddata);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('OCA\\Contacts\\VObject\\VCard', $vcard);
|
|
|
|
|
|
|
|
$this->assertTrue($this->backend->updateContact('foo', '123', $vcard));
|
|
|
|
|
|
|
|
$contact = $this->backend->getContact('foo', '123');
|
|
|
|
|
|
|
|
$this->assertEquals('John Q. Public', $contact['displayname']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-10 16:39:42 +01:00
|
|
|
public function testDeleteContact() {
|
2013-08-26 20:10:29 +02:00
|
|
|
|
|
|
|
$this->assertTrue($this->backend->deleteContact('foo', '123'));
|
|
|
|
$this->assertEquals(array(), $this->backend->getContacts('foo'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|