b41e19ccb1bda588d464117921351928c90d8497
[tine20] / tests / tine20 / Calendar / Frontend / CalDAV / ProxyTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Calendar
6  * @subpackage  Frontend
7  * @license     http://www.gnu.org/licenses/agpl.html
8  * @copyright   Copyright (c) 2014-2014 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Lars Kneschke <l.kneschke@metaways.de>
10  */
11
12 /**
13  * Test helper
14  */
15 require_once __DIR__ . '/../../../../../tine20/vendor/sabre/dav/tests/Sabre/HTTP/ResponseMock.php';
16
17 /**
18  * Test class for Calendar_Frontend_CalDAV_ProxyTest
19  */
20 class Calendar_Frontend_CalDAV_ProxyTest extends TestCase
21 {
22     /**
23      * 
24      * @var Sabre\DAV\Server
25      */
26     protected $server;
27     
28     /**
29      * Sets up the fixture.
30      * This method is called before a test is executed.
31      *
32      * @access protected
33      */
34     protected function setUp()
35     {
36         parent::setUp();
37         
38         $this->server = new Sabre\DAV\Server(new Tinebase_WebDav_Root());
39         $this->server->debugExceptions = true;
40         $this->server->addPlugin(new \Sabre\CalDAV\Plugin());
41         $this->server->addPlugin(new \Sabre\CalDAV\SharingPlugin());
42         
43         $aclPlugin = new \Sabre\DAVACL\Plugin();
44         $aclPlugin->defaultUsernamePath    = Tinebase_WebDav_PrincipalBackend::PREFIX_USERS;
45         $aclPlugin->principalCollectionSet = array (Tinebase_WebDav_PrincipalBackend::PREFIX_USERS, Tinebase_WebDav_PrincipalBackend::PREFIX_GROUPS);
46         $this->server->addPlugin($aclPlugin);
47         
48         $this->response = new Sabre\HTTP\ResponseMock();
49         $this->server->httpResponse = $this->response;
50     }
51
52     /**
53      * test testGetProperties method
54      */
55     public function testGetProperties()
56     {
57         $body = '<?xml version="1.0" encoding="UTF-8"?>
58             <A:expand-property xmlns:A="DAV:">
59                 <A:property name="calendar-proxy-read-for" namespace="http://calendarserver.org/ns/">
60                     <A:property name="email-address-set" namespace="http://calendarserver.org/ns/"/>
61                     <A:property name="displayname" namespace="DAV:"/>
62                     <A:property name="calendar-user-address-set" namespace="urn:ietf:params:xml:ns:caldav"/>
63                 </A:property>
64                 <A:property name="calendar-proxy-write-for" namespace="http://calendarserver.org/ns/">
65                     <A:property name="email-address-set" namespace="http://calendarserver.org/ns/"/>
66                     <A:property name="displayname" namespace="DAV:"/>
67                     <A:property name="calendar-user-address-set" namespace="urn:ietf:params:xml:ns:caldav"/>
68                 </A:property>
69             </A:expand-property>';
70
71         $request = new Sabre\HTTP\Request(array(
72             'REQUEST_METHOD' => 'REPORT',
73             'REQUEST_URI'    => '/' . Tinebase_WebDav_PrincipalBackend::PREFIX_USERS . '/' . Tinebase_Core::getUser()->contact_id
74         ));
75         $request->setBody($body);
76
77         $this->server->httpRequest = $request;
78         $this->server->exec();
79         //var_dump($this->response->body);
80         
81         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
82         
83         $responseDoc = new DOMDocument();
84         $responseDoc->loadXML($this->response->body);
85         //$responseDoc->formatOutput = true; echo $responseDoc->saveXML();
86         $xpath = new DomXPath($responseDoc);
87         $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
88         $xpath->registerNamespace('cs',  'http://calendarserver.org/ns/');
89         
90         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cs:calendar-proxy-read-for');
91         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
92         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
93         
94         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cs:calendar-proxy-write-for');
95         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
96         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
97     }
98     
99     /**
100      * test testGetProperties method
101      */
102     public function testGetPropertiesSharedUser()
103     {
104         $body = '<?xml version="1.0" encoding="UTF-8"?>
105             <A:expand-property xmlns:A="DAV:">
106                 <A:property name="calendar-proxy-read-for" namespace="http://calendarserver.org/ns/">
107                     <A:property name="email-address-set" namespace="http://calendarserver.org/ns/"/>
108                     <A:property name="displayname" namespace="DAV:"/>
109                     <A:property name="calendar-user-address-set" namespace="urn:ietf:params:xml:ns:caldav"/>
110                 </A:property>
111                 <A:property name="calendar-proxy-write-for" namespace="http://calendarserver.org/ns/">
112                     <A:property name="email-address-set" namespace="http://calendarserver.org/ns/"/>
113                     <A:property name="displayname" namespace="DAV:"/>
114                     <A:property name="calendar-user-address-set" namespace="urn:ietf:params:xml:ns:caldav"/>
115                 </A:property>
116             </A:expand-property>';
117
118         $request = new Sabre\HTTP\Request(array(
119             'REQUEST_METHOD' => 'REPORT',
120             'REQUEST_URI'    => '/' . Tinebase_WebDav_PrincipalBackend::PREFIX_USERS . '/' . Tinebase_WebDav_PrincipalBackend::SHARED
121         ));
122         $request->setBody($body);
123
124         $this->server->httpRequest = $request;
125         $this->server->exec();
126         //var_dump($this->response->body);
127         
128         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
129         
130         $responseDoc = new DOMDocument();
131         $responseDoc->loadXML($this->response->body);
132         //$responseDoc->formatOutput = true; echo $responseDoc->saveXML();
133         $xpath = new DomXPath($responseDoc);
134         $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
135         $xpath->registerNamespace('cs',  'http://calendarserver.org/ns/');
136         
137         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cs:calendar-proxy-read-for');
138         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
139         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
140         
141         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cs:calendar-proxy-write-for');
142         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
143         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
144     }
145     
146     /**
147      * test testGetProperties method
148      */
149     public function testGetPropertiesSharedUserPrincipal()
150     {
151         $body = '<?xml version="1.0" encoding="UTF-8"?>
152             <A:propfind xmlns:A="DAV:">
153               <A:prop>
154                 <B:calendar-home-set xmlns:B="urn:ietf:params:xml:ns:caldav"/>
155                 <B:calendar-user-address-set xmlns:B="urn:ietf:params:xml:ns:caldav"/>
156                 <A:current-user-principal/>
157                 <A:displayname/>
158                 <C:dropbox-home-URL xmlns:C="http://calendarserver.org/ns/"/>
159                 <C:email-address-set xmlns:C="http://calendarserver.org/ns/"/>
160                 <C:notification-URL xmlns:C="http://calendarserver.org/ns/"/>
161                 <A:principal-collection-set/>
162                 <A:principal-URL/>
163                 <A:resource-id/>
164                 <B:schedule-inbox-URL xmlns:B="urn:ietf:params:xml:ns:caldav"/>
165                 <B:schedule-outbox-URL xmlns:B="urn:ietf:params:xml:ns:caldav"/>
166                 <A:supported-report-set/>
167               </A:prop>
168             </A:propfind>';
169
170         $request = new Sabre\HTTP\Request(array(
171             'REQUEST_METHOD' => 'PROPFIND',
172             'REQUEST_URI'    => '/' . Tinebase_WebDav_PrincipalBackend::PREFIX_USERS . '/' . Tinebase_WebDav_PrincipalBackend::SHARED,
173             'HTTP_BRIEF'     => 't',
174             'HTTP_DEPTH'     => '0'
175         ));
176         $request->setBody($body);
177
178         $this->server->httpRequest = $request;
179         $this->server->exec();
180         //var_dump($this->response->body);
181         
182         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
183         
184         $responseDoc = new DOMDocument();
185         $responseDoc->loadXML($this->response->body);
186         //$responseDoc->formatOutput = true; echo $responseDoc->saveXML();
187         $xpath = new DomXPath($responseDoc);
188         $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
189         $xpath->registerNamespace('cs',  'http://calendarserver.org/ns/');
190         $xpath->registerNamespace('d',  'DAV');
191         
192         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cal:calendar-home-set');
193         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
194         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
195         
196         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/d:principal-URL');
197         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
198         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
199     }
200 }