Initial Release of the calendar plugin

This commit is contained in:
Tobias Hößl 2012-06-03 18:19:28 +00:00
parent 45cc9885fc
commit 7115197a33
561 changed files with 189494 additions and 0 deletions

View file

@ -0,0 +1,39 @@
<?php
require_once 'Sabre/CardDAV/Backend/Mock.php';
require_once 'Sabre/DAVACL/MockPrincipalBackend.php';
abstract class Sabre_CardDAV_AbstractPluginTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_CardDAV_Plugin
*/
protected $plugin;
/**
* @var Sabre_DAV_Server
*/
protected $server;
/**
* @var Sabre_CardDAV_MockBackend
*/
protected $backend;
function setUp() {
$this->backend = new Sabre_CardDAV_Backend_Mock();
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$tree = array(
new Sabre_CardDAV_AddressBookRoot($principalBackend, $this->backend),
new Sabre_DAVACL_PrincipalCollection($principalBackend)
);
$this->plugin = new Sabre_CardDAV_Plugin();
$this->plugin->directories = array('directory');
$this->server = new Sabre_DAV_Server($tree);
$this->server->addPlugin($this->plugin);
$this->server->debugExceptions = true;
}
}

View file

@ -0,0 +1,325 @@
<?php
class Sabre_CardDAV_AddressBookQueryParserTest extends PHPUnit_Framework_TestCase {
function parse($xml) {
$xml = implode("\n", $xml);
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$q = new Sabre_CardDAV_AddressBookQueryParser($dom);
$q->parse();
return $q;
}
function testFilterBasic() {
$xml = array(
'<?xml version="1.0"?>',
'<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
' <d:prop>',
' <d:foo />',
' </d:prop>',
' <c:filter>',
' <c:prop-filter name="NICKNAME" />',
' </c:filter>',
'</c:addressbook-query>'
);
$q = $this->parse($xml);
$this->assertEquals(
array('{DAV:}foo'),
$q->requestedProperties
);
$this->assertEquals(
array(
array(
'name' => 'NICKNAME',
'test' => 'anyof',
'is-not-defined' => false,
'param-filters' => array(),
'text-matches' => array(),
),
),
$q->filters
);
$this->assertNull($q->limit);
$this->assertEquals('anyof', $q->test);
}
function testNoFilter() {
// This is non-standard, but helps working around a KDE bug
$xml = array(
'<?xml version="1.0"?>',
'<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
' <d:prop>',
' <d:foo />',
' </d:prop>',
'</c:addressbook-query>'
);
$q = $this->parse($xml);
$this->assertEquals(
array('{DAV:}foo'),
$q->requestedProperties
);
$this->assertEquals(
array(),
$q->filters
);
$this->assertNull($q->limit);
$this->assertEquals('anyof', $q->test);
}
/**
* @expectedException Sabre_DAV_Exception_BadRequest
*/
function testFilterDoubleFilter() {
$xml = array(
'<?xml version="1.0"?>',
'<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
' <d:prop>',
' <d:foo />',
' </d:prop>',
' <c:filter>',
' <c:prop-filter name="NICKNAME" />',
' </c:filter>',
' <c:filter>',
' <c:prop-filter name="NICKNAME" />',
' </c:filter>',
'</c:addressbook-query>'
);
$q = $this->parse($xml);
}
/**
* @expectedException Sabre_DAV_Exception_BadRequest
*/
function testFilterCorruptTest() {
$xml = array(
'<?xml version="1.0"?>',
'<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
' <d:prop>',
' <d:foo />',
' </d:prop>',
' <c:filter test="foo">',
' <c:prop-filter name="NICKNAME" />',
' </c:filter>',
'</c:addressbook-query>'
);
$q = $this->parse($xml);
}
function testPropFilter() {
$xml = array(
'<?xml version="1.0"?>',
'<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
' <d:prop>',
' <d:foo />',
' </d:prop>',
' <c:filter test="allof">',
' <c:prop-filter name="NICKNAME" />',
' <c:prop-filter name="EMAIL" test="allof" />',
' <c:prop-filter name="FN">',
' <c:is-not-defined />',
' </c:prop-filter>',
' </c:filter>',
' <c:limit><c:nresults>4</c:nresults></c:limit>',
'</c:addressbook-query>'
);
$q = $this->parse($xml);
$this->assertEquals(
array(
array(
'name' => 'NICKNAME',
'test' => 'anyof',
'is-not-defined' => false,
'param-filters' => array(),
'text-matches' => array(),
),
array(
'name' => 'EMAIL',
'test' => 'allof',
'is-not-defined' => false,
'param-filters' => array(),
'text-matches' => array(),
),
array(
'name' => 'FN',
'test' => 'anyof',
'is-not-defined' => true,
'param-filters' => array(),
'text-matches' => array(),
),
),
$q->filters
);
$this->assertEquals(4,$q->limit);
$this->assertEquals('allof', $q->test);
}
function testParamFilter() {
$xml = array(
'<?xml version="1.0"?>',
'<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
' <d:prop>',
' <d:foo />',
' </d:prop>',
' <c:filter>',
' <c:prop-filter name="NICKNAME">',
' <c:param-filter name="BLA" />',
' <c:param-filter name="BLA2">',
' <c:is-not-defined />',
' </c:param-filter>',
' </c:prop-filter>',
' </c:filter>',
'</c:addressbook-query>'
);
$q = $this->parse($xml);
$this->assertEquals(
array(
array(
'name' => 'NICKNAME',
'test' => 'anyof',
'is-not-defined' => false,
'param-filters' => array(
array(
'name' => 'BLA',
'is-not-defined' => false,
'text-match' => null
),
array(
'name' => 'BLA2',
'is-not-defined' => true,
'text-match' => null
),
),
'text-matches' => array(),
),
),
$q->filters
);
}
function testTextMatch() {
$xml = array(
'<?xml version="1.0"?>',
'<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
' <d:prop>',
' <d:foo />',
' </d:prop>',
' <c:filter>',
' <c:prop-filter name="NICKNAME">',
' <c:text-match>evert</c:text-match>',
' <c:text-match collation="i;octet">evert</c:text-match>',
' <c:text-match negate-condition="yes">rene</c:text-match>',
' <c:text-match match-type="starts-with">e</c:text-match>',
' <c:param-filter name="BLA">',
' <c:text-match>foo</c:text-match>',
' </c:param-filter>',
' </c:prop-filter>',
' </c:filter>',
'</c:addressbook-query>'
);
$q = $this->parse($xml);
$this->assertEquals(
array(
array(
'name' => 'NICKNAME',
'test' => 'anyof',
'is-not-defined' => false,
'param-filters' => array(
array(
'name' => 'BLA',
'is-not-defined' => false,
'text-match' => array(
'negate-condition' => false,
'collation' => 'i;unicode-casemap',
'match-type' => 'contains',
'value' => 'foo',
),
),
),
'text-matches' => array(
array(
'negate-condition' => false,
'collation' => 'i;unicode-casemap',
'match-type' => 'contains',
'value' => 'evert',
),
array(
'negate-condition' => false,
'collation' => 'i;octet',
'match-type' => 'contains',
'value' => 'evert',
),
array(
'negate-condition' => true,
'collation' => 'i;unicode-casemap',
'match-type' => 'contains',
'value' => 'rene',
),
array(
'negate-condition' => false,
'collation' => 'i;unicode-casemap',
'match-type' => 'starts-with',
'value' => 'e',
),
),
),
),
$q->filters
);
}
/**
* @expectedException Sabre_DAV_Exception_BadRequest
*/
function testBadTextMatch() {
$xml = array(
'<?xml version="1.0"?>',
'<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
' <d:prop>',
' <d:foo />',
' </d:prop>',
' <c:filter>',
' <c:prop-filter name="NICKNAME">',
' <c:text-match match-type="foo">evert</c:text-match>',
' </c:prop-filter>',
' </c:filter>',
'</c:addressbook-query>'
);
$q = $this->parse($xml);
}
}

View file

@ -0,0 +1,187 @@
<?php
require_once 'Sabre/CardDAV/AbstractPluginTest.php';
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_CardDAV_AddressBookQueryTest extends Sabre_CardDAV_AbstractPluginTest {
function testQuery() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'REPORT',
'REQUEST_URI' => '/addressbooks/user1/book1',
'HTTP_DEPTH' => '1',
));
$request->setBody(
'<?xml version="1.0"?>
<c:addressbook-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
<d:prop>
<d:getetag />
</d:prop>
<c:filter>
<c:prop-filter name="uid" />
</c:filter>
</c:addressbook-query>'
);
$response = new Sabre_HTTP_ResponseMock();
$this->server->httpRequest = $request;
$this->server->httpResponse = $response;
$this->server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $response->status, 'Incorrect status code. Full response body:' . $response->body);
// using the client for parsing
$client = new Sabre_DAV_Client(array('baseUri'=>'/'));
$result = $client->parseMultiStatus($response->body);
$this->assertEquals(array(
'/addressbooks/user1/book1/card1' => array(
200 => array(
'{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD") . '"',
),
),
'/addressbooks/user1/book1/card2' => array(
200 => array(
'{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:45678\nEND:VCARD") . '"',
),
)
), $result);
}
function testQueryDepth0() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'REPORT',
'REQUEST_URI' => '/addressbooks/user1/book1/card1',
'HTTP_DEPTH' => '0',
));
$request->setBody(
'<?xml version="1.0"?>
<c:addressbook-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
<d:prop>
<d:getetag />
</d:prop>
<c:filter>
<c:prop-filter name="uid" />
</c:filter>
</c:addressbook-query>'
);
$response = new Sabre_HTTP_ResponseMock();
$this->server->httpRequest = $request;
$this->server->httpResponse = $response;
$this->server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $response->status, 'Incorrect status code. Full response body:' . $response->body);
// using the client for parsing
$client = new Sabre_DAV_Client(array('baseUri'=>'/'));
$result = $client->parseMultiStatus($response->body);
$this->assertEquals(array(
'/addressbooks/user1/book1/card1' => array(
200 => array(
'{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD") . '"',
),
),
), $result);
}
function testQueryNoMatch() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'REPORT',
'REQUEST_URI' => '/addressbooks/user1/book1',
'HTTP_DEPTH' => '1',
));
$request->setBody(
'<?xml version="1.0"?>
<c:addressbook-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
<d:prop>
<d:getetag />
</d:prop>
<c:filter>
<c:prop-filter name="email" />
</c:filter>
</c:addressbook-query>'
);
$response = new Sabre_HTTP_ResponseMock();
$this->server->httpRequest = $request;
$this->server->httpResponse = $response;
$this->server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $response->status, 'Incorrect status code. Full response body:' . $response->body);
// using the client for parsing
$client = new Sabre_DAV_Client(array('baseUri'=>'/'));
$result = $client->parseMultiStatus($response->body);
$this->assertEquals(array(), $result);
}
function testQueryLimit() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'REPORT',
'REQUEST_URI' => '/addressbooks/user1/book1',
'HTTP_DEPTH' => '1',
));
$request->setBody(
'<?xml version="1.0"?>
<c:addressbook-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
<d:prop>
<d:getetag />
</d:prop>
<c:filter>
<c:prop-filter name="uid" />
</c:filter>
<c:limit><c:nresults>1</c:nresults></c:limit>
</c:addressbook-query>'
);
$response = new Sabre_HTTP_ResponseMock();
$this->server->httpRequest = $request;
$this->server->httpResponse = $response;
$this->server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $response->status, 'Incorrect status code. Full response body:' . $response->body);
// using the client for parsing
$client = new Sabre_DAV_Client(array('baseUri'=>'/'));
$result = $client->parseMultiStatus($response->body);
$this->assertEquals(array(
'/addressbooks/user1/book1/card1' => array(
200 => array(
'{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD"). '"',
),
),
), $result);
}
}

View file

@ -0,0 +1,27 @@
<?php
class Sabre_CardDAV_AddressBookRootTest extends PHPUnit_Framework_TestCase {
function testGetName() {
$pBackend = new Sabre_DAVACL_MockPrincipalBackend();
$cBackend = new Sabre_CardDAV_Backend_Mock();
$root = new Sabre_CardDAV_AddressBookRoot($pBackend, $cBackend);
$this->assertEquals('addressbooks', $root->getName());
}
function testGetChildForPrincipal() {
$pBackend = new Sabre_DAVACL_MockPrincipalBackend();
$cBackend = new Sabre_CardDAV_Backend_Mock();
$root = new Sabre_CardDAV_AddressBookRoot($pBackend, $cBackend);
$children = $root->getChildren();
$this->assertEquals(3, count($children));
$this->assertInstanceOf('Sabre_CardDAV_UserAddressBooks', $children[0]);
$this->assertEquals('user1', $children[0]->getName());
}
}

View file

@ -0,0 +1,159 @@
<?php
require_once 'Sabre/CardDAV/Backend/Mock.php';
class Sabre_CardDAV_AddressBookTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_CardDAV_AddressBook
*/
protected $ab;
protected $backend;
function setUp() {
$this->backend = new Sabre_CardDAV_Backend_Mock();
$this->ab = new Sabre_CardDAV_AddressBook(
$this->backend,
array(
'uri' => 'book1',
'id' => 'foo',
'{DAV:}displayname' => 'd-name',
'principaluri' => 'principals/user1',
)
);
}
function testGetName() {
$this->assertEquals('book1', $this->ab->getName());
}
function testGetChild() {
$card = $this->ab->getChild('card1');
$this->assertInstanceOf('Sabre_CardDAV_Card', $card);
$this->assertEquals('card1', $card->getName());
}
/**
* @expectedException Sabre_DAV_Exception_NotFound
*/
function testGetChildNotFound() {
$card = $this->ab->getChild('card3');
}
function testGetChildren() {
$cards = $this->ab->getChildren();
$this->assertEquals(2, count($cards));
$this->assertEquals('card1', $cards[0]->getName());
$this->assertEquals('card2', $cards[1]->getName());
}
/**
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
function testCreateDirectory() {
$this->ab->createDirectory('name');
}
function testCreateFile() {
$file = fopen('php://memory','r+');
fwrite($file,'foo');
rewind($file);
$this->ab->createFile('card2',$file);
$this->assertEquals('foo', $this->backend->cards['foo']['card2']);
}
function testDelete() {
$this->ab->delete();
$this->assertEquals(array(), $this->backend->addressBooks);
}
/**
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
function testSetName() {
$this->ab->setName('foo');
}
function testGetLastModified() {
$this->assertNull($this->ab->getLastModified());
}
function testUpdateProperties() {
$this->assertTrue(
$this->ab->updateProperties(array('{DAV:}displayname' => 'barrr'))
);
$this->assertEquals('barrr', $this->backend->addressBooks[0]['{DAV:}displayname']);
}
function testGetProperties() {
$props = $this->ab->getProperties(array('{DAV:}displayname'));
$this->assertEquals(array(
'{DAV:}displayname' => 'd-name',
), $props);
}
function testACLMethods() {
$this->assertEquals('principals/user1', $this->ab->getOwner());
$this->assertNull($this->ab->getGroup());
$this->assertEquals(array(
array(
'privilege' => '{DAV:}read',
'principal' => 'principals/user1',
'protected' => true,
),
array(
'privilege' => '{DAV:}write',
'principal' => 'principals/user1',
'protected' => true,
),
), $this->ab->getACL());
}
/**
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
function testSetACL() {
$this->ab->setACL(array());
}
function testGetSupportedPrivilegeSet() {
$this->assertNull(
$this->ab->getSupportedPrivilegeSet()
);
}
}

View file

@ -0,0 +1,245 @@
<?php
abstract class Sabre_CardDAV_Backend_AbstractPDOTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_CardDAV_Backend_PDO
*/
protected $backend;
/**
* @abstract
* @return PDO
*/
abstract function getPDO();
public function setUp() {
$this->backend = new Sabre_CardDAV_Backend_PDO($this->getPDO());
}
public function testGetAddressBooksForUser() {
$result = $this->backend->getAddressBooksForUser('principals/user1');
$expected = array(
array(
'id' => 1,
'uri' => 'book1',
'principaluri' => 'principals/user1',
'{DAV:}displayname' => 'book1',
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
'{http://calendarserver.org/ns/}getctag' => 1,
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' => new Sabre_CardDAV_Property_SupportedAddressData(),
)
);
$this->assertEquals($expected, $result);
}
public function testUpdateAddressBookInvalidProp() {
$result = $this->backend->updateAddressBook(1, array(
'{DAV:}displayname' => 'updated',
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
'{DAV:}foo' => 'bar',
));
$this->assertFalse($result);
$result = $this->backend->getAddressBooksForUser('principals/user1');
$expected = array(
array(
'id' => 1,
'uri' => 'book1',
'principaluri' => 'principals/user1',
'{DAV:}displayname' => 'book1',
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
'{http://calendarserver.org/ns/}getctag' => 1,
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' => new Sabre_CardDAV_Property_SupportedAddressData(),
)
);
$this->assertEquals($expected, $result);
}
public function testUpdateAddressBookNoProps() {
$result = $this->backend->updateAddressBook(1, array());
$this->assertFalse($result);
$result = $this->backend->getAddressBooksForUser('principals/user1');
$expected = array(
array(
'id' => 1,
'uri' => 'book1',
'principaluri' => 'principals/user1',
'{DAV:}displayname' => 'book1',
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
'{http://calendarserver.org/ns/}getctag' => 1,
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' => new Sabre_CardDAV_Property_SupportedAddressData(),
)
);
$this->assertEquals($expected, $result);
}
public function testUpdateAddressBookSuccess() {
$result = $this->backend->updateAddressBook(1, array(
'{DAV:}displayname' => 'updated',
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
));
$this->assertTrue($result);
$result = $this->backend->getAddressBooksForUser('principals/user1');
$expected = array(
array(
'id' => 1,
'uri' => 'book1',
'principaluri' => 'principals/user1',
'{DAV:}displayname' => 'updated',
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
'{http://calendarserver.org/ns/}getctag' => 2,
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' => new Sabre_CardDAV_Property_SupportedAddressData(),
)
);
$this->assertEquals($expected, $result);
}
public function testDeleteAddressBook() {
$this->backend->deleteAddressBook(1);
$this->assertEquals(array(), $this->backend->getAddressBooksForUser('principals/user1'));
}
/**
* @expectedException Sabre_DAV_Exception_BadRequest
*/
public function testCreateAddressBookUnsupportedProp() {
$this->backend->createAddressBook('principals/user1','book2', array(
'{DAV:}foo' => 'bar',
));
}
public function testCreateAddressBookSuccess() {
$this->backend->createAddressBook('principals/user1','book2', array(
'{DAV:}displayname' => 'book2',
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 2',
));
$expected = array(
array(
'id' => 1,
'uri' => 'book1',
'principaluri' => 'principals/user1',
'{DAV:}displayname' => 'book1',
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
'{http://calendarserver.org/ns/}getctag' => 1,
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' => new Sabre_CardDAV_Property_SupportedAddressData(),
),
array(
'id' => 2,
'uri' => 'book2',
'principaluri' => 'principals/user1',
'{DAV:}displayname' => 'book2',
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 2',
'{http://calendarserver.org/ns/}getctag' => 1,
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' => new Sabre_CardDAV_Property_SupportedAddressData(),
)
);
$result = $this->backend->getAddressBooksForUser('principals/user1');
$this->assertEquals($expected, $result);
}
public function testGetCards() {
$result = $this->backend->getCards(1);
$expected = array(
array(
'id' => 1,
'uri' => 'card1',
'carddata' => 'card1',
'lastmodified' => 0,
)
);
$this->assertEquals($expected, $result);
}
public function testGetCard() {
$result = $this->backend->getCard(1,'card1');
$expected = array(
'id' => 1,
'uri' => 'card1',
'carddata' => 'card1',
'lastmodified' => 0,
);
$this->assertEquals($expected, $result);
}
/**
* @depends testGetCard
*/
public function testCreateCard() {
$result = $this->backend->createCard(1, 'card2', 'data2');
$this->assertEquals('"' . md5('data2') . '"', $result);
$result = $this->backend->getCard(1,'card2');
$this->assertEquals(2, $result['id']);
$this->assertEquals('card2', $result['uri']);
$this->assertEquals('data2', $result['carddata']);
}
/**
* @depends testGetCard
*/
public function testUpdateCard() {
$result = $this->backend->updateCard(1, 'card1', 'newdata');
$this->assertEquals('"' . md5('newdata') . '"', $result);
$result = $this->backend->getCard(1,'card1');
$this->assertEquals(1, $result['id']);
$this->assertEquals('newdata', $result['carddata']);
}
/**
* @depends testGetCard
*/
public function testDeleteCard() {
$this->backend->deleteCard(1, 'card1');
$result = $this->backend->getCard(1,'card1');
$this->assertFalse($result);
}
}

View file

@ -0,0 +1,125 @@
<?php
class Sabre_CardDAV_Backend_Mock extends Sabre_CardDAV_Backend_Abstract {
public $addressBooks;
public $cards;
function __construct($addressBooks = null, $cards = null) {
$this->addressBooks = $addressBooks;
$this->cards = $cards;
if (is_null($this->addressBooks)) {
$this->addressBooks = array(
array(
'id' => 'foo',
'uri' => 'book1',
'principaluri' => 'principals/user1',
'{DAV:}displayname' => 'd-name',
),
);
$this->cards = array(
'foo' => array(
'card1' => "BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD",
'card2' => "BEGIN:VCARD\nVERSION:3.0\nUID:45678\nEND:VCARD",
),
);
}
}
function getAddressBooksForUser($principalUri) {
$books = array();
foreach($this->addressBooks as $book) {
if ($book['principaluri'] === $principalUri) {
$books[] = $book;
}
}
return $books;
}
function updateAddressBook($addressBookId, array $mutations) {
foreach($this->addressBooks as &$book) {
if ($book['id'] !== $addressBookId)
continue;
foreach($mutations as $key=>$value) {
$book[$key] = $value;
}
return true;
}
return false;
}
function createAddressBook($principalUri, $url, array $properties) {
$this->addressBooks[] = array_merge($properties, array(
'id' => $url,
'uri' => $url,
'principaluri' => $principalUri,
));
}
function deleteAddressBook($addressBookId) {
foreach($this->addressBooks as $key=>$value) {
if ($value['id'] === $addressBookId)
unset($this->addressBooks[$key]);
}
unset($this->cards[$addressBookId]);
}
function getCards($addressBookId) {
$cards = array();
foreach($this->cards[$addressBookId] as $uri=>$data) {
$cards[] = array(
'uri' => $uri,
'carddata' => $data,
);
}
return $cards;
}
function getCard($addressBookId, $cardUri) {
if (!isset($this->cards[$addressBookId][$cardUri])) {
return false;
}
return array(
'uri' => $cardUri,
'carddata' => $this->cards[$addressBookId][$cardUri],
);
}
function createCard($addressBookId, $cardUri, $cardData) {
$this->cards[$addressBookId][$cardUri] = $cardData;
}
function updateCard($addressBookId, $cardUri, $cardData) {
$this->cards[$addressBookId][$cardUri] = $cardData;
}
function deleteCard($addressBookId, $cardUri) {
unset($this->cards[$addressBookId][$cardUri]);
}
}

View file

@ -0,0 +1,58 @@
<?php
require_once 'Sabre/TestUtil.php';
class Sabre_CardDAV_Backend_PDOMySQLTest extends Sabre_CardDAV_Backend_AbstractPDOTest {
/**
* @return PDO
*/
public function getPDO() {
if (!SABRE_HASMYSQL) $this->markTestSkipped('MySQL driver is not available, or not properly configured');
$pdo = Sabre_TestUtil::getMySQLDB();
if (!$pdo) $this->markTestSkipped('Could not connect to MySQL database');
$pdo->query("DROP TABLE IF EXISTS addressbooks");
$pdo->query("DROP TABLE IF EXISTS cards");
$pdo->query("
CREATE TABLE addressbooks (
id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
principaluri VARCHAR(255),
displayname VARCHAR(255),
uri VARCHAR(100),
description TEXT,
ctag INT(11) UNSIGNED NOT NULL DEFAULT '1'
);
");
$pdo->query("
INSERT INTO addressbooks
(principaluri, displayname, uri, description, ctag)
VALUES
('principals/user1', 'book1', 'book1', 'addressbook 1', 1);
");
$pdo->query("
CREATE TABLE cards (
id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
addressbookid INT(11) UNSIGNED NOT NULL,
carddata TEXT,
uri VARCHAR(100),
lastmodified INT(11) UNSIGNED
);
");
$pdo->query("
INSERT INTO cards
(addressbookid, carddata, uri, lastmodified)
VALUES
(1, 'card1', 'card1', 0);
");
return $pdo;
}
}

View file

@ -0,0 +1,67 @@
<?php
require_once 'Sabre/TestUtil.php';
class Sabre_CardDAV_Backend_PDOSqliteTest extends Sabre_CardDAV_Backend_AbstractPDOTest {
function tearDown() {
if (file_exists(SABRE_TEMPDIR . '/pdobackend')) unlink(SABRE_TEMPDIR . '/pdobackend');
if (file_exists(SABRE_TEMPDIR . '/pdobackend2')) unlink(SABRE_TEMPDIR . '/pdobackend2');
}
/**
* @return PDO
*/
function getPDO() {
if (!SABRE_HASSQLITE) $this->markTestSkipped('SQLite driver is not available');
$pdo = new PDO('sqlite:'.SABRE_TEMPDIR.'/pdobackend');
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->query("DROP TABLE IF EXISTS addressbooks");
$pdo->query("DROP TABLE IF EXISTS cards");
$pdo->query("
CREATE TABLE addressbooks (
id integer primary key asc,
principaluri text,
displayname text,
uri text,
description text,
ctag integer
);
");
$pdo->query("
INSERT INTO addressbooks
(principaluri, displayname, uri, description, ctag)
VALUES
('principals/user1', 'book1', 'book1', 'addressbook 1', 1);
");
$pdo->query("
CREATE TABLE cards (
id integer primary key asc,
addressbookid integer,
carddata text,
uri text,
lastmodified integer
);
");
$pdo->query("
INSERT INTO cards
(addressbookid, carddata, uri, lastmodified)
VALUES
(1, 'card1', 'card1', 0);
");
return $pdo;
}
}

View file

@ -0,0 +1,182 @@
<?php
class Sabre_CardDAV_CardTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_CardDAV_Card
*/
protected $card;
/**
* @var Sabre_CardDAV_MockBackend
*/
protected $backend;
function setUp() {
$this->backend = new Sabre_CardDAV_Backend_Mock();
$this->card = new Sabre_CardDAV_Card(
$this->backend,
array(
'uri' => 'book1',
'id' => 'foo',
'principaluri' => 'principals/user1',
),
array(
'uri' => 'card1',
'addressbookid' => 'foo',
'carddata' => 'card',
)
);
}
function testGet() {
$result = $this->card->get();
$this->assertEquals('card', $result);
}
function testGet2() {
$this->card = new Sabre_CardDAV_Card(
$this->backend,
array(
'uri' => 'book1',
'id' => 'foo',
'principaluri' => 'principals/user1',
),
array(
'uri' => 'card1',
'addressbookid' => 'foo',
)
);
$result = $this->card->get();
$this->assertEquals("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD", $result);
}
/**
* @depends testGet
*/
function testPut() {
$file = fopen('php://memory','r+');
fwrite($file, 'newdata');
rewind($file);
$this->card->put($file);
$result = $this->card->get();
$this->assertEquals('newdata', $result);
}
function testDelete() {
$this->card->delete();
$this->assertEquals(1, count($this->backend->cards['foo']));
}
function testGetContentType() {
$this->assertEquals('text/x-vcard; charset=utf-8', $this->card->getContentType());
}
function testGetETag() {
$this->assertEquals('"' . md5('card') . '"' , $this->card->getETag());
}
function testGetETag2() {
$card = new Sabre_CardDAV_Card(
$this->backend,
array(
'uri' => 'book1',
'id' => 'foo',
'principaluri' => 'principals/user1',
),
array(
'uri' => 'card1',
'addressbookid' => 'foo',
'carddata' => 'card',
'etag' => '"blabla"',
)
);
$this->assertEquals('"blabla"' , $card->getETag());
}
function testGetLastModified() {
$this->assertEquals(null, $this->card->getLastModified());
}
function testGetSize() {
$this->assertEquals(4, $this->card->getSize());
$this->assertEquals(4, $this->card->getSize());
}
function testGetSize2() {
$card = new Sabre_CardDAV_Card(
$this->backend,
array(
'uri' => 'book1',
'id' => 'foo',
'principaluri' => 'principals/user1',
),
array(
'uri' => 'card1',
'addressbookid' => 'foo',
'etag' => '"blabla"',
'size' => 4,
)
);
$this->assertEquals(4, $card->getSize());
}
function testACLMethods() {
$this->assertEquals('principals/user1', $this->card->getOwner());
$this->assertNull($this->card->getGroup());
$this->assertEquals(array(
array(
'privilege' => '{DAV:}read',
'principal' => 'principals/user1',
'protected' => true,
),
array(
'privilege' => '{DAV:}write',
'principal' => 'principals/user1',
'protected' => true,
),
), $this->card->getACL());
}
/**
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
function testSetACL() {
$this->card->setACL(array());
}
function testGetSupportedPrivilegeSet() {
$this->assertNull(
$this->card->getSupportedPrivilegeSet()
);
}
}

View file

@ -0,0 +1,25 @@
<?php
class Sabre_CardDAV_IDirectoryTest extends PHPUnit_Framework_TestCase {
function testResourceType() {
$tree = array(
new Sabre_CardDAV_DirectoryMock('directory')
);
$server = new Sabre_DAV_Server($tree);
$plugin = new Sabre_CardDAV_Plugin();
$server->addPlugin($plugin);
$props = $server->getProperties('directory', array('{DAV:}resourcetype'));
$this->assertTrue($props['{DAV:}resourcetype']->is('{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}directory'));
}
}
class Sabre_CardDAV_DirectoryMock extends Sabre_DAV_SimpleCollection implements Sabre_CardDAV_IDirectory {
}

View file

@ -0,0 +1,50 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_CardDAV_MultiGetTest extends Sabre_CardDAV_AbstractPluginTest {
function testMultiGet() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'REPORT',
'REQUEST_URI' => '/addressbooks/user1/book1',
));
$request->setBody(
'<?xml version="1.0"?>
<c:addressbook-multiget xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
<d:prop>
<d:getetag />
<c:address-data />
</d:prop>
<d:href>/addressbooks/user1/book1/card1</d:href>
</c:addressbook-multiget>'
);
$response = new Sabre_HTTP_ResponseMock();
$this->server->httpRequest = $request;
$this->server->httpResponse = $response;
$this->server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $response->status, 'Incorrect status code. Full response body:' . $response->body);
// using the client for parsing
$client = new Sabre_DAV_Client(array('baseUri'=>'/'));
$result = $client->parseMultiStatus($response->body);
$this->assertEquals(array(
'/addressbooks/user1/book1/card1' => array(
200 => array(
'{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD") . '"',
'{urn:ietf:params:xml:ns:carddav}address-data' => "BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD",
)
)
), $result);
}
}

View file

@ -0,0 +1,146 @@
<?php
require_once 'Sabre/DAVACL/MockPrincipalBackend.php';
require_once 'Sabre/CardDAV/AbstractPluginTest.php';
class Sabre_CardDAV_PluginTest extends Sabre_CardDAV_AbstractPluginTest {
function testConstruct() {
$this->assertEquals('card', $this->server->xmlNamespaces[Sabre_CardDAV_Plugin::NS_CARDDAV]);
$this->assertEquals('{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook', $this->server->resourceTypeMapping['Sabre_CardDAV_IAddressBook']);
$this->assertTrue(in_array('addressbook', $this->plugin->getFeatures()));
}
function testSupportedReportSet() {
$this->assertEquals(array(
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-multiget',
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-query',
), $this->plugin->getSupportedReportSet('addressbooks/user1/book1'));
}
function testSupportedReportSetEmpty() {
$this->assertEquals(array(
), $this->plugin->getSupportedReportSet(''));
}
function testAddressBookHomeSet() {
$result = $this->server->getProperties('principals/user1', array('{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-home-set'));
$this->assertEquals(1, count($result));
$this->assertTrue(isset($result['{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-home-set']));
$this->assertEquals('addressbooks/user1/', $result['{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-home-set']->getHref());
}
function testMeCardTest() {
$result = $this->server->getProperties(
'addressbooks/user1',
array(
'{http://calendarserver.org/ns/}me-card',
)
);
$this->assertEquals(
array(
'{http://calendarserver.org/ns/}me-card' =>
new Sabre_DAV_Property_Href('addressbooks/user1/book1/vcard1.vcf')
),
$result
);
}
function testDirectoryGateway() {
$result = $this->server->getProperties('principals/user1', array('{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}directory-gateway'));
$this->assertEquals(1, count($result));
$this->assertTrue(isset($result['{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}directory-gateway']));
$this->assertEquals(array('directory'), $result['{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}directory-gateway']->getHrefs());
}
function testReportPassThrough() {
$this->assertNull($this->plugin->report('{DAV:}foo', new DomDocument()));
}
function testHTMLActionsPanel() {
$output = '';
$r = $this->server->broadcastEvent('onHTMLActionsPanel', array($this->server->tree->getNodeForPath('addressbooks/user1'), &$output));
$this->assertFalse($r);
$this->assertTrue(!!strpos($output,'Display name'));
}
function testBrowserPostAction() {
$r = $this->server->broadcastEvent('onBrowserPostAction', array('addressbooks/user1', 'mkaddressbook', array(
'name' => 'NEWADDRESSBOOK',
'{DAV:}displayname' => 'foo',
)));
$this->assertFalse($r);
$addressbooks = $this->backend->getAddressBooksforUser('principals/user1');
$this->assertEquals(2, count($addressbooks));
$newAddressBook = null;
foreach($addressbooks as $addressbook) {
if ($addressbook['uri'] === 'NEWADDRESSBOOK') {
$newAddressBook = $addressbook;
break;
}
}
if (!$newAddressBook)
$this->fail('Could not find newly created addressbook');
}
function testUpdatePropertiesMeCard() {
$result = $this->server->updateProperties('addressbooks/user1', array(
'{http://calendarserver.org/ns/}me-card' => new Sabre_DAV_Property_Href('/addressbooks/user1/book1/vcard2',true),
));
$this->assertEquals(
array(
'href' => 'addressbooks/user1',
200 => array(
'{http://calendarserver.org/ns/}me-card' => null,
),
),
$result
);
}
function testUpdatePropertiesMeCardBadValue() {
$result = $this->server->updateProperties('addressbooks/user1', array(
'{http://calendarserver.org/ns/}me-card' => new Sabre_DAV_Property_HrefList(array()),
));
$this->assertEquals(
array(
'href' => 'addressbooks/user1',
400 => array(
'{http://calendarserver.org/ns/}me-card' => null,
),
),
$result
);
}
}

View file

@ -0,0 +1,39 @@
<?php
class Sabre_CardDAV_Property_SupportedAddressDataDataTest extends PHPUnit_Framework_TestCase {
function testSimple() {
$property = new Sabre_CardDAV_Property_SupportedAddressData();
}
/**
* @depends testSimple
*/
function testSerialize() {
$property = new Sabre_CardDAV_Property_SupportedAddressData();
$doc = new DOMDocument();
$root = $doc->createElementNS(Sabre_CardDAV_Plugin::NS_CARDDAV, 'card:root');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$server = new Sabre_DAV_Server();
$property->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<card:root xmlns:card="' . Sabre_CardDAV_Plugin::NS_CARDDAV . '" xmlns:d="DAV:">' .
'<card:address-data-type content-type="text/vcard" version="3.0"/>' .
'<card:address-data-type content-type="text/vcard" version="4.0"/>' .
'</card:root>
', $xml);
}
}

View file

@ -0,0 +1,39 @@
<?php
class Sabre_CardDAV_SogoStripContentType extends Sabre_DAVServerTest {
protected $setupCardDAV = true;
protected $carddavAddressBooks = array(
array(
'id' => 1,
'uri' => 'book1',
'principaluri' => 'principals/user1',
),
);
protected $carddavCards = array(
1 => array(
'card1.vcf' => "BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD",
),
);
function testDontStrip() {
$result = $this->server->getProperties('addressbooks/user1/book1/card1.vcf',array('{DAV:}getcontenttype'));
$this->assertEquals(array(
'{DAV:}getcontenttype' => 'text/x-vcard; charset=utf-8'
), $result);
}
function testStrip() {
$this->server->httpRequest = new Sabre_HTTP_Request(array(
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 Lightning/1.2.1',
));
$result = $this->server->getProperties('addressbooks/user1/book1/card1.vcf',array('{DAV:}getcontenttype'));
$this->assertEquals(array(
'{DAV:}getcontenttype' => 'text/x-vcard'
), $result);
}
}

View file

@ -0,0 +1,160 @@
<?php
class Sabre_CardDAV_UserAddressBooksTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_CardDAV_UserAddressBooks
*/
protected $s;
protected $backend;
function setUp() {
$this->backend = new Sabre_CardDAV_Backend_Mock();
$this->s = new Sabre_CardDAV_UserAddressBooks(
$this->backend,
'principals/user1'
);
}
function testGetName() {
$this->assertEquals('user1', $this->s->getName());
}
/**
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
function testSetName() {
$this->s->setName('user2');
}
/**
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
function testDelete() {
$this->s->delete();
}
function testGetLastModified() {
$this->assertNull($this->s->getLastModified());
}
/**
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
function testCreateFile() {
$this->s->createFile('bla');
}
/**
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
function testCreateDirectory() {
$this->s->createDirectory('bla');
}
function testGetChild() {
$child = $this->s->getChild('book1');
$this->assertInstanceOf('Sabre_CardDAV_AddressBook', $child);
$this->assertEquals('book1', $child->getName());
}
/**
* @expectedException Sabre_DAV_Exception_NotFound
*/
function testGetChild404() {
$this->s->getChild('book2');
}
function testGetChildren() {
$children = $this->s->getChildren();
$this->assertEquals(1, count($children));
$this->assertInstanceOf('Sabre_CardDAV_AddressBook', $children[0]);
$this->assertEquals('book1', $children[0]->getName());
}
function testCreateExtendedCollection() {
$resourceType = array(
'{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook',
'{DAV:}collection',
);
$this->s->createExtendedCollection('book2', $resourceType, array('{DAV:}displayname' => 'a-book 2'));
$this->assertEquals(array(
'id' => 'book2',
'uri' => 'book2',
'{DAV:}displayname' => 'a-book 2',
'principaluri' => 'principals/user1',
), $this->backend->addressBooks[1]);
}
/**
* @expectedException Sabre_DAV_Exception_InvalidResourceType
*/
function testCreateExtendedCollectionInvalid() {
$resourceType = array(
'{DAV:}collection',
);
$this->s->createExtendedCollection('book2', $resourceType, array('{DAV:}displayname' => 'a-book 2'));
}
function testACLMethods() {
$this->assertEquals('principals/user1', $this->s->getOwner());
$this->assertNull($this->s->getGroup());
$this->assertEquals(array(
array(
'privilege' => '{DAV:}read',
'principal' => 'principals/user1',
'protected' => true,
),
array(
'privilege' => '{DAV:}write',
'principal' => 'principals/user1',
'protected' => true,
),
), $this->s->getACL());
}
/**
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
function testSetACL() {
$this->s->setACL(array());
}
function testGetSupportedPrivilegeSet() {
$this->assertNull(
$this->s->getSupportedPrivilegeSet()
);
}
}

View file

@ -0,0 +1,202 @@
<?php
require_once 'Sabre/CardDAV/AbstractPluginTest.php';
class Sabre_CardDAV_ValidateFilterTest extends Sabre_CardDAV_AbstractPluginTest {
/**
* @dataProvider data
*/
function testFilter($input, $filters, $test, $result, $message = null) {
if ($result) {
$this->assertTrue($this->plugin->validateFilters($input, $filters, $test), $message);
} else {
$this->assertFalse($this->plugin->validateFilters($input, $filters, $test), $message);
}
}
function data() {
$body1 = <<<HELLO
BEGIN:VCARD
VERSION:3.0
ORG:Company;
TITLE:Title
TEL;TYPE=IPHONE;TYPE=pref:(222) 22 22 22
TEL;TYPE=HOME:(33) 333 66 66
TEL;TYPE=WORK:(444) 44 44 44
TEL;TYPE=MAIN:(55) 555 55 55
ITEM4.TEL:(111) 11 11 11
ITEM5.TEL:(6) 66 66 66 66
ITEM6.TEL:(77) 777 77 77
UID:3151DE6A-BC35-4612-B340-B53A034A2B27
ITEM1.EMAIL:1111@111.com
ITEM2.EMAIL:bbbbb@bbbb.com
ITEM3.EMAIL:ccccc@ccccc.com
FN:First Last
N:Last;First;Middle;Dr
BDAY:1985-07-20
ADR;TYPE=HOME:;;Street;City;;3556;Montenegro
ADR;TYPE=WORK:;;Street\\nStreet2;Harkema;;35444;Australia
URL:http://google.com
END:VCARD
HELLO;
// Check if TITLE is defined
$filter1 =
array('name' => 'title', 'is-not-defined' => false, 'param-filters' => array(), 'text-matches' => array());
// Check if FOO is defined
$filter2 =
array('name' => 'foo', 'is-not-defined' => false, 'param-filters' => array(), 'text-matches' => array());
// Check if TITLE is not defined
$filter3 =
array('name' => 'title', 'is-not-defined' => true, 'param-filters' => array(), 'text-matches' => array());
// Check if FOO is not defined
$filter4 =
array('name' => 'foo', 'is-not-defined' => true, 'param-filters' => array(), 'text-matches' => array());
// Check if TEL[TYPE] is defined
$filter5 =
array(
'name' => 'tel',
'is-not-defined' => false,
'test' => 'anyof',
'param-filters' => array(
array(
'name' => 'type',
'is-not-defined' => false,
'text-match' => null
),
),
'text-matches' => array(),
);
// Check if TEL[FOO] is defined
$filter6 = $filter5;
$filter6['param-filters'][0]['name'] = 'FOO';
// Check if TEL[TYPE] is not defined
$filter7 = $filter5;
$filter7['param-filters'][0]['is-not-defined'] = true;
// Check if TEL[FOO] is not defined
$filter8 = $filter5;
$filter8['param-filters'][0]['name'] = 'FOO';
$filter8['param-filters'][0]['is-not-defined'] = true;
// Combining property filters
$filter9 = $filter5;
$filter9['param-filters'][] = $filter6['param-filters'][0];
$filter10 = $filter5;
$filter10['param-filters'][] = $filter6['param-filters'][0];
$filter10['test'] = 'allof';
// Check if URL contains 'google'
$filter11 =
array(
'name' => 'url',
'is-not-defined' => false,
'test' => 'anyof',
'param-filters' => array(),
'text-matches' => array(
array(
'match-type' => 'contains',
'value' => 'google',
'negate-condition' => false,
'collation' => 'i;octet',
),
),
);
// Check if URL contains 'bing'
$filter12 = $filter11;
$filter12['text-matches'][0]['value'] = 'bing';
// Check if URL does not contain 'google'
$filter13 = $filter11;
$filter13['text-matches'][0]['negate-condition'] = true;
// Check if URL does not contain 'bing'
$filter14 = $filter11;
$filter14['text-matches'][0]['value'] = 'bing';
$filter14['text-matches'][0]['negate-condition'] = true;
// Param filter with text
$filter15 = $filter5;
$filter15['param-filters'][0]['text-match'] = array(
'match-type' => 'contains',
'value' => 'WORK',
'collation' => 'i;octet',
'negate-condition' => false,
);
$filter16 = $filter15;
$filter16['param-filters'][0]['text-match']['negate-condition'] = true;
// Param filter + text filter
$filter17 = $filter5;
$filter17['test'] = 'anyof';
$filter17['text-matches'][] = array(
'match-type' => 'contains',
'value' => '444',
'collation' => 'i;octet',
'negate-condition' => false,
);
$filter18 = $filter17;
$filter18['text-matches'][0]['negate-condition'] = true;
$filter18['test'] = 'allof';
return array(
// Basic filters
array($body1, array($filter1), 'anyof',true),
array($body1, array($filter2), 'anyof',false),
array($body1, array($filter3), 'anyof',false),
array($body1, array($filter4), 'anyof',true),
// Combinations
array($body1, array($filter1, $filter2), 'anyof',true),
array($body1, array($filter1, $filter2), 'allof',false),
array($body1, array($filter1, $filter4), 'anyof',true),
array($body1, array($filter1, $filter4), 'allof',true),
array($body1, array($filter2, $filter3), 'anyof',false),
array($body1, array($filter2, $filter3), 'allof',false),
// Basic parameters
array($body1, array($filter5), 'anyof', true, 'TEL;TYPE is defined, so this should return true'),
array($body1, array($filter6), 'anyof', false, 'TEL;FOO is not defined, so this should return false'),
array($body1, array($filter7), 'anyof', false, 'TEL;TYPE is defined, so this should return false'),
array($body1, array($filter8), 'anyof', true, 'TEL;TYPE is not defined, so this should return true'),
// Combined parameters
array($body1, array($filter9), 'anyof', true),
array($body1, array($filter10), 'anyof', false),
// Text-filters
array($body1, array($filter11), 'anyof', true),
array($body1, array($filter12), 'anyof', false),
array($body1, array($filter13), 'anyof', false),
array($body1, array($filter14), 'anyof', true),
// Param filter with text-match
array($body1, array($filter15), 'anyof', true),
array($body1, array($filter16), 'anyof', false),
// Param filter + text filter
array($body1, array($filter17), 'anyof', true),
array($body1, array($filter18), 'anyof', false),
array($body1, array($filter18), 'anyof', false),
);
}
}

View file

@ -0,0 +1,134 @@
<?php
require_once 'Sabre/CardDAV/Backend/Mock.php';
require_once 'Sabre/DAVACL/MockPrincipalBackend.php';
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_CardDAV_ValidateVCardTest extends PHPUnit_Framework_TestCase {
protected $server;
protected $cardBackend;
function setUp() {
$addressbooks = array(
array(
'id' => 'addressbook1',
'principaluri' => 'principals/admin',
'uri' => 'addressbook1',
)
);
$this->cardBackend = new Sabre_CardDAV_Backend_Mock($addressbooks,array());
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$tree = array(
new Sabre_CardDAV_AddressBookRoot($principalBackend, $this->cardBackend),
);
$this->server = new Sabre_DAV_Server($tree);
$this->server->debugExceptions = true;
$plugin = new Sabre_CardDAV_Plugin();
$this->server->addPlugin($plugin);
$response = new Sabre_HTTP_ResponseMock();
$this->server->httpResponse = $response;
}
function request(Sabre_HTTP_Request $request) {
$this->server->httpRequest = $request;
$this->server->exec();
return $this->server->httpResponse;
}
function testCreateFile() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PUT',
'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf',
));
$response = $this->request($request);
$this->assertEquals('HTTP/1.1 415 Unsupported Media Type', $response->status);
}
function testCreateFileValid() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PUT',
'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf',
));
$request->setBody("BEGIN:VCARD\r\nEND:VCARD\r\n");
$response = $this->request($request);
$this->assertEquals('HTTP/1.1 201 Created', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
$expected = array(
'uri' => 'blabla.vcf',
'carddata' => "BEGIN:VCARD\r\nEND:VCARD\r\n",
);
$this->assertEquals($expected, $this->cardBackend->getCard('addressbook1','blabla.vcf'));
}
function testCreateFileVCalendar() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PUT',
'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf',
));
$request->setBody("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n");
$response = $this->request($request);
$this->assertEquals('HTTP/1.1 415 Unsupported Media Type', $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
}
function testUpdateFile() {
$this->cardBackend->createCard('addressbook1','blabla.vcf','foo');
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PUT',
'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf',
));
$response = $this->request($request);
$this->assertEquals('HTTP/1.1 415 Unsupported Media Type', $response->status);
}
function testUpdateFileParsableBody() {
$this->cardBackend->createCard('addressbook1','blabla.vcf','foo');
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PUT',
'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf',
));
$body = "BEGIN:VCARD\r\nEND:VCARD\r\n";
$request->setBody($body);
$response = $this->request($request);
$this->assertEquals('HTTP/1.1 204 No Content', $response->status);
$expected = array(
'uri' => 'blabla.vcf',
'carddata' => $body,
);
$this->assertEquals($expected, $this->cardBackend->getCard('addressbook1','blabla.vcf'));
}
}
?>

View file

@ -0,0 +1,15 @@
<?php
class Sabre_CardDAV_VersionTest extends PHPUnit_Framework_TestCase {
function testString() {
$v = Sabre_CardDAV_Version::VERSION;
$this->assertEquals(-1, version_compare('0.1',$v));
$s = Sabre_CardDAV_Version::STABILITY;
$this->assertTrue($s == 'alpha' || $s == 'beta' || $s =='stable');
}
}