0011428: support caldav sync token
[tine20] / tests / tine20 / Tinebase / WebDav / Plugin / InverseTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Tinebase
6  * @subpackage  Frontend
7  * @license     http://www.gnu.org/licenses/agpl.html
8  * @copyright   Copyright (c) 2013-2015 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Lars Kneschke <l.kneschke@metaways.de>
10  */
11
12 /**
13  * Test class for Tinebase_WebDav_Plugin_Inverse
14  */
15 class Tinebase_WebDav_Plugin_InverseTest extends Tinebase_WebDav_Plugin_AbstractBaseTest
16 {
17
18     
19     /**
20      * Runs the test methods of this class.
21      *
22      * @access public
23      * @static
24      */
25     public static function main()
26     {
27         $suite  = new PHPUnit_Framework_TestSuite('Tine 2.0 Calendar CalDAV PluginInverse Tests');
28         PHPUnit_TextUI_TestRunner::run($suite);
29     }
30
31     /**
32      * Sets up the fixture.
33      * This method is called before a test is executed.
34      *
35      * @access protected
36      */
37     protected function setUp()
38     {
39         parent::setUp();
40         
41         parent::setupCalendarContainer();
42         
43         $this->plugin = new Tinebase_WebDav_Plugin_Inverse();
44         
45         $this->server->addPlugin($this->plugin);
46     }
47
48     /**
49      * test getPluginName method
50      */
51     public function testGetPluginName()
52     {
53         $pluginName = $this->plugin->getPluginName();
54         
55         $this->assertEquals('Tinebase_WebDav_Plugin_Inverse', $pluginName);
56     }
57     
58     /**
59      * test getSupportedReportSet method
60      */
61     public function testGetSupportedReportSet()
62     {
63         $set = $this->plugin->getSupportedReportSet('/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id);
64         
65         $this->assertContains('{' . Tinebase_WebDav_Plugin_Inverse::NS_DAV . '}principal-match', $set);
66         $this->assertContains('{' . Tinebase_WebDav_Plugin_Inverse::NS_INVERSE . '}acl-query',   $set);
67         $this->assertContains('{' . Tinebase_WebDav_Plugin_Inverse::NS_INVERSE . '}user-query',  $set);
68     }
69     
70     /**
71      * test aclQuery->userList method
72      */
73     public function testAclQueryUserList()
74     {
75         $body = '<?xml version="1.0" encoding="utf-8"?>
76                  <acl-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
77                      <user-list/>
78                  </acl-query>';
79
80         $request = new Sabre\HTTP\Request(array(
81             'REQUEST_METHOD' => 'REPORT',
82             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
83             'HTTP_DEPTH'     => '1',
84         ));
85         $request->setBody($body);
86
87         $this->server->httpRequest = $request;
88         $this->server->exec();
89         //var_dump($this->response->body);
90         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
91         $this->assertContains('<user><id>' . Tinebase_Core::getUser()->contact_id . '</id>', $this->response->body);
92     }
93     
94     /**
95      * test aclQuery method
96      */
97     public function testAclQueryRoles()
98     {
99         $body = '<?xml version="1.0" encoding="utf-8"?>
100                  <acl-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
101                      <roles user="' . Tinebase_Core::getUser()->contact_id . '"/>
102                  </acl-query>';
103
104         $request = new Sabre\HTTP\Request(array(
105             'REQUEST_METHOD' => 'REPORT',
106             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
107             'HTTP_DEPTH'     => '1',
108         ));
109         $request->setBody($body);
110
111         $this->server->httpRequest = $request;
112         $this->server->exec();
113         
114         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
115         $this->assertContains('<roles><ObjectViewer/><ObjectCreator/><ObjectEditor/><ObjectEraser/><PrivateViewer/></roles>', $this->response->body);
116     }
117     
118     /**
119      * test aclQuery method
120      */
121     public function testAclAddUser()
122     {
123         $body = '<?xml version="1.0" encoding="utf-8"?>
124                  <acl-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
125                      <add-user user="' . Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->contact_id . '"/>
126                  </acl-query>';
127         
128         $request = new Sabre\HTTP\Request(array(
129             'REQUEST_METHOD' => 'POST',
130             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
131             'HTTP_DEPTH'     => '1',
132         ));
133         $request->setBody($body);
134
135         $this->server->httpRequest = $request;
136         $this->server->exec();
137         
138         $this->assertEquals('HTTP/1.1 201 Created', $this->response->status);
139         
140         $grants = Tinebase_Container::getInstance()->getGrantsOfContainer($this->objects['initialContainer']->id);
141         $this->assertContains(Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->accountId, $grants->account_id);
142     }
143     
144     /**
145      * test aclQuery method
146      */
147     public function testAclRemoveUser()
148     {
149         // add user to grants of container
150         $this->testAclAddUser();
151         
152         $body = '<?xml version="1.0" encoding="utf-8"?>
153                  <acl-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
154                      <remove-user user="' . Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->contact_id . '"/>
155                  </acl-query>';
156         
157         $request = new Sabre\HTTP\Request(array(
158             'REQUEST_METHOD' => 'POST',
159             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
160             'HTTP_DEPTH'     => '1',
161         ));
162         $request->setBody($body);
163
164         $this->server->httpRequest = $request;
165         $this->server->exec();
166         
167         $this->assertEquals('HTTP/1.1 201 Created', $this->response->status);
168         
169         $grants = Tinebase_Container::getInstance()->getGrantsOfContainer($this->objects['initialContainer']->id);
170         $this->assertNotContains(Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->accountId, $grants->account_id);
171     }
172     
173     /**
174      * test aclQuery method
175      */
176     public function testAclSetRoles()
177     {
178         // add user to grants of container
179         $this->testAclAddUser();
180         
181         $body = '<?xml version="1.0" encoding="utf-8"?>
182                  <acl-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
183                      <set-roles user="' . Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->contact_id . '">
184                          <ObjectEraser/>
185                      </set-roles>
186                  </acl-query>';
187
188         $request = new Sabre\HTTP\Request(array(
189             'REQUEST_METHOD' => 'POST',
190             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
191             'HTTP_DEPTH'     => '1',
192         ));
193         $request->setBody($body);
194
195         $this->server->httpRequest = $request;
196         $this->server->exec();
197         
198         $this->assertEquals('HTTP/1.1 201 Created', $this->response->status);
199         
200         $grantsOfPwulf = Tinebase_Container::getInstance()
201             ->getGrantsOfContainer($this->objects['initialContainer']->id)
202             ->filter('account_id', Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->accountId)
203             ->getFirstRecord();
204         $this->assertTrue($grantsOfPwulf->deleteGrant);
205         $this->assertFalse($grantsOfPwulf->addGrant);
206     }
207     
208     /**
209      * test principalMatch method
210      */
211     public function testPrincipalMatch()
212     {
213         $body = '<?xml version="1.0" encoding="utf-8"?>
214                  <D:principal-match xmlns:D="DAV:">
215                      <D:principal-property>
216                          <D:owner/>
217                      </D:principal-property>
218                  </D:principal-match>';
219
220         $request = new Sabre\HTTP\Request(array(
221             'REQUEST_METHOD' => 'REPORT',
222             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
223             'HTTP_DEPTH'     => '1',
224         ));
225         $request->setBody($body);
226
227         $this->server->httpRequest = $request;
228         $this->server->exec();
229         
230         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
231         $this->assertContains('<d:response><d:href>/principals/users/' . Tinebase_Core::getUser()->contact_id . '</d:href>', $this->response->body);
232     }
233     
234     /**
235      * test userQuery method
236      */
237     public function testUserQuery()
238     {
239         $body = '<?xml version="1.0" encoding="UTF-8"?>
240                  <user-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
241                      <users match-name="' . Tinebase_Core::getUser()->accountFullName . '"/>
242                  </user-query>';
243
244         $request = new Sabre\HTTP\Request(array(
245             'REQUEST_METHOD' => 'REPORT',
246             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
247             'HTTP_DEPTH'     => '1',
248         ));
249         $request->setBody($body);
250
251         $this->server->httpRequest = $request;
252         $this->server->exec();
253         
254         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
255         $this->assertContains('<displayName>' . Tinebase_Core::getUser()->accountDisplayName . '</displayName>', $this->response->body);
256         $this->assertContains('<id>' . Tinebase_Core::getUser()->contact_id . '</id>', $this->response->body);
257     }
258 }