ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ValidateVCardTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\CardDAV;
4 
5 use Sabre\DAV;
6 use Sabre\DAVACL;
7 use Sabre\HTTP;
8 
9 require_once 'Sabre/HTTP/ResponseMock.php';
10 
12 
13  protected $server;
14  protected $cardBackend;
15 
16  function setUp() {
17 
18  $addressbooks = [
19  [
20  'id' => 'addressbook1',
21  'principaluri' => 'principals/admin',
22  'uri' => 'addressbook1',
23  ]
24  ];
25 
26  $this->cardBackend = new Backend\Mock($addressbooks, []);
28 
29  $tree = [
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, $expectedStatus = null) {
46 
47  $this->server->httpRequest = $request;
48  $this->server->exec();
49 
50  if ($expectedStatus) {
51 
52  $realStatus = $this->server->httpResponse->getStatus();
53 
54  $msg = '';
55  if ($realStatus !== $expectedStatus) {
56  $msg = 'Response body: ' . $this->server->httpResponse->getBodyAsString();
57  }
58  $this->assertEquals(
59  $expectedStatus,
60  $realStatus,
61  $msg
62  );
63  }
64 
65  return $this->server->httpResponse;
66 
67  }
68 
69  function testCreateFile() {
70 
72  'REQUEST_METHOD' => 'PUT',
73  'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf',
74  ]);
75 
76  $response = $this->request($request);
77 
78  $this->assertEquals(415, $response->status);
79 
80  }
81 
82  function testCreateFileValid() {
83 
84  $request = new HTTP\Request(
85  'PUT',
86  '/addressbooks/admin/addressbook1/blabla.vcf'
87  );
88 
89  $vcard = <<<VCF
90 BEGIN:VCARD
91 VERSION:4.0
92 UID:foo
93 FN:Firstname LastName
94 N:LastName;FirstName;;;
95 END:VCARD
96 VCF;
97  $request->setBody($vcard);
98 
99  $response = $this->request($request, 201);
100 
101  // The custom Ew header should not be set
102  $this->assertNull(
103  $response->getHeader('X-Sabre-Ew-Gross')
104  );
105  // Valid, non-auto-fixed responses should contain an ETag.
106  $this->assertTrue(
107  $response->getHeader('ETag') !== null,
108  'We did not receive an etag'
109  );
110 
111 
112  $expected = [
113  'uri' => 'blabla.vcf',
114  'carddata' => $vcard,
115  'size' => strlen($vcard),
116  'etag' => '"' . md5($vcard) . '"',
117  ];
118 
119  $this->assertEquals($expected, $this->cardBackend->getCard('addressbook1', 'blabla.vcf'));
120 
121  }
122 
130 
131  $request = new HTTP\Request(
132  'PUT',
133  '/addressbooks/admin/addressbook1/blabla.vcf'
134  );
135 
136  // The error in this vcard is that there's not enough semi-colons in N
137  $vcard = <<<VCF
138 BEGIN:VCARD
139 VERSION:4.0
140 UID:foo
141 FN:Firstname LastName
142 N:LastName;FirstName;;
143 END:VCARD
144 VCF;
145 
146  $request->setBody($vcard);
147 
148  $response = $this->request($request, 201);
149 
150  // Auto-fixed vcards should NOT return an etag
151  $this->assertNull(
152  $response->getHeader('ETag')
153  );
154 
155  // We should have gotten an Ew header
156  $this->assertNotNull(
157  $response->getHeader('X-Sabre-Ew-Gross')
158  );
159 
160  $expectedVCard = <<<VCF
161 BEGIN:VCARD\r
162 VERSION:4.0\r
163 UID:foo\r
164 FN:Firstname LastName\r
165 N:LastName;FirstName;;;\r
166 END:VCARD\r
167 
168 VCF;
169 
170  $expected = [
171  'uri' => 'blabla.vcf',
172  'carddata' => $expectedVCard,
173  'size' => strlen($expectedVCard),
174  'etag' => '"' . md5($expectedVCard) . '"',
175  ];
176 
177  $this->assertEquals($expected, $this->cardBackend->getCard('addressbook1', 'blabla.vcf'));
178 
179  }
180 
191 
192  $request = new HTTP\Request(
193  'PUT',
194  '/addressbooks/admin/addressbook1/blabla.vcf',
195  [
196  'Prefer' => 'handling=strict',
197  ]
198  );
199 
200  // The error in this vcard is that there's not enough semi-colons in N
201  $vcard = <<<VCF
202 BEGIN:VCARD
203 VERSION:4.0
204 UID:foo
205 FN:Firstname LastName
206 N:LastName;FirstName;;
207 END:VCARD
208 VCF;
209 
210  $request->setBody($vcard);
211  $this->request($request, 415);
212 
213  }
214 
215  function testCreateFileNoUID() {
216 
217  $request = new HTTP\Request(
218  'PUT',
219  '/addressbooks/admin/addressbook1/blabla.vcf'
220  );
221  $vcard = <<<VCF
222 BEGIN:VCARD
223 VERSION:4.0
224 FN:Firstname LastName
225 N:LastName;FirstName;;;
226 END:VCARD
227 VCF;
228  $request->setBody($vcard);
229 
230  $response = $this->request($request, 201);
231 
232  $foo = $this->cardBackend->getCard('addressbook1', 'blabla.vcf');
233  $this->assertTrue(
234  strpos($foo['carddata'], 'UID') !== false,
235  print_r($foo, true)
236  );
237  }
238 
239  function testCreateFileJson() {
240 
241  $request = new HTTP\Request(
242  'PUT',
243  '/addressbooks/admin/addressbook1/blabla.vcf'
244  );
245  $request->setBody('[ "vcard" , [ [ "VERSION", {}, "text", "4.0"], [ "UID" , {}, "text", "foo" ], [ "FN", {}, "text", "FirstName LastName"] ] ]');
246 
247  $response = $this->request($request);
248 
249  $this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
250 
251  $foo = $this->cardBackend->getCard('addressbook1', 'blabla.vcf');
252  $this->assertEquals("BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nFN:FirstName LastName\r\nEND:VCARD\r\n", $foo['carddata']);
253 
254  }
255 
257 
259  'REQUEST_METHOD' => 'PUT',
260  'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf',
261  ]);
262  $request->setBody("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n");
263 
264  $response = $this->request($request);
265 
266  $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
267 
268  }
269 
270  function testUpdateFile() {
271 
272  $this->cardBackend->createCard('addressbook1', 'blabla.vcf', 'foo');
273  $request = new HTTP\Request(
274  'PUT',
275  '/addressbooks/admin/addressbook1/blabla.vcf'
276  );
277 
278  $response = $this->request($request, 415);
279 
280  }
281 
283 
284  $this->cardBackend->createCard('addressbook1', 'blabla.vcf', 'foo');
285  $request = new HTTP\Request(
286  'PUT',
287  '/addressbooks/admin/addressbook1/blabla.vcf'
288  );
289 
290  $body = "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nFN:FirstName LastName\r\nEND:VCARD\r\n";
291  $request->setBody($body);
292 
293  $response = $this->request($request, 204);
294 
295  $expected = [
296  'uri' => 'blabla.vcf',
297  'carddata' => $body,
298  'size' => strlen($body),
299  'etag' => '"' . md5($body) . '"',
300  ];
301 
302  $this->assertEquals($expected, $this->cardBackend->getCard('addressbook1', 'blabla.vcf'));
303 
304  }
305 }
foreach($paths as $path) $request
Definition: asyncclient.php:32
The Request class represents a single HTTP request.
Definition: Request.php:18
testCreateVCardStrictFail()
This test creates an intentionally broken vCard that vobject is able to automatically repair...
CardDAV plugin.
Definition: Plugin.php:23
Main DAV server class.
Definition: Server.php:23
request(HTTP\Request $request, $expectedStatus=null)
testCreateVCardAutoFix()
This test creates an intentionally broken vCard that vobject is able to automatically repair...
AddressBook rootnode.
static createFromServerArray(array $serverArray)
This static method will create a new Request object, based on a PHP $_SERVER array.
Definition: Sapi.php:107
$response
$principalBackend