Commit | Line | Data |
---|---|---|
eaefbeb2 N |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
eaefbeb2 N |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
eaefbeb2 | 27 | |
10ffff26 | 28 | require_once 'CiviTest/CiviCaseTestCase.php'; |
e9479dcf EM |
29 | |
30 | /** | |
31 | * Class api_v3_CaseTypeTest | |
32 | */ | |
10ffff26 | 33 | class api_v3_CaseTypeTest extends CiviCaseTestCase { |
eaefbeb2 | 34 | |
00be9182 | 35 | public 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 | ), | |
21dfd5f5 | 61 | ), |
10ffff26 | 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. | |
eaefbeb2 | 68 | */ |
00be9182 | 69 | public function tearDown() { |
10ffff26 TO |
70 | parent::tearDown(); |
71 | $this->quickCleanup(array('civicrm_case_type')); | |
eaefbeb2 N |
72 | } |
73 | ||
74 | /** | |
100fef9d | 75 | * Check with empty array |
eaefbeb2 | 76 | */ |
00be9182 | 77 | public function testCaseTypeCreateEmpty() { |
9eebe75f | 78 | $this->callAPIFailure('CaseType', 'create', array()); |
eaefbeb2 N |
79 | } |
80 | ||
81 | /** | |
100fef9d | 82 | * Check if required fields are not passed |
eaefbeb2 | 83 | */ |
00be9182 | 84 | public function testCaseTypeCreateWithoutRequired() { |
eaefbeb2 N |
85 | $params = array( |
86 | 'name' => 'this case should fail', | |
87 | ); | |
9eebe75f | 88 | $this->callAPIFailure('CaseType', 'create', $params); |
eaefbeb2 N |
89 | |
90 | $params = array( | |
91 | 'name' => 'this case should fail', | |
92 | 'weight' => 4, | |
93 | ); | |
9eebe75f | 94 | $this->callAPIFailure('CaseType', 'create', $params); |
eaefbeb2 N |
95 | } |
96 | ||
a771c43f | 97 | /** |
100fef9d | 98 | * Test create methods with valid data |
a771c43f | 99 | * success expected |
100 | */ | |
00be9182 | 101 | public function testCaseTypeCreate() { |
eaefbeb2 N |
102 | // Create Case Type |
103 | $params = array( | |
104 | 'title' => 'Application', | |
105 | 'name' => 'Application', | |
106 | 'is_active' => 1, | |
107 | 'weight' => 4, | |
108 | ); | |
109 | ||
110 | $result = $this->callAPISuccess('CaseType', 'create', $params); | |
111 | $id = $result['id']; | |
112 | ||
113 | // Check result | |
114 | $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id)); | |
115 | $this->assertEquals($result['values'][$id]['id'], $id, 'in line ' . __LINE__); | |
116 | $this->assertEquals($result['values'][$id]['title'], $params['title'], 'in line ' . __LINE__); | |
117 | } | |
118 | ||
076f81b6 | 119 | /** |
120 | * Create a case with an invalid name | |
121 | */ | |
00be9182 | 122 | public function testCaseTypeCreate_invalidName() { |
076f81b6 | 123 | // Create Case Type |
124 | $params = array( | |
125 | 'title' => 'Application', | |
126 | 'name' => 'Appl ication', // spaces are not allowed | |
127 | 'is_active' => 1, | |
128 | 'weight' => 4, | |
129 | ); | |
130 | ||
131 | $this->callAPIFailure('CaseType', 'create', $params); | |
132 | } | |
133 | ||
134 | ||
eaefbeb2 N |
135 | /** |
136 | * Test update (create with id) function with valid parameters | |
137 | */ | |
00be9182 | 138 | public function testCaseTypeUpdate() { |
eaefbeb2 | 139 | // Create Case Type |
6c6e6187 | 140 | $params = array( |
eaefbeb2 N |
141 | 'title' => 'Application', |
142 | 'name' => 'Application', | |
143 | 'is_active' => 1, | |
144 | 'weight' => 4, | |
145 | ); | |
146 | $result = $this->callAPISuccess('CaseType', 'create', $params); | |
147 | $id = $result['id']; | |
148 | $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id)); | |
149 | $caseType = $result['values'][$id]; | |
150 | ||
151 | // Update Case Type | |
152 | $params = array('id' => $id); | |
153 | $params['title'] = $caseType['title'] = 'Something Else'; | |
9eebe75f | 154 | $this->callAPISuccess('CaseType', 'create', $params); |
eaefbeb2 N |
155 | |
156 | // Verify that updated case Type is exactly equal to the original with new title | |
157 | $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id)); | |
158 | $this->assertEquals($result['values'][$id], $caseType, 'in line ' . __LINE__); | |
159 | } | |
160 | ||
161 | /** | |
162 | * Test delete function with valid parameters | |
163 | */ | |
00be9182 | 164 | public function testCaseTypeDelete_New() { |
eaefbeb2 | 165 | // Create Case Type |
6c6e6187 | 166 | $params = array( |
eaefbeb2 N |
167 | 'title' => 'Application', |
168 | 'name' => 'Application', | |
169 | 'is_active' => 1, | |
170 | 'weight' => 4, | |
171 | ); | |
172 | $result = $this->callAPISuccess('CaseType', 'create', $params); | |
173 | ||
174 | $id = $result['id']; | |
9eebe75f | 175 | $this->callAPISuccess('CaseType', 'delete', array('id' => $id)); |
eaefbeb2 N |
176 | |
177 | // Check result - case type should no longer exist | |
178 | $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id)); | |
179 | $this->assertEquals(0, $result['count']); | |
180 | } | |
a771c43f | 181 | |
182 | /** | |
100fef9d | 183 | * Test create methods with xml file |
a771c43f | 184 | * success expected |
185 | */ | |
00be9182 | 186 | public function testCaseTypeCreateWithDefinition() { |
a771c43f | 187 | // Create Case Type |
10ffff26 | 188 | $params = $this->fixtures['Application_with_Definition']; |
a771c43f | 189 | $result = $this->callAPISuccess('CaseType', 'create', $params); |
190 | $id = $result['id']; | |
191 | ||
192 | // Check result | |
193 | $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id)); | |
194 | $this->assertEquals($result['values'][$id]['id'], $id, 'in line ' . __LINE__); | |
195 | $this->assertEquals($result['values'][$id]['title'], $params['title'], 'in line ' . __LINE__); | |
02f3f951 | 196 | $this->assertEquals($result['values'][$id]['definition'], $params['definition'], 'in line ' . __LINE__); |
10ffff26 TO |
197 | |
198 | $caseXml = CRM_Case_XMLRepository::singleton()->retrieve('Application_with_Definition'); | |
199 | $this->assertTrue($caseXml instanceof SimpleXMLElement); | |
200 | } | |
201 | ||
202 | /** | |
203 | * Create a CaseType+case then delete the CaseType. | |
204 | */ | |
00be9182 | 205 | public function testCaseTypeDelete_InUse() { |
10ffff26 TO |
206 | // Create Case Type |
207 | $params = $this->fixtures['Application_with_Definition']; | |
208 | $createCaseType = $this->callAPISuccess('CaseType', 'create', $params); | |
209 | ||
210 | $createCase = $this->callAPISuccess('Case', 'create', array( | |
211 | 'case_type_id' => $createCaseType['id'], | |
212 | 'contact_id' => $this->_loggedInUser, | |
213 | 'subject' => 'Example', | |
214 | )); | |
215 | ||
216 | // Deletion fails while case-type is in-use | |
217 | $deleteCaseType = $this->callAPIFailure('CaseType', 'delete', array('id' => $createCaseType['id'])); | |
6bf8b6b7 | 218 | $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 |
219 | $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id'])); |
220 | $this->assertEquals(1, $getCaseType['count']); | |
221 | ||
222 | // Deletion succeeds when it's not in-use | |
223 | $this->callAPISuccess('Case', 'delete', array('id' => $createCase['id'])); | |
224 | ||
225 | // Check result - case type should no longer exist | |
226 | $this->callAPISuccess('CaseType', 'delete', array('id' => $createCaseType['id'])); | |
227 | $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id'])); | |
228 | $this->assertEquals(0, $getCaseType['count']); | |
a771c43f | 229 | } |
96025800 | 230 | |
eaefbeb2 | 231 | } |