/**
  * test timeaccount - sales contract filter
  * also tests Tinebase_Model_Filter_ExplicitRelatedRecord
  */
 public function testTimeaccountContractFilter()
 {
     $this->_getTimeaccount(array('title' => 'TA1', 'number' => 12345, 'description' => 'UnitTest'), true);
     $ta1 = $this->_timeaccountController->get($this->_lastCreatedRecord['id']);
     $this->_getTimeaccount(array('title' => 'TA2', 'number' => 12346, 'description' => 'UnitTest'), true);
     $ta2 = $this->_timeaccountController->get($this->_lastCreatedRecord['id']);
     $cId = Tinebase_Container::getInstance()->getDefaultContainer('Sales_Model_Contract')->getId();
     $contract = Sales_Controller_Contract::getInstance()->create(new Sales_Model_Contract(array('title' => 'testRelateTimeaccount', 'number' => Tinebase_Record_Abstract::generateUID(), 'container_id' => $cId)));
     $ta1->relations = array($this->_getRelation($contract, $ta1));
     $this->_timeaccountController->update($ta1);
     // search by contract
     $f = new Timetracker_Model_TimeaccountFilter(array(array('field' => 'contract', 'operator' => 'AND', 'value' => array(array('field' => ':id', 'operator' => 'equals', 'value' => $contract->getId())))));
     $filterArray = $f->toArray();
     $this->assertEquals($contract->getId(), $filterArray[0]['value'][0]['value']['id']);
     $result = $this->_timeaccountController->search($f);
     $this->assertEquals(1, $result->count());
     $this->assertEquals('TA1', $result->getFirstRecord()->title);
     // test empty filter (without contract)
     $f = new Timetracker_Model_TimeaccountFilter(array(array('field' => 'contract', 'operator' => 'AND', 'value' => array(array('field' => ':id', 'operator' => 'equals', 'value' => null))), array('field' => 'description', 'operator' => 'equals', 'value' => 'UnitTest')));
     $result = $this->_timeaccountController->search($f);
     $this->assertEquals(1, $result->count(), 'Only one record should have been found!');
     $this->assertEquals('TA2', $result->getFirstRecord()->title);
     // test generic relation filter
     $f = new Timetracker_Model_TimeaccountFilter(array(array('field' => 'foreignRecord', 'operator' => 'AND', 'value' => array('appName' => 'Sales', 'linkType' => 'relation', 'modelName' => 'Contract', 'filters' => array('field' => 'query', 'operator' => 'contains', 'value' => 'TA1')))));
     $result = $this->_timeaccountController->search($f);
     $this->assertEquals(1, $result->count());
     $this->assertEquals('TA1', $result->getFirstRecord()->title);
     // test "not" operator
     $f = new Timetracker_Model_TimeaccountFilter(array(array('field' => 'contract', 'operator' => 'AND', 'value' => array(array('field' => ':id', 'operator' => 'not', 'value' => $contract->getId()))), array('field' => 'description', 'operator' => 'equals', 'value' => 'UnitTest')));
     $result = $this->_timeaccountController->search($f);
     // TODO is this correct? do we expect the timaccount without contract to be missing from results?
     $this->assertEquals(0, $result->count(), 'No record should be found');
 }
 /**
  * try to add a Timesheet with different grants
  * 
  * @param   Tinebase_Record_RecordSet $_grants
  * @param   string $_action
  * @param   mixed $_expect
  * @param   Timetracker_Model_Timesheet
  */
 protected function _grantTestHelper($_grants, $_action = 'create', $_expect = NULL, $_ts = NULL)
 {
     // take default ts?
     $ts = $_ts ? $_ts : $this->_objects['timesheet'];
     // remove BOOK_OWN + BOOK_ALL + ADMIN grant
     Timetracker_Model_TimeaccountGrants::setTimeaccountGrants($this->_objects['timeaccount'], $_grants, TRUE);
     // try to create timesheet
     switch ($_action) {
         case 'create':
             if ($_expect === 'Exception') {
                 $this->setExpectedException('Tinebase_Exception_AccessDenied');
                 $this->_timesheetController->create($ts);
             } else {
                 $ts = $this->_timesheetController->create($ts);
                 $this->assertEquals(Tinebase_Core::getUser()->getId(), $ts->created_by);
             }
             break;
         case 'create_deadline':
             // date is before deadline
             $date = new Tinebase_DateTime();
             $date->sub(8, Tinebase_DateTime::MODIFIER_DAY);
             $ts->start_date = $date->toString('Y-m-d');
             $this->setExpectedException('Timetracker_Exception_Deadline');
             $this->_timesheetController->create($ts);
             break;
         case 'search_bookable':
             $filter = $this->_getTimeaccountFilter(TRUE);
             $result = $this->_timeaccountController->search($filter);
             $this->assertEquals($_expect, count($result));
             break;
         case 'searchTA':
             $filter = $this->_getTimeaccountFilter();
             $result = $this->_timeaccountController->search($filter);
             $this->assertEquals($_expect, count($result));
             break;
         case 'searchTS':
             $filter = $this->_getTimesheetFilter();
             $ts = $this->_timesheetController->create($ts);
             $result = $this->_timesheetController->search($filter);
             $this->assertEquals($_expect, count($result));
             break;
         case 'searchTSExport':
             $filter = $this->_getTimesheetFilter();
             $result = $this->_timesheetController->search($filter, NULL, FALSE, FALSE, 'export');
             $this->assertEquals($_expect, count($result));
             break;
         default:
             echo "nothing tested.";
     }
     // delete (set delete grant first)
     $grants = new Tinebase_Record_RecordSet('Timetracker_Model_TimeaccountGrants', array(array('account_id' => Tinebase_Core::getUser()->getId(), 'account_type' => 'user', Timetracker_Model_TimeaccountGrants::MANAGE_BILLABLE => TRUE, Tinebase_Model_Grants::GRANT_ADMIN => TRUE, Timetracker_Model_TimeaccountGrants::BOOK_ALL => TRUE, Timetracker_Model_TimeaccountGrants::BOOK_OWN => TRUE, Timetracker_Model_TimeaccountGrants::VIEW_ALL => TRUE, Tinebase_Model_Grants::GRANT_EXPORT => TRUE)));
     Timetracker_Model_TimeaccountGrants::setTimeaccountGrants($this->_objects['timeaccount'], $grants, TRUE);
 }