Action schedule API modifications
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTypeTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /*
30 +--------------------------------------------------------------------+
31 | CiviCRM version 4.4 |
32 +--------------------------------------------------------------------+
33 | Copyright CiviCRM LLC (c) 2004-2013 |
34 +--------------------------------------------------------------------+
35 | This file is a part of CiviCRM. |
36 | |
37 | CiviCRM is free software; you can copy, modify, and distribute it |
38 | under the terms of the GNU Affero General Public License |
39 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
40 | |
41 | CiviCRM is distributed in the hope that it will be useful, but |
42 | WITHOUT ANY WARRANTY; without even the implied warranty of |
43 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
44 | See the GNU Affero General Public License for more details. |
45 | |
46 | You should have received a copy of the GNU Affero General Public |
47 | License and the CiviCRM Licensing Exception along |
48 | with this program; if not, contact CiviCRM LLC |
49 | at info[AT]civicrm[DOT]org. If you have questions about the |
50 | GNU Affero General Public License or the licensing of CiviCRM, |
51 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
52 +--------------------------------------------------------------------+
53 */
54
55 require_once 'CiviTest/CiviUnitTestCase.php';
56
57 /**
58 * Class contains api test cases for "civicrm_relationship_type"
59 *
60 */
61 class api_v3_RelationshipTypeTest extends CiviUnitTestCase {
62 protected $_cId_a;
63 protected $_cId_b;
64 protected $_relTypeID;
65 protected $_apiversion = 3;
66 public $_eNoticeCompliant = TRUE;
67 function get_info() {
68 return array(
69 'name' => 'RelationshipType Create',
70 'description' => 'Test all RelationshipType Create API methods.',
71 'group' => 'CiviCRM API Tests',
72 );
73 }
74
75 function setUp() {
76
77 parent::setUp();
78 $this->_cId_a = $this->individualCreate();
79 $this->_cId_b = $this->organizationCreate();
80 }
81
82 function tearDown() {
83
84 $tablesToTruncate = array(
85 'civicrm_contact',
86 'civicrm_relationship_type',
87 );
88 $this->quickCleanup($tablesToTruncate);
89 }
90
91 ///////////////// civicrm_relationship_type_add methods
92
93 /**
94 * check with no name
95 */
96 function testRelationshipTypeCreateWithoutName() {
97 $relTypeParams = array(
98 'contact_type_a' => 'Individual',
99 'contact_type_b' => 'Organization',
100 );
101 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
102 'Mandatory key(s) missing from params array: name_a_b, name_b_a'
103 );
104 }
105
106 /**
107 * check with no contact type
108 */
109 function testRelationshipTypeCreateWithoutContactType() {
110 $relTypeParams = array(
111 'name_a_b' => 'Relation 1 without contact type',
112 'name_b_a' => 'Relation 2 without contact type',
113 );
114 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
115 'Mandatory key(s) missing from params array: contact_type_a, contact_type_b'
116 );
117 }
118
119 /**
120 * create relationship type
121 */
122 function testRelationshipTypeCreate() {
123 $params = array(
124 'name_a_b' => 'Relation 1 for relationship type create',
125 'name_b_a' => 'Relation 2 for relationship type create',
126 'contact_type_a' => 'Individual',
127 'contact_type_b' => 'Organization',
128 'is_reserved' => 1,
129 'is_active' => 1,
130 'sequential' => 1,
131 );
132 $result = $this->callAPIAndDocument('relationship_type', 'create', $params, __FUNCTION__, __FILE__);
133 $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__);
134 unset($params['sequential']);
135 //assertDBState compares expected values in $result to actual values in the DB
136 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
137 }
138
139 /**
140 * Test using example code
141 */
142 function testRelationshipTypeCreateExample() {
143 require_once 'api/v3/examples/RelationshipTypeCreate.php';
144 $result = relationship_type_create_example();
145 $expectedResult = relationship_type_create_expectedresult();
146 $this->assertAPISuccess($result);
147 }
148
149 /**
150 * check if required fields are not passed
151 */
152 function testRelationshipTypeDeleteWithoutRequired() {
153 $params = array(
154 'name_b_a' => 'Relation 2 delete without required',
155 'contact_type_b' => 'Individual',
156 'is_reserved' => 0,
157 'is_active' => 0,
158 );
159
160 $result = $this->callAPIFailure('relationship_type', 'delete', $params);
161 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
162 }
163
164 /**
165 * check with incorrect required fields
166 */
167 function testRelationshipTypeDeleteWithIncorrectData() {
168 $params = array(
169 'id' => 'abcd',
170 'name_b_a' => 'Relation 2 delete with incorrect',
171 'description' => 'Testing relationship type',
172 'contact_type_a' => 'Individual',
173 'contact_type_b' => 'Individual',
174 'is_reserved' => 0,
175 'is_active' => 0,
176 );
177 $result = $this->callAPIFailure('relationship_type', 'delete', $params,
178 'Invalid value for relationship type ID'
179 );
180 }
181
182 /**
183 * check relationship type delete
184 */
185 function testRelationshipTypeDelete() {
186 $id = $this->_relationshipTypeCreate();
187 // create sample relationship type.
188 $params = array(
189 'id' => $id,
190 );
191 $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__, __FILE__);
192 $this->assertAPIDeleted('relationship_type', $id);
193 }
194
195 ///////////////// civicrm_relationship_type_update
196
197 /**
198 * check with empty array
199 */
200 function testRelationshipTypeUpdateEmpty() {
201 $params = array();
202 $result = $this->callAPIFailure('relationship_type', 'create', $params);
203 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: name_a_b, name_b_a, contact_type_a, contact_type_b');
204 }
205
206 /**
207 * check with no contact type
208 */
209 function testRelationshipTypeUpdateWithoutContactType() {
210 // create sample relationship type.
211 $this->_relTypeID = $this->_relationshipTypeCreate();
212
213 $relTypeParams = array(
214 'id' => $this->_relTypeID,
215 'name_a_b' => 'Test 1',
216 'name_b_a' => 'Test 2',
217 'description' => 'Testing relationship type',
218 'is_reserved' => 1,
219 'is_active' => 0,
220 );
221
222 $result = $this->callAPISuccess('relationship_type', 'create', $relTypeParams);
223 $this->assertNotNull($result['id']);
224 // assertDBState compares expected values in $result to actual values in the DB
225 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $relTypeParams);
226 }
227
228 /**
229 * check with all parameters
230 */
231 function testRelationshipTypeUpdate() {
232 // create sample relationship type.
233 $this->_relTypeID = $this->_relationshipTypeCreate();
234
235 $params = array(
236 'id' => $this->_relTypeID,
237 'name_a_b' => 'Test 1 for update',
238 'name_b_a' => 'Test 2 for update',
239 'description' => 'SUNIL PAWAR relationship type',
240 'contact_type_a' => 'Individual',
241 'contact_type_b' => 'Individual',
242 'is_reserved' => 0,
243 'is_active' => 0,
244 );
245
246 $result = $this->callAPISuccess('relationship_type', 'create', $params);
247 $this->assertNotNull($result['id']);
248
249 // assertDBState compares expected values in $result to actual values in the DB
250 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
251 }
252
253 ///////////////// civicrm_relationship_types_get methods
254
255 /**
256 * check with empty array
257 */
258 function testRelationshipTypesGetEmptyParams() {
259 $firstRelTypeParams = array(
260 'name_a_b' => 'Relation 27 for create',
261 'name_b_a' => 'Relation 28 for create',
262 'description' => 'Testing relationship type',
263 'contact_type_a' => 'Individual',
264 'contact_type_b' => 'Organization',
265 'is_reserved' => 1,
266 'is_active' => 1,
267 );
268
269 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
270
271 $secondRelTypeParams = array(
272 'name_a_b' => 'Relation 25 for create',
273 'name_b_a' => 'Relation 26 for create',
274 'description' => 'Testing relationship type second',
275 'contact_type_a' => 'Individual',
276 'contact_type_b' => 'Organization',
277 'is_reserved' => 0,
278 'is_active' => 1,
279 );
280 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
281 $results = $this->callAPISuccess('relationship_type', 'get', array());
282
283 $this->assertEquals(2, $results['count']);
284 }
285
286 /**
287 * check with params Not Array.
288 */
289 function testRelationshipTypesGetParamsNotArray() {
290
291 $results = $this->callAPIFailure('relationship_type', 'get', 'string');
292 }
293
294 /**
295 * check with valid params array.
296 */
297 function testRelationshipTypesGet() {
298 $firstRelTypeParams = array(
299 'name_a_b' => 'Relation 30 for create',
300 'name_b_a' => 'Relation 31 for create',
301 'description' => 'Testing relationship type',
302 'contact_type_a' => 'Individual',
303 'contact_type_b' => 'Organization',
304 'is_reserved' => 1,
305 'is_active' => 1,
306 );
307
308 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
309
310 $secondRelTypeParams = array(
311 'name_a_b' => 'Relation 32 for create',
312 'name_b_a' => 'Relation 33 for create',
313 'description' => 'Testing relationship type second',
314 'contact_type_a' => 'Individual',
315 'contact_type_b' => 'Organization',
316 'is_reserved' => 0,
317 'is_active' => 1,
318 );
319 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
320
321 $params = array(
322 'name_a_b' => 'Relation 32 for create',
323 'name_b_a' => 'Relation 33 for create',
324 'description' => 'Testing relationship type second',
325 );
326 $results = $this->callAPISuccess('relationship_type', 'get', $params);
327
328 $this->assertEquals(1, $results['count'], ' in line ' . __LINE__);
329 $this->assertEquals(1, $results['values'][$results['id']]['is_active'], ' in line ' . __LINE__);
330 }
331
332 /**
333 * create relationship type.
334 */
335 function _relationshipTypeCreate($params = NULL) {
336 if (!is_array($params) || empty($params)) {
337 $params = array(
338 'name_a_b' => 'Relation 1 for create',
339 'name_b_a' => 'Relation 2 for create',
340 'description' => 'Testing relationship type',
341 'contact_type_a' => 'Individual',
342 'contact_type_b' => 'Organization',
343 'is_reserved' => 1,
344 'is_active' => 1,
345 );
346 }
347
348 return $this->relationshipTypeCreate($params);
349 }
350 }
351