0011172: optimize getGroupmemberships in Principalbackend
[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         // create shared folder and other users folder
39         $this->sharedContainer = Tinebase_Container::getInstance()->addContainer(new Tinebase_Model_Container(array(
40             'name'           => __CLASS__ . Tinebase_Record_Abstract::generateUID(),
41             'type'           => Tinebase_Model_Container::TYPE_SHARED,
42             'application_id' => Tinebase_Application::getInstance()->getApplicationByName('Calendar')->getId(),
43             'backend'        => 'Sql'
44         )));
45
46         $sclever = Tinebase_Helper::array_value('sclever', Zend_Registry::get('personas'));
47         $this->otherUsersContainer = Tinebase_Container::getInstance()->addContainer(new Tinebase_Model_Container(array(
48             'name'           => __CLASS__ . Tinebase_Record_Abstract::generateUID(),
49             'type'           => Tinebase_Model_Container::TYPE_PERSONAL,
50             'owner_id'       => $sclever->getId(),
51             'application_id' => Tinebase_Application::getInstance()->getApplicationByName('Calendar')->getId(),
52             'backend'        => 'Sql'
53         )));
54         Tinebase_Container::getInstance()->addGrants($this->otherUsersContainer, Tinebase_Acl_Rights::ACCOUNT_TYPE_USER, Tinebase_Core::getUser(), array(
55             Tinebase_Model_Grants::GRANT_READ,
56             Tinebase_Model_Grants::GRANT_SYNC,
57         ), true);
58
59         // clear container caches (brute force)
60         Tinebase_Core::getCache()->clean(Zend_Cache::CLEANING_MODE_ALL);
61
62         $this->server = new Sabre\DAV\Server(new Tinebase_WebDav_Root());
63         $this->server->debugExceptions = true;
64         $this->server->addPlugin(new \Sabre\CalDAV\Plugin());
65         $this->server->addPlugin(new \Sabre\CalDAV\SharingPlugin());
66         
67         $aclPlugin = new \Sabre\DAVACL\Plugin();
68         $aclPlugin->defaultUsernamePath    = Tinebase_WebDav_PrincipalBackend::PREFIX_USERS;
69         $aclPlugin->principalCollectionSet = array (Tinebase_WebDav_PrincipalBackend::PREFIX_USERS, Tinebase_WebDav_PrincipalBackend::PREFIX_GROUPS);
70         $this->server->addPlugin($aclPlugin);
71         
72         $this->response = new Sabre\HTTP\ResponseMock();
73         $this->server->httpResponse = $this->response;
74     }
75
76     /**
77      * test testGetProperties method
78      */
79     public function testGetProperties()
80     {
81         $body = '<?xml version="1.0" encoding="UTF-8"?>
82             <A:expand-property xmlns:A="DAV:">
83                 <A:property name="calendar-proxy-read-for" namespace="http://calendarserver.org/ns/">
84                     <A:property name="email-address-set" namespace="http://calendarserver.org/ns/"/>
85                     <A:property name="displayname" namespace="DAV:"/>
86                     <A:property name="calendar-user-address-set" namespace="urn:ietf:params:xml:ns:caldav"/>
87                 </A:property>
88                 <A:property name="calendar-proxy-write-for" namespace="http://calendarserver.org/ns/">
89                     <A:property name="email-address-set" namespace="http://calendarserver.org/ns/"/>
90                     <A:property name="displayname" namespace="DAV:"/>
91                     <A:property name="calendar-user-address-set" namespace="urn:ietf:params:xml:ns:caldav"/>
92                 </A:property>
93             </A:expand-property>';
94
95         $request = new Sabre\HTTP\Request(array(
96             'REQUEST_METHOD' => 'REPORT',
97             'REQUEST_URI'    => '/' . Tinebase_WebDav_PrincipalBackend::PREFIX_USERS . '/' . Tinebase_Core::getUser()->contact_id
98         ));
99         $request->setBody($body);
100
101         $this->server->httpRequest = $request;
102         $this->server->exec();
103         //var_dump($this->response->body);
104
105         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
106         
107         $responseDoc = new DOMDocument();
108         $responseDoc->loadXML($this->response->body);
109         //$responseDoc->formatOutput = true; echo $responseDoc->saveXML();
110         $xpath = new DomXPath($responseDoc);
111         $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
112         $xpath->registerNamespace('cs',  'http://calendarserver.org/ns/');
113         
114         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cs:calendar-proxy-read-for');
115         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
116         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
117         
118         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cs:calendar-proxy-write-for');
119         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
120         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
121
122         $nodes = $xpath->query('///d:response/d:href[text()="/principals/users/shared/"]');
123         $this->assertEquals(1, $nodes->length, "shared principal is missing in \n" . $responseDoc->saveXML());
124
125         $sclever = Tinebase_Helper::array_value('sclever', Zend_Registry::get('personas'));
126         $nodes = $xpath->query('///d:response/d:href[text()="/principals/users/'. $sclever->contact_id .'/"]');
127         $this->assertEquals(1, $nodes->length, "sclevers principal is missing in \n" .$responseDoc->saveXML());
128     }
129     
130     /**
131      * test testGetProperties method
132      */
133     public function testGetPropertiesSharedUser()
134     {
135         $body = '<?xml version="1.0" encoding="UTF-8"?>
136             <A:expand-property xmlns:A="DAV:">
137                 <A:property name="calendar-proxy-read-for" namespace="http://calendarserver.org/ns/">
138                     <A:property name="email-address-set" namespace="http://calendarserver.org/ns/"/>
139                     <A:property name="displayname" namespace="DAV:"/>
140                     <A:property name="calendar-user-address-set" namespace="urn:ietf:params:xml:ns:caldav"/>
141                 </A:property>
142                 <A:property name="calendar-proxy-write-for" namespace="http://calendarserver.org/ns/">
143                     <A:property name="email-address-set" namespace="http://calendarserver.org/ns/"/>
144                     <A:property name="displayname" namespace="DAV:"/>
145                     <A:property name="calendar-user-address-set" namespace="urn:ietf:params:xml:ns:caldav"/>
146                 </A:property>
147             </A:expand-property>';
148
149         $request = new Sabre\HTTP\Request(array(
150             'REQUEST_METHOD' => 'REPORT',
151             'REQUEST_URI'    => '/' . Tinebase_WebDav_PrincipalBackend::PREFIX_USERS . '/' . Tinebase_WebDav_PrincipalBackend::SHARED
152         ));
153         $request->setBody($body);
154
155         $this->server->httpRequest = $request;
156         $this->server->exec();
157         //var_dump($this->response->body);
158         
159         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
160         
161         $responseDoc = new DOMDocument();
162         $responseDoc->loadXML($this->response->body);
163         //$responseDoc->formatOutput = true; echo $responseDoc->saveXML();
164         $xpath = new DomXPath($responseDoc);
165         $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
166         $xpath->registerNamespace('cs',  'http://calendarserver.org/ns/');
167         
168         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cs:calendar-proxy-read-for');
169         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
170         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
171         
172         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cs:calendar-proxy-write-for');
173         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
174         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
175     }
176     
177     /**
178      * test testGetProperties method
179      */
180     public function testGetPropertiesSharedUserPrincipal()
181     {
182         $body = '<?xml version="1.0" encoding="UTF-8"?>
183             <A:propfind xmlns:A="DAV:">
184               <A:prop>
185                 <B:calendar-home-set xmlns:B="urn:ietf:params:xml:ns:caldav"/>
186                 <B:calendar-user-address-set xmlns:B="urn:ietf:params:xml:ns:caldav"/>
187                 <A:current-user-principal/>
188                 <A:displayname/>
189                 <C:dropbox-home-URL xmlns:C="http://calendarserver.org/ns/"/>
190                 <C:email-address-set xmlns:C="http://calendarserver.org/ns/"/>
191                 <C:notification-URL xmlns:C="http://calendarserver.org/ns/"/>
192                 <A:principal-collection-set/>
193                 <A:principal-URL/>
194                 <A:resource-id/>
195                 <B:schedule-inbox-URL xmlns:B="urn:ietf:params:xml:ns:caldav"/>
196                 <B:schedule-outbox-URL xmlns:B="urn:ietf:params:xml:ns:caldav"/>
197                 <A:supported-report-set/>
198               </A:prop>
199             </A:propfind>';
200
201         $request = new Sabre\HTTP\Request(array(
202             'REQUEST_METHOD' => 'PROPFIND',
203             'REQUEST_URI'    => '/' . Tinebase_WebDav_PrincipalBackend::PREFIX_USERS . '/' . Tinebase_WebDav_PrincipalBackend::SHARED,
204             'HTTP_BRIEF'     => 't',
205             'HTTP_DEPTH'     => '0'
206         ));
207         $request->setBody($body);
208
209         $this->server->httpRequest = $request;
210         $this->server->exec();
211         //var_dump($this->response->body);
212         
213         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
214         
215         $responseDoc = new DOMDocument();
216         $responseDoc->loadXML($this->response->body);
217         //$responseDoc->formatOutput = true; echo $responseDoc->saveXML();
218         $xpath = new DomXPath($responseDoc);
219         $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
220         $xpath->registerNamespace('cs',  'http://calendarserver.org/ns/');
221         $xpath->registerNamespace('d',  'DAV');
222         
223         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cal:calendar-home-set');
224         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
225         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
226         
227         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/d:principal-URL');
228         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
229         #$this->assertEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
230     }
231 }