/**
  * add timesheet with customfield
  *
  * @param Tinebase_Model_CustomField_Config $_customField1
  * @param string $_cf1Value
  */
 protected function _addTsWithCf($_customField1, $_cf1Value)
 {
     // create custom fields
     $customField2 = $this->_getCustomField();
     // create timesheet and add custom fields
     $timesheetArray = $this->_getTimesheet()->toArray();
     $timesheetArray[$_customField1->name] = $_cf1Value;
     $timesheetArray[$customField2->name] = Tinebase_Record_Abstract::generateUID();
     $timesheetData = $this->_json->saveTimesheet($timesheetArray);
     // checks
     $this->assertTrue(isset($timesheetData['customfields'][$_customField1->name]) || array_key_exists($_customField1->name, $timesheetData['customfields']), 'cf 1 not found');
     $this->assertTrue(isset($timesheetData['customfields'][$customField2->name]) || array_key_exists($customField2->name, $timesheetData['customfields']), 'cf 2 not found');
     $this->assertGreaterThan(0, count($timesheetData['customfields']));
     $this->assertEquals($timesheetArray[$_customField1->name], $timesheetData['customfields'][$_customField1->name]);
     $this->assertEquals($timesheetArray[$customField2->name], $timesheetData['customfields'][$customField2->name]);
     // check if custom fields are returned with search
     $searchResult = $this->_json->searchTimesheets($this->_getTimesheetFilter(), $this->_getPaging());
     $this->assertGreaterThan(0, count($searchResult['results'][0]['customfields']));
     foreach ($searchResult['results'] as $result) {
         if ($result['id'] == $timesheetData['id']) {
             $ts = $result;
         }
     }
     $this->assertTrue(isset($ts));
     $this->assertTrue(isset($ts['customfields'][$_customField1->name]) || array_key_exists($_customField1->name, $ts['customfields']));
     $this->assertTrue(isset($ts['customfields'][$customField2->name]) || array_key_exists($customField2->name, $ts['customfields']));
     $this->assertEquals($timesheetArray[$_customField1->name], $ts['customfields'][$_customField1->name]);
     $this->assertEquals($timesheetArray[$customField2->name], $ts['customfields'][$customField2->name]);
     // test search with custom field filter
     $searchResult = $this->_json->searchTimesheets($this->_getTimesheetFilterWithCustomField($_customField1->getId(), $_cf1Value), $this->_getPaging());
     $this->assertGreaterThan(0, $searchResult['totalcount'], 'cf filter not working');
 }
 /**
  * test constraints after changing relation
  */
 public function testTimeaccountRelation()
 {
     $sjson = new Sales_Frontend_Json();
     $tjson = new Timetracker_Frontend_Json();
     $ta = $tjson->saveTimeaccount(array('number' => 43379, 'title' => 'bla'));
     $c1 = $sjson->saveContract(array('number' => '1', 'description' => 'blub bla', 'title' => 'blub'));
     $c2 = $sjson->saveContract(array('number' => '2', 'description' => 'bla blub', 'title' => 'bla'));
     $c1['relations'] = array(array('related_model' => 'Timetracker_Model_Timeaccount', 'related_id' => $ta['id'], 'related_degree' => 'sibling', 'type' => 'TIME_ACCOUNT', 'remark' => 'unittest', 'related_backend' => 'Sql'));
     $c1 = $sjson->saveContract($c1);
     $c1Id = $c1['id'];
     // delete timeaccount relation from the first contract
     $c1 = $sjson->getContract($c1Id);
     $c1['relations'] = array();
     $c1 = $sjson->saveContract($c1);
     // save second contract having the timeaccount related
     $c2['relations'] = array(array('related_model' => 'Timetracker_Model_Timeaccount', 'related_id' => $ta['id'], 'related_degree' => 'sibling', 'type' => 'TIME_ACCOUNT', 'remark' => 'unittest', 'related_backend' => 'Sql'));
     $c2 = $sjson->saveContract($c2);
     $this->assertEquals(1, count($c2['relations']));
 }
 /**
  * test and filter
  * @see: 0009730: Fix & use Explicit_Related_Record Filter in all applications
  */
 public function testTimeaccountFailureFilter()
 {
     $req = Zend_Json::decode('{"params":{"filter":
         [{"condition":"OR","filters":[{"condition":"AND","filters":
         [{"field":"start_date","operator":"within","value":"weekLast","id":"ext-record-1"},{"field":"account_id","operator":"AND","value":
         [{"field":"query","operator":"contains","value":"43518","id":"ext-record-
         95"}],"id":"ext-record-2"}],"id":"ext-comp-1074","label":"Stundenzettel"}]}],"paging":
         {"sort":"start_date","dir":"ASC","start":0,"limit":50}}');
     $feTa = new Timetracker_Frontend_Json();
     $result = $feTa->searchTimesheets($req['params']['filter'], $req['params']['paging']);
     $this->assertArrayHasKey('results', $result);
 }
 /**
  * test if relation record gets deleted on both sides on deleting the relation on one side
  */
 public function testDeleteTimeaccountWitContractRelation()
 {
     $taContainer = Tinebase_Container::getInstance()->getDefaultContainer('Timetracker_Model_Timeaccount');
     $cContainer = Tinebase_Container::getInstance()->getDefaultContainer('Sales_Model_Contract');
     $ta = new Timetracker_Model_Timeaccount(array('number' => 83209, 'title' => 'unitttest', 'container_id' => $taContainer->getId()));
     $contract = new Sales_Model_Contract(array('number' => 83209, 'title' => 'unittest', 'container_id' => $cContainer->getId()));
     $contract = Sales_Controller_Contract::getInstance()->create($contract);
     $ta = Timetracker_Controller_Timeaccount::getInstance()->create($ta);
     $r = new Tinebase_Model_Relation(array('own_model' => 'Timetracker_Model_Timeaccount', 'own_backend' => 'Sql', 'related_degree' => 'sibling', 'own_id' => $ta->getId(), 'remark' => 'PHP UNITTEST', 'related_model' => 'Sales_Model_Contract', 'related_backend' => 'Sql', 'related_id' => $contract->getId(), 'type' => 'CONTRACT'));
     $ta->relations = array($r);
     $ta = Timetracker_Controller_Timeaccount::getInstance()->update($ta);
     $feTa = new Timetracker_Frontend_Json();
     $feCo = new Sales_Frontend_Json();
     $jsonTa = $feTa->getTimeaccount($ta->getId());
     $jsonCo = $feCo->getContract($contract->getId());
     $this->assertEquals(1, count($jsonTa['relations']));
     $this->assertEquals(1, count($jsonCo['relations']));
     $feTa->deleteTimeaccounts(array($ta->getId()));
     $jsonCo = $feCo->getContract($contract->getId());
     $this->assertEquals(0, count($jsonCo['relations']));
 }