From 10ffff26ef72c93624d83fcf13bcbb763712c5d4 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 13 Jul 2014 23:01:32 -0700 Subject: [PATCH] CRM-14789 - CaseType.delete API - Throw error if there are existing cases --- CRM/Case/BAO/CaseType.php | 10 +- tests/phpunit/CiviTest/CiviCaseTestCase.php | 1 + tests/phpunit/api/v3/CaseTypeTest.php | 103 ++++++++++++-------- 3 files changed, 73 insertions(+), 41 deletions(-) diff --git a/CRM/Case/BAO/CaseType.php b/CRM/Case/BAO/CaseType.php index 9e4ae0a80b..e7f56f9525 100644 --- a/CRM/Case/BAO/CaseType.php +++ b/CRM/Case/BAO/CaseType.php @@ -277,6 +277,7 @@ class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType { CRM_Utils_Hook::post('create', 'CaseType', $caseType->id, $case); } $transaction->commit(); + CRM_Case_XMLRepository::singleton(TRUE); return $caseType; } @@ -310,7 +311,14 @@ class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType { static function del($caseTypeId) { $caseType = new CRM_Case_DAO_CaseType(); $caseType->id = $caseTypeId; - return $caseType->delete(); + $refCounts = $caseType->getReferenceCounts(); + $total = array_sum(CRM_Utils_Array::collect('count', $refCounts)); + if ($total) { + throw new CRM_Core_Exception("Cannot delete case type -- {$total} record(s) depend on it"); + } + $result = $caseType->delete(); + CRM_Case_XMLRepository::singleton(TRUE); + return $result; } /** diff --git a/tests/phpunit/CiviTest/CiviCaseTestCase.php b/tests/phpunit/CiviTest/CiviCaseTestCase.php index eb28b6efc1..9fe873a62a 100644 --- a/tests/phpunit/CiviTest/CiviCaseTestCase.php +++ b/tests/phpunit/CiviTest/CiviCaseTestCase.php @@ -95,6 +95,7 @@ class CiviCaseTestCase extends CiviUnitTestCase { 'civicrm_case_activity', 'civicrm_case_type', 'civicrm_activity_contact', + 'civicrm_managed', 'civicrm_relationship', 'civicrm_relationship_type', ); diff --git a/tests/phpunit/api/v3/CaseTypeTest.php b/tests/phpunit/api/v3/CaseTypeTest.php index 432fa29d6b..19a58952ed 100644 --- a/tests/phpunit/api/v3/CaseTypeTest.php +++ b/tests/phpunit/api/v3/CaseTypeTest.php @@ -25,27 +25,41 @@ +--------------------------------------------------------------------+ */ -require_once 'CiviTest/CiviUnitTestCase.php'; +require_once 'CiviTest/CiviCaseTestCase.php'; /** * Class api_v3_CaseTypeTest */ -class api_v3_CaseTypeTest extends CiviUnitTestCase { - protected $_apiversion = 3; +class api_v3_CaseTypeTest extends CiviCaseTestCase { function setUp() { - $this->_entity = 'CaseType'; - + $this->quickCleanup(array('civicrm_case_type')); parent::setUp(); - $this->_apiversion = 3; - $this->tablesToTruncate = array( - 'civicrm_case_type', - ); - $this->quickCleanup($this->tablesToTruncate); - $this->createLoggedInUser(); - $session = CRM_Core_Session::singleton(); - $this->_loggedInUser = $session->get('userID'); + $this->fixtures['Application_with_Definition'] = array( + 'title' => 'Application with Definition', + 'name' => 'Application_with_Definition', + 'is_active' => 1, + 'weight' => 4, + 'definition' => array( + 'activityTypes' => array( + array('name' => 'First act'), + ), + 'activitySets' => array( + array( + 'name' => 'set1', + 'label' => 'Label 1', + 'timeline' => 1, + 'activityTypes' => array( + array('name' => 'Open Case', 'status' => 'Completed'), + ), + ), + ), + 'caseRoles' => array( + array('name' => 'First role', 'creator' => 1, 'manager' => 1), + ), + ) + ); } /** @@ -54,7 +68,8 @@ class api_v3_CaseTypeTest extends CiviUnitTestCase { * */ function tearDown() { - $this->quickCleanup($this->tablesToTruncate, TRUE); + parent::tearDown(); + $this->quickCleanup(array('civicrm_case_type')); } /** @@ -147,7 +162,7 @@ class api_v3_CaseTypeTest extends CiviUnitTestCase { /** * Test delete function with valid parameters */ - function testCaseTypeDelete() { + function testCaseTypeDelete_New() { // Create Case Type $params = array( 'title' => 'Application', @@ -171,31 +186,7 @@ class api_v3_CaseTypeTest extends CiviUnitTestCase { */ function testCaseTypeCreateWithDefinition() { // Create Case Type - $params = array( - 'title' => 'Application with Definition', - 'name' => 'Application_with_Definition', - 'is_active' => 1, - 'weight' => 4, - 'definition' => array( - 'activityTypes' => array( - array('name' => 'First act'), - ), - 'activitySets' => array( - array( - 'name' => 'set1', - 'label' => 'Label 1', - 'timeline' => 1, - 'activityTypes' => array( - array('name' => 'Open Case', 'status' => 'Completed'), - ), - ), - ), - 'caseRoles' => array( - array('name' => 'First role', 'creator' => 1, 'manager' => 1), - ), - ) - ); - + $params = $this->fixtures['Application_with_Definition']; $result = $this->callAPISuccess('CaseType', 'create', $params); $id = $result['id']; @@ -204,6 +195,38 @@ class api_v3_CaseTypeTest extends CiviUnitTestCase { $this->assertEquals($result['values'][$id]['id'], $id, 'in line ' . __LINE__); $this->assertEquals($result['values'][$id]['title'], $params['title'], 'in line ' . __LINE__); $this->assertEquals($result['values'][$id]['definition'], $params['definition'], 'in line ' . __LINE__); + + $caseXml = CRM_Case_XMLRepository::singleton()->retrieve('Application_with_Definition'); + $this->assertTrue($caseXml instanceof SimpleXMLElement); + } + + /** + * Create a CaseType+case then delete the CaseType. + */ + function testCaseTypeDelete_InUse() { + // Create Case Type + $params = $this->fixtures['Application_with_Definition']; + $createCaseType = $this->callAPISuccess('CaseType', 'create', $params); + + $createCase = $this->callAPISuccess('Case', 'create', array( + 'case_type_id' => $createCaseType['id'], + 'contact_id' => $this->_loggedInUser, + 'subject' => 'Example', + )); + + // Deletion fails while case-type is in-use + $deleteCaseType = $this->callAPIFailure('CaseType', 'delete', array('id' => $createCaseType['id'])); + $this->assertEquals("Cannot delete case type -- 1 record(s) depend on it", $deleteCaseType['error_message']); + $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id'])); + $this->assertEquals(1, $getCaseType['count']); + + // Deletion succeeds when it's not in-use + $this->callAPISuccess('Case', 'delete', array('id' => $createCase['id'])); + + // Check result - case type should no longer exist + $this->callAPISuccess('CaseType', 'delete', array('id' => $createCaseType['id'])); + $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id'])); + $this->assertEquals(0, $getCaseType['count']); } } -- 2.25.1