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