1<?php
2
3namespace Sabre\CardDAV\Backend;
4
5use Sabre\CardDAV;
6use Sabre\DAV\PropPatch;
7
8abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
9
10    /**
11     * @var CardDAV\Backend\PDO
12     */
13    protected $backend;
14
15    /**
16     * @abstract
17     * @return PDO
18     */
19    abstract function getPDO();
20
21    public function setUp() {
22
23        $pdo = $this->getPDO();
24        $this->backend = new PDO($pdo);
25        $pdo->exec('INSERT INTO addressbooks (principaluri, displayname, uri, description, synctoken) VALUES ("principals/user1", "book1", "book1", "addressbook 1", 1)');
26        $pdo->exec('INSERT INTO cards (addressbookid, carddata, uri, lastmodified, etag, size) VALUES (1, "card1", "card1", 0, "' . md5('card1') . '", 5)');
27
28    }
29
30    public function testGetAddressBooksForUser() {
31
32        $result = $this->backend->getAddressBooksForUser('principals/user1');
33
34        $expected = array(
35            array(
36                'id' => 1,
37                'uri' => 'book1',
38                'principaluri' => 'principals/user1',
39                '{DAV:}displayname' => 'book1',
40                '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
41                '{http://calendarserver.org/ns/}getctag' => 1,
42                '{http://sabredav.org/ns}sync-token' => "1"
43            )
44        );
45
46        $this->assertEquals($expected, $result);
47
48    }
49
50    public function testUpdateAddressBookInvalidProp() {
51
52        $propPatch = new PropPatch([
53            '{DAV:}displayname' => 'updated',
54            '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
55            '{DAV:}foo' => 'bar',
56        ]);
57
58        $this->backend->updateAddressBook(1, $propPatch);
59        $result = $propPatch->commit();
60
61        $this->assertFalse($result);
62
63        $result = $this->backend->getAddressBooksForUser('principals/user1');
64
65        $expected = array(
66            array(
67                'id' => 1,
68                'uri' => 'book1',
69                'principaluri' => 'principals/user1',
70                '{DAV:}displayname' => 'book1',
71                '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
72                '{http://calendarserver.org/ns/}getctag' => 1,
73                '{http://sabredav.org/ns}sync-token' => 1
74            )
75        );
76
77        $this->assertEquals($expected, $result);
78
79    }
80
81    public function testUpdateAddressBookNoProps() {
82
83        $propPatch = new PropPatch([
84        ]);
85
86        $this->backend->updateAddressBook(1, $propPatch);
87        $result = $propPatch->commit();
88        $this->assertTrue($result);
89
90        $result = $this->backend->getAddressBooksForUser('principals/user1');
91
92        $expected = array(
93            array(
94                'id' => 1,
95                'uri' => 'book1',
96                'principaluri' => 'principals/user1',
97                '{DAV:}displayname' => 'book1',
98                '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
99                '{http://calendarserver.org/ns/}getctag' => 1,
100                '{http://sabredav.org/ns}sync-token' => 1
101            )
102        );
103
104        $this->assertEquals($expected, $result);
105
106
107    }
108
109    public function testUpdateAddressBookSuccess() {
110
111        $propPatch = new PropPatch([
112            '{DAV:}displayname' => 'updated',
113            '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
114        ]);
115
116        $this->backend->updateAddressBook(1, $propPatch);
117        $result = $propPatch->commit();
118
119        $this->assertTrue($result);
120
121        $result = $this->backend->getAddressBooksForUser('principals/user1');
122
123        $expected = array(
124            array(
125                'id' => 1,
126                'uri' => 'book1',
127                'principaluri' => 'principals/user1',
128                '{DAV:}displayname' => 'updated',
129                '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
130                '{http://calendarserver.org/ns/}getctag' => 2,
131                '{http://sabredav.org/ns}sync-token' => 2
132            )
133        );
134
135        $this->assertEquals($expected, $result);
136
137
138    }
139
140    public function testDeleteAddressBook() {
141
142        $this->backend->deleteAddressBook(1);
143
144        $this->assertEquals(array(), $this->backend->getAddressBooksForUser('principals/user1'));
145
146    }
147
148    /**
149     * @expectedException Sabre\DAV\Exception\BadRequest
150     */
151    public function testCreateAddressBookUnsupportedProp() {
152
153        $this->backend->createAddressBook('principals/user1','book2', array(
154            '{DAV:}foo' => 'bar',
155        ));
156
157    }
158
159    public function testCreateAddressBookSuccess() {
160
161        $this->backend->createAddressBook('principals/user1','book2', array(
162            '{DAV:}displayname' => 'book2',
163            '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 2',
164        ));
165
166        $expected = array(
167            array(
168                'id' => 1,
169                'uri' => 'book1',
170                'principaluri' => 'principals/user1',
171                '{DAV:}displayname' => 'book1',
172                '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
173                '{http://calendarserver.org/ns/}getctag' => 1,
174                '{http://sabredav.org/ns}sync-token' => 1,
175            ),
176            array(
177                'id' => 2,
178                'uri' => 'book2',
179                'principaluri' => 'principals/user1',
180                '{DAV:}displayname' => 'book2',
181                '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 2',
182                '{http://calendarserver.org/ns/}getctag' => 1,
183                '{http://sabredav.org/ns}sync-token' => 1,
184            )
185        );
186        $result = $this->backend->getAddressBooksForUser('principals/user1');
187        $this->assertEquals($expected, $result);
188
189    }
190
191    public function testGetCards() {
192
193        $result = $this->backend->getCards(1);
194
195        $expected = array(
196            array(
197                'id' => 1,
198                'uri' => 'card1',
199                'lastmodified' => 0,
200                'etag' => '"' . md5('card1') . '"',
201                'size' => 5
202            )
203        );
204
205        $this->assertEquals($expected, $result);
206
207    }
208
209    public function testGetCard() {
210
211        $result = $this->backend->getCard(1,'card1');
212
213        $expected = array(
214            'id' => 1,
215            'uri' => 'card1',
216            'carddata' => 'card1',
217            'lastmodified' => 0,
218            'etag' => '"' . md5('card1') . '"',
219            'size' => 5
220        );
221
222        $this->assertEquals($expected, $result);
223
224    }
225
226    /**
227     * @depends testGetCard
228     */
229    public function testCreateCard() {
230
231        $result = $this->backend->createCard(1, 'card2', 'data2');
232        $this->assertEquals('"' . md5('data2') . '"', $result);
233        $result = $this->backend->getCard(1,'card2');
234        $this->assertEquals(2, $result['id']);
235        $this->assertEquals('card2', $result['uri']);
236        $this->assertEquals('data2', $result['carddata']);
237
238    }
239
240    /**
241     * @depends testCreateCard
242     */
243    public function testGetMultiple() {
244
245        $result = $this->backend->createCard(1, 'card2', 'data2');
246        $result = $this->backend->createCard(1, 'card3', 'data3');
247        $check = [
248            [
249                'id' => 1,
250                'uri' => 'card1',
251                'carddata' => 'card1',
252                'lastmodified' => 0,
253            ],
254            [
255                'id' => 2,
256                'uri' => 'card2',
257                'carddata' => 'data2',
258                'lastmodified' => time(),
259            ],
260            [
261                'id' => 3,
262                'uri' => 'card3',
263                'carddata' => 'data3',
264                'lastmodified' => time(),
265            ],
266        ];
267
268        $result = $this->backend->getMultipleCards(1, ['card1','card2','card3']);
269
270        foreach($check as $index=>$node) {
271
272            foreach($node as $k=>$v) {
273
274                if ($k!=='lastmodified') {
275                    $this->assertEquals($v, $result[$index][$k]);
276                } else {
277                    $this->assertTrue(isset($result[$index][$k]));
278                }
279
280            }
281
282        }
283
284
285    }
286
287    /**
288     * @depends testGetCard
289     */
290    public function testUpdateCard() {
291
292        $result = $this->backend->updateCard(1, 'card1', 'newdata');
293        $this->assertEquals('"' . md5('newdata') . '"', $result);
294
295        $result = $this->backend->getCard(1,'card1');
296        $this->assertEquals(1, $result['id']);
297        $this->assertEquals('newdata', $result['carddata']);
298
299    }
300
301    /**
302     * @depends testGetCard
303     */
304    public function testDeleteCard() {
305
306        $this->backend->deleteCard(1, 'card1');
307        $result = $this->backend->getCard(1,'card1');
308        $this->assertFalse($result);
309
310    }
311
312    function testGetChanges() {
313
314        $backend = $this->backend;
315        $id = $backend->createAddressBook(
316            'principals/user1',
317            'bla',
318            []
319        );
320        $result = $backend->getChangesForAddressBook($id, null, 1);
321
322        $this->assertEquals([
323            'syncToken' => 1,
324            "added"     => [],
325            'modified'  => [],
326            'deleted'   => [],
327        ], $result);
328
329        $currentToken = $result['syncToken'];
330
331        $dummyCard = "BEGIN:VCARD\r\nEND:VCARD\r\n";
332
333        $backend->createCard($id, "card1.ics", $dummyCard);
334        $backend->createCard($id, "card2.ics", $dummyCard);
335        $backend->createCard($id, "card3.ics", $dummyCard);
336        $backend->updateCard($id, "card1.ics", $dummyCard);
337        $backend->deleteCard($id, "card2.ics");
338
339        $result = $backend->getChangesForAddressBook($id, $currentToken, 1);
340
341        $this->assertEquals([
342            'syncToken' => 6,
343            'modified'  => ["card1.ics"],
344            'deleted'   => ["card2.ics"],
345            "added"     => ["card3.ics"],
346        ], $result);
347
348    }
349}
350
351