From 8ffdec1716359d3e71a8c89c5a7142c8422dbefa Mon Sep 17 00:00:00 2001 From: Adam Roses Wight Date: Tue, 29 Apr 2014 14:45:21 -0700 Subject: [PATCH] Case Type refactoring TODO: * Test code paths not covered by units. CRM-14473: Migrate case-types from OptionGroup to new table --- CRM/Admin/Form/CaseType.php | 134 +++++++++++++++ CRM/Admin/Page/CaseType.php | 122 ++++++++++++++ CRM/Case/BAO/Case.php | 74 +++------ CRM/Case/BAO/CaseType.php | 157 ++++++++++++++++++ CRM/Case/BAO/Query.php | 19 +-- .../Form/Activity/ChangeCaseStartDate.php | 11 +- CRM/Case/Form/Activity/ChangeCaseType.php | 11 +- CRM/Case/Form/Activity/OpenCase.php | 7 - CRM/Case/Form/Case.php | 4 +- CRM/Case/Form/CaseView.php | 9 +- CRM/Case/Info.php | 47 ------ CRM/Case/Page/AJAX.php | 11 +- CRM/Case/PseudoConstant.php | 73 +++----- CRM/Case/XMLProcessor/Process.php | 2 +- CRM/Case/xml/HRD/HRD.mysql | 9 +- CRM/Case/xml/Menu/Case.xml | 2 +- .../configuration.sample/SampleConfig.mysql | 8 +- api/v3/Case.php | 9 +- templates/CRM/Admin/Form/CaseType.tpl | 52 ++++++ templates/CRM/Admin/Page/CaseType.tpl | 77 +++++++++ tests/phpunit/CRM/Case/AllTests.php | 33 ++++ tests/phpunit/CRM/Case/BAO/CaseTest.php | 72 ++++++++ tests/phpunit/CRM/Case/PseudoConstantTest.php | 28 ++++ tests/phpunit/api/v3/CaseTest.php | 41 +---- tests/phpunit/api/v3/dataset/case_types.xml | 21 +++ 25 files changed, 781 insertions(+), 252 deletions(-) create mode 100644 CRM/Admin/Form/CaseType.php create mode 100644 CRM/Admin/Page/CaseType.php create mode 100644 CRM/Case/BAO/CaseType.php create mode 100644 templates/CRM/Admin/Form/CaseType.tpl create mode 100644 templates/CRM/Admin/Page/CaseType.tpl create mode 100644 tests/phpunit/CRM/Case/AllTests.php create mode 100644 tests/phpunit/CRM/Case/BAO/CaseTest.php create mode 100644 tests/phpunit/CRM/Case/PseudoConstantTest.php create mode 100644 tests/phpunit/api/v3/dataset/case_types.xml diff --git a/CRM/Admin/Form/CaseType.php b/CRM/Admin/Form/CaseType.php new file mode 100644 index 0000000000..201fe24dc9 --- /dev/null +++ b/CRM/Admin/Form/CaseType.php @@ -0,0 +1,134 @@ +_action & CRM_Core_Action::DELETE) { + return; + } + $this->applyFilter('__ALL__', 'trim'); + $this->add('text', 'title', ts('Name'), + CRM_Core_DAO::getAttribute('CRM_Case_DAO_CaseType', 'title'), + TRUE + ); + $enabled = $this->add('checkbox', 'is_active', ts('Enabled?')); + $this->add('text', 'description', ts('Description'), + CRM_Core_DAO::getAttribute('CRM_Case_DAO_CaseType', 'description') + ); + + $this->assign('cid', $this->_id); + $this->addFormRule(array('CRM_Admin_Form_CaseType', 'formRule'), $this); + } + + /** + * global form rule + * + * @param array $fields the input form values + * + * @return true if no errors, else array of errors + * @access public + * @static + */ + static function formRule($fields, $files, $self) { + + $errors = array(); + + if ($self->_id) { + $caseName = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $self->_id, 'name'); + } + else { + $caseName = ucfirst(CRM_Utils_String::munge($fields['title'])); + } + + if (!CRM_Core_DAO::objectExists($caseName, 'CRM_Case_DAO_CaseType', $self->_id)) { + $errors['title'] = ts('This case type name already exists in database. Case type names must be unique.'); + } + + $reservedKeyWords = CRM_Core_SelectValues::customGroupExtends(); + //restrict "name" from being a reserved keyword when a new contact subtype is created + if (!$self->_id && in_array($caseName, array_keys($reservedKeyWords))) { + $errors['title'] = ts('Case Type names should not use reserved keywords.'); + } + return empty($errors) ? TRUE : $errors; + } + + /** + * Function to process the form + * + * @access public + * + * @return void + */ + public function postProcess() { + CRM_Utils_System::flushCache(); + + if ($this->_action & CRM_Core_Action::DELETE) { + $isDelete = CRM_Case_BAO_CaseType::del($this->_id); + if ($isDelete) { + CRM_Core_Session::setStatus(ts('Selected case type has been deleted.'), ts('Record Deleted'), 'success'); + } + else { + CRM_Core_Session::setStatus(ts("Selected case type can not be deleted."), ts('Sorry'), 'error'); + } + return; + } + // store the submitted values in an array + $params = $this->exportValues(); + + if ($this->_action & CRM_Core_Action::ADD) { + $params['name'] = ucfirst(CRM_Utils_String::munge($params['title'])); + } else { + $params['id'] = $this->_id; + $params['name'] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $this->_id, 'name'); + } + $caseType = CRM_Case_BAO_CaseType::add($params); + CRM_Core_Session::setStatus(ts("The Case Type '%1' has been saved.", + array(1 => $caseType->title) + ), ts('Saved'), 'success'); + } +} + diff --git a/CRM/Admin/Page/CaseType.php b/CRM/Admin/Page/CaseType.php new file mode 100644 index 0000000000..7f4e393f5b --- /dev/null +++ b/CRM/Admin/Page/CaseType.php @@ -0,0 +1,122 @@ + + array( + 'name' => ts('Edit'), + 'url' => 'civicrm/admin/options/case_type', + 'qs' => 'action=update&id=%%id%%&reset=1', + 'title' => ts('Edit Case Type'), + ), + CRM_Core_Action::DISABLE => + array( + 'name' => ts('Disable'), + 'ref' => 'crm-enable-disable', + 'title' => ts('Disable Case Type'), + ), + CRM_Core_Action::ENABLE => + array( + 'name' => ts('Enable'), + 'ref' => 'crm-enable-disable', + 'title' => ts('Enable Case Type'), + ), + CRM_Core_Action::DELETE => + array( + 'name' => ts('Delete'), + 'url' => 'civicrm/admin/options/case_type', + 'qs' => 'action=delete&id=%%id%%', + 'title' => ts('Delete Case Type'), + ), + ); + } + return self::$_links; + } + + /** + * Get name of edit form + * + * @return string Classname of edit form. + */ + function editForm() { + return 'CRM_Admin_Form_CaseType'; + } + + /** + * Get edit form name + * + * @return string name of this page. + */ + function editName() { + return 'Case Types'; + } + + /** + * Get user context. + * + * @return string user context. + */ + function userContext($mode = NULL) { + return 'civicrm/admin/options/case_type'; + } +} diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index c5ab00933e..f7c3139d52 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -158,12 +158,12 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { $caseContact->save(); // add to recently viewed - $caseType = CRM_Case_PseudoConstant::caseTypeName($caseContact->case_id, 'label'); + $caseType = CRM_Case_BAO_Case::getCaseType($caseContact->case_id); $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&id={$caseContact->case_id}&cid={$caseContact->contact_id}&context=home" ); - $title = CRM_Contact_BAO_Contact::displayName($caseContact->contact_id) . ' - ' . $caseType['name']; + $title = CRM_Contact_BAO_Contact::displayName($caseContact->contact_id) . ' - ' . $caseType; $recentOther = array(); if (CRM_Core_Permission::checkActionPermission('CiviCase', CRM_Core_Action::DELETE)) { @@ -300,22 +300,16 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { * @access public * @static */ - static function getCaseType($caseId, $colName = 'label') { - $caseType = NULL; - if (!$caseId) { - return $caseType; - } - - $sql = " - SELECT ov.{$colName} - FROM civicrm_case ca - INNER JOIN civicrm_option_group og ON og.name='case_type' - INNER JOIN civicrm_option_value ov ON ( ca.case_type_id=ov.value AND ov.option_group_id=og.id ) - WHERE ca.id = %1"; + static function getCaseType($caseId, $colName = 'title') { + $query = " +SELECT civicrm_case_type.{$colName} FROM civicrm_case +LEFT JOIN civicrm_case_type ON + civicrm_case.case_type_id = civicrm_case_type.id +WHERE civicrm_case.id = %1"; - $params = array(1 => array($caseId, 'Integer')); + $queryParams = array(1 => array($caseId, 'Integer')); - return CRM_Core_DAO::singleValueQuery($sql, $params); + return CRM_Core_DAO::singleValueQuery($query, $queryParams); } /** @@ -523,9 +517,8 @@ INNER JOIN civicrm_case ca ON cc.case_id = ca.id "; if (isset($caseType)) { $query .= -"INNER JOIN civicrm_option_value cov ON (cov.value = ca.case_type_id) -INNER JOIN civicrm_option_group cog ON (cog.id = cov.option_group_id and cog.name = 'case_type') -WHERE cc.contact_id = %1 AND cov.name = '{$caseType}'"; +"INNER JOIN civicrm_case_type ON civicrm_case_type.id = ca.case_type_id +WHERE cc.contact_id = %1 AND civicrm_case_type.name = '{$caseType}'"; } if (!isset($caseType)) { $query .= "WHERE cc.contact_id = %1"; @@ -564,8 +557,8 @@ civicrm_phone.phone as phone, civicrm_contact.contact_type as contact_type, civicrm_contact.contact_sub_type as contact_sub_type, t_act.activity_type_id, -cov_type.label as case_type, -cov_type.name as case_type_name, +c_type.title as case_type, +civicrm_case.case_type_id as case_type_id, cov_status.label as case_status, cov_status.label as case_status_name, t_act.status_id, @@ -666,12 +659,8 @@ LEFT JOIN civicrm_option_group aog ON aog.name='activity_type' ON ( case_relation_type.id = case_relationship.relationship_type_id AND case_relation_type.id = case_relationship.relationship_type_id ) - LEFT JOIN civicrm_option_group cog_type - ON cog_type.name = 'case_type' - - LEFT JOIN civicrm_option_value cov_type - ON ( civicrm_case.case_type_id = cov_type.value - AND cog_type.id = cov_type.option_group_id ) + LEFT JOIN civicrm_case_type c_type + ON civicrm_case.case_type_id = c_type.id LEFT JOIN civicrm_option_group cog_status ON cog_status.name = 'case_status' @@ -762,7 +751,7 @@ AND civicrm_case.status_id != $closedId"; 'case_id', 'case_subject', 'case_type', - 'case_type_name', + 'case_type_id', 'status_id', 'case_status', 'case_status_name', @@ -916,15 +905,11 @@ AND civicrm_case.status_id != $closedId"; $myGroupByClause = " GROUP BY CONCAT(case_relationship.case_id,'-',case_relationship.contact_id_b)"; } - $seperator = CRM_Core_DAO::VALUE_SEPARATOR; - $query = " -SELECT case_status.label AS case_status, status_id, case_type.label AS case_type, - REPLACE(case_type_id,'{$seperator}','') AS case_type_id, case_relationship.contact_id_b +SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS case_type, + case_type_id, case_relationship.contact_id_b FROM civicrm_case - LEFT JOIN civicrm_option_group option_group_case_type ON ( option_group_case_type.name = 'case_type' ) - LEFT JOIN civicrm_option_value case_type ON ( civicrm_case.case_type_id = case_type.value - AND option_group_case_type.id = case_type.option_group_id ) + LEFT JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id LEFT JOIN civicrm_option_group option_group_case_status ON ( option_group_case_status.name = 'case_status' ) LEFT JOIN civicrm_option_value case_status ON ( civicrm_case.status_id = case_status.value AND option_group_case_status.id = case_status.option_group_id ) @@ -2002,7 +1987,7 @@ SELECT civicrm_contact.id as casemanager_id, //params from ajax call. $where = array('( ca.end_date is null )'); if ($caseType = CRM_Utils_Array::value('case_type', $params)) { - $where[] = "( ov.label LIKE '%$caseType%' )"; + $where[] = "( civicrm_case_type.title LIKE '%$caseType%' )"; } if ($sortName = CRM_Utils_Array::value('sort_name', $params)) { $config = CRM_Core_Config::singleton(); @@ -2038,12 +2023,11 @@ SELECT civicrm_contact.id as casemanager_id, c.sort_name, ca.id, ca.subject as case_subject, - ov.label as case_type, + civicrm_case_type.title as case_type, ca.start_date as start_date FROM civicrm_case ca INNER JOIN civicrm_case_contact cc ON ca.id=cc.case_id INNER JOIN civicrm_contact c ON cc.contact_id=c.id - INNER JOIN civicrm_option_group og ON og.name='case_type' - INNER JOIN civicrm_option_value ov ON (ca.case_type_id=ov.value AND ov.option_group_id=og.id) + INNER JOIN civicrm_case_type ON ca.case_type_id = civicrm_case_type.id WHERE {$whereClause} ORDER BY c.sort_name {$limitClause} @@ -2124,12 +2108,10 @@ LEFT JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.case } $query = " - SELECT civicrm_case.id, case_type_ov.label as case_type, civicrm_case.start_date + SELECT civicrm_case.id, civicrm_case_type.title as case_type, civicrm_case.start_date FROM civicrm_case INNER JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.case_id ) - LEFT JOIN civicrm_option_group case_type_og ON ( case_type_og.name = 'case_type' ) - LEFT JOIN civicrm_option_value case_type_ov ON ( civicrm_case.case_type_id = case_type_ov.value - AND case_type_og.id = case_type_ov.option_group_id ) + LEFT JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id WHERE {$whereClause}"; $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($contactId, 'Integer'))); @@ -2218,15 +2200,13 @@ INNER JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.cas //2. fetch the details of related cases. $query = " SELECT relCase.id as id, - case_type_ov.label as case_type, + civicrm_case_type.title as case_type, client.display_name as client_name, client.id as client_id FROM civicrm_case relCase INNER JOIN civicrm_case_contact relCaseContact ON ( relCase.id = relCaseContact.case_id ) INNER JOIN civicrm_contact client ON ( client.id = relCaseContact.contact_id ) - LEFT JOIN civicrm_option_group case_type_og ON ( case_type_og.name = 'case_type' ) - LEFT JOIN civicrm_option_value case_type_ov ON ( relCase.case_type_id = case_type_ov.value - AND case_type_og.id = case_type_ov.option_group_id ) + LEFT JOIN civicrm_case_type ON relCase.case_type_id = civicrm_case_type.id WHERE {$whereClause}"; $dao = CRM_Core_DAO::executeQuery($query); diff --git a/CRM/Case/BAO/CaseType.php b/CRM/Case/BAO/CaseType.php new file mode 100644 index 0000000000..3a9f02ca1d --- /dev/null +++ b/CRM/Case/BAO/CaseType.php @@ -0,0 +1,157 @@ +copyValues($params); + return $caseTypeDAO->save(); + } + + /** + * Given the list of params in the params array, fetch the object + * and store the values in the values array + * + * @param array $params input parameters to find object + * @param array $values output values of the object + * @param array $ids the array that holds all the db ids + * + * @return CRM_Case_BAO_CaseType|null the found object or null + * @access public + * @static + */ + static function &getValues(&$params, &$values) { + $caseType = new CRM_Case_BAO_CaseType(); + + $caseType->copyValues($params); + + if ($caseType->find(TRUE)) { + CRM_Core_DAO::storeValues($caseType, $values); + return $caseType; + } + return NULL; + } + + /** + * takes an associative array and creates a case type object + * + * @param array $params (reference ) an assoc array of name/value pairs + * @param array $ids the array that holds all the db ids + * + * @return object CRM_Case_BAO_CaseType object + * @access public + * @static + */ + static function &create(&$params) { + $transaction = new CRM_Core_Transaction(); + + if (!empty($params['id'])) { + CRM_Utils_Hook::pre('edit', 'CaseType', $params['id'], $params); + } + else { + CRM_Utils_Hook::pre('create', 'CaseType', NULL, $params); + } + + $caseType = self::add($params); + + if (is_a($caseType, 'CRM_Core_Error')) { + $transaction->rollback(); + return $caseType; + } + + if (!empty($params['id'])) { + CRM_Utils_Hook::post('edit', 'CaseType', $caseType->id, $case); + } + else { + CRM_Utils_Hook::post('create', 'CaseType', $caseType->id, $case); + } + $transaction->commit(); + + return $caseType; + } + + /** + * Takes a bunch of params that are needed to match certain criteria and + * retrieves the relevant objects. We'll tweak this function to be more + * full featured over a period of time. This is the inverse function of + * create. It also stores all the retrieved values in the default array + * + * @param array $params (reference ) an assoc array of name/value pairs + * @param array $defaults (reference ) an assoc array to hold the name / value pairs + * in a hierarchical manner + * @param array $ids (reference) the array that holds all the db ids + * + * @return object CRM_Case_BAO_CaseType object + * @access public + * @static + */ + static function retrieve(&$params, &$defaults) { + $caseType = CRM_Case_BAO_CaseType::getValues($params, $defaults); + return $caseType; + } + + static function del($caseTypeId) { + $caseType = new CRM_Case_DAO_CaseType(); + $caseType->id = $caseTypeId; + return $caseType->delete(); + } +} diff --git a/CRM/Case/BAO/Query.php b/CRM/Case/BAO/Query.php index 664bb841cd..aeeaaad7c4 100644 --- a/CRM/Case/BAO/Query.php +++ b/CRM/Case/BAO/Query.php @@ -61,16 +61,16 @@ class CRM_Case_BAO_Query { } if (!empty($query->_returnProperties['case_type_id'])) { - $query->_select['case_type_id'] = "case_type.id as case_type_id"; + $query->_select['case_type_id'] = "civicrm_case_type.id as case_type_id"; $query->_element['case_type_id'] = 1; $query->_tables['case_type'] = $query->_whereTables['case_type'] = 1; $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; } - if (!empty($query->_returnProperties['case_type'])) { - $query->_select['case_type'] = "case_type.label as case_type"; + if (!empty($query->_returnProperties['civicrm_case_type'])) { + $query->_select['case_type'] = "civicrm_case_type.title as case_type"; $query->_element['case_type'] = 1; - $query->_tables['case_type'] = $query->_whereTables['case_type'] = 1; + $query->_tables['civicrm_case_type'] = $query->_whereTables['civicrm_case_type'] = 1; $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; } @@ -269,7 +269,7 @@ class CRM_Case_BAO_Query { return; case 'case_type_id': - $caseTypes = CRM_Case_PseudoConstant::caseType('label', FALSE); + $caseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE); if (is_array($value)) { foreach ($value as $k => $v) { @@ -288,11 +288,7 @@ class CRM_Case_BAO_Query { $names[] = $caseTypes[$caseTypeId]; } - $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR . "%' OR civicrm_case.case_type_id LIKE '%" . - CRM_Core_DAO::VALUE_SEPARATOR, $val - ) . CRM_Core_DAO::VALUE_SEPARATOR; - - $query->_where[$grouping][] = "(civicrm_case.case_type_id LIKE '%{$value}%')"; + $query->_where[$grouping][] = "(civicrm_case.case_type_id LIKE '%{$val}%')"; $query->_qill[$grouping][] = ts('Case Type is %1', array(1 => implode(' ' . ts('or') . ' ', $names))); $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; @@ -537,8 +533,7 @@ class CRM_Case_BAO_Query { break; case 'case_type': - $from .= " $side JOIN civicrm_option_group option_group_case_type ON (option_group_case_type.name = 'case_type')"; - $from .= " $side JOIN civicrm_option_value case_type ON (civicrm_case.case_type_id = case_type.value AND option_group_case_type.id = case_type.option_group_id ) "; + $from .= " $side JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id "; break; case 'case_activity_type': diff --git a/CRM/Case/Form/Activity/ChangeCaseStartDate.php b/CRM/Case/Form/Activity/ChangeCaseStartDate.php index 1c080a13f4..1e2cfede90 100644 --- a/CRM/Case/Form/Activity/ChangeCaseStartDate.php +++ b/CRM/Case/Form/Activity/ChangeCaseStartDate.php @@ -127,16 +127,7 @@ class CRM_Case_Form_Activity_ChangeCaseStartDate { $caseType = $form->_caseType; if (!$caseType && $form->_caseId) { - - $query = " -SELECT cov_type.label as case_type FROM civicrm_case -LEFT JOIN civicrm_option_group cog_type ON cog_type.name = 'case_type' -LEFT JOIN civicrm_option_value cov_type ON -( civicrm_case.case_type_id = cov_type.value AND cog_type.id = cov_type.option_group_id ) -WHERE civicrm_case.id= %1"; - - $queryParams = array(1 => array($form->_caseId, 'Integer')); - $caseType = CRM_Core_DAO::singleValueQuery($query, $queryParams); + $caseType = CRM_Case_BAO_Case::getCaseType($form->_caseId, 'title'); } if (!$form->_currentlyViewedContactId || diff --git a/CRM/Case/Form/Activity/ChangeCaseType.php b/CRM/Case/Form/Activity/ChangeCaseType.php index 14c10b3c2c..25e9f13266 100644 --- a/CRM/Case/Form/Activity/ChangeCaseType.php +++ b/CRM/Case/Form/Activity/ChangeCaseType.php @@ -70,13 +70,12 @@ class CRM_Case_Form_Activity_ChangeCaseType { $form->removeElement('priority_id'); $form->_caseType = CRM_Case_PseudoConstant::caseType(); - $caseTypeId = explode(CRM_Case_BAO_Case::VALUE_SEPARATOR, CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', - $form->_caseId, - 'case_type_id' - )); - $form->_caseTypeId = $caseTypeId[1]; + $form->_caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', + $form->_caseId, + 'case_type_id' + ); if (!in_array($form->_caseTypeId, $form->_caseType)) { - $form->_caseType[$form->_caseTypeId] = CRM_Core_OptionGroup::getLabel('case_type', $form->_caseTypeId, FALSE); + $form->_caseType[$form->_caseTypeId] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $form->_caseTypeId, 'label'); } $form->add('select', 'case_type_id', ts('New Case Type'), diff --git a/CRM/Case/Form/Activity/OpenCase.php b/CRM/Case/Form/Activity/OpenCase.php index cc3ed18dc0..c4bfef3dcf 100644 --- a/CRM/Case/Form/Activity/OpenCase.php +++ b/CRM/Case/Form/Activity/OpenCase.php @@ -115,13 +115,6 @@ class CRM_Case_Form_Activity_OpenCase { if ($form->_caseTypeId) { $caseType = $form->_caseTypeId; } - else { - // set default case type if only one of it exists - $caseType = CRM_Core_OptionGroup::values('case_type', FALSE, FALSE, FALSE, 'AND is_default = 1'); - if (count($caseType) == 1) { - $caseType = key($caseType); - } - } $defaults['case_type_id'] = $caseType; $medium = CRM_Core_OptionGroup::values('encounter_medium', FALSE, FALSE, FALSE, 'AND is_default = 1'); diff --git a/CRM/Case/Form/Case.php b/CRM/Case/Form/Case.php index 3d99af4222..e8c6e9d4eb 100644 --- a/CRM/Case/Form/Case.php +++ b/CRM/Case/Form/Case.php @@ -384,10 +384,8 @@ class CRM_Case_Form_Case extends CRM_Core_Form { // 2. create/edit case if (!empty($params['case_type_id'])) { - $caseType = CRM_Case_PseudoConstant::caseType('name'); - $params['case_type'] = $caseType[$params['case_type_id']]; + $params['case_type'] = $params['case_type_id']; $params['subject'] = $params['activity_subject']; - $params['case_type_id'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['case_type_id'] . CRM_Core_DAO::VALUE_SEPARATOR; } $caseObj = CRM_Case_BAO_Case::create($params); $params['case_id'] = $caseObj->id; diff --git a/CRM/Case/Form/CaseView.php b/CRM/Case/Form/CaseView.php index 40178bb818..5b6532a29e 100644 --- a/CRM/Case/Form/CaseView.php +++ b/CRM/Case/Form/CaseView.php @@ -113,13 +113,6 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form { $returnProperties = array('case_type_id', 'subject', 'status_id', 'start_date'); CRM_Core_DAO::commonRetrieve('CRM_Case_BAO_Case', $params, $values, $returnProperties); - $values['case_type_id'] = trim(CRM_Utils_Array::value('case_type_id', $values), - CRM_Core_DAO::VALUE_SEPARATOR - ); - $values['case_type_id'] = explode(CRM_Core_DAO::VALUE_SEPARATOR, - CRM_Utils_Array::value('case_type_id', $values) - ); - $statuses = CRM_Case_PseudoConstant::caseStatus('label', FALSE); $caseTypeName = CRM_Case_BAO_Case::getCaseType($this->_caseID, 'name'); $caseType = CRM_Case_BAO_Case::getCaseType($this->_caseID); @@ -186,7 +179,7 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form { ))); } - $entitySubType = !empty($values['case_type_id']) ? $values['case_type_id'][0] : NULL; + $entitySubType = !empty($values['case_type_id']) ? $values['case_type_id'] : NULL; $this->assign('caseTypeID', $entitySubType); $groupTree = &CRM_Core_BAO_CustomGroup::getTree('Case', $this, diff --git a/CRM/Case/Info.php b/CRM/Case/Info.php index b77352ef8f..e0f4428d1c 100644 --- a/CRM/Case/Info.php +++ b/CRM/Case/Info.php @@ -52,53 +52,6 @@ class CRM_Case_Info extends CRM_Core_Component_Info { ); } - // docs inherited from interface - public function getManagedEntities() { - // Use hook_civicrm_caseTypes to build a list of OptionValues - // In the long run, we may want more specialized logic for this, but - // this design is fairly convenient and will allow us to replace it - // without changing the hook_civicrm_caseTypes interface. - $entities = array(); - - $caseTypes = array(); - CRM_Utils_Hook::caseTypes($caseTypes); - - $proc = new CRM_Case_XMLProcessor(); - $caseTypesGroupId = civicrm_api3('OptionGroup', 'getvalue', array('name' => 'case_type', 'return' => 'id')); - if (!is_numeric($caseTypesGroupId)) { - throw new CRM_Core_Exception("Found invalid ID for OptionGroup (case_type)"); - } - foreach ($caseTypes as $name => $caseType) { - $xml = $proc->retrieve($name); - if (!$xml) { - throw new CRM_Core_Exception("Failed to load XML for case type (" . $name . ")"); - } - - if (isset($caseType['module'], $caseType['name'], $caseType['file'])) { - $entities[] = array( - 'module' => $caseType['module'], - 'name' => $caseType['name'], - 'entity' => 'OptionValue', - 'params' => array( - 'version' => 3, - 'name' => $caseType['name'], - 'label' => (string) $xml->name, - 'description' => (string) $xml->description, // CRM_Utils_Array::value('description', $caseType, ''), - 'option_group_id' => $caseTypesGroupId, - 'filter' => empty($xml->filter) ? 0 : 1, - 'grouping' => $xml->grouping, - 'is_reserved' => 1, - ), - ); - } - else { - throw new CRM_Core_Exception("Invalid case type"); - } - } - - return $entities; - } - // docs inherited from interface public function getPermissions($getAllUnconditionally = FALSE) { return array( diff --git a/CRM/Case/Page/AJAX.php b/CRM/Case/Page/AJAX.php index 1bb41c16d8..7b466e7f09 100644 --- a/CRM/Case/Page/AJAX.php +++ b/CRM/Case/Page/AJAX.php @@ -125,18 +125,17 @@ class CRM_Case_Page_AJAX { function caseDetails() { $caseId = CRM_Utils_Type::escape($_GET['caseId'], 'Integer'); - $sql = "SELECT * FROM civicrm_case where id = %1"; + $sql = "SELECT civicrm_case.*, civicrm_case_type.title as case_type + FROM civicrm_case + INNER JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id + WHERE civicrm_case.id = %1"; $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($caseId, 'Integer'))); if ($dao->fetch()) { - $caseType = CRM_Case_BAO_Case::getCaseType((str_replace(CRM_Core_DAO::VALUE_SEPARATOR, - "", - $dao->case_type_id - ))); $caseStatuses = CRM_Case_PseudoConstant::caseStatus(); $cs = $caseStatuses[$dao->status_id]; $caseDetails = " - + " . CRM_Utils_Date::customFormat($dao->end_date) . "
" . ts('Case Subject') . "{$dao->subject}
" . ts('Case Type') . "{$caseType}
" . ts('Case Type') . "{$dao->case_type}
" . ts('Case Status') . "{$cs}
" . ts('Case Start Date') . "" . CRM_Utils_Date::customFormat($dao->start_date) . "
" . ts('Case End Date') . "
"; diff --git a/CRM/Case/PseudoConstant.php b/CRM/Case/PseudoConstant.php index 5f00dbd311..7696629b9e 100644 --- a/CRM/Case/PseudoConstant.php +++ b/CRM/Case/PseudoConstant.php @@ -74,13 +74,6 @@ class CRM_Case_PseudoConstant extends CRM_Core_PseudoConstant { */ static $activityTypeList = array(); - /** - * case type - * @var array - * @static - */ - static $caseTypePair = array(); - /** * Get all the case statues * @@ -141,19 +134,26 @@ class CRM_Case_PseudoConstant extends CRM_Core_PseudoConstant { * @return array - array reference of all case type * @static */ - public static function caseType($column = 'label', $onlyActive = TRUE, $condition = NULL) { - $cacheKey = "{$column}_" . (int)$onlyActive; - if (!$condition) { - $condition = 'AND filter = 0'; - } - if (!isset(self::$caseType[$cacheKey])) { - self::$caseType[$cacheKey] = CRM_Core_OptionGroup::values('case_type', - FALSE, FALSE, FALSE, $condition, - $column, $onlyActive - ); + public static function caseType($column = 'title', $onlyActive = TRUE) { + if ($onlyActive) { + $condition = " is_active = 1 "; + } else { + $condition = NULL; } - - return self::$caseType[$cacheKey]; + $caseType = NULL; + // FIXME: deprecated? + CRM_Core_PseudoConstant::populate( + $caseType, + 'CRM_Case_DAO_CaseType', + TRUE, + $column, + '', + $condition, + 'weight', + 'id' + ); + + return $caseType; } /** @@ -235,41 +235,6 @@ class CRM_Case_PseudoConstant extends CRM_Core_PseudoConstant { return self::$activityTypeList[$cache]; } - /** - * Get the associated case type name/id, given a case Id - * - * @access public - * - * @return array - array reference of all case type name/id - * @static - */ - public static function caseTypeName($caseId, $column = 'name') { - if (!$caseId) { - return FALSE; - } - - if (!array_key_exists($caseId, self::$caseTypePair) || empty(self::$caseTypePair[$caseId][$column])) { - $caseTypes = self::caseType($column); - $caseTypeIds = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', - $caseId, - 'case_type_id' - ); - $caseTypeId = explode(CRM_Core_DAO::VALUE_SEPARATOR, - trim($caseTypeIds, - CRM_Core_DAO::VALUE_SEPARATOR - ) - ); - $caseTypeId = $caseTypeId[0]; - - self::$caseTypePair[$caseId][$column] = array( - 'id' => $caseTypeId, - 'name' => $caseTypes[$caseTypeId], - ); - } - - return self::$caseTypePair[$caseId][$column]; - } - /** * Flush given pseudoconstant so it can be reread from db * next time it's requested. diff --git a/CRM/Case/XMLProcessor/Process.php b/CRM/Case/XMLProcessor/Process.php index f6991cb870..fa05dac55f 100644 --- a/CRM/Case/XMLProcessor/Process.php +++ b/CRM/Case/XMLProcessor/Process.php @@ -286,7 +286,7 @@ AND a.is_deleted = 0 $count = CRM_Core_DAO::singleValueQuery($query, $sqlParams); // check for max instance - $caseType = CRM_Case_BAO_Case::getCaseType($params['caseID'], 'name'); + $caseType = CRM_Case_BAO_Case::getCaseType($params['caseID'], 'title'); $maxInstance = self::getMaxInstance($caseType, $params['activityTypeName']); return $maxInstance ? ($count < $maxInstance ? FALSE : TRUE) : FALSE; diff --git a/CRM/Case/xml/HRD/HRD.mysql b/CRM/Case/xml/HRD/HRD.mysql index fb77d4cf03..9ffce8a6c7 100644 --- a/CRM/Case/xml/HRD/HRD.mysql +++ b/CRM/Case/xml/HRD/HRD.mysql @@ -10,15 +10,14 @@ -- * Case Types -- * -- *******************************************************/ -SELECT @option_group_id_case_type := max(id) from civicrm_option_group where name = 'case_type'; SELECT @caseCompId := id FROM `civicrm_component` where `name` like 'CiviCase'; INSERT INTO - `civicrm_option_value` (`option_group_id`, `label`, `value`, `name`, `grouping`, `filter`, `is_default`, `weight`, `description`, `is_optgroup`, `is_reserved`, `is_active`) + `civicrm_case_type` (`title`, `name`, `weight`, `is_reserved`, `is_active`) VALUES -(@option_group_id_case_type, 'Civil and Political' , 1, 'Civil and Political' , NULL, 0, 1, 1, NULL, 0, 1, 1), -(@option_group_id_case_type, 'Economic, Social and Cultural', 2, 'Economic, Social and Cultural', NULL, 0, 0, 2, NULL, 0, 1, 1), -(@option_group_id_case_type, 'Gender Issues' , 3, 'Gender Issues' , NULL, 0, 0, 3, NULL, 0, 1, 1); +('Civil and Political' , 'Civil and Political' , 1, NULL, 1, 1), +('Economic, Social and Cultural', 'Economic, Social and Cultural', 2, NULL, 1, 1), +('Gender Issues' , 'Gender Issues' , 3, NULL, 1, 1); -- /******************************************************* diff --git a/CRM/Case/xml/Menu/Case.xml b/CRM/Case/xml/Menu/Case.xml index 9cfc2c805f..c7c95ce638 100644 --- a/CRM/Case/xml/Menu/Case.xml +++ b/CRM/Case/xml/Menu/Case.xml @@ -69,7 +69,7 @@ Case Types List of types which can be assigned to Cases. (Enable the Cases tab from System Settings - Enable Components if you want to track cases.) administer CiviCase - CRM_Admin_Page_Options + CRM_Admin_Page_CaseType CiviCase admin/small/case_type.png 390 diff --git a/CRM/Case/xml/configuration.sample/SampleConfig.mysql b/CRM/Case/xml/configuration.sample/SampleConfig.mysql index ed6a9bdb3d..091a8f26a6 100644 --- a/CRM/Case/xml/configuration.sample/SampleConfig.mysql +++ b/CRM/Case/xml/configuration.sample/SampleConfig.mysql @@ -12,11 +12,11 @@ SELECT @caseCompId := id FROM `civicrm_component` where `name` like 'CiviCase'; -- * Case Types -- * -- *******************************************************/ -SELECT @option_group_id_case_type := max(id) from civicrm_option_group where name = 'case_type'; -SELECT @max_val := IF ( value <> 'NULL',max(value) , 0 ) from civicrm_option_value where option_group_id=@option_group_id_case_type; -SELECT @max_wt := IF ( value <> 'NULL',max(weight), 0 ) from civicrm_option_value where option_group_id=@option_group_id_case_type; +SELECT @max_wt := COALESCE ( max(weight), 0 ) from civicrm_case_type; -INSERT INTO `civicrm_option_value` (`option_group_id`, `label`, `value`, `name`, `grouping`, `filter`, `is_default`, `weight`, `description`, `is_optgroup`, `is_reserved`, `is_active`) VALUES (@option_group_id_case_type, 'Housing Support', @max_val + 1, 'housing_support', NULL, 0, 0, @max_wt + 1, 'Help homeless individuals obtain temporary and long-term housing', 0, 0, 1), (@option_group_id_case_type, 'Adult Day Care Referral', @max_val + 2, 'adult_day_care_referral', NULL, 0, 0, @max_wt + 2, 'Arranging adult day care for senior individuals', 0, 0, 1); +INSERT INTO `civicrm_case_type` (`title`, `name`, `weight`, `description`, `is_reserved`, `is_active`) VALUES +('Housing Support', 'housing_support', @max_wt + 1, 'Help homeless individuals obtain temporary and long-term housing', 0, 1), +('Adult Day Care Referral', 'adult_day_care_referral', @max_wt + 2, 'Arranging adult day care for senior individuals', 0, 1); -- /******************************************************* -- * diff --git a/api/v3/Case.php b/api/v3/Case.php index 92012d48e2..afad807393 100644 --- a/api/v3/Case.php +++ b/api/v3/Case.php @@ -391,10 +391,6 @@ function _civicrm_api3_case_read($caseId, $options) { // Legacy support for client_id - TODO: in apiv4 remove 'client_id' $case['client_id'] = $case['contact_id'] = $dao->retrieveContactIdsByCaseId($caseId); - //handle multi-value case type - $sep = CRM_Core_DAO::VALUE_SEPARATOR; - $case['case_type_id'] = trim(str_replace($sep, ',', $case['case_type_id']), ','); - if (!empty($return['contacts'])) { //get case contacts $contacts = CRM_Case_BAO_Case::getcontactNames($caseId); @@ -419,14 +415,11 @@ function _civicrm_api3_case_read($caseId, $options) { */ function _civicrm_api3_case_format_params(&$params) { // figure out case type id from case type and vice-versa - $caseTypes = CRM_Case_PseudoConstant::caseType('label', FALSE); + $caseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE); if (empty($params['case_type_id'])) { $params['case_type_id'] = array_search($params['case_type'], $caseTypes); } elseif (empty($params['case_type'])) { $params['case_type'] = $caseTypes[$params['case_type_id']]; } - // format input with value separators - $sep = CRM_Core_DAO::VALUE_SEPARATOR; - $params['case_type_id'] = $sep . implode($sep, (array) $params['case_type_id']) . $sep; } diff --git a/templates/CRM/Admin/Form/CaseType.tpl b/templates/CRM/Admin/Form/CaseType.tpl new file mode 100644 index 0000000000..e40abe48d9 --- /dev/null +++ b/templates/CRM/Admin/Form/CaseType.tpl @@ -0,0 +1,52 @@ +{* + +--------------------------------------------------------------------+ + | CiviCRM version 4.5 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2014 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License and the CiviCRM Licensing Exception along | + | with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ +*} +{* This template is used for adding/editing a case type. *} +

{if $action eq 1}{ts}New Case Type{/ts}{elseif $action eq 2}{ts}Edit Case Type{/ts}{else}{ts}Delete Case Type{/ts}{/if}

+
+
{include file="CRM/common/formButtons.tpl" location="top"}
+{if $action eq 8} +
+
+ {ts}WARNING: Deleting this option will result in the loss of all case records which use the option.{/ts} {ts}This may mean the loss of a substantial amount of data, and the action cannot be undone.{/ts} {ts}Do you want to continue?{/ts} +
+{else} + + + + + + + + + + + + + +
{$form.title.label}{$form.title.html}
{$form.description.label}{$form.description.html}
{$form.is_active.label}{$form.is_active.html}
+{/if} +
{include file="CRM/common/formButtons.tpl" location="bottom"}
+
diff --git a/templates/CRM/Admin/Page/CaseType.tpl b/templates/CRM/Admin/Page/CaseType.tpl new file mode 100644 index 0000000000..965ab4c9f7 --- /dev/null +++ b/templates/CRM/Admin/Page/CaseType.tpl @@ -0,0 +1,77 @@ +{* + +--------------------------------------------------------------------+ + | CiviCRM version 4.5 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2014 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License and the CiviCRM Licensing Exception along | + | with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ +*} +{if $action eq 1 or $action eq 2 or $action eq 8} + {include file="CRM/Admin/Form/CaseType.tpl"} +{else} +
+ {ts}A Case Type describes a group of related tasks, interactions, or processes.{/ts} +
+ +{if $rows} +
+ {strip} + {* handle enable/disable actions*} + {include file="CRM/common/enableDisableApi.tpl"} + {include file="CRM/common/crmeditable.tpl"} + {include file="CRM/common/jsortable.tpl"} + + + + + + + + + + + + {foreach from=$rows item=row} + + + + + + + + + {/foreach} +
{ts}Name{/ts}{ts}Title{/ts}{ts}Description{/ts}{ts}Enabled?{/ts}{ts}Reserved?{/ts}
{$row.name}{$row.title}{$row.description}{if $row.is_active eq 1} {ts}Yes{/ts} {else} {ts}No{/ts} {/if}{if $row.is_reserved eq 1}{ts}Reserved{/ts}{/if} {$row.action|replace:'xx':$row.id}
+ {/strip} + + {if $action ne 1 and $action ne 2} + + {/if} +
+{else} +
+ {ts}status{/ts} + {capture assign=crmURL}{crmURL p='civicrm/admin/options/case_type' q="action=add&reset=1"}{/capture} + {ts 1=$crmURL}There are no Case Types yet. You can add one.{/ts} +
+{/if} +{/if} diff --git a/tests/phpunit/CRM/Case/AllTests.php b/tests/phpunit/CRM/Case/AllTests.php new file mode 100644 index 0000000000..d0f255d3d2 --- /dev/null +++ b/tests/phpunit/CRM/Case/AllTests.php @@ -0,0 +1,33 @@ +implSuite(__FILE__); + } +} diff --git a/tests/phpunit/CRM/Case/BAO/CaseTest.php b/tests/phpunit/CRM/Case/BAO/CaseTest.php new file mode 100644 index 0000000000..53fb7a406e --- /dev/null +++ b/tests/phpunit/CRM/Case/BAO/CaseTest.php @@ -0,0 +1,72 @@ + 'Case BAOs', + 'description' => 'Test Case_BAO_Case methods.', + 'group' => 'CiviCRM BAO Tests', + ); + } + + function setUp() { + parent::setUp(); + + $this->loadAllFixtures(); + + CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); + } + + function testAddCaseToContact() { + $params = array( + 'case_id' => 1, + 'contact_id' => 17, + ); + CRM_Case_BAO_Case::addCaseToContact($params); + + $recent = CRM_Utils_Recent::get(); + $this->assertEquals('Test Contact - Housing Support', $recent[0]['title']); + } + + function testGetCaseType() { + $caseTypeLabel = CRM_Case_BAO_Case::getCaseType(1); + $this->assertEquals('Housing Support', $caseTypeLabel); + } + + function testRetrieveCaseIdsByContactId() { + $caseIds = CRM_Case_BAO_Case::retrieveCaseIdsByContactId(3, FALSE, 'housing_support'); + $this->assertEquals(array(1), $caseIds); + } + + /* FIXME: need to create an activity to run this test + * function testGetCases() { + * $cases = CRM_Case_BAO_Case::getCases(TRUE, 3); + * $this->assertEquals('Housing Support', $cases[1]['case_type']); + * $this->assertEquals(1, $cases[1]['case_type_id']); + * } + */ + + function testGetCasesSummary() { + $cases = CRM_Case_BAO_Case::getCasesSummary(TRUE, 3); + $this->assertEquals(1, $cases['rows']['Housing Support']['Ongoing']['count']); + } + + function testGetUnclosedCases() { + $params = array( + 'case_type' => 'ousing Suppor', + ); + $cases = CRM_Case_BAO_Case::getUnclosedCases($params); + $this->assertEquals('Housing Support', $cases[1]['case_type']); + } + + function testGetContactCases() { + $cases = CRM_Case_BAO_Case::getContactCases(3); + $this->assertEquals('Housing Support', $cases[1]['case_type']); + } + + /* FIXME: requires activities + * function testGetRelatedCases() { + * } + */ +} diff --git a/tests/phpunit/CRM/Case/PseudoConstantTest.php b/tests/phpunit/CRM/Case/PseudoConstantTest.php new file mode 100644 index 0000000000..c57f364b82 --- /dev/null +++ b/tests/phpunit/CRM/Case/PseudoConstantTest.php @@ -0,0 +1,28 @@ + 'Case PseudoConstants', + 'description' => 'Test Case_PseudoConstant methods.', + 'group' => 'Case', + ); + } + + function setUp() { + parent::setUp(); + + $this->loadAllFixtures(); + + CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); + } + + function testCaseType() { + $caseTypes = CRM_Case_PseudoConstant::caseType(); + $expectedTypes = array( + 1 => 'Housing Support', + ); + $this->assertEquals($expectedTypes, $caseTypes); + } +} diff --git a/tests/phpunit/api/v3/CaseTest.php b/tests/phpunit/api/v3/CaseTest.php index e65953d41f..82cb124868 100644 --- a/tests/phpunit/api/v3/CaseTest.php +++ b/tests/phpunit/api/v3/CaseTest.php @@ -45,7 +45,6 @@ class api_v3_CaseTest extends CiviUnitTestCase { protected $followup_activity_type_value; protected $caseTypeId; protected $caseStatusGroup; - protected $caseTypeGroup; protected $optionValues; /** @@ -66,20 +65,6 @@ class api_v3_CaseTest extends CiviUnitTestCase { 'name' => 'case_status', 'format.only_id' => 1) ); - $this->caseTypeGroup = $this->callAPISuccess('option_group', 'get', array( - 'name' => 'case_type', - 'format.only_id' => 1) - ); - $caseTypes = $this->callAPISuccess('option_value', 'Create', array( - 'option_group_id' => $this->caseTypeGroup, - 'name' => 'housing_support', - 'label' => "Housing Support", - 'sequential' => 1, - 'description' => 'Help homeless individuals obtain temporary and long-term housing', - )); - - $this->caseTypeId = $caseTypes['values'][0]['value']; - $this->optionValues[] = $caseTypes['id']; $optionValues = array( 'Medical evaluation' => 'Medical evaluation', 'Mental health evaluation' => "Mental health evaluation", @@ -98,7 +83,8 @@ class api_v3_CaseTest extends CiviUnitTestCase { // store for cleanup $this->optionValues[] = $activityTypes['id']; } - $tablesToTruncate = array( + $this->caseTypeId = 1; + $this->tablesToTruncate = array( 'civicrm_activity', 'civicrm_contact', 'civicrm_custom_group', @@ -106,12 +92,13 @@ class api_v3_CaseTest extends CiviUnitTestCase { 'civicrm_case', 'civicrm_case_contact', 'civicrm_case_activity', + 'civicrm_case_type', 'civicrm_activity_contact', 'civicrm_relationship', 'civicrm_relationship_type', ); - $this->quickCleanup($tablesToTruncate); + $this->quickCleanup($this->tablesToTruncate); $this->loadAllFixtures(); @@ -153,20 +140,7 @@ class api_v3_CaseTest extends CiviUnitTestCase { * @access protected */ function tearDown() { - foreach ($this->optionValues as $id) { - $this->callAPISuccess('option_value', 'delete', array('id' => $id)); - } - $tablesToTruncate = array( - 'civicrm_contact', - 'civicrm_activity', - 'civicrm_case', - 'civicrm_case_contact', - 'civicrm_case_activity', - 'civicrm_activity_contact', - 'civicrm_relationship', - 'civicrm_relationship_type', - ); - $this->quickCleanup($tablesToTruncate, TRUE); + $this->quickCleanup($this->tablesToTruncate, TRUE); $this->customDirectories(array('template_path' => FALSE)); } @@ -204,7 +178,7 @@ class api_v3_CaseTest extends CiviUnitTestCase { // Check result $result = $this->callAPISuccess('case', 'get', array('id' => $id)); - $this->assertEquals($result['values'][$id]['id'], 1, 'in line ' . __LINE__); + $this->assertEquals($result['values'][$id]['id'], $id, 'in line ' . __LINE__); $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId, 'in line ' . __LINE__); $this->assertEquals($result['values'][$id]['subject'], $params['subject'], 'in line ' . __LINE__); } @@ -216,7 +190,8 @@ class api_v3_CaseTest extends CiviUnitTestCase { // Create Case $params = $this->_params; // Test using name instead of value - $params['case_type_id'] = 'housing_support'; + unset($params['case_type_id']); + $params['case_type'] = 'Housing Support'; $result = $this->callAPISuccess('case', 'create', $params); $id = $result['id']; $result = $this->callAPISuccess('case', 'get', array('id' => $id)); diff --git a/tests/phpunit/api/v3/dataset/case_types.xml b/tests/phpunit/api/v3/dataset/case_types.xml new file mode 100644 index 0000000000..1c4766a220 --- /dev/null +++ b/tests/phpunit/api/v3/dataset/case_types.xml @@ -0,0 +1,21 @@ + + + + id + name + title + description + is_active + is_reserved + weight + + 1 + housing_support + Housing Support + Help homeless individuals obtain temporary and long-term housing + 1 + 0 + 1 + +
+
-- 2.25.1