add inClassCache for Tinebase_Acl_Roles class
[tine20] / tests / tine20 / Timetracker / AbstractTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  *
5  * @package     Timetracker
6  * @license     http://www.gnu.org/licenses/agpl.html
7  * @copyright   Copyright (c) 2010-2012 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  */
10
11 /**
12  * Timetracker_AbstractTest Test class
13  */
14 abstract class Timetracker_AbstractTest extends PHPUnit_Framework_TestCase
15 {
16     /**
17      * @var Timetracker_Frontend_Json
18      */
19     protected $_json = array();
20
21     /**
22      * last record created by _getTime(account|sheet) or _getCustomField
23      * @var Tinebase_Record_Abstract
24      */
25     protected $_lastCreatedRecord = null;
26
27     /**
28      * Runs the test methods of this class.
29      *
30      * @access public
31      * @static
32      */
33     public static function main()
34     {
35         $suite  = new PHPUnit_Framework_TestSuite('Tine 2.0 Timetracker Json Tests');
36         PHPUnit_TextUI_TestRunner::run($suite);
37     }
38
39     /**
40      * Sets up the fixture.
41      * This method is called before a test is executed.
42      *
43      * @access protected
44      */
45     protected function setUp()
46     {
47         Tinebase_Acl_Roles::getInstance()->resetClassCache();
48         Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb());
49         $this->_json = new Timetracker_Frontend_Json();
50     }
51
52     /**
53      * Tears down the fixture
54      * This method is called after a test is executed.
55      *
56      * @access protected
57      */
58     protected function tearDown()
59     {
60         Tinebase_TransactionManager::getInstance()->rollBack();
61         Tinebase_Acl_Roles::getInstance()->resetClassCache();
62     }
63
64     /************ protected helper funcs *************/
65
66     /**
67      * get Timesheet
68      *
69      * @param array $_data
70      * @param boolean $_forceCreation
71      * @return Timetracker_Model_Timeaccount
72      */
73     protected function _getTimeaccount($_data = array(), $_forceCreation = false)
74     {
75         $defaultData = array(
76             'title'         => Tinebase_Record_Abstract::generateUID(),
77             'description'   => 'blabla',
78         );
79
80         $data = array_replace($defaultData, $_data);
81
82         $ta = new Timetracker_Model_Timeaccount($data, true);
83
84         if($_forceCreation) {
85             $taRec = $this->_json->saveTimeaccount($ta->toArray(), $_forceCreation);
86             $this->_lastCreatedRecord = $taRec;
87         }
88
89         return $ta;
90     }
91
92     /**
93      * get grants
94      *
95      * @return array
96      */
97     protected function _getGrants()
98     {
99         return array(
100             array(
101                 'account_id'    => 0,
102                 'account_type'  => 'anyone',
103                 Timetracker_Model_TimeaccountGrants::BOOK_OWN           => TRUE,
104                 Timetracker_Model_TimeaccountGrants::VIEW_ALL           => TRUE,
105                 Timetracker_Model_TimeaccountGrants::BOOK_ALL           => TRUE,
106                 Timetracker_Model_TimeaccountGrants::MANAGE_BILLABLE    => TRUE,
107                 Tinebase_Model_Grants::GRANT_EXPORT                     => TRUE,
108                 Tinebase_Model_Grants::GRANT_ADMIN                      => FALSE,
109             )
110         );
111     }
112
113     /**
114      * get Timesheet (create timeaccount as well)
115      *
116      * @param array fields data
117      * @param boolean force creation of the record
118      * @return Timetracker_Model_Timesheet
119      */
120     protected function _getTimesheet($_data = array(), $_forceCreation = false)
121     {
122         $defaultData = array('account_id'        => Tinebase_Core::getUser()->getId(),
123              'description'       => 'blabla',
124              'duration'          => 30,
125              'timeaccount_id'    => NULL,
126              'start_date'        => NULL
127         );
128
129         $data = array_replace($defaultData, $_data);
130
131         if ($data['timeaccount_id'] === NULL) {
132             $timeaccount = Timetracker_Controller_Timeaccount::getInstance()->create($this->_getTimeaccount());
133             $data['timeaccount_id'] = $timeaccount->getId();
134         }
135
136         if($data['start_date'] === NULL) {
137             $data['start_date'] = Tinebase_DateTime::now()->toString('Y-m-d');
138         }
139
140         $ts = new Timetracker_Model_Timesheet($data, TRUE);
141
142         if ($_forceCreation) {
143             $tsRec = $this->_json->saveTimesheet($ts->toArray(), $_forceCreation);
144             $this->_lastCreatedRecord = $tsRec;
145         }
146
147         return $ts;
148     }
149
150     /**
151      * get custom field record
152      *
153      * @return Tinebase_Model_CustomField_Config
154      */
155     protected function _getCustomField()
156     {
157         $record = new Tinebase_Model_CustomField_Config(array(
158             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName('Timetracker')->getId(),
159             'model'             => 'Timetracker_Model_Timesheet',
160             'name'              => Tinebase_Record_Abstract::generateUID(),
161             'definition'        => array(
162                 'label' => Tinebase_Record_Abstract::generateUID(),
163                 'type'  => 'string',
164                 'uiconfig' => array(
165                     'xtype'  => Tinebase_Record_Abstract::generateUID(),
166                     'length' => 10,
167                     'group'  => 'unittest',
168                     'order'  => 100,
169                 )
170             )
171         ));
172
173         $result = Tinebase_CustomField::getInstance()->addCustomField($record);
174
175         $this->_lastCreatedRecord = $result;
176
177         return $result;
178     }
179
180     /**
181      * get paging
182      *
183      * @param string $_sort
184      * @return array
185      */
186     protected function _getPaging($_sort = 'creation_time')
187     {
188         return array(
189             'start' => 0,
190             'limit' => 50,
191             'sort' => $_sort,
192             'dir' => 'ASC',
193         );
194     }
195
196     /**
197      * get Timeaccount filter
198      *
199      * @param boolean $_showClosed
200      * @return array
201      */
202     protected function _getTimeaccountFilter($_showClosed = FALSE)
203     {
204         $result = array(
205             array(
206                 'field' => 'description',
207                 'operator' => 'contains',
208                 'value' => 'blabla'
209             ),
210             array(
211                 'field' => 'containerType',
212                 'operator' => 'equals',
213                 'value' => Tinebase_Model_Container::TYPE_SHARED
214             ),
215         );
216
217         if (! $_showClosed) {
218             $result[] = array(
219                 'field' => 'is_open',
220                 'operator' => 'equals',
221                 'value' => TRUE
222             );
223         }
224
225         return $result;
226     }
227
228     /**
229      * get Timesheet filter
230      *
231      * @param array $_cfFilter
232      * @return array
233      */
234     protected function _getTimesheetFilter($_cfFilter = NULL)
235     {
236         $result = array(
237             array(
238                 'field' => 'query',
239                 'operator' => 'contains',
240                 'value' => 'blabla'
241             ),
242         );
243
244         if ($_cfFilter !== NULL) {
245             $result[] = $_cfFilter;
246         }
247
248         return $result;
249     }
250
251     /**
252      * get customfield value filter
253      *
254      * @param string $_cfid
255      * @return array
256      */
257     protected function _getCfValueFilter($_cfid)
258     {
259         return array(
260             array(
261                 'field' => 'customfield_id',
262                 'operator' => 'equals',
263                 'value' => $_cfid
264             ),
265             array(
266                 'field' => 'value',
267                 'operator' => 'group',
268                 'value' => ''
269             )
270         );
271     }
272
273     /**
274      * get Timesheet filter with custom field
275      *
276      * @return array
277      */
278     protected function _getTimesheetFilterWithCustomField($_cfId, $_value)
279     {
280         return array(
281             array(
282                 'field' => 'query',
283                 'operator' => 'contains',
284                 'value' => 'blabla'
285             ),
286             array(
287                 'field' => 'customfield',
288                 'operator' => 'equals',
289                 'value' => array('cfId' => $_cfId, 'value' => $_value)
290             ),
291         );
292     }
293
294     /**
295      * get persistent filter filter
296      *
297      * @param string $_name
298      * @return array
299      */
300     protected function _getPersistentFilterFilter($_name)
301     {
302         return array(
303             array(
304                 'field'     => 'name',
305                 'operator'  => 'equals',
306                 'value'     => $_name
307             ),
308         );
309     }
310
311     /**
312      * get Timesheet filter with date
313      *
314      * @param string $_type week filter type
315      * @return array
316      */
317     protected function _getTimesheetDateFilter($_type = 'weekThis')
318     {
319         $result = array(
320             array(
321                 'field' => 'query',
322                 'operator' => 'contains',
323                 'value' => 'blabla'
324             ),
325             array(
326                 'field' => 'start_date',
327                 'operator' => 'after',
328                 'value' => '2008-12-12'
329             ),
330         );
331
332         if ($_type == 'inweek') {
333             $date = Tinebase_DateTime::now();
334             $weekNumber = $date->get('W');
335             $result[] = array(
336                 'field' => 'start_date',
337                 'operator' => 'inweek',
338                 'value' => $weekNumber
339             );
340         } else {
341             $result[] = array(
342                 'field' => 'start_date',
343                 'operator' => 'within',
344                 'value' => $_type
345             );
346         }
347
348         return $result;
349     }
350
351     /**
352      * add timesheet with customfield
353      *
354      * @param Tinebase_Model_CustomField_Config $_customField1
355      * @param string $_cf1Value
356      */
357     protected function _addTsWithCf($_customField1, $_cf1Value)
358     {
359         // create custom fields
360         $customField2 = $this->_getCustomField();
361
362         // create timesheet and add custom fields
363         $timesheetArray = $this->_getTimesheet()->toArray();
364         $timesheetArray[$_customField1->name] = $_cf1Value;
365         $timesheetArray[$customField2->name] = Tinebase_Record_Abstract::generateUID();
366
367         $timesheetData = $this->_json->saveTimesheet($timesheetArray);
368         
369         // checks
370         $this->assertTrue((isset($timesheetData['customfields'][$_customField1->name]) || array_key_exists($_customField1->name, $timesheetData['customfields'])), 'cf 1 not found');
371         $this->assertTrue((isset($timesheetData['customfields'][$customField2->name]) || array_key_exists($customField2->name, $timesheetData['customfields'])), 'cf 2 not found');
372         $this->assertGreaterThan(0, count($timesheetData['customfields']));
373         $this->assertEquals($timesheetArray[$_customField1->name], $timesheetData['customfields'][$_customField1->name]);
374         $this->assertEquals($timesheetArray[$customField2->name], $timesheetData['customfields'][$customField2->name]);
375
376         // check if custom fields are returned with search
377         $searchResult = $this->_json->searchTimesheets($this->_getTimesheetFilter(), $this->_getPaging());
378         $this->assertGreaterThan(0, count($searchResult['results'][0]['customfields']));
379         foreach($searchResult['results'] as $result) {
380             if ($result['id'] == $timesheetData['id']) {
381                 $ts = $result;
382             }
383         }
384         $this->assertTrue(isset($ts));
385         $this->assertTrue((isset($ts['customfields'][$_customField1->name]) || array_key_exists($_customField1->name, $ts['customfields'])));
386         $this->assertTrue((isset($ts['customfields'][$customField2->name]) || array_key_exists($customField2->name, $ts['customfields'])));
387         $this->assertEquals($timesheetArray[$_customField1->name], $ts['customfields'][$_customField1->name]);
388         $this->assertEquals($timesheetArray[$customField2->name], $ts['customfields'][$customField2->name]);
389
390         // test search with custom field filter
391         $searchResult = $this->_json->searchTimesheets(
392             $this->_getTimesheetFilterWithCustomField($_customField1->getId(), $_cf1Value),
393             $this->_getPaging()
394         );
395         $this->assertGreaterThan(0, $searchResult['totalcount'], 'cf filter not working');
396     }
397
398     /**
399      * get last created record
400      */
401     protected function _getLastCreatedRecord() {
402         return $this->_lastCreatedRecord;
403     }
404 }