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