b645dd7c21cfab0efd980bbea9851e0c3195da48
[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-2013 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 'vendor/sabre/dav/tests/Sabre/HTTP/ResponseMock.php';
16
17 /**
18  * Test class for Tinebase_WebDav_Plugin_Inverse
19  */
20 class Tinebase_WebDav_Plugin_InverseTest extends TestCase
21 {
22     /**
23      * @var array test objects
24      */
25     protected $objects = array();
26     
27     /**
28      * 
29      * @var Sabre\DAV\Server
30      */
31     protected $server;
32     
33     /**
34      * Runs the test methods of this class.
35      *
36      * @access public
37      * @static
38      */
39     public static function main()
40     {
41         $suite  = new PHPUnit_Framework_TestSuite('Tine 2.0 Calendar CalDAV PluginInverse Tests');
42         PHPUnit_TextUI_TestRunner::run($suite);
43     }
44
45     /**
46      * Sets up the fixture.
47      * This method is called before a test is executed.
48      *
49      * @access protected
50      */
51     protected function setUp()
52     {
53         parent::setUp();
54         
55         $this->objects['initialContainer'] = Tinebase_Container::getInstance()->addContainer(new Tinebase_Model_Container(array(
56             'name'              => Tinebase_Record_Abstract::generateUID(),
57             'type'              => Tinebase_Model_Container::TYPE_PERSONAL,
58             'backend'           => 'Sql',
59             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName('Calendar')->getId(),
60         )));
61         
62         $this->server = new Sabre\DAV\Server(new Tinebase_WebDav_Root());
63         
64         $this->plugin = new Tinebase_WebDav_Plugin_Inverse();
65         
66         $this->server->addPlugin($this->plugin);
67         
68         $this->response = new Sabre\HTTP\ResponseMock();
69         $this->server->httpResponse = $this->response;
70     }
71
72     /**
73      * test getPluginName method
74      */
75     public function testGetPluginName()
76     {
77         $pluginName = $this->plugin->getPluginName();
78         
79         $this->assertEquals('Tinebase_WebDav_Plugin_Inverse', $pluginName);
80     }
81     
82     /**
83      * test getSupportedReportSet method
84      */
85     public function testGetSupportedReportSet()
86     {
87         $set = $this->plugin->getSupportedReportSet('/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id);
88         
89         $this->assertContains('{' . Tinebase_WebDav_Plugin_Inverse::NS_DAV . '}principal-match', $set);
90         $this->assertContains('{' . Tinebase_WebDav_Plugin_Inverse::NS_INVERSE . '}acl-query',   $set);
91         $this->assertContains('{' . Tinebase_WebDav_Plugin_Inverse::NS_INVERSE . '}user-query',  $set);
92     }
93     
94     /**
95      * test aclQuery->userList method
96      */
97     public function testAclQueryUserList()
98     {
99         $body = '<?xml version="1.0" encoding="utf-8"?>
100                  <acl-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
101                      <user-list/>
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         //var_dump($this->response->body);
114         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
115         $this->assertContains('<user><id>' . Tinebase_Core::getUser()->contact_id . '</id>', $this->response->body);
116     }
117     
118     /**
119      * test aclQuery method
120      */
121     public function testAclQueryRoles()
122     {
123         $body = '<?xml version="1.0" encoding="utf-8"?>
124                  <acl-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
125                      <roles user="' . Tinebase_Core::getUser()->contact_id . '"/>
126                  </acl-query>';
127
128         $request = new Sabre\HTTP\Request(array(
129             'REQUEST_METHOD' => 'REPORT',
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 207 Multi-Status', $this->response->status);
139         $this->assertContains('<roles><ObjectViewer/><ObjectCreator/><ObjectEditor/><ObjectEraser/><PrivateViewer/></roles>', $this->response->body);
140     }
141     
142     /**
143      * test aclQuery method
144      */
145     public function testAclAddUser()
146     {
147         $body = '<?xml version="1.0" encoding="utf-8"?>
148                  <acl-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
149                      <add-user user="' . Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->contact_id . '"/>
150                  </acl-query>';
151         
152         $request = new Sabre\HTTP\Request(array(
153             'REQUEST_METHOD' => 'POST',
154             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
155             'HTTP_DEPTH'     => '1',
156         ));
157         $request->setBody($body);
158
159         $this->server->httpRequest = $request;
160         $this->server->exec();
161         
162         $this->assertEquals('HTTP/1.1 201 Created', $this->response->status);
163         
164         $grants = Tinebase_Container::getInstance()->getGrantsOfContainer($this->objects['initialContainer']->id);
165         $this->assertContains(Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->accountId, $grants->account_id);
166     }
167     
168     /**
169      * test aclQuery method
170      */
171     public function testAclRemoveUser()
172     {
173         // add user to grants of container
174         $this->testAclAddUser();
175         
176         $body = '<?xml version="1.0" encoding="utf-8"?>
177                  <acl-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
178                      <remove-user user="' . Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->contact_id . '"/>
179                  </acl-query>';
180         
181         $request = new Sabre\HTTP\Request(array(
182             'REQUEST_METHOD' => 'POST',
183             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
184             'HTTP_DEPTH'     => '1',
185         ));
186         $request->setBody($body);
187
188         $this->server->httpRequest = $request;
189         $this->server->exec();
190         
191         $this->assertEquals('HTTP/1.1 201 Created', $this->response->status);
192         
193         $grants = Tinebase_Container::getInstance()->getGrantsOfContainer($this->objects['initialContainer']->id);
194         $this->assertNotContains(Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->accountId, $grants->account_id);
195     }
196     
197     /**
198      * test aclQuery method
199      */
200     public function testAclSetRoles()
201     {
202         // add user to grants of container
203         $this->testAclAddUser();
204         
205         $body = '<?xml version="1.0" encoding="utf-8"?>
206                  <acl-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
207                      <set-roles user="' . Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->contact_id . '">
208                          <ObjectEraser/>
209                      </set-roles>
210                  </acl-query>';
211
212         $request = new Sabre\HTTP\Request(array(
213             'REQUEST_METHOD' => 'POST',
214             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
215             'HTTP_DEPTH'     => '1',
216         ));
217         $request->setBody($body);
218
219         $this->server->httpRequest = $request;
220         $this->server->exec();
221         
222         $this->assertEquals('HTTP/1.1 201 Created', $this->response->status);
223         
224         $grantsOfPwulf = Tinebase_Container::getInstance()
225             ->getGrantsOfContainer($this->objects['initialContainer']->id)
226             ->filter('account_id', Tinebase_Helper::array_value('pwulf', Zend_Registry::get('personas'))->accountId)
227             ->getFirstRecord();
228         $this->assertTrue($grantsOfPwulf->deleteGrant);
229         $this->assertFalse($grantsOfPwulf->addGrant);
230     }
231     
232     /**
233      * test principalMatch method
234      */
235     public function testPrincipalMatch()
236     {
237         $body = '<?xml version="1.0" encoding="utf-8"?>
238                  <D:principal-match xmlns:D="DAV:">
239                      <D:principal-property>
240                          <D:owner/>
241                      </D:principal-property>
242                  </D:principal-match>';
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('<d:response><d:href>/principals/users/' . Tinebase_Core::getUser()->contact_id . '</d:href>', $this->response->body);
256     }
257     
258     /**
259      * test userQuery method
260      */
261     public function testUserQuery()
262     {
263         $body = '<?xml version="1.0" encoding="UTF-8"?>
264                  <user-query xmlns="urn:inverse:params:xml:ns:inverse-dav">
265                      <users match-name="' . Tinebase_Core::getUser()->accountFullName . '"/>
266                  </user-query>';
267
268         $request = new Sabre\HTTP\Request(array(
269             'REQUEST_METHOD' => 'REPORT',
270             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
271             'HTTP_DEPTH'     => '1',
272         ));
273         $request->setBody($body);
274
275         $this->server->httpRequest = $request;
276         $this->server->exec();
277         
278         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
279         $this->assertContains('<displayName>' . Tinebase_Core::getUser()->accountDisplayName . '</displayName>', $this->response->body);
280         $this->assertContains('<id>' . Tinebase_Core::getUser()->contact_id . '</id>', $this->response->body);
281     }
282 }