3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Class api_v3_CaseTypeTest
32 class api_v3_CaseTypeTest
extends CiviCaseTestCase
{
34 public function setUp() {
35 $this->quickCleanup(array('civicrm_case_type'));
38 $this->fixtures
['Application_with_Definition'] = array(
39 'title' => 'Application with Definition',
40 'name' => 'Application_with_Definition',
43 'definition' => array(
44 'activityTypes' => array(
45 array('name' => 'First act'),
47 'activitySets' => array(
52 'activityTypes' => array(
53 array('name' => 'Open Case', 'status' => 'Completed'),
57 'timelineActivityTypes' => array(
58 array('name' => 'Open Case', 'status' => 'Completed'),
61 array('name' => 'First role', 'creator' => 1, 'manager' => 1),
68 * Tears down the fixture, for example, closes a network connection.
70 * This method is called after a test is executed.
72 public function tearDown() {
74 $this->quickCleanup(array('civicrm_case_type', 'civicrm_uf_match'));
78 * Check with empty array.
80 public function testCaseTypeCreateEmpty() {
81 $this->callAPIFailure('CaseType', 'create', array());
85 * Check if required fields are not passed.
87 public function testCaseTypeCreateWithoutRequired() {
89 'name' => 'this case should fail',
91 $this->callAPIFailure('CaseType', 'create', $params);
94 'name' => 'this case should fail',
97 $this->callAPIFailure('CaseType', 'create', $params);
101 * Test create methods with valid data.
105 public function testCaseTypeCreate() {
108 'title' => 'Application',
109 'name' => 'Application',
114 $result = $this->callAPISuccess('CaseType', 'create', $params);
118 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
119 $this->assertEquals($result['values'][$id]['id'], $id);
120 $this->assertEquals($result['values'][$id]['title'], $params['title']);
124 * Create a case with an invalid name.
126 public function testCaseTypeCreate_invalidName() {
129 'title' => 'Application',
130 'name' => 'Appl ication', // spaces are not allowed
135 $this->callAPIFailure('CaseType', 'create', $params);
140 * Test update (create with id) function with valid parameters.
142 public function testCaseTypeUpdate() {
145 'title' => 'Application',
146 'name' => 'Application',
150 $result = $this->callAPISuccess('CaseType', 'create', $params);
152 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
153 $caseType = $result['values'][$id];
156 $params = array('id' => $id);
157 $params['title'] = $caseType['title'] = 'Something Else';
158 $this->callAPISuccess('CaseType', 'create', $params);
160 // Verify that updated case Type is exactly equal to the original with new title.
161 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
162 $this->assertEquals($result['values'][$id], $caseType);
166 * Test delete function with valid parameters.
168 public function testCaseTypeDelete_New() {
171 'title' => 'Application',
172 'name' => 'Application',
176 $result = $this->callAPISuccess('CaseType', 'create', $params);
179 $this->callAPISuccess('CaseType', 'delete', array('id' => $id));
181 // Check result - case type should no longer exist
182 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
183 $this->assertEquals(0, $result['count']);
187 * Test create methods with xml file.
191 public function testCaseTypeCreateWithDefinition() {
193 $params = $this->fixtures
['Application_with_Definition'];
194 $result = $this->callAPISuccess('CaseType', 'create', $params);
198 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
199 $this->assertEquals($result['values'][$id]['id'], $id);
200 $this->assertEquals($result['values'][$id]['title'], $params['title']);
201 $this->assertEquals($result['values'][$id]['definition'], $params['definition']);
203 $caseXml = CRM_Case_XMLRepository
::singleton()->retrieve('Application_with_Definition');
204 $this->assertTrue($caseXml instanceof SimpleXMLElement
);
208 * Create a CaseType+case then delete the CaseType.
210 public function testCaseTypeDelete_InUse() {
212 $params = $this->fixtures
['Application_with_Definition'];
213 $createCaseType = $this->callAPISuccess('CaseType', 'create', $params);
215 $createCase = $this->callAPISuccess('Case', 'create', array(
216 'case_type_id' => $createCaseType['id'],
217 'contact_id' => $this->_loggedInUser
,
218 'subject' => 'Example',
221 // Deletion fails while case-type is in-use
222 $deleteCaseType = $this->callAPIFailure('CaseType', 'delete', array('id' => $createCaseType['id']));
223 $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']);
224 $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id']));
225 $this->assertEquals(1, $getCaseType['count']);
227 // Deletion succeeds when it's not in-use.
228 $this->callAPISuccess('Case', 'delete', array('id' => $createCase['id']));
230 // Check result - case type should no longer exist.
231 $this->callAPISuccess('CaseType', 'delete', array('id' => $createCaseType['id']));
232 $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id']));
233 $this->assertEquals(0, $getCaseType['count']);
237 * Test the api returns case statuses filtered by case type.
239 * Api getoptions should respect the case statuses declared in the case type definition.
243 public function testCaseStatusByCaseType() {
244 $this->markTestIncomplete('Cannot figure out why this passes locally but fails on Jenkins.');
245 $statusName = md5(mt_rand());
246 $template = $this->callAPISuccess('CaseType', 'getsingle', array('id' => $this->caseTypeId
));
247 unset($template['id']);
248 $template['name'] = $template['title'] = 'test_case_type';
249 $template['definition']['statuses'] = array('Closed', $statusName);
250 $this->callAPISuccess('CaseType', 'create', $template);
251 $this->callAPISuccess('OptionValue', 'create', array(
252 'option_group_id' => 'case_status',
253 'name' => $statusName,
254 'label' => $statusName,
257 $result = $this->callAPISuccess('Case', 'getoptions', array('field' => 'status_id', 'case_type_id' => 'test_case_type', 'context' => 'validate'));
258 $this->assertEquals($template['definition']['statuses'], array_values($result['values']));