aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/sabre/dav/tests/Sabre/DAV/Property/HrefTest.php
blob: e5607f51b1c41068fe40054abba7a34415fa0c6a (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
<?php

namespace Sabre\DAV\Property;

use Sabre\DAV;

class HrefTest extends \PHPUnit_Framework_TestCase {

    function testConstruct() {

        $href = new Href('path');
        $this->assertEquals('path',$href->getHref());

    }

    function testSerialize() {

        $href = new Href('path');
        $this->assertEquals('path',$href->getHref());

        $doc = new \DOMDocument();
        $root = $doc->createElement('d:anything');
        $root->setAttribute('xmlns:d','DAV:');

        $doc->appendChild($root);
        $server = new DAV\Server();
        $server->setBaseUri('/bla/');

        $href->serialize($server, $root);

        $xml = $doc->saveXML();

        $this->assertEquals(
'<?xml version="1.0"?>
<d:anything xmlns:d="DAV:"><d:href>/bla/path</d:href></d:anything>
', $xml);

    }

    function testSerializeNoPrefix() {

        $href = new Href('path',false);
        $this->assertEquals('path',$href->getHref());

        $doc = new \DOMDocument();
        $root = $doc->createElement('d:anything');
        $root->setAttribute('xmlns:d','DAV:');

        $doc->appendChild($root);
        $server = new DAV\Server();
        $server->setBaseUri('/bla/');

        $href->serialize($server, $root);

        $xml = $doc->saveXML();

        $this->assertEquals(
'<?xml version="1.0"?>
<d:anything xmlns:d="DAV:"><d:href>path</d:href></d:anything>
', $xml);

    }

    function testUnserialize() {

        $xml = '<?xml version="1.0"?>
<d:anything xmlns:d="urn:DAV"><d:href>/bla/path</d:href></d:anything>
';

        $dom = new \DOMDocument();
        $dom->loadXML($xml);

        $href = Href::unserialize($dom->firstChild);
        $this->assertEquals('/bla/path',$href->getHref());

    }

    function testUnserializeIncompatible() {

        $xml = '<?xml version="1.0"?>
<d:anything xmlns:d="urn:DAV"><d:href2>/bla/path</d:href2></d:anything>
';

        $dom = new \DOMDocument();
        $dom->loadXML($xml);

        $href = Href::unserialize($dom->firstChild);
        $this->assertNull($href);

    }

    /**
     * This method tests if hrefs containing & are correctly encoded.
     */
    function testSerializeEntity() {

        $href = new Href('http://example.org/?a&b', false);
        $this->assertEquals('http://example.org/?a&b',$href->getHref());

        $doc = new \DOMDocument();
        $root = $doc->createElement('d:anything');
        $root->setAttribute('xmlns:d','DAV:');

        $doc->appendChild($root);
        $server = new DAV\Server();
        $server->setBaseUri('/bla/');

        $href->serialize($server, $root);

        $xml = $doc->saveXML();

        $this->assertEquals(
'<?xml version="1.0"?>
<d:anything xmlns:d="DAV:"><d:href>http://example.org/?a&amp;b</d:href></d:anything>
', $xml);

    }

}