f36196eb561cca8744b8acaa3b6ef17a107e2420
[tine20] / tests / tine20 / Calendar / Import / CalDAVTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Calendar
6  * @license     http://www.gnu.org/licenses/agpl.html
7  * @copyright   Copyright (c) 2014 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  * 
10  */
11
12 /**
13  * Test class for Calendar_Import_CalDAV
14  */
15 class Calendar_Import_CalDAVTest extends Calendar_TestCase
16 {
17     /**
18      * unit in test
19      *
20      * @var Calendar_Import_CalDAV_ClientMock
21      */
22     protected $_uit = null;
23     
24     /**
25      * lazy init of uit
26      *
27      * @return Calendar_Import_CalDAV_ClientMock
28      */
29     protected function _getUit()
30     {
31         if ($this->_uit === null) {
32             $caldavClientOptions = array(
33                 'baseUri' => 'localhost',
34                 'userName' => Tinebase_Core::getUser()->accountLoginName,
35                 'password' => Zend_Registry::get('testConfig')->password, // TODO use credential cache?
36             );
37             $this->_uit = new Calendar_Import_CalDAV_ClientMock($caldavClientOptions, 'MacOSX');
38             $this->_uit->setVerifyPeer(false);
39         }
40         
41         return $this->_uit;
42     }
43     
44     /**
45      * test import of a single container/calendar of current user
46      */
47     public function testImportCalendars()
48     {
49         $this->_getUit()->importAllCalendars();
50         
51         $importedCalendar = $this->_getImportCalendar();
52         
53         $this->assertEquals('calendar', $importedCalendar->name);
54         $this->assertEquals('#711A76', $importedCalendar->color);
55         $this->assertEquals('Calendar_Model_Event', $importedCalendar->model, print_r($importedCalendar->toArray(), true));
56         $this->assertEquals( Tinebase_Core::getUser()->getId(), $importedCalendar->owner_id, print_r($importedCalendar->toArray(), true));
57     }
58     
59     /**
60      * fetch import calendar
61      * 
62      * @return Tinebase_Model_Container
63      */
64     protected function _getImportCalendar()
65     {
66         $calendarUuid = sha1('/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/');
67         return Tinebase_Container::getInstance()->getByProperty($calendarUuid, 'uuid');
68     }
69     
70     /**
71      * test import of events
72      */
73     public function testImportEvents()
74     {
75         $this->testImportCalendars();
76         $this->_getUit()->importAllCalendarData();
77         
78         $importedCalendar = $this->_getImportCalendar();
79         
80         $events = Calendar_Controller_Event::getInstance()->search(new Calendar_Model_EventFilter(array(
81             array('field' => 'container_id', 'operator' => 'in', 'value' => array($importedCalendar->getId()))
82         )));
83         $this->assertEquals(3, count($events));
84         $this->assertTrue(array(
85             '"bcc36c611f0b60bfee64b4d42e44aa1d"',
86             '"8b89914690ad7290fa9a2dc1da490489"',
87             '"0b3621a20e9045d8679075db57e881dd"'
88         ) == $events->etag);
89     }
90     
91     /**
92      * test update of events
93      */
94     public function testUpdateEvents()
95     {
96         $this->testImportEvents();
97         
98         $importedCalendar = $this->_getImportCalendar();
99         
100         $tine20Event = $this->_getEvent();
101         $tine20Event->container_id = $importedCalendar->getId();
102         Calendar_Controller_Event::getInstance()->create($tine20Event);
103         
104         $this->_getUit()->updateServerEvents();
105         
106         $this->_getUit()->updateAllCalendarData();
107
108         $events = Calendar_Controller_Event::getInstance()->search(new Calendar_Model_EventFilter(array(
109             array('field' => 'container_id', 'operator' => 'in', 'value' => array($importedCalendar->getId()))
110         )));
111         $this->assertEquals(4, count($events));
112         $expectedEtags = array(
113             '"bcc36c611f0b60bfee64b4d42e44aa1d"', // unchanged
114             '"3333914690ad7290fa9a2dc1da490489"', // added on server
115             '"aa3621a20e9045d8679075db57e881dd"', // updated
116             null,                                 // added on client (tine20)
117         );
118         sort($expectedEtags);
119         $currentEtags = $events->etag;
120         sort($currentEtags);
121         $this->assertEquals($expectedEtags, $currentEtags, 'etag mismatch');
122         
123         $updatedEvent = $events->filter('etag', '"aa3621a20e9045d8679075db57e881dd"')->getFirstRecord();
124         $this->assertEquals('test update', $updatedEvent->summary);
125     }
126 }