1<?php
2
3namespace Sabre\DAV\Xml\Property;
4
5use Sabre\DAV;
6use Sabre\DAV\Browser\HtmlOutputHelper;
7use Sabre\DAV\Xml\XmlTest;
8
9class HrefTest extends XmlTest {
10
11    function testConstruct() {
12
13        $href = new Href('path');
14        $this->assertEquals('path', $href->getHref());
15
16    }
17
18    function testSerialize() {
19
20        $href = new Href('path');
21        $this->assertEquals('path', $href->getHref());
22
23        $this->contextUri = '/bla/';
24
25        $xml = $this->write(['{DAV:}anything' => $href]);
26
27        $this->assertXmlStringEqualsXmlString(
28'<?xml version="1.0"?>
29<d:anything xmlns:d="DAV:"><d:href>/bla/path</d:href></d:anything>
30', $xml);
31
32    }
33
34    function testSerializeNoPrefix() {
35
36        $href = new Href('path', false);
37        $this->assertEquals('path', $href->getHref());
38
39        $xml = $this->write(['{DAV:}anything' => $href]);
40
41        $this->assertXmlStringEqualsXmlString(
42'<?xml version="1.0"?>
43<d:anything xmlns:d="DAV:"><d:href>path</d:href></d:anything>
44', $xml);
45
46    }
47
48    function testUnserialize() {
49
50        $xml = '<?xml version="1.0"?>
51<d:anything xmlns:d="DAV:"><d:href>/bla/path</d:href></d:anything>
52';
53
54        $result = $this->parse($xml, ['{DAV:}anything' => 'Sabre\\DAV\\Xml\\Property\\Href']);
55
56        $href = $result['value'];
57
58        $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $href);
59
60        $this->assertEquals('/bla/path', $href->getHref());
61
62    }
63
64    function testUnserializeIncompatible() {
65
66        $xml = '<?xml version="1.0"?>
67<d:anything xmlns:d="DAV:"><d:href2>/bla/path</d:href2></d:anything>
68';
69        $result = $this->parse($xml, ['{DAV:}anything' => 'Sabre\\DAV\\Xml\\Property\\Href']);
70        $href = $result['value'];
71        $this->assertNull($href);
72
73    }
74    function testUnserializeEmpty() {
75
76        $xml = '<?xml version="1.0"?>
77<d:anything xmlns:d="DAV:"></d:anything>
78';
79        $result = $this->parse($xml, ['{DAV:}anything' => 'Sabre\\DAV\\Xml\\Property\\Href']);
80        $href = $result['value'];
81        $this->assertNull($href);
82
83    }
84
85    /**
86     * This method tests if hrefs containing & are correctly encoded.
87     */
88    function testSerializeEntity() {
89
90        $href = new Href('http://example.org/?a&b', false);
91        $this->assertEquals('http://example.org/?a&b', $href->getHref());
92
93        $xml = $this->write(['{DAV:}anything' => $href]);
94
95        $this->assertXmlStringEqualsXmlString(
96'<?xml version="1.0"?>
97<d:anything xmlns:d="DAV:"><d:href>http://example.org/?a&amp;b</d:href></d:anything>
98', $xml);
99
100    }
101
102    function testToHtml() {
103
104        $href = new Href([
105            '/foo/bar',
106            'foo/bar',
107            'http://example.org/bar'
108        ]);
109
110        $html = new HtmlOutputHelper(
111            '/base/',
112            []
113        );
114
115        $expected =
116            '<a href="/foo/bar">/foo/bar</a><br />' .
117            '<a href="/base/foo/bar">/base/foo/bar</a><br />' .
118            '<a href="http://example.org/bar">http://example.org/bar</a>';
119        $this->assertEquals($expected, $href->toHtml($html));
120
121    }
122
123}
124