1*a1a3b679SAndreas Boehler<?php 2*a1a3b679SAndreas Boehler 3*a1a3b679SAndreas Boehlernamespace Sabre\CardDAV; 4*a1a3b679SAndreas Boehler 5*a1a3b679SAndreas Boehleruse Sabre\DAV\PropPatch; 6*a1a3b679SAndreas Boehler 7*a1a3b679SAndreas Boehlerrequire_once 'Sabre/CardDAV/Backend/Mock.php'; 8*a1a3b679SAndreas Boehler 9*a1a3b679SAndreas Boehlerclass AddressBookTest extends \PHPUnit_Framework_TestCase { 10*a1a3b679SAndreas Boehler 11*a1a3b679SAndreas Boehler /** 12*a1a3b679SAndreas Boehler * @var Sabre\CardDAV\AddressBook 13*a1a3b679SAndreas Boehler */ 14*a1a3b679SAndreas Boehler protected $ab; 15*a1a3b679SAndreas Boehler protected $backend; 16*a1a3b679SAndreas Boehler 17*a1a3b679SAndreas Boehler function setUp() { 18*a1a3b679SAndreas Boehler 19*a1a3b679SAndreas Boehler $this->backend = new Backend\Mock(); 20*a1a3b679SAndreas Boehler $this->ab = new AddressBook( 21*a1a3b679SAndreas Boehler $this->backend, 22*a1a3b679SAndreas Boehler array( 23*a1a3b679SAndreas Boehler 'uri' => 'book1', 24*a1a3b679SAndreas Boehler 'id' => 'foo', 25*a1a3b679SAndreas Boehler '{DAV:}displayname' => 'd-name', 26*a1a3b679SAndreas Boehler 'principaluri' => 'principals/user1', 27*a1a3b679SAndreas Boehler ) 28*a1a3b679SAndreas Boehler ); 29*a1a3b679SAndreas Boehler 30*a1a3b679SAndreas Boehler } 31*a1a3b679SAndreas Boehler 32*a1a3b679SAndreas Boehler function testGetName() { 33*a1a3b679SAndreas Boehler 34*a1a3b679SAndreas Boehler $this->assertEquals('book1', $this->ab->getName()); 35*a1a3b679SAndreas Boehler 36*a1a3b679SAndreas Boehler } 37*a1a3b679SAndreas Boehler 38*a1a3b679SAndreas Boehler function testGetChild() { 39*a1a3b679SAndreas Boehler 40*a1a3b679SAndreas Boehler $card = $this->ab->getChild('card1'); 41*a1a3b679SAndreas Boehler $this->assertInstanceOf('Sabre\\CardDAV\\Card', $card); 42*a1a3b679SAndreas Boehler $this->assertEquals('card1', $card->getName()); 43*a1a3b679SAndreas Boehler 44*a1a3b679SAndreas Boehler } 45*a1a3b679SAndreas Boehler 46*a1a3b679SAndreas Boehler /** 47*a1a3b679SAndreas Boehler * @expectedException Sabre\DAV\Exception\NotFound 48*a1a3b679SAndreas Boehler */ 49*a1a3b679SAndreas Boehler function testGetChildNotFound() { 50*a1a3b679SAndreas Boehler 51*a1a3b679SAndreas Boehler $card = $this->ab->getChild('card3'); 52*a1a3b679SAndreas Boehler 53*a1a3b679SAndreas Boehler } 54*a1a3b679SAndreas Boehler 55*a1a3b679SAndreas Boehler function testGetChildren() { 56*a1a3b679SAndreas Boehler 57*a1a3b679SAndreas Boehler $cards = $this->ab->getChildren(); 58*a1a3b679SAndreas Boehler $this->assertEquals(2, count($cards)); 59*a1a3b679SAndreas Boehler 60*a1a3b679SAndreas Boehler $this->assertEquals('card1', $cards[0]->getName()); 61*a1a3b679SAndreas Boehler $this->assertEquals('card2', $cards[1]->getName()); 62*a1a3b679SAndreas Boehler 63*a1a3b679SAndreas Boehler } 64*a1a3b679SAndreas Boehler 65*a1a3b679SAndreas Boehler /** 66*a1a3b679SAndreas Boehler * @expectedException Sabre\DAV\Exception\MethodNotAllowed 67*a1a3b679SAndreas Boehler */ 68*a1a3b679SAndreas Boehler function testCreateDirectory() { 69*a1a3b679SAndreas Boehler 70*a1a3b679SAndreas Boehler $this->ab->createDirectory('name'); 71*a1a3b679SAndreas Boehler 72*a1a3b679SAndreas Boehler } 73*a1a3b679SAndreas Boehler 74*a1a3b679SAndreas Boehler function testCreateFile() { 75*a1a3b679SAndreas Boehler 76*a1a3b679SAndreas Boehler $file = fopen('php://memory','r+'); 77*a1a3b679SAndreas Boehler fwrite($file,'foo'); 78*a1a3b679SAndreas Boehler rewind($file); 79*a1a3b679SAndreas Boehler $this->ab->createFile('card2',$file); 80*a1a3b679SAndreas Boehler 81*a1a3b679SAndreas Boehler $this->assertEquals('foo', $this->backend->cards['foo']['card2']); 82*a1a3b679SAndreas Boehler 83*a1a3b679SAndreas Boehler } 84*a1a3b679SAndreas Boehler 85*a1a3b679SAndreas Boehler function testDelete() { 86*a1a3b679SAndreas Boehler 87*a1a3b679SAndreas Boehler $this->ab->delete(); 88*a1a3b679SAndreas Boehler $this->assertEquals(array(), $this->backend->addressBooks); 89*a1a3b679SAndreas Boehler 90*a1a3b679SAndreas Boehler } 91*a1a3b679SAndreas Boehler 92*a1a3b679SAndreas Boehler /** 93*a1a3b679SAndreas Boehler * @expectedException Sabre\DAV\Exception\MethodNotAllowed 94*a1a3b679SAndreas Boehler */ 95*a1a3b679SAndreas Boehler function testSetName() { 96*a1a3b679SAndreas Boehler 97*a1a3b679SAndreas Boehler $this->ab->setName('foo'); 98*a1a3b679SAndreas Boehler 99*a1a3b679SAndreas Boehler } 100*a1a3b679SAndreas Boehler 101*a1a3b679SAndreas Boehler function testGetLastModified() { 102*a1a3b679SAndreas Boehler 103*a1a3b679SAndreas Boehler $this->assertNull($this->ab->getLastModified()); 104*a1a3b679SAndreas Boehler 105*a1a3b679SAndreas Boehler } 106*a1a3b679SAndreas Boehler 107*a1a3b679SAndreas Boehler function testUpdateProperties() { 108*a1a3b679SAndreas Boehler 109*a1a3b679SAndreas Boehler $propPatch = new PropPatch([ 110*a1a3b679SAndreas Boehler '{DAV:}displayname' => 'barrr', 111*a1a3b679SAndreas Boehler ]); 112*a1a3b679SAndreas Boehler $this->ab->propPatch($propPatch); 113*a1a3b679SAndreas Boehler $this->assertTrue($propPatch->commit()); 114*a1a3b679SAndreas Boehler 115*a1a3b679SAndreas Boehler $this->assertEquals('barrr', $this->backend->addressBooks[0]['{DAV:}displayname']); 116*a1a3b679SAndreas Boehler 117*a1a3b679SAndreas Boehler } 118*a1a3b679SAndreas Boehler 119*a1a3b679SAndreas Boehler function testGetProperties() { 120*a1a3b679SAndreas Boehler 121*a1a3b679SAndreas Boehler $props = $this->ab->getProperties(array('{DAV:}displayname')); 122*a1a3b679SAndreas Boehler $this->assertEquals(array( 123*a1a3b679SAndreas Boehler '{DAV:}displayname' => 'd-name', 124*a1a3b679SAndreas Boehler ), $props); 125*a1a3b679SAndreas Boehler 126*a1a3b679SAndreas Boehler } 127*a1a3b679SAndreas Boehler 128*a1a3b679SAndreas Boehler function testACLMethods() { 129*a1a3b679SAndreas Boehler 130*a1a3b679SAndreas Boehler $this->assertEquals('principals/user1', $this->ab->getOwner()); 131*a1a3b679SAndreas Boehler $this->assertNull($this->ab->getGroup()); 132*a1a3b679SAndreas Boehler $this->assertEquals(array( 133*a1a3b679SAndreas Boehler array( 134*a1a3b679SAndreas Boehler 'privilege' => '{DAV:}read', 135*a1a3b679SAndreas Boehler 'principal' => 'principals/user1', 136*a1a3b679SAndreas Boehler 'protected' => true, 137*a1a3b679SAndreas Boehler ), 138*a1a3b679SAndreas Boehler array( 139*a1a3b679SAndreas Boehler 'privilege' => '{DAV:}write', 140*a1a3b679SAndreas Boehler 'principal' => 'principals/user1', 141*a1a3b679SAndreas Boehler 'protected' => true, 142*a1a3b679SAndreas Boehler ), 143*a1a3b679SAndreas Boehler ), $this->ab->getACL()); 144*a1a3b679SAndreas Boehler 145*a1a3b679SAndreas Boehler } 146*a1a3b679SAndreas Boehler 147*a1a3b679SAndreas Boehler /** 148*a1a3b679SAndreas Boehler * @expectedException Sabre\DAV\Exception\MethodNotAllowed 149*a1a3b679SAndreas Boehler */ 150*a1a3b679SAndreas Boehler function testSetACL() { 151*a1a3b679SAndreas Boehler 152*a1a3b679SAndreas Boehler $this->ab->setACL(array()); 153*a1a3b679SAndreas Boehler 154*a1a3b679SAndreas Boehler } 155*a1a3b679SAndreas Boehler 156*a1a3b679SAndreas Boehler function testGetSupportedPrivilegeSet() { 157*a1a3b679SAndreas Boehler 158*a1a3b679SAndreas Boehler $this->assertNull( 159*a1a3b679SAndreas Boehler $this->ab->getSupportedPrivilegeSet() 160*a1a3b679SAndreas Boehler ); 161*a1a3b679SAndreas Boehler 162*a1a3b679SAndreas Boehler } 163*a1a3b679SAndreas Boehler 164*a1a3b679SAndreas Boehler function testGetSyncTokenNoSyncSupport() { 165*a1a3b679SAndreas Boehler 166*a1a3b679SAndreas Boehler $this->assertNull(null, $this->ab->getSyncToken()); 167*a1a3b679SAndreas Boehler 168*a1a3b679SAndreas Boehler } 169*a1a3b679SAndreas Boehler function testGetChangesNoSyncSupport() { 170*a1a3b679SAndreas Boehler 171*a1a3b679SAndreas Boehler $this->assertNull($this->ab->getChanges(1,null)); 172*a1a3b679SAndreas Boehler 173*a1a3b679SAndreas Boehler } 174*a1a3b679SAndreas Boehler 175*a1a3b679SAndreas Boehler function testGetSyncToken() { 176*a1a3b679SAndreas Boehler 177*a1a3b679SAndreas Boehler if (!SABRE_HASSQLITE) { 178*a1a3b679SAndreas Boehler $this->markTestSkipped('Sqlite is required for this test to run'); 179*a1a3b679SAndreas Boehler } 180*a1a3b679SAndreas Boehler $ab = new AddressBook(TestUtil::getBackend(), [ 'id' => 1, '{DAV:}sync-token' => 2]); 181*a1a3b679SAndreas Boehler $this->assertEquals(2, $ab->getSyncToken()); 182*a1a3b679SAndreas Boehler } 183*a1a3b679SAndreas Boehler 184*a1a3b679SAndreas Boehler function testGetSyncToken2() { 185*a1a3b679SAndreas Boehler 186*a1a3b679SAndreas Boehler if (!SABRE_HASSQLITE) { 187*a1a3b679SAndreas Boehler $this->markTestSkipped('Sqlite is required for this test to run'); 188*a1a3b679SAndreas Boehler } 189*a1a3b679SAndreas Boehler $ab = new AddressBook(TestUtil::getBackend(), [ 'id' => 1, '{http://sabredav.org/ns}sync-token' => 2]); 190*a1a3b679SAndreas Boehler $this->assertEquals(2, $ab->getSyncToken()); 191*a1a3b679SAndreas Boehler } 192*a1a3b679SAndreas Boehler 193*a1a3b679SAndreas Boehler function testGetChanges() { 194*a1a3b679SAndreas Boehler 195*a1a3b679SAndreas Boehler if (!SABRE_HASSQLITE) { 196*a1a3b679SAndreas Boehler $this->markTestSkipped('Sqlite is required for this test to run'); 197*a1a3b679SAndreas Boehler } 198*a1a3b679SAndreas Boehler $ab = new AddressBook(TestUtil::getBackend(), [ 'id' => 1, '{DAV:}sync-token' => 2]); 199*a1a3b679SAndreas Boehler $this->assertEquals([ 200*a1a3b679SAndreas Boehler 'syncToken' => 2, 201*a1a3b679SAndreas Boehler 'modified' => [], 202*a1a3b679SAndreas Boehler 'deleted' => [], 203*a1a3b679SAndreas Boehler 'added' => ['UUID-2345'], 204*a1a3b679SAndreas Boehler ], $ab->getChanges(1, 1)); 205*a1a3b679SAndreas Boehler 206*a1a3b679SAndreas Boehler } 207*a1a3b679SAndreas Boehler 208*a1a3b679SAndreas Boehler 209*a1a3b679SAndreas Boehler} 210