0012180: fix broken xml input
[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         $testCredentials = TestServer::getInstance()->getTestCredentials();
32         if ($this->_uit === null) {
33             $caldavClientOptions = array(
34                 'baseUri' => 'localhost',
35                 'userName' => Tinebase_Core::getUser()->accountLoginName,
36                 'password' => $testCredentials['password'],
37             );
38             $this->_uit = new Calendar_Import_CalDAV_ClientMock($caldavClientOptions, 'MacOSX');
39             $this->_uit->setVerifyPeer(false);
40         }
41         
42         return $this->_uit;
43     }
44     
45     /**
46      * test import of a single container/calendar of current user
47      */
48     public function testImportCalendars()
49     {
50         $this->_getUit()->importAllCalendars();
51         
52         $importedCalendar = $this->_getImportCalendar();
53         
54         $this->assertEquals('calendar', $importedCalendar->name);
55         $this->assertEquals('#711A76', $importedCalendar->color);
56         $this->assertEquals('Calendar_Model_Event', $importedCalendar->model, print_r($importedCalendar->toArray(), true));
57         $this->assertEquals( Tinebase_Core::getUser()->getId(), $importedCalendar->owner_id, print_r($importedCalendar->toArray(), true));
58     }
59     
60     /**
61      * fetch import calendar
62      * 
63      * @return Tinebase_Model_Container
64      */
65     protected function _getImportCalendar()
66     {
67         $calendarUuid = sha1('/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/');
68         return Tinebase_Container::getInstance()->getByProperty($calendarUuid, 'uuid');
69     }
70     
71     /**
72      * test import of events
73      */
74     public function testImportEvents()
75     {
76         $this->testImportCalendars();
77         $this->_getUit()->importAllCalendarData();
78         
79         $importedCalendar = $this->_getImportCalendar();
80         
81         $events = Calendar_Controller_Event::getInstance()->search(new Calendar_Model_EventFilter(array(
82             array('field' => 'container_id', 'operator' => 'in', 'value' => array($importedCalendar->getId()))
83         )));
84         $this->assertEquals(3, count($events));
85         $this->assertTrue(array(
86             '"bcc36c611f0b60bfee64b4d42e44aa1d"',
87             '"8b89914690ad7290fa9a2dc1da490489"',
88             '"0b3621a20e9045d8679075db57e881dd"'
89         ) == $events->etag);
90     }
91     
92     /**
93      * test update of events
94      */
95     public function testUpdateEvents()
96     {
97         $this->testImportEvents();
98         
99         $importedCalendar = $this->_getImportCalendar();
100         
101         $tine20Event = $this->_getEvent();
102         $tine20Event->container_id = $importedCalendar->getId();
103         Calendar_Controller_Event::getInstance()->create($tine20Event);
104         
105         $this->_getUit()->updateServerEvents();
106         
107         $this->_getUit()->updateAllCalendarData();
108
109         $events = Calendar_Controller_Event::getInstance()->search(new Calendar_Model_EventFilter(array(
110             array('field' => 'container_id', 'operator' => 'in', 'value' => array($importedCalendar->getId()))
111         )));
112         $this->assertEquals(4, count($events));
113         $expectedEtags = array(
114             '"bcc36c611f0b60bfee64b4d42e44aa1d"', // unchanged
115             '"-1030341843%40citrixonlinecom"',    // added on server
116             '"aa3621a20e9045d8679075db57e881dd"', // updated
117             null,                                 // added on client (tine20)
118         );
119         sort($expectedEtags);
120         $currentEtags = $events->etag;
121         sort($currentEtags);
122         $this->assertEquals($expectedEtags, $currentEtags, 'etag mismatch');
123         
124         $updatedEvent = $events->filter('etag', '"aa3621a20e9045d8679075db57e881dd"')->getFirstRecord();
125         $this->assertEquals('test update', $updatedEvent->summary);
126     }
127     
128     /**
129      * testBrokenXml
130      */
131     public function testBrokenXml()
132     {
133         $brokenBody = file_get_contents(dirname(__FILE__) . '/files/broken_ics.xml');
134         $result = $this->_getUit()->parseMultiStatus($brokenBody);
135         $this->assertTrue(is_array($result));
136     }
137 }