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