/** * returns record prepared for json transport * * @param Tinebase_Record_Interface $_record * @return array record data */ protected function _recordToJson($_record) { switch (get_class($_record)) { case 'Timetracker_Model_Timesheet': $_record['timeaccount_id'] = $_record['timeaccount_id'] ? $this->_timeaccountController->get($_record['timeaccount_id']) : $_record['timeaccount_id']; $_record['timeaccount_id']['account_grants'] = Timetracker_Model_TimeaccountGrants::getGrantsOfAccount(Tinebase_Core::get('currentAccount'), $_record['timeaccount_id']); $_record['timeaccount_id']['account_grants'] = $this->_resolveTimesheetGrantsByTimeaccountGrants($_record['timeaccount_id']['account_grants'], $_record['account_id']); Tinebase_User::getInstance()->resolveUsers($_record, 'account_id'); $recordArray = parent::_recordToJson($_record); break; case 'Timetracker_Model_Timeaccount': $recordArray = parent::_recordToJson($_record); // When editing a single TA we send _ALL_ grants to the client $recordArray['grants'] = Timetracker_Model_TimeaccountGrants::getTimeaccountGrants($_record)->toArray(); foreach ($recordArray['grants'] as &$value) { switch ($value['account_type']) { case Tinebase_Acl_Rights::ACCOUNT_TYPE_USER: $value['account_name'] = Tinebase_User::getInstance()->getUserById($value['account_id'])->toArray(); break; case Tinebase_Acl_Rights::ACCOUNT_TYPE_GROUP: $value['account_name'] = Tinebase_Group::getInstance()->getGroupById($value['account_id'])->toArray(); break; case Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE: $value['account_name'] = array('accountDisplayName' => 'Anyone'); break; default: throw new Tinebase_Exception_InvalidArgument('Unsupported accountType.'); break; } } break; } return $recordArray; }
/** * 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'); }
/** * Sets up the fixture. * This method is called before a test is executed. * * @access protected */ protected function setUp() { parent::setUp(); $this->_timeaccountController = Timetracker_Controller_Timeaccount::getInstance(); $this->_timesheetController = Timetracker_Controller_Timesheet::getInstance(); // get timesheet $this->_objects['timesheet'] = $this->_getTimesheet(); $this->_objects['timeaccount'] = $this->_timeaccountController->get($this->_objects['timesheet']->timeaccount_id); Tinebase_Acl_Roles::getInstance()->resetClassCache(); $this->_roleRights = self::removeManageAllRight(); }
/** * returns record prepared for json transport * * @param Tinebase_Record_Interface $_record * @return array record data */ protected function _recordToJson($_record) { switch (get_class($_record)) { case 'Timetracker_Model_Timesheet': $_record['timeaccount_id'] = $_record['timeaccount_id'] ? $this->_timeaccountController->get($_record['timeaccount_id']) : $_record['timeaccount_id']; $_record['timeaccount_id']['account_grants'] = Timetracker_Model_TimeaccountGrants::getGrantsOfAccount(Tinebase_Core::get('currentAccount'), $_record['timeaccount_id']); $_record['timeaccount_id']['account_grants'] = $this->_resolveTimesheetGrantsByTimeaccountGrants($_record['timeaccount_id']['account_grants'], $_record['account_id']); Tinebase_User::getInstance()->resolveUsers($_record, 'account_id'); if (Tinebase_Core::getUser()->hasRight('Sales', 'manage_invoices') && !empty($_record['invoice_id'])) { try { $_record['invoice_id'] = Sales_Controller_Invoice::getInstance()->get($_record['invoice_id']); } catch (Tinebase_Exception_NotFound $nfe) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not resolve invoice with id ' . $_record['invoice_id']); } } $recordArray = parent::_recordToJson($_record); break; case 'Timetracker_Model_Timeaccount': $recordArray = parent::_recordToJson($_record); // When editing a single TA we send _ALL_ grants to the client $recordArray['grants'] = Timetracker_Model_TimeaccountGrants::getTimeaccountGrants($_record)->toArray(); foreach ($recordArray['grants'] as &$value) { switch ($value['account_type']) { case Tinebase_Acl_Rights::ACCOUNT_TYPE_USER: $value['account_name'] = Tinebase_User::getInstance()->getUserById($value['account_id'])->toArray(); break; case Tinebase_Acl_Rights::ACCOUNT_TYPE_GROUP: $value['account_name'] = Tinebase_Group::getInstance()->getGroupById($value['account_id'])->toArray(); break; case Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE: $value['account_name'] = array('accountDisplayName' => 'Anyone'); break; default: throw new Tinebase_Exception_InvalidArgument('Unsupported accountType.'); break; } } break; } return $recordArray; }