9578: Cli Ex / Import Calendar doesn't work
[tine20] / tests / tine20 / Calendar / TestCase.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 AGPL Version 3
7  * @copyright   Copyright (c) 2009-2013 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Cornelius Weiss <c.weiss@metaways.de>
9  */
10
11 /**
12  * Test helper
13  */
14 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'TestHelper.php';
15
16 /**
17  * Test class for Calendar_Backend_Sql
18  * 
19  * @package     Calendar
20  */
21 abstract class Calendar_TestCase extends TestCase
22 {
23     /**
24      * @var Calendar_Backend_Sql SQL Backend in test
25      */
26     protected $_backend;
27     
28     /**
29      * @var Tinebase_Model_Container
30      */
31     protected $_testCalendar;
32     
33     /**
34      * @var Tinebase_Record_RecordSet test calendars to be deleted on tearDown
35      */
36     protected $_testCalendars;
37     
38     /**
39      * @var Tinebase_Model_FullUser
40      */
41     protected $_testUser;
42     
43     /**
44      * @var Addressbook_Model_Contact
45      */
46     protected $_testUserContact;
47     
48     /**
49      * personas
50      *
51      * @var array
52      */
53     protected $_personas;
54     
55     /**
56      * personas contacts
57      * @var array
58      */
59     protected $_personasContacts;
60     
61     /**
62      * persona calendars
63      *
64      * @var array
65      */
66     protected $_personasDefaultCals = array();
67     
68     /**
69      * set up tests
70      */
71     public function setUp()
72     {
73         parent::setUp();
74         
75         $this->_backend = new Calendar_Backend_Sql();
76         
77         $this->_personas = Zend_Registry::get('personas');
78         foreach ($this->_personas as $loginName => $user) {
79             $defaultCalendarId = Tinebase_Core::getPreference('Calendar')->getValueForUser(Calendar_Preference::DEFAULTCALENDAR, $user->getId());
80             $this->_personasContacts[$loginName] = Addressbook_Controller_Contact::getInstance()->getContactByUserId($user->getId());
81             $this->_personasDefaultCals[$loginName] = Tinebase_Container::getInstance()->getContainerById($defaultCalendarId);
82         }
83         
84         $this->_testUser = Tinebase_Core::getUser();
85         $this->_testUserContact = Addressbook_Controller_Contact::getInstance()->getContactByUserId($this->_testUser->getId());
86         $this->_testCalendar = Tinebase_Container::getInstance()->addContainer(new Tinebase_Model_Container(array(
87             'name'           => 'PHPUnit test calendar',
88             'type'           => Tinebase_Model_Container::TYPE_PERSONAL,
89             'owner_id'       => Tinebase_Core::getUser(),
90             'backend'        => $this->_backend->getType(),
91             'application_id' => Tinebase_Application::getInstance()->getApplicationByName('Calendar')->getId()
92         ), true));
93         
94         $this->_testCalendars = new Tinebase_Record_RecordSet('Tinebase_Model_Container');
95         $this->_testCalendars->addRecord($this->_testCalendar);
96     }
97     
98     /**
99      * tear down tests
100      */
101     public function tearDown()
102     {
103         parent::tearDown();
104         
105         Calendar_Controller_Event::getInstance()->sendNotifications(false);
106         
107         
108         if (! $this->_transactionId) {
109             if ($this->_backend != NULL) {
110                 $events = $this->_backend->search(new Calendar_Model_EventFilter(array(
111                     array('field' => 'container_id', 'operator' => 'in', 'value' => $this->_testCalendars->getId()),
112                 )), new Tinebase_Model_Pagination(array()));
113                 
114                 // delete alarms
115                 Tinebase_Alarm::getInstance()->deleteAlarmsOfRecord('Calendar_Model_Event', $events->getArrayOfIds());
116                 
117                 foreach ($events as $event) {
118                     $this->_backend->delete($event->getId());
119                 }
120             }
121             foreach ($this->_testCalendars as $cal) {
122                 Tinebase_Container::getInstance()->deleteContainer($cal, true);
123             }
124         }
125         
126         if ($this->_testUser->getId() !== Tinebase_Core::getUser()->getId()) {
127             // reset test user
128             Tinebase_Core::set(Tinebase_Core::USER, $this->_testUser);
129         }
130         
131     }
132     
133     /**
134      * returns a simple event
135      * 
136      * @param bool $now
137      * @return Calendar_Model_Event
138      */
139     protected function _getEvent($now = FALSE)
140     {
141         $event = new Calendar_Model_Event(array(
142             'summary'     => 'Wakeup',
143             'dtstart'     => '2009-03-25 06:00:00',
144             'dtend'       => '2009-03-25 06:15:00',
145             'description' => 'Early to bed and early to rise, makes a men healthy, wealthy and wise',
146             'attendee'    => $this->_getAttendee(),
147         
148             'container_id' => $this->_testCalendar->getId(),
149             'organizer'    => $this->_testUserContact->getId(),
150             'uid'          => Calendar_Model_Event::generateUID(),
151         
152             Tinebase_Model_Grants::GRANT_READ    => true,
153             Tinebase_Model_Grants::GRANT_EDIT    => true,
154             Tinebase_Model_Grants::GRANT_DELETE  => true,
155         ));
156         
157         if ($now) {
158             $event->dtstart = Tinebase_DateTime::now();
159             $event->dtend = Tinebase_DateTime::now()->addMinute(15);
160         }
161         
162         return $event;
163     }
164
165     /**
166      * returns a simple event
167      *
168      * @param bool $now use date of now
169      * @return Calendar_Model_Event
170      */
171     protected function _getEventWithAlarm($now = FALSE)
172     {
173         $event = $this->_getEvent($now);
174         $event->alarms = new Tinebase_Record_RecordSet('Tinebase_Model_Alarm', array(
175             array(
176                 'minutes_before'    => 0
177             ),
178         ), TRUE);
179         
180         return $event;
181     }
182     
183     /**
184      * get test attendee
185      *
186      * @return Tinebase_Record_RecordSet
187      */
188     protected function _getAttendee()
189     {
190         return new Tinebase_Record_RecordSet('Calendar_Model_Attender', array(
191             array(
192                 'user_id'        => $this->_testUserContact->getId(),
193                 'user_type'      => Calendar_Model_Attender::USERTYPE_USER,
194                 'role'           => Calendar_Model_Attender::ROLE_REQUIRED,
195                 'status_authkey' => Tinebase_Record_Abstract::generateUID(),
196             ),
197             array(
198                 'user_id'        => $this->_personasContacts['sclever']->getId(),
199                 'user_type'      => Calendar_Model_Attender::USERTYPE_USER,
200                 'role'           => Calendar_Model_Attender::ROLE_REQUIRED,
201                 'status_authkey' => Tinebase_Record_Abstract::generateUID(),
202             ),
203             /* no group suppoert yet
204             array(
205                 'user_id'        => Tinebase_Core::getUser()->accountPrimaryGroup,
206                 'user_type'      => Calendar_Model_Attender::USERTYPE_GROUP,
207                 'status_authkey' => Tinebase_Record_Abstract::generateUID(),
208             )
209             */
210         ));
211     }
212     
213     /**
214      * get all calendar grants
215      * 
216      * @param Tinebase_Model_User $user
217      * @return array
218      */
219     protected function _getAllCalendarGrants($user = null)
220     {
221         return array(
222             'account_id'    => $user ? $user->getId() : Tinebase_Core::getUser()->getId(),
223             'account_type'  => 'user',
224             Tinebase_Model_Grants::GRANT_READ     => true,
225             Tinebase_Model_Grants::GRANT_ADD      => true,
226             Tinebase_Model_Grants::GRANT_EDIT     => true,
227             Tinebase_Model_Grants::GRANT_DELETE   => true,
228             Tinebase_Model_Grants::GRANT_PRIVATE  => true,
229             Tinebase_Model_Grants::GRANT_ADMIN    => true,
230             Tinebase_Model_Grants::GRANT_FREEBUSY => true,
231         );
232     }
233     
234     /**
235      * helper function for getting attender (current user or persona) from attendee set
236      * 
237      * @param Tinebase_Record_RecordSet $attendee
238      * @param string $persona
239      * @return Calendar_Model_Attender
240      */
241     protected function _getAttenderFromAttendeeSet($attendee, $persona = null)
242     {
243         $contactId = $persona ? $this->_personasContacts[$persona]->getId() : Tinebase_Core::getUser()->contact_id;
244         $attender = new Calendar_Model_Attender(array(
245             'user_id'        => $contactId,
246             'user_type'      => Calendar_Model_Attender::USERTYPE_USER,
247         ));
248         
249         return Calendar_Model_Attender::getAttendee($attendee, $attender);
250     }
251 }