ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
AbstractPDOTest.php
Go to the documentation of this file.
1<?php
2
4
7
9
11
15 protected $backend;
16
17 function setUp() {
18
19 $this->dropTables([
20 'addressbooks',
21 'cards',
22 'addressbookchanges',
23 ]);
24 $this->createSchema('addressbooks');
25 $pdo = $this->getPDO();
26
27 $this->backend = new PDO($pdo);
28 $pdo->exec("INSERT INTO addressbooks (principaluri, displayname, uri, description, synctoken) VALUES ('principals/user1', 'book1', 'book1', 'addressbook 1', 1)");
29 $pdo->exec("INSERT INTO cards (addressbookid, carddata, uri, lastmodified, etag, size) VALUES (1, 'card1', 'card1', 0, '" . md5('card1') . "', 5)");
30
31 }
32
34
35 $result = $this->backend->getAddressBooksForUser('principals/user1');
36
37 $expected = [
38 [
39 'id' => 1,
40 'uri' => 'book1',
41 'principaluri' => 'principals/user1',
42 '{DAV:}displayname' => 'book1',
43 '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
44 '{http://calendarserver.org/ns/}getctag' => 1,
45 '{http://sabredav.org/ns}sync-token' => 1
46 ]
47 ];
48
49 $this->assertEquals($expected, $result);
50
51 }
52
54
55 $propPatch = new PropPatch([
56 '{DAV:}displayname' => 'updated',
57 '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
58 '{DAV:}foo' => 'bar',
59 ]);
60
61 $this->backend->updateAddressBook(1, $propPatch);
62 $result = $propPatch->commit();
63
64 $this->assertFalse($result);
65
66 $result = $this->backend->getAddressBooksForUser('principals/user1');
67
68 $expected = [
69 [
70 'id' => 1,
71 'uri' => 'book1',
72 'principaluri' => 'principals/user1',
73 '{DAV:}displayname' => 'book1',
74 '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
75 '{http://calendarserver.org/ns/}getctag' => 1,
76 '{http://sabredav.org/ns}sync-token' => 1
77 ]
78 ];
79
80 $this->assertEquals($expected, $result);
81
82 }
83
85
86 $propPatch = new PropPatch([
87 ]);
88
89 $this->backend->updateAddressBook(1, $propPatch);
90 $result = $propPatch->commit();
91 $this->assertTrue($result);
92
93 $result = $this->backend->getAddressBooksForUser('principals/user1');
94
95 $expected = [
96 [
97 'id' => 1,
98 'uri' => 'book1',
99 'principaluri' => 'principals/user1',
100 '{DAV:}displayname' => 'book1',
101 '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
102 '{http://calendarserver.org/ns/}getctag' => 1,
103 '{http://sabredav.org/ns}sync-token' => 1
104 ]
105 ];
106
107 $this->assertEquals($expected, $result);
108
109
110 }
111
113
114 $propPatch = new PropPatch([
115 '{DAV:}displayname' => 'updated',
116 '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
117 ]);
118
119 $this->backend->updateAddressBook(1, $propPatch);
120 $result = $propPatch->commit();
121
122 $this->assertTrue($result);
123
124 $result = $this->backend->getAddressBooksForUser('principals/user1');
125
126 $expected = [
127 [
128 'id' => 1,
129 'uri' => 'book1',
130 'principaluri' => 'principals/user1',
131 '{DAV:}displayname' => 'updated',
132 '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
133 '{http://calendarserver.org/ns/}getctag' => 2,
134 '{http://sabredav.org/ns}sync-token' => 2
135 ]
136 ];
137
138 $this->assertEquals($expected, $result);
139
140
141 }
142
144
145 $this->backend->deleteAddressBook(1);
146
147 $this->assertEquals([], $this->backend->getAddressBooksForUser('principals/user1'));
148
149 }
150
155
156 $this->backend->createAddressBook('principals/user1', 'book2', [
157 '{DAV:}foo' => 'bar',
158 ]);
159
160 }
161
163
164 $this->backend->createAddressBook('principals/user1', 'book2', [
165 '{DAV:}displayname' => 'book2',
166 '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 2',
167 ]);
168
169 $expected = [
170 [
171 'id' => 1,
172 'uri' => 'book1',
173 'principaluri' => 'principals/user1',
174 '{DAV:}displayname' => 'book1',
175 '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
176 '{http://calendarserver.org/ns/}getctag' => 1,
177 '{http://sabredav.org/ns}sync-token' => 1,
178 ],
179 [
180 'id' => 2,
181 'uri' => 'book2',
182 'principaluri' => 'principals/user1',
183 '{DAV:}displayname' => 'book2',
184 '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 2',
185 '{http://calendarserver.org/ns/}getctag' => 1,
186 '{http://sabredav.org/ns}sync-token' => 1,
187 ]
188 ];
189 $result = $this->backend->getAddressBooksForUser('principals/user1');
190 $this->assertEquals($expected, $result);
191
192 }
193
194 function testGetCards() {
195
196 $result = $this->backend->getCards(1);
197
198 $expected = [
199 [
200 'id' => 1,
201 'uri' => 'card1',
202 'lastmodified' => 0,
203 'etag' => '"' . md5('card1') . '"',
204 'size' => 5
205 ]
206 ];
207
208 $this->assertEquals($expected, $result);
209
210 }
211
212 function testGetCard() {
213
214 $result = $this->backend->getCard(1, 'card1');
215
216 $expected = [
217 'id' => 1,
218 'uri' => 'card1',
219 'carddata' => 'card1',
220 'lastmodified' => 0,
221 'etag' => '"' . md5('card1') . '"',
222 'size' => 5
223 ];
224
225 if (is_resource($result['carddata'])) {
226 $result['carddata'] = stream_get_contents($result['carddata']);
227 }
228
229 $this->assertEquals($expected, $result);
230
231 }
232
236 function testCreateCard() {
237
238 $result = $this->backend->createCard(1, 'card2', 'data2');
239 $this->assertEquals('"' . md5('data2') . '"', $result);
240 $result = $this->backend->getCard(1, 'card2');
241 $this->assertEquals(2, $result['id']);
242 $this->assertEquals('card2', $result['uri']);
243 if (is_resource($result['carddata'])) {
244 $result['carddata'] = stream_get_contents($result['carddata']);
245 }
246 $this->assertEquals('data2', $result['carddata']);
247
248 }
249
253 function testGetMultiple() {
254
255 $result = $this->backend->createCard(1, 'card2', 'data2');
256 $result = $this->backend->createCard(1, 'card3', 'data3');
257 $check = [
258 [
259 'id' => 1,
260 'uri' => 'card1',
261 'carddata' => 'card1',
262 'lastmodified' => 0,
263 ],
264 [
265 'id' => 2,
266 'uri' => 'card2',
267 'carddata' => 'data2',
268 'lastmodified' => time(),
269 ],
270 [
271 'id' => 3,
272 'uri' => 'card3',
273 'carddata' => 'data3',
274 'lastmodified' => time(),
275 ],
276 ];
277
278 $result = $this->backend->getMultipleCards(1, ['card1', 'card2', 'card3']);
279
280 foreach ($check as $index => $node) {
281
282 foreach ($node as $k => $v) {
283
284 $expected = $v;
285 $actual = $result[$index][$k];
286
287 switch ($k) {
288 case 'lastmodified' :
289 $this->assertInternalType('int', $actual);
290 break;
291 case 'carddata' :
292 if (is_resource($actual)) {
293 $actual = stream_get_contents($actual);
294 }
295 // No break intended.
296 default :
297 $this->assertEquals($expected, $actual);
298 break;
299 }
300
301 }
302
303 }
304
305
306 }
307
311 function testUpdateCard() {
312
313 $result = $this->backend->updateCard(1, 'card1', 'newdata');
314 $this->assertEquals('"' . md5('newdata') . '"', $result);
315
316 $result = $this->backend->getCard(1, 'card1');
317 $this->assertEquals(1, $result['id']);
318 if (is_resource($result['carddata'])) {
319 $result['carddata'] = stream_get_contents($result['carddata']);
320 }
321 $this->assertEquals('newdata', $result['carddata']);
322
323 }
324
328 function testDeleteCard() {
329
330 $this->backend->deleteCard(1, 'card1');
331 $result = $this->backend->getCard(1, 'card1');
332 $this->assertFalse($result);
333
334 }
335
336 function testGetChanges() {
337
339 $id = $backend->createAddressBook(
340 'principals/user1',
341 'bla',
342 []
343 );
344 $result = $backend->getChangesForAddressBook($id, null, 1);
345
346 $this->assertEquals([
347 'syncToken' => 1,
348 "added" => [],
349 'modified' => [],
350 'deleted' => [],
351 ], $result);
352
353 $currentToken = $result['syncToken'];
354
355 $dummyCard = "BEGIN:VCARD\r\nEND:VCARD\r\n";
356
357 $backend->createCard($id, "card1.ics", $dummyCard);
358 $backend->createCard($id, "card2.ics", $dummyCard);
359 $backend->createCard($id, "card3.ics", $dummyCard);
360 $backend->updateCard($id, "card1.ics", $dummyCard);
361 $backend->deleteCard($id, "card2.ics");
362
363 $result = $backend->getChangesForAddressBook($id, $currentToken, 1);
364
365 $this->assertEquals([
366 'syncToken' => 6,
367 'modified' => ["card1.ics"],
368 'deleted' => ["card2.ics"],
369 "added" => ["card3.ics"],
370 ], $result);
371
372 }
373}
$result
An exception for terminatinating execution or to throw for unit testing.
testGetMultiple()
@depends testCreateCard
testCreateAddressBookUnsupportedProp()
@expectedException Sabre\DAV\Exception\BadRequest
PDO CardDAV backend.
Definition: PDO.php:17
const NS_CARDDAV
xml namespace for CardDAV elements
Definition: Plugin.php:33
This class represents a set of properties that are going to be updated.
Definition: PropPatch.php:20
if(!array_key_exists('StateId', $_REQUEST)) $id
$index
Definition: metadata.php:60
$pdo
Definition: migrateto20.php:62
trait DbTestHelperTrait
dropTables($tableNames)
Drops tables, if they exist.
getPDO()
Alias for getDb.
createSchema($schemaName)
Uses .sql files from the examples directory to initialize the database.