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