0011428: support caldav sync token
[tine20] / tests / tine20 / Tinebase / WebDav / Plugin / SyncTokenTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  *
5  * @package     Tinebase
6  * @subpackage  WebDav
7  * @license     http://www.gnu.org/licenses/agpl.html
8  * @copyright   Copyright (c) 2015-2015 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Paul Mehrer <p.mehrer@metaways.de>
10  */
11
12 class Tinebase_WebDav_Plugin_SyncTokenTest extends Tinebase_WebDav_Plugin_AbstractBaseTest
13 {
14     /**
15      * @var Tinebase_WebDav_Plugin_SyncToken
16      */
17     protected $plugin;
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 Tinebase WebDav Plugin SyncToken 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->setupCalendarContent();
44
45         $this->plugin = new Tinebase_WebDav_Plugin_SyncToken();
46         $this->server->addPlugin($this->plugin);
47     }
48
49     /**
50      * tear down tests
51      */
52     public function tearDown()
53     {
54         parent::tearDown();
55
56         Tinebase_Container::getInstance()->getContentBackend()->delete($this->objects['containerContent']);
57     }
58
59     protected function setupCalendarContent()
60     {
61         /**
62          * @var Tinebase_Backend_Sql
63          */
64         $contentBackend = Tinebase_Container::getInstance()->getContentBackend();
65         $this->objects['containerContent'] = new Tinebase_Model_ContainerContent(array(
66             'action'          => Tinebase_Model_ContainerContent::ACTION_CREATE,
67             'record_id'       => 'testRecordId',
68             'time'            => Tinebase_DateTime::now(),
69             'container_id'    => $this->objects['initialContainer']->id,
70             'content_seq'     => 1,
71         ));
72         $contentBackend->create($this->objects['containerContent']);
73     }
74
75     /**
76      * test getSupportedReportSet method
77      */
78     public function testGetSupportedReportSetForCalendarNode()
79     {
80         $set = $this->plugin->getSupportedReportSet('/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id);
81
82         $this->assertContains('{DAV:}sync-collection', $set);
83     }
84
85     /**
86      * test propfind sync-token request
87      */
88     public function testPropfindSyncToken()
89     {
90         $body = '<?xml version="1.0" encoding="utf-8"?>
91                  <A:propfind xmlns:A="DAV:">
92                      <A:prop>
93                         <A:sync-token/>
94                      </A:prop>
95                  </A:propfind>';
96
97         $request = new Sabre\HTTP\Request(array(
98             'REQUEST_METHOD' => 'PROPFIND',
99             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
100             'HTTP_DEPTH'     => '1',
101         ));
102         $request->setBody($body);
103
104         $this->server->httpRequest = $request;
105         $this->server->exec();
106         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
107         $this->assertContains('<d:sync-token>http://tine20.net/ns/sync/1</d:sync-token></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response></d:multistatus>', $this->response->body);
108     }
109
110     /**
111      * test sync-collection request
112      */
113     public function testSyncCollection()
114     {
115         $body = '<?xml version="1.0" encoding="UTF-8"?>
116                 <A:sync-collection xmlns:A="DAV:">
117                     <A:sync-token>http://tine20.net/ns/sync/0</A:sync-token>
118                     <A:sync-level>1</A:sync-level>
119                     <A:prop>
120                         <A:getetag/>
121                         <A:getcontenttype/>
122                     </A:prop>
123                 </A:sync-collection>';
124
125         $request = new Sabre\HTTP\Request(array(
126             'REQUEST_METHOD' => 'REPORT',
127             'REQUEST_URI'    => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/' . $this->objects['initialContainer']->id,
128             'HTTP_DEPTH'     => '1',
129         ));
130         $request->setBody($body);
131
132         $this->server->httpRequest = $request;
133         $this->server->exec();
134         
135         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
136         $this->assertContains('<d:sync-token>http://tine20.net/ns/sync/1</d:sync-token></d:multistatus>', $this->response->body);
137     }
138 }