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