Merge pull request #11735 from mukeshcompucorp/CRM-21814-add-proper-container-to...
[civicrm-core.git] / tests / phpunit / api / v3 / CaseTypeTest.php
CommitLineData
eaefbeb2
N
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
eaefbeb2 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
eaefbeb2
N
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
e9479dcf
EM
28/**
29 * Class api_v3_CaseTypeTest
acb109b7 30 * @group headless
e9479dcf 31 */
10ffff26 32class api_v3_CaseTypeTest extends CiviCaseTestCase {
eaefbeb2 33
00be9182 34 public function setUp() {
10ffff26 35 $this->quickCleanup(array('civicrm_case_type'));
eaefbeb2 36 parent::setUp();
eaefbeb2 37
10ffff26
TO
38 $this->fixtures['Application_with_Definition'] = array(
39 'title' => 'Application with Definition',
40 'name' => 'Application_with_Definition',
41 'is_active' => 1,
42 'weight' => 4,
43 'definition' => array(
44 'activityTypes' => array(
45 array('name' => 'First act'),
46 ),
47 'activitySets' => array(
48 array(
49 'name' => 'set1',
50 'label' => 'Label 1',
51 'timeline' => 1,
52 'activityTypes' => array(
53 array('name' => 'Open Case', 'status' => 'Completed'),
54 ),
55 ),
56 ),
57 'caseRoles' => array(
58 array('name' => 'First role', 'creator' => 1, 'manager' => 1),
59 ),
21dfd5f5 60 ),
10ffff26 61 );
eaefbeb2
N
62 }
63
64 /**
65 * Tears down the fixture, for example, closes a network connection.
6d6dc885 66 *
eaefbeb2 67 * This method is called after a test is executed.
eaefbeb2 68 */
00be9182 69 public function tearDown() {
10ffff26 70 parent::tearDown();
225d474b 71 $this->quickCleanup(array('civicrm_case_type', 'civicrm_uf_match'));
eaefbeb2
N
72 }
73
74 /**
6d6dc885 75 * Check with empty array.
eaefbeb2 76 */
00be9182 77 public function testCaseTypeCreateEmpty() {
9eebe75f 78 $this->callAPIFailure('CaseType', 'create', array());
eaefbeb2
N
79 }
80
81 /**
6d6dc885 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 /**
6d6dc885
EM
98 * Test create methods with valid data.
99 *
100 * Success expected.
a771c43f 101 */
00be9182 102 public function testCaseTypeCreate() {
6d6dc885 103 // Create Case Type.
eaefbeb2
N
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
6d6dc885 114 // Check result.
eaefbeb2 115 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
a15773db 116 $this->assertEquals($result['values'][$id]['id'], $id);
ba4a1892 117 $this->assertEquals($result['values'][$id]['title'], $params['title']);
eaefbeb2
N
118 }
119
076f81b6 120 /**
6d6dc885 121 * Create a case with an invalid name.
076f81b6 122 */
00be9182 123 public function testCaseTypeCreate_invalidName() {
076f81b6 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 136 /**
6d6dc885 137 * Test update (create with id) function with valid parameters.
eaefbeb2 138 */
00be9182 139 public function testCaseTypeUpdate() {
eaefbeb2 140 // Create Case Type
6c6e6187 141 $params = array(
eaefbeb2
N
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
6d6dc885 152 // Update Case Type.
eaefbeb2
N
153 $params = array('id' => $id);
154 $params['title'] = $caseType['title'] = 'Something Else';
9eebe75f 155 $this->callAPISuccess('CaseType', 'create', $params);
eaefbeb2 156
6d6dc885 157 // Verify that updated case Type is exactly equal to the original with new title.
eaefbeb2 158 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
6d6dc885 159 $this->assertEquals($result['values'][$id], $caseType);
eaefbeb2
N
160 }
161
162 /**
6d6dc885 163 * Test delete function with valid parameters.
eaefbeb2 164 */
00be9182 165 public function testCaseTypeDelete_New() {
6d6dc885 166 // Create Case Type.
6c6e6187 167 $params = array(
eaefbeb2
N
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 /**
6d6dc885
EM
184 * Test create methods with xml file.
185 *
186 * Success expected.
a771c43f 187 */
00be9182 188 public function testCaseTypeCreateWithDefinition() {
a771c43f 189 // Create Case Type
10ffff26 190 $params = $this->fixtures['Application_with_Definition'];
a771c43f 191 $result = $this->callAPISuccess('CaseType', 'create', $params);
192 $id = $result['id'];
193
194 // Check result
195 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
a15773db 196 $this->assertEquals($result['values'][$id]['id'], $id);
ba4a1892
TM
197 $this->assertEquals($result['values'][$id]['title'], $params['title']);
198 $this->assertEquals($result['values'][$id]['definition'], $params['definition']);
10ffff26
TO
199
200 $caseXml = CRM_Case_XMLRepository::singleton()->retrieve('Application_with_Definition');
201 $this->assertTrue($caseXml instanceof SimpleXMLElement);
202 }
203
204 /**
205 * Create a CaseType+case then delete the CaseType.
206 */
00be9182 207 public function testCaseTypeDelete_InUse() {
10ffff26
TO
208 // Create Case Type
209 $params = $this->fixtures['Application_with_Definition'];
210 $createCaseType = $this->callAPISuccess('CaseType', 'create', $params);
211
212 $createCase = $this->callAPISuccess('Case', 'create', array(
213 'case_type_id' => $createCaseType['id'],
214 'contact_id' => $this->_loggedInUser,
215 'subject' => 'Example',
216 ));
217
218 // Deletion fails while case-type is in-use
219 $deleteCaseType = $this->callAPIFailure('CaseType', 'delete', array('id' => $createCaseType['id']));
6bf8b6b7 220 $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
221 $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id']));
222 $this->assertEquals(1, $getCaseType['count']);
223
6d6dc885 224 // Deletion succeeds when it's not in-use.
10ffff26
TO
225 $this->callAPISuccess('Case', 'delete', array('id' => $createCase['id']));
226
6d6dc885 227 // Check result - case type should no longer exist.
10ffff26
TO
228 $this->callAPISuccess('CaseType', 'delete', array('id' => $createCaseType['id']));
229 $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id']));
230 $this->assertEquals(0, $getCaseType['count']);
a771c43f 231 }
96025800 232
31c28ed5
CW
233 /**
234 * Test the api returns case statuses filtered by case type.
235 *
236 * Api getoptions should respect the case statuses declared in the case type definition.
237 *
238 * @throws \Exception
239 */
240 public function testCaseStatusByCaseType() {
d70a9131 241 $this->markTestIncomplete('Cannot figure out why this passes locally but fails on Jenkins.');
31c28ed5
CW
242 $statusName = md5(mt_rand());
243 $template = $this->callAPISuccess('CaseType', 'getsingle', array('id' => $this->caseTypeId));
244 unset($template['id']);
245 $template['name'] = $template['title'] = 'test_case_type';
246 $template['definition']['statuses'] = array('Closed', $statusName);
247 $this->callAPISuccess('CaseType', 'create', $template);
248 $this->callAPISuccess('OptionValue', 'create', array(
249 'option_group_id' => 'case_status',
250 'name' => $statusName,
251 'label' => $statusName,
252 'weight' => 99,
253 ));
254 $result = $this->callAPISuccess('Case', 'getoptions', array('field' => 'status_id', 'case_type_id' => 'test_case_type', 'context' => 'validate'));
255 $this->assertEquals($template['definition']['statuses'], array_values($result['values']));
256 }
257
eaefbeb2 258}