#6752: ci ldap tests
[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         if (! $this->_transactionId) {
108             $events = $this->_backend->search(new Calendar_Model_EventFilter(array(
109                 array('field' => 'container_id', 'operator' => 'in', 'value' => $this->_testCalendars->getId()),
110             )), new Tinebase_Model_Pagination(array()));
111             
112             // delete alarms
113             Tinebase_Alarm::getInstance()->deleteAlarmsOfRecord('Calendar_Model_Event', $events->getArrayOfIds());
114             
115             foreach ($events as $event) {
116                 $this->_backend->delete($event->getId());
117             }
118             
119             foreach ($this->_testCalendars as $cal) {
120                 Tinebase_Container::getInstance()->deleteContainer($cal, true);
121             }
122         }
123     }
124     
125     /**
126      * returns a simple event
127      * 
128      * @param bool $now
129      * @return Calendar_Model_Event
130      */
131     protected function _getEvent($now = FALSE)
132     {
133         $event = new Calendar_Model_Event(array(
134             'summary'     => 'Wakeup',
135             'dtstart'     => '2009-03-25 06:00:00',
136             'dtend'       => '2009-03-25 06:15:00',
137             'description' => 'Early to bed and early to rise, makes a men healthy, wealthy and wise',
138             'attendee'    => $this->_getAttendee(),
139         
140             'container_id' => $this->_testCalendar->getId(),
141             'organizer'    => $this->_testUserContact->getId(),
142             'uid'          => Calendar_Model_Event::generateUID(),
143         
144             Tinebase_Model_Grants::GRANT_READ    => true,
145             Tinebase_Model_Grants::GRANT_EDIT    => true,
146             Tinebase_Model_Grants::GRANT_DELETE  => true,
147         ));
148         
149         if ($now) {
150             $event->dtstart = Tinebase_DateTime::now();
151             $event->dtend = Tinebase_DateTime::now()->addMinute(15);
152         }
153         
154         return $event;
155     }
156
157     /**
158      * returns a simple event
159      *
160      * @param bool $now use date of now
161      * @return Calendar_Model_Event
162      */
163     protected function _getEventWithAlarm($now = FALSE)
164     {
165         $event = $this->_getEvent($now);
166         $event->alarms = new Tinebase_Record_RecordSet('Tinebase_Model_Alarm', array(
167             array(
168                 'minutes_before'    => 0
169             ),
170         ), TRUE);
171         
172         return $event;
173     }
174     
175     /**
176      * get test attendee
177      *
178      * @return Tinebase_Record_RecordSet
179      */
180     protected function _getAttendee()
181     {
182         return new Tinebase_Record_RecordSet('Calendar_Model_Attender', array(
183             array(
184                 'user_id'        => $this->_testUserContact->getId(),
185                 'user_type'      => Calendar_Model_Attender::USERTYPE_USER,
186                 'role'           => Calendar_Model_Attender::ROLE_REQUIRED,
187                 'status_authkey' => Tinebase_Record_Abstract::generateUID(),
188             ),
189             array(
190                 'user_id'        => $this->_personasContacts['sclever']->getId(),
191                 'user_type'      => Calendar_Model_Attender::USERTYPE_USER,
192                 'role'           => Calendar_Model_Attender::ROLE_REQUIRED,
193                 'status_authkey' => Tinebase_Record_Abstract::generateUID(),
194             ),
195             /* no group suppoert yet
196             array(
197                 'user_id'        => Tinebase_Core::getUser()->accountPrimaryGroup,
198                 'user_type'      => Calendar_Model_Attender::USERTYPE_GROUP,
199                 'status_authkey' => Tinebase_Record_Abstract::generateUID(),
200             )
201             */
202         ));
203     }
204     
205     /**
206      * get all calendar grants
207      * 
208      * @param Tinebase_Model_User $user
209      * @return array
210      */
211     protected function _getAllCalendarGrants($user = null)
212     {
213         return array(
214             'account_id'    => $user ? $user->getId() : Tinebase_Core::getUser()->getId(),
215             'account_type'  => 'user',
216             Tinebase_Model_Grants::GRANT_READ     => true,
217             Tinebase_Model_Grants::GRANT_ADD      => true,
218             Tinebase_Model_Grants::GRANT_EDIT     => true,
219             Tinebase_Model_Grants::GRANT_DELETE   => true,
220             Tinebase_Model_Grants::GRANT_PRIVATE  => true,
221             Tinebase_Model_Grants::GRANT_ADMIN    => true,
222             Tinebase_Model_Grants::GRANT_FREEBUSY => true,
223         );
224     }
225 }