aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/sabre/dav/tests/Sabre/CardDAV/CardTest.php
blob: 1de10b7195f8e597f06280ff7cc296927e734241 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php

declare(strict_types=1);

namespace Sabre\CardDAV;

class CardTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var Sabre\CardDAV\Card
     */
    protected $card;
    /**
     * @var Sabre\CardDAV\MockBackend
     */
    protected $backend;

    public function setup(): void
    {
        $this->backend = new Backend\Mock();
        $this->card = new Card(
            $this->backend,
            [
                'uri' => 'book1',
                'id' => 'foo',
                'principaluri' => 'principals/user1',
            ],
            [
                'uri' => 'card1',
                'addressbookid' => 'foo',
                'carddata' => 'card',
            ]
        );
    }

    public function testGet()
    {
        $result = $this->card->get();
        $this->assertEquals('card', $result);
    }

    public function testGet2()
    {
        $this->card = new Card(
            $this->backend,
            [
                'uri' => 'book1',
                'id' => 'foo',
                'principaluri' => 'principals/user1',
            ],
            [
                'uri' => 'card1',
                'addressbookid' => 'foo',
            ]
        );
        $result = $this->card->get();
        $this->assertEquals("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD", $result);
    }

    /**
     * @depends testGet
     */
    public function testPut()
    {
        $file = fopen('php://memory', 'r+');
        fwrite($file, 'newdata');
        rewind($file);
        $this->card->put($file);
        $result = $this->card->get();
        $this->assertEquals('newdata', $result);
    }

    public function testDelete()
    {
        $this->card->delete();
        $this->assertEquals(1, count($this->backend->cards['foo']));
    }

    public function testGetContentType()
    {
        $this->assertEquals('text/vcard; charset=utf-8', $this->card->getContentType());
    }

    public function testGetETag()
    {
        $this->assertEquals('"'.md5('card').'"', $this->card->getETag());
    }

    public function testGetETag2()
    {
        $card = new Card(
            $this->backend,
            [
                'uri' => 'book1',
                'id' => 'foo',
                'principaluri' => 'principals/user1',
            ],
            [
                'uri' => 'card1',
                'addressbookid' => 'foo',
                'carddata' => 'card',
                'etag' => '"blabla"',
            ]
        );
        $this->assertEquals('"blabla"', $card->getETag());
    }

    public function testGetLastModified()
    {
        $this->assertEquals(null, $this->card->getLastModified());
    }

    public function testGetSize()
    {
        $this->assertEquals(4, $this->card->getSize());
        $this->assertEquals(4, $this->card->getSize());
    }

    public function testGetSize2()
    {
        $card = new Card(
            $this->backend,
            [
                'uri' => 'book1',
                'id' => 'foo',
                'principaluri' => 'principals/user1',
            ],
            [
                'uri' => 'card1',
                'addressbookid' => 'foo',
                'etag' => '"blabla"',
                'size' => 4,
            ]
        );
        $this->assertEquals(4, $card->getSize());
    }

    public function testACLMethods()
    {
        $this->assertEquals('principals/user1', $this->card->getOwner());
        $this->assertNull($this->card->getGroup());
        $this->assertEquals([
            [
                'privilege' => '{DAV:}all',
                'principal' => 'principals/user1',
                'protected' => true,
            ],
        ], $this->card->getACL());
    }

    public function testOverrideACL()
    {
        $card = new Card(
            $this->backend,
            [
                'uri' => 'book1',
                'id' => 'foo',
                'principaluri' => 'principals/user1',
            ],
            [
                'uri' => 'card1',
                'addressbookid' => 'foo',
                'carddata' => 'card',
                'acl' => [
                    [
                        'privilege' => '{DAV:}read',
                        'principal' => 'principals/user1',
                        'protected' => true,
                    ],
                ],
            ]
        );
        $this->assertEquals([
            [
                'privilege' => '{DAV:}read',
                'principal' => 'principals/user1',
                'protected' => true,
            ],
        ], $card->getACL());
    }

    public function testSetACL()
    {
        $this->expectException('Sabre\DAV\Exception\Forbidden');
        $this->card->setACL([]);
    }

    public function testGetSupportedPrivilegeSet()
    {
        $this->assertNull(
            $this->card->getSupportedPrivilegeSet()
        );
    }
}