1<?php 2 3namespace Sabre\DAVACL\Xml\Property; 4 5use Sabre\DAV; 6use Sabre\DAV\Browser\HtmlOutputHelper; 7use Sabre\HTTP; 8use Sabre\Xml\Reader; 9 10class PrincipalTest extends \PHPUnit_Framework_TestCase { 11 12 function testSimple() { 13 14 $principal = new Principal(Principal::UNAUTHENTICATED); 15 $this->assertEquals(Principal::UNAUTHENTICATED, $principal->getType()); 16 $this->assertNull($principal->getHref()); 17 18 $principal = new Principal(Principal::AUTHENTICATED); 19 $this->assertEquals(Principal::AUTHENTICATED, $principal->getType()); 20 $this->assertNull($principal->getHref()); 21 22 $principal = new Principal(Principal::HREF, 'admin'); 23 $this->assertEquals(Principal::HREF, $principal->getType()); 24 $this->assertEquals('admin/', $principal->getHref()); 25 26 } 27 28 /** 29 * @depends testSimple 30 * @expectedException Sabre\DAV\Exception 31 */ 32 function testNoHref() { 33 34 $principal = new Principal(Principal::HREF); 35 36 } 37 38 /** 39 * @depends testSimple 40 */ 41 function testSerializeUnAuthenticated() { 42 43 $prin = new Principal(Principal::UNAUTHENTICATED); 44 45 $xml = (new DAV\Server())->xml->write('{DAV:}principal', $prin); 46 47 $this->assertXmlStringEqualsXmlString(' 48<d:principal xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns"> 49<d:unauthenticated/> 50</d:principal>', $xml); 51 52 } 53 54 55 /** 56 * @depends testSerializeUnAuthenticated 57 */ 58 function testSerializeAuthenticated() { 59 60 $prin = new Principal(Principal::AUTHENTICATED); 61 $xml = (new DAV\Server())->xml->write('{DAV:}principal', $prin); 62 63 $this->assertXmlStringEqualsXmlString(' 64<d:principal xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns"> 65<d:authenticated/> 66</d:principal>', $xml); 67 68 } 69 70 71 /** 72 * @depends testSerializeUnAuthenticated 73 */ 74 function testSerializeHref() { 75 76 $prin = new Principal(Principal::HREF, 'principals/admin'); 77 $xml = (new DAV\Server())->xml->write('{DAV:}principal', $prin, '/'); 78 79 $this->assertXmlStringEqualsXmlString(' 80<d:principal xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns"> 81<d:href>/principals/admin/</d:href> 82</d:principal>', $xml); 83 84 } 85 86 function testUnserializeHref() { 87 88 $xml = '<?xml version="1.0"?> 89<d:principal xmlns:d="DAV:">' . 90'<d:href>/principals/admin</d:href>' . 91'</d:principal>'; 92 93 $principal = $this->parse($xml); 94 $this->assertEquals(Principal::HREF, $principal->getType()); 95 $this->assertEquals('/principals/admin/', $principal->getHref()); 96 97 } 98 99 function testUnserializeAuthenticated() { 100 101 $xml = '<?xml version="1.0"?> 102<d:principal xmlns:d="DAV:">' . 103' <d:authenticated />' . 104'</d:principal>'; 105 106 $principal = $this->parse($xml); 107 $this->assertEquals(Principal::AUTHENTICATED, $principal->getType()); 108 109 } 110 111 function testUnserializeUnauthenticated() { 112 113 $xml = '<?xml version="1.0"?> 114<d:principal xmlns:d="DAV:">' . 115' <d:unauthenticated />' . 116'</d:principal>'; 117 118 $principal = $this->parse($xml); 119 $this->assertEquals(Principal::UNAUTHENTICATED, $principal->getType()); 120 121 } 122 123 /** 124 * @expectedException Sabre\DAV\Exception\BadRequest 125 */ 126 function testUnserializeUnknown() { 127 128 $xml = '<?xml version="1.0"?> 129<d:principal xmlns:d="DAV:">' . 130' <d:foo />' . 131'</d:principal>'; 132 133 $this->parse($xml); 134 135 } 136 137 function parse($xml) { 138 139 $reader = new Reader(); 140 $reader->elementMap['{DAV:}principal'] = 'Sabre\\DAVACL\\Xml\\Property\\Principal'; 141 $reader->xml($xml); 142 $result = $reader->parse(); 143 return $result['value']; 144 145 } 146 147 /** 148 * @depends testSimple 149 * @dataProvider htmlProvider 150 */ 151 function testToHtml($principal, $output) { 152 153 $html = $principal->toHtml(new HtmlOutputHelper('/', [])); 154 155 $this->assertXmlStringEqualsXmlString( 156 $output, 157 $html 158 ); 159 160 } 161 162 /** 163 * Provides data for the html tests 164 * 165 * @return array 166 */ 167 function htmlProvider() { 168 169 return [ 170 [ 171 new Principal(Principal::UNAUTHENTICATED), 172 '<em>unauthenticated</em>', 173 ], 174 [ 175 new Principal(Principal::AUTHENTICATED), 176 '<em>authenticated</em>', 177 ], 178 [ 179 new Principal(Principal::ALL), 180 '<em>all</em>', 181 ], 182 [ 183 new Principal(Principal::HREF, 'principals/admin'), 184 '<a href="/principals/admin/">/principals/admin/</a>', 185 ], 186 187 ]; 188 189 } 190 191} 192