<?php
/*
+--------------------------------------------------------------------+
- | CiviCRM version 4.5 |
+ | CiviCRM version 4.6 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2014 |
+--------------------------------------------------------------------+
+--------------------------------------------------------------------+
*/
-require_once 'CiviTest/CiviUnitTestCase.php';
+require_once 'CiviTest/CiviCaseTestCase.php';
/**
* Class api_v3_CaseTypeTest
*/
-class api_v3_CaseTypeTest extends CiviUnitTestCase {
- protected $_apiversion = 3;
-
- function setUp() {
- $this->_entity = 'CaseType';
+class api_v3_CaseTypeTest extends CiviCaseTestCase {
+ public function setUp() {
+ $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),
+ ),
+ ),
+ );
}
/**
* This method is called after a test is executed.
*
*/
- function tearDown() {
- $this->quickCleanup($this->tablesToTruncate, TRUE);
+ public function tearDown() {
+ parent::tearDown();
+ $this->quickCleanup(array('civicrm_case_type'));
}
/**
- * check with empty array
+ * Check with empty array
*/
- function testCaseTypeCreateEmpty() {
+ public function testCaseTypeCreateEmpty() {
$this->callAPIFailure('CaseType', 'create', array());
}
/**
- * check if required fields are not passed
+ * Check if required fields are not passed
*/
- function testCaseTypeCreateWithoutRequired() {
+ public function testCaseTypeCreateWithoutRequired() {
$params = array(
'name' => 'this case should fail',
);
}
/**
- * test create methods with valid data
+ * Test create methods with valid data
* success expected
*/
- function testCaseTypeCreate() {
+ public function testCaseTypeCreate() {
// Create Case Type
$params = array(
'title' => 'Application',
/**
* Create a case with an invalid name
*/
- function testCaseTypeCreate_invalidName() {
+ public function testCaseTypeCreate_invalidName() {
// Create Case Type
$params = array(
'title' => 'Application',
/**
* Test update (create with id) function with valid parameters
*/
- function testCaseTypeUpdate() {
+ public function testCaseTypeUpdate() {
// Create Case Type
- $params = array(
+ $params = array(
'title' => 'Application',
'name' => 'Application',
'is_active' => 1,
/**
* Test delete function with valid parameters
*/
- function testCaseTypeDelete() {
+ public function testCaseTypeDelete_New() {
// Create Case Type
- $params = array(
+ $params = array(
'title' => 'Application',
'name' => 'Application',
'is_active' => 1,
}
/**
- * test create methods with xml file
+ * Test create methods with xml file
* success expected
*/
- function testCaseTypeCreateWithDefinition() {
+ public 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'];
$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.
+ */
+ public 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("You can not delete this case type -- it is assigned to 1 existing case record(s). If you do not want this case type to be used going forward, consider disabling it instead.", $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']);
+ }
+}