1<?php
2
3namespace Sabre\CardDAV;
4
5class CardTest extends \PHPUnit_Framework_TestCase {
6
7    /**
8     * @var Sabre\CardDAV\Card
9     */
10    protected $card;
11    /**
12     * @var Sabre\CardDAV\MockBackend
13     */
14    protected $backend;
15
16    function setUp() {
17
18        $this->backend = new Backend\Mock();
19        $this->card = new Card(
20            $this->backend,
21            array(
22                'uri' => 'book1',
23                'id' => 'foo',
24                'principaluri' => 'principals/user1',
25            ),
26            array(
27                'uri' => 'card1',
28                'addressbookid' => 'foo',
29                'carddata' => 'card',
30            )
31        );
32
33    }
34
35    function testGet() {
36
37        $result = $this->card->get();
38        $this->assertEquals('card', $result);
39
40    }
41    function testGet2() {
42
43        $this->card = new Card(
44            $this->backend,
45            array(
46                'uri' => 'book1',
47                'id' => 'foo',
48                'principaluri' => 'principals/user1',
49            ),
50            array(
51                'uri' => 'card1',
52                'addressbookid' => 'foo',
53            )
54        );
55        $result = $this->card->get();
56        $this->assertEquals("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD", $result);
57
58    }
59
60
61    /**
62     * @depends testGet
63     */
64    function testPut() {
65
66        $file = fopen('php://memory','r+');
67        fwrite($file, 'newdata');
68        rewind($file);
69        $this->card->put($file);
70        $result = $this->card->get();
71        $this->assertEquals('newdata', $result);
72
73    }
74
75
76    function testDelete() {
77
78        $this->card->delete();
79        $this->assertEquals(1, count($this->backend->cards['foo']));
80
81    }
82
83    function testGetContentType() {
84
85        $this->assertEquals('text/vcard; charset=utf-8', $this->card->getContentType());
86
87    }
88
89    function testGetETag() {
90
91        $this->assertEquals('"' . md5('card') . '"' , $this->card->getETag());
92
93    }
94
95    function testGetETag2() {
96
97        $card = new Card(
98            $this->backend,
99            array(
100                'uri' => 'book1',
101                'id' => 'foo',
102                'principaluri' => 'principals/user1',
103            ),
104            array(
105                'uri' => 'card1',
106                'addressbookid' => 'foo',
107                'carddata' => 'card',
108                'etag' => '"blabla"',
109            )
110        );
111        $this->assertEquals('"blabla"' , $card->getETag());
112
113    }
114
115    function testGetLastModified() {
116
117        $this->assertEquals(null, $this->card->getLastModified());
118
119    }
120
121    function testGetSize() {
122
123        $this->assertEquals(4, $this->card->getSize());
124        $this->assertEquals(4, $this->card->getSize());
125
126    }
127
128    function testGetSize2() {
129
130        $card = new Card(
131            $this->backend,
132            array(
133                'uri' => 'book1',
134                'id' => 'foo',
135                'principaluri' => 'principals/user1',
136            ),
137            array(
138                'uri' => 'card1',
139                'addressbookid' => 'foo',
140                'etag' => '"blabla"',
141                'size' => 4,
142            )
143        );
144        $this->assertEquals(4, $card->getSize());
145
146    }
147
148    function testACLMethods() {
149
150        $this->assertEquals('principals/user1', $this->card->getOwner());
151        $this->assertNull($this->card->getGroup());
152        $this->assertEquals(array(
153            array(
154                'privilege' => '{DAV:}read',
155                'principal' => 'principals/user1',
156                'protected' => true,
157            ),
158            array(
159                'privilege' => '{DAV:}write',
160                'principal' => 'principals/user1',
161                'protected' => true,
162            ),
163        ), $this->card->getACL());
164
165    }
166    function testOverrideACL() {
167
168        $card = new Card(
169            $this->backend,
170            array(
171                'uri' => 'book1',
172                'id' => 'foo',
173                'principaluri' => 'principals/user1',
174            ),
175            array(
176                'uri' => 'card1',
177                'addressbookid' => 'foo',
178                'carddata' => 'card',
179                'acl' => array(
180                    array(
181                        'privilege' => '{DAV:}read',
182                        'principal' => 'principals/user1',
183                        'protected' => true,
184                    ),
185                ),
186            )
187        );
188        $this->assertEquals(array(
189            array(
190                'privilege' => '{DAV:}read',
191                'principal' => 'principals/user1',
192                'protected' => true,
193            ),
194        ), $card->getACL());
195
196    }
197
198    /**
199     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
200     */
201    function testSetACL() {
202
203       $this->card->setACL(array());
204
205    }
206
207    function testGetSupportedPrivilegeSet() {
208
209        $this->assertNull(
210            $this->card->getSupportedPrivilegeSet()
211        );
212
213    }
214
215}
216