1*a1a3b679SAndreas Boehler<?php 2*a1a3b679SAndreas Boehler 3*a1a3b679SAndreas Boehlernamespace Sabre\CardDAV; 4*a1a3b679SAndreas Boehler 5*a1a3b679SAndreas Boehleruse Sabre\DAV; 6*a1a3b679SAndreas Boehleruse Sabre\HTTP; 7*a1a3b679SAndreas Boehleruse Sabre\DAVACL; 8*a1a3b679SAndreas Boehler 9*a1a3b679SAndreas Boehlerrequire_once 'Sabre/HTTP/ResponseMock.php'; 10*a1a3b679SAndreas Boehler 11*a1a3b679SAndreas Boehlerclass ValidateVCardTest extends \PHPUnit_Framework_TestCase { 12*a1a3b679SAndreas Boehler 13*a1a3b679SAndreas Boehler protected $server; 14*a1a3b679SAndreas Boehler protected $cardBackend; 15*a1a3b679SAndreas Boehler 16*a1a3b679SAndreas Boehler function setUp() { 17*a1a3b679SAndreas Boehler 18*a1a3b679SAndreas Boehler $addressbooks = array( 19*a1a3b679SAndreas Boehler array( 20*a1a3b679SAndreas Boehler 'id' => 'addressbook1', 21*a1a3b679SAndreas Boehler 'principaluri' => 'principals/admin', 22*a1a3b679SAndreas Boehler 'uri' => 'addressbook1', 23*a1a3b679SAndreas Boehler ) 24*a1a3b679SAndreas Boehler ); 25*a1a3b679SAndreas Boehler 26*a1a3b679SAndreas Boehler $this->cardBackend = new Backend\Mock($addressbooks,array()); 27*a1a3b679SAndreas Boehler $principalBackend = new DAVACL\PrincipalBackend\Mock(); 28*a1a3b679SAndreas Boehler 29*a1a3b679SAndreas Boehler $tree = array( 30*a1a3b679SAndreas Boehler new AddressBookRoot($principalBackend, $this->cardBackend), 31*a1a3b679SAndreas Boehler ); 32*a1a3b679SAndreas Boehler 33*a1a3b679SAndreas Boehler $this->server = new DAV\Server($tree); 34*a1a3b679SAndreas Boehler $this->server->sapi = new HTTP\SapiMock(); 35*a1a3b679SAndreas Boehler $this->server->debugExceptions = true; 36*a1a3b679SAndreas Boehler 37*a1a3b679SAndreas Boehler $plugin = new Plugin(); 38*a1a3b679SAndreas Boehler $this->server->addPlugin($plugin); 39*a1a3b679SAndreas Boehler 40*a1a3b679SAndreas Boehler $response = new HTTP\ResponseMock(); 41*a1a3b679SAndreas Boehler $this->server->httpResponse = $response; 42*a1a3b679SAndreas Boehler 43*a1a3b679SAndreas Boehler } 44*a1a3b679SAndreas Boehler 45*a1a3b679SAndreas Boehler function request(HTTP\Request $request) { 46*a1a3b679SAndreas Boehler 47*a1a3b679SAndreas Boehler $this->server->httpRequest = $request; 48*a1a3b679SAndreas Boehler $this->server->exec(); 49*a1a3b679SAndreas Boehler 50*a1a3b679SAndreas Boehler return $this->server->httpResponse; 51*a1a3b679SAndreas Boehler 52*a1a3b679SAndreas Boehler } 53*a1a3b679SAndreas Boehler 54*a1a3b679SAndreas Boehler function testCreateFile() { 55*a1a3b679SAndreas Boehler 56*a1a3b679SAndreas Boehler $request = HTTP\Sapi::createFromServerArray(array( 57*a1a3b679SAndreas Boehler 'REQUEST_METHOD' => 'PUT', 58*a1a3b679SAndreas Boehler 'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf', 59*a1a3b679SAndreas Boehler )); 60*a1a3b679SAndreas Boehler 61*a1a3b679SAndreas Boehler $response = $this->request($request); 62*a1a3b679SAndreas Boehler 63*a1a3b679SAndreas Boehler $this->assertEquals(415, $response->status); 64*a1a3b679SAndreas Boehler 65*a1a3b679SAndreas Boehler } 66*a1a3b679SAndreas Boehler 67*a1a3b679SAndreas Boehler function testCreateFileValid() { 68*a1a3b679SAndreas Boehler 69*a1a3b679SAndreas Boehler $request = HTTP\Sapi::createFromServerArray(array( 70*a1a3b679SAndreas Boehler 'REQUEST_METHOD' => 'PUT', 71*a1a3b679SAndreas Boehler 'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf', 72*a1a3b679SAndreas Boehler )); 73*a1a3b679SAndreas Boehler $request->setBody("BEGIN:VCARD\r\nUID:foo\r\nEND:VCARD\r\n"); 74*a1a3b679SAndreas Boehler 75*a1a3b679SAndreas Boehler $response = $this->request($request); 76*a1a3b679SAndreas Boehler 77*a1a3b679SAndreas Boehler $this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: ' . $response->body); 78*a1a3b679SAndreas Boehler $expected = array( 79*a1a3b679SAndreas Boehler 'uri' => 'blabla.vcf', 80*a1a3b679SAndreas Boehler 'carddata' => "BEGIN:VCARD\r\nUID:foo\r\nEND:VCARD\r\n", 81*a1a3b679SAndreas Boehler ); 82*a1a3b679SAndreas Boehler 83*a1a3b679SAndreas Boehler $this->assertEquals($expected, $this->cardBackend->getCard('addressbook1','blabla.vcf')); 84*a1a3b679SAndreas Boehler 85*a1a3b679SAndreas Boehler } 86*a1a3b679SAndreas Boehler 87*a1a3b679SAndreas Boehler function testCreateFileNoUID() { 88*a1a3b679SAndreas Boehler 89*a1a3b679SAndreas Boehler $request = new HTTP\Request( 90*a1a3b679SAndreas Boehler 'PUT', 91*a1a3b679SAndreas Boehler '/addressbooks/admin/addressbook1/blabla.vcf' 92*a1a3b679SAndreas Boehler ); 93*a1a3b679SAndreas Boehler $request->setBody("BEGIN:VCARD\r\nEND:VCARD\r\n"); 94*a1a3b679SAndreas Boehler 95*a1a3b679SAndreas Boehler $response = $this->request($request); 96*a1a3b679SAndreas Boehler 97*a1a3b679SAndreas Boehler $this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: ' . $response->body); 98*a1a3b679SAndreas Boehler 99*a1a3b679SAndreas Boehler $foo = $this->cardBackend->getCard('addressbook1','blabla.vcf'); 100*a1a3b679SAndreas Boehler $this->assertTrue(strpos($foo['carddata'],'UID')!==false); 101*a1a3b679SAndreas Boehler } 102*a1a3b679SAndreas Boehler 103*a1a3b679SAndreas Boehler function testCreateFileJson() { 104*a1a3b679SAndreas Boehler 105*a1a3b679SAndreas Boehler $request = new HTTP\Request( 106*a1a3b679SAndreas Boehler 'PUT', 107*a1a3b679SAndreas Boehler '/addressbooks/admin/addressbook1/blabla.vcf' 108*a1a3b679SAndreas Boehler ); 109*a1a3b679SAndreas Boehler $request->setBody('[ "vcard" , [ [ "UID" , {}, "text", "foo" ] ] ]'); 110*a1a3b679SAndreas Boehler 111*a1a3b679SAndreas Boehler $response = $this->request($request); 112*a1a3b679SAndreas Boehler 113*a1a3b679SAndreas Boehler $this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: ' . $response->body); 114*a1a3b679SAndreas Boehler 115*a1a3b679SAndreas Boehler $foo = $this->cardBackend->getCard('addressbook1','blabla.vcf'); 116*a1a3b679SAndreas Boehler $this->assertEquals("BEGIN:VCARD\r\nUID:foo\r\nEND:VCARD\r\n", $foo['carddata']); 117*a1a3b679SAndreas Boehler 118*a1a3b679SAndreas Boehler } 119*a1a3b679SAndreas Boehler 120*a1a3b679SAndreas Boehler function testCreateFileVCalendar() { 121*a1a3b679SAndreas Boehler 122*a1a3b679SAndreas Boehler $request = HTTP\Sapi::createFromServerArray(array( 123*a1a3b679SAndreas Boehler 'REQUEST_METHOD' => 'PUT', 124*a1a3b679SAndreas Boehler 'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf', 125*a1a3b679SAndreas Boehler )); 126*a1a3b679SAndreas Boehler $request->setBody("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n"); 127*a1a3b679SAndreas Boehler 128*a1a3b679SAndreas Boehler $response = $this->request($request); 129*a1a3b679SAndreas Boehler 130*a1a3b679SAndreas Boehler $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: ' . $response->body); 131*a1a3b679SAndreas Boehler 132*a1a3b679SAndreas Boehler } 133*a1a3b679SAndreas Boehler 134*a1a3b679SAndreas Boehler function testUpdateFile() { 135*a1a3b679SAndreas Boehler 136*a1a3b679SAndreas Boehler $this->cardBackend->createCard('addressbook1','blabla.vcf','foo'); 137*a1a3b679SAndreas Boehler $request = HTTP\Sapi::createFromServerArray(array( 138*a1a3b679SAndreas Boehler 'REQUEST_METHOD' => 'PUT', 139*a1a3b679SAndreas Boehler 'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf', 140*a1a3b679SAndreas Boehler )); 141*a1a3b679SAndreas Boehler 142*a1a3b679SAndreas Boehler $response = $this->request($request); 143*a1a3b679SAndreas Boehler 144*a1a3b679SAndreas Boehler $this->assertEquals(415, $response->status); 145*a1a3b679SAndreas Boehler 146*a1a3b679SAndreas Boehler } 147*a1a3b679SAndreas Boehler 148*a1a3b679SAndreas Boehler function testUpdateFileParsableBody() { 149*a1a3b679SAndreas Boehler 150*a1a3b679SAndreas Boehler $this->cardBackend->createCard('addressbook1','blabla.vcf','foo'); 151*a1a3b679SAndreas Boehler $request = HTTP\Sapi::createFromServerArray(array( 152*a1a3b679SAndreas Boehler 'REQUEST_METHOD' => 'PUT', 153*a1a3b679SAndreas Boehler 'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf', 154*a1a3b679SAndreas Boehler )); 155*a1a3b679SAndreas Boehler $body = "BEGIN:VCARD\r\nUID:foo\r\nEND:VCARD\r\n"; 156*a1a3b679SAndreas Boehler $request->setBody($body); 157*a1a3b679SAndreas Boehler 158*a1a3b679SAndreas Boehler $response = $this->request($request); 159*a1a3b679SAndreas Boehler 160*a1a3b679SAndreas Boehler $this->assertEquals(204, $response->status); 161*a1a3b679SAndreas Boehler 162*a1a3b679SAndreas Boehler $expected = array( 163*a1a3b679SAndreas Boehler 'uri' => 'blabla.vcf', 164*a1a3b679SAndreas Boehler 'carddata' => $body, 165*a1a3b679SAndreas Boehler ); 166*a1a3b679SAndreas Boehler 167*a1a3b679SAndreas Boehler $this->assertEquals($expected, $this->cardBackend->getCard('addressbook1','blabla.vcf')); 168*a1a3b679SAndreas Boehler 169*a1a3b679SAndreas Boehler } 170*a1a3b679SAndreas Boehler} 171*a1a3b679SAndreas Boehler 172*a1a3b679SAndreas Boehler?> 173