From 31c28ed5ab009d494a473a49e5418d764fac57bf Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 23 Jun 2017 12:21:27 -0400 Subject: [PATCH] CRM-19778 - Api.getoptions - Filter status id by case type id --- CRM/Case/BAO/Case.php | 11 +++++++++++ CRM/Case/BAO/CaseType.php | 1 + tests/phpunit/api/v3/CaseTest.php | 1 - tests/phpunit/api/v3/CaseTypeTest.php | 24 ++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index 98705c2e1b..df23c5fa1c 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -3136,6 +3136,17 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; case 'medium_id': $className = 'CRM_Activity_BAO_Activity'; break; + + // Filter status id by case type id + case 'status_id': + if (!empty($props['case_type_id'])) { + $idField = is_numeric($props['case_type_id']) ? 'id' : 'name'; + $caseType = civicrm_api3('CaseType', 'getsingle', array($idField => $props['case_type_id'], 'return' => 'definition')); + if (!empty($caseType['definition']['statuses'])) { + $params['condition'] = 'v.name IN ("' . implode('","', $caseType['definition']['statuses']) . '")'; + } + } + break; } return CRM_Core_PseudoConstant::get($className, $fieldName, $params, $context); } diff --git a/CRM/Case/BAO/CaseType.php b/CRM/Case/BAO/CaseType.php index 65a49863bf..bf21b11f33 100644 --- a/CRM/Case/BAO/CaseType.php +++ b/CRM/Case/BAO/CaseType.php @@ -329,6 +329,7 @@ class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType { } $transaction->commit(); CRM_Case_XMLRepository::singleton(TRUE); + CRM_Core_OptionGroup::flushAll(); return $caseType; } diff --git a/tests/phpunit/api/v3/CaseTest.php b/tests/phpunit/api/v3/CaseTest.php index 4e2e48e917..6e9845fc0c 100644 --- a/tests/phpunit/api/v3/CaseTest.php +++ b/tests/phpunit/api/v3/CaseTest.php @@ -704,7 +704,6 @@ class api_v3_CaseTest extends CiviCaseTestCase { $this->assertEquals('Follow up', $result['values'][1]['activity_type_id.name']); } - /** * Test the case merge function. * diff --git a/tests/phpunit/api/v3/CaseTypeTest.php b/tests/phpunit/api/v3/CaseTypeTest.php index f844ebf526..74129bca8e 100644 --- a/tests/phpunit/api/v3/CaseTypeTest.php +++ b/tests/phpunit/api/v3/CaseTypeTest.php @@ -230,4 +230,28 @@ class api_v3_CaseTypeTest extends CiviCaseTestCase { $this->assertEquals(0, $getCaseType['count']); } + /** + * Test the api returns case statuses filtered by case type. + * + * Api getoptions should respect the case statuses declared in the case type definition. + * + * @throws \Exception + */ + public function testCaseStatusByCaseType() { + $statusName = md5(mt_rand()); + $template = $this->callAPISuccess('CaseType', 'getsingle', array('id' => $this->caseTypeId)); + unset($template['id']); + $template['name'] = $template['title'] = 'test_case_type'; + $template['definition']['statuses'] = array('Closed', $statusName); + $this->callAPISuccess('CaseType', 'create', $template); + $this->callAPISuccess('OptionValue', 'create', array( + 'option_group_id' => 'case_status', + 'name' => $statusName, + 'label' => $statusName, + 'weight' => 99, + )); + $result = $this->callAPISuccess('Case', 'getoptions', array('field' => 'status_id', 'case_type_id' => 'test_case_type', 'context' => 'validate')); + $this->assertEquals($template['definition']['statuses'], array_values($result['values'])); + } + } -- 2.25.1