aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/sabre/dav/tests/Sabre/CardDAV/VCFExportTest.php
blob: 48477d7f25884296a08adfb176593ffd0fee4fe9 (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
<?php

declare(strict_types=1);

namespace Sabre\CardDAV;

use Sabre\HTTP;

class VCFExportTest extends \Sabre\DAVServerTest
{
    protected $setupCardDAV = true;
    protected $autoLogin = 'user1';
    protected $setupACL = true;

    protected $carddavAddressBooks = [
        [
            'id' => 'book1',
            'uri' => 'book1',
            'principaluri' => 'principals/user1',
        ],
    ];
    protected $carddavCards = [
        'book1' => [
            'card1' => "BEGIN:VCARD\r\nFN:Person1\r\nEND:VCARD\r\n",
            'card2' => "BEGIN:VCARD\r\nFN:Person2\r\nEND:VCARD",
            'card3' => "BEGIN:VCARD\r\nFN:Person3\r\nEND:VCARD\r\n",
            'card4' => "BEGIN:VCARD\nFN:Person4\nEND:VCARD\n",
        ],
    ];

    public function setUp()
    {
        parent::setUp();
        $plugin = new VCFExportPlugin();
        $this->server->addPlugin(
            $plugin
        );
    }

    public function testSimple()
    {
        $plugin = $this->server->getPlugin('vcf-export');
        $this->assertInstanceOf('Sabre\\CardDAV\\VCFExportPlugin', $plugin);

        $this->assertEquals(
            'vcf-export',
            $plugin->getPluginInfo()['name']
        );
    }

    public function testExport()
    {
        $request = HTTP\Sapi::createFromServerArray([
            'REQUEST_URI' => '/addressbooks/user1/book1?export',
            'QUERY_STRING' => 'export',
            'REQUEST_METHOD' => 'GET',
        ]);

        $response = $this->request($request);
        $this->assertEquals(200, $response->status, $response->getBodyAsString());

        $expected = 'BEGIN:VCARD
FN:Person1
END:VCARD
BEGIN:VCARD
FN:Person2
END:VCARD
BEGIN:VCARD
FN:Person3
END:VCARD
BEGIN:VCARD
FN:Person4
END:VCARD
';
        // We actually expected windows line endings
        $expected = str_replace("\n", "\r\n", $expected);

        $this->assertEquals($expected, $response->getBodyAsString());
    }

    public function testBrowserIntegration()
    {
        $plugin = $this->server->getPlugin('vcf-export');
        $actions = '';
        $addressbook = new AddressBook($this->carddavBackend, []);
        $this->server->emit('browserButtonActions', ['/foo', $addressbook, &$actions]);
        $this->assertContains('/foo?export', $actions);
    }

    public function testContentDisposition()
    {
        $request = new HTTP\Request(
            'GET',
            '/addressbooks/user1/book1?export'
        );

        $response = $this->request($request, 200);
        $this->assertEquals('text/directory', $response->getHeader('Content-Type'));
        $this->assertEquals(
            'attachment; filename="book1-'.date('Y-m-d').'.vcf"',
            $response->getHeader('Content-Disposition')
        );
    }

    public function testContentDispositionBadChars()
    {
        $this->carddavBackend->createAddressBook(
            'principals/user1',
            'book-b_ad"(ch)ars',
            []
        );
        $this->carddavBackend->createCard(
            'book-b_ad"(ch)ars',
            'card1',
            "BEGIN:VCARD\r\nFN:Person1\r\nEND:VCARD\r\n"
        );

        $request = new HTTP\Request(
            'GET',
            '/addressbooks/user1/book-b_ad"(ch)ars?export'
        );

        $response = $this->request($request, 200);
        $this->assertEquals('text/directory', $response->getHeader('Content-Type'));
        $this->assertEquals(
            'attachment; filename="book-b_adchars-'.date('Y-m-d').'.vcf"',
            $response->getHeader('Content-Disposition')
        );
    }
}