Merge branch 'JohnFF-patch-1'
[civicrm-core.git] / tests / phpunit / api / v3 / CaseTypeTest.php
CommitLineData
eaefbeb2
N
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
10ffff26 28require_once 'CiviTest/CiviCaseTestCase.php';
e9479dcf
EM
29
30/**
31 * Class api_v3_CaseTypeTest
32 */
10ffff26 33class api_v3_CaseTypeTest extends CiviCaseTestCase {
eaefbeb2
N
34
35 function setUp() {
10ffff26 36 $this->quickCleanup(array('civicrm_case_type'));
eaefbeb2 37 parent::setUp();
eaefbeb2 38
10ffff26
TO
39 $this->fixtures['Application_with_Definition'] = array(
40 'title' => 'Application with Definition',
41 'name' => 'Application_with_Definition',
42 'is_active' => 1,
43 'weight' => 4,
44 'definition' => array(
45 'activityTypes' => array(
46 array('name' => 'First act'),
47 ),
48 'activitySets' => array(
49 array(
50 'name' => 'set1',
51 'label' => 'Label 1',
52 'timeline' => 1,
53 'activityTypes' => array(
54 array('name' => 'Open Case', 'status' => 'Completed'),
55 ),
56 ),
57 ),
58 'caseRoles' => array(
59 array('name' => 'First role', 'creator' => 1, 'manager' => 1),
60 ),
61 )
62 );
eaefbeb2
N
63 }
64
65 /**
66 * Tears down the fixture, for example, closes a network connection.
67 * This method is called after a test is executed.
68 *
69 */
70 function tearDown() {
10ffff26
TO
71 parent::tearDown();
72 $this->quickCleanup(array('civicrm_case_type'));
eaefbeb2
N
73 }
74
75 /**
76 * check with empty array
77 */
78 function testCaseTypeCreateEmpty() {
9eebe75f 79 $this->callAPIFailure('CaseType', 'create', array());
eaefbeb2
N
80 }
81
82 /**
83 * check if required fields are not passed
84 */
85 function testCaseTypeCreateWithoutRequired() {
86 $params = array(
87 'name' => 'this case should fail',
88 );
9eebe75f 89 $this->callAPIFailure('CaseType', 'create', $params);
eaefbeb2
N
90
91 $params = array(
92 'name' => 'this case should fail',
93 'weight' => 4,
94 );
9eebe75f 95 $this->callAPIFailure('CaseType', 'create', $params);
eaefbeb2
N
96 }
97
a771c43f 98 /**
99 * test create methods with valid data
100 * success expected
101 */
eaefbeb2
N
102 function testCaseTypeCreate() {
103 // Create Case Type
104 $params = array(
105 'title' => 'Application',
106 'name' => 'Application',
107 'is_active' => 1,
108 'weight' => 4,
109 );
110
111 $result = $this->callAPISuccess('CaseType', 'create', $params);
112 $id = $result['id'];
113
114 // Check result
115 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
116 $this->assertEquals($result['values'][$id]['id'], $id, 'in line ' . __LINE__);
117 $this->assertEquals($result['values'][$id]['title'], $params['title'], 'in line ' . __LINE__);
118 }
119
076f81b6 120 /**
121 * Create a case with an invalid name
122 */
123 function testCaseTypeCreate_invalidName() {
124 // Create Case Type
125 $params = array(
126 'title' => 'Application',
127 'name' => 'Appl ication', // spaces are not allowed
128 'is_active' => 1,
129 'weight' => 4,
130 );
131
132 $this->callAPIFailure('CaseType', 'create', $params);
133 }
134
135
eaefbeb2
N
136 /**
137 * Test update (create with id) function with valid parameters
138 */
139 function testCaseTypeUpdate() {
140 // Create Case Type
141 $params = array(
142 'title' => 'Application',
143 'name' => 'Application',
144 'is_active' => 1,
145 'weight' => 4,
146 );
147 $result = $this->callAPISuccess('CaseType', 'create', $params);
148 $id = $result['id'];
149 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
150 $caseType = $result['values'][$id];
151
152 // Update Case Type
153 $params = array('id' => $id);
154 $params['title'] = $caseType['title'] = 'Something Else';
9eebe75f 155 $this->callAPISuccess('CaseType', 'create', $params);
eaefbeb2
N
156
157 // Verify that updated case Type is exactly equal to the original with new title
158 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
159 $this->assertEquals($result['values'][$id], $caseType, 'in line ' . __LINE__);
160 }
161
162 /**
163 * Test delete function with valid parameters
164 */
10ffff26 165 function testCaseTypeDelete_New() {
eaefbeb2
N
166 // Create Case Type
167 $params = array(
168 'title' => 'Application',
169 'name' => 'Application',
170 'is_active' => 1,
171 'weight' => 4,
172 );
173 $result = $this->callAPISuccess('CaseType', 'create', $params);
174
175 $id = $result['id'];
9eebe75f 176 $this->callAPISuccess('CaseType', 'delete', array('id' => $id));
eaefbeb2
N
177
178 // Check result - case type should no longer exist
179 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
180 $this->assertEquals(0, $result['count']);
181 }
a771c43f 182
183 /**
184 * test create methods with xml file
185 * success expected
186 */
02f3f951 187 function testCaseTypeCreateWithDefinition() {
a771c43f 188 // Create Case Type
10ffff26 189 $params = $this->fixtures['Application_with_Definition'];
a771c43f 190 $result = $this->callAPISuccess('CaseType', 'create', $params);
191 $id = $result['id'];
192
193 // Check result
194 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
195 $this->assertEquals($result['values'][$id]['id'], $id, 'in line ' . __LINE__);
196 $this->assertEquals($result['values'][$id]['title'], $params['title'], 'in line ' . __LINE__);
02f3f951 197 $this->assertEquals($result['values'][$id]['definition'], $params['definition'], 'in line ' . __LINE__);
10ffff26
TO
198
199 $caseXml = CRM_Case_XMLRepository::singleton()->retrieve('Application_with_Definition');
200 $this->assertTrue($caseXml instanceof SimpleXMLElement);
201 }
202
203 /**
204 * Create a CaseType+case then delete the CaseType.
205 */
206 function testCaseTypeDelete_InUse() {
207 // Create Case Type
208 $params = $this->fixtures['Application_with_Definition'];
209 $createCaseType = $this->callAPISuccess('CaseType', 'create', $params);
210
211 $createCase = $this->callAPISuccess('Case', 'create', array(
212 'case_type_id' => $createCaseType['id'],
213 'contact_id' => $this->_loggedInUser,
214 'subject' => 'Example',
215 ));
216
217 // Deletion fails while case-type is in-use
218 $deleteCaseType = $this->callAPIFailure('CaseType', 'delete', array('id' => $createCaseType['id']));
6bf8b6b7 219 $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']);
10ffff26
TO
220 $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id']));
221 $this->assertEquals(1, $getCaseType['count']);
222
223 // Deletion succeeds when it's not in-use
224 $this->callAPISuccess('Case', 'delete', array('id' => $createCase['id']));
225
226 // Check result - case type should no longer exist
227 $this->callAPISuccess('CaseType', 'delete', array('id' => $createCaseType['id']));
228 $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id']));
229 $this->assertEquals(0, $getCaseType['count']);
a771c43f 230 }
eaefbeb2
N
231}
232