Merge pull request #1216 from davecivicrm/CRM-13062
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTypeTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2
3/*
4 +--------------------------------------------------------------------+
5| CiviCRM version 4.3 |
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*/
6a488035
TO
28
29/*
30 +--------------------------------------------------------------------+
31 | CiviCRM version 4.3 |
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
55require_once 'CiviTest/CiviUnitTestCase.php';
56
57/**
58 * Class contains api test cases for "civicrm_relationship_type"
59 *
60 */
61class api_v3_RelationshipTypeTest extends CiviUnitTestCase {
62 protected $_cId_a;
63 protected $_cId_b;
64 protected $_relTypeID;
b2402735 65 protected $_apiversion = 3;
6a488035
TO
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();
6a488035
TO
78 $this->_cId_a = $this->individualCreate(NULL);
79 $this->_cId_b = $this->organizationCreate(NULL);
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 'version' => $this->_apiversion,
101 );
d0e1eff2 102 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams);
6a488035
TO
103 $this->assertEquals($result['error_message'],
104 'Mandatory key(s) missing from params array: name_a_b, name_b_a'
105 );
106 }
107
108 /**
109 * check with no contact type
110 */
111 function testRelationshipTypeCreateWithoutContactType() {
112 $relTypeParams = array(
113 'name_a_b' => 'Relation 1 without contact type',
114 'name_b_a' => 'Relation 2 without contact type',
115 'version' => $this->_apiversion,
116 );
d0e1eff2 117 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams);
6a488035
TO
118 $this->assertEquals($result['error_message'],
119 'Mandatory key(s) missing from params array: contact_type_a, contact_type_b'
120 );
121 }
122
123 /**
124 * create relationship type
125 */
126 function testRelationshipTypeCreate() {
127 $params = array(
128 'name_a_b' => 'Relation 1 for relationship type create',
129 'name_b_a' => 'Relation 2 for relationship type create',
130 'contact_type_a' => 'Individual',
131 'contact_type_b' => 'Organization',
132 'is_reserved' => 1,
133 'is_active' => 1,
134 'version' => $this->_apiversion,
135 'sequential' => 1,
136 );
137 $result = civicrm_api('relationship_type', 'create', $params);
138 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
139 $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__);
140 unset($params['version']);
141 unset($params['sequential']);
142 //assertDBState compares expected values in $result to actual values in the DB
143 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
144 }
145
146 /**
147 * Test using example code
148 */
149 function testRelationshipTypeCreateExample() {
150 require_once 'api/v3/examples/RelationshipTypeCreate.php';
151 $result = relationship_type_create_example();
152 $expectedResult = relationship_type_create_expectedresult();
791c263c 153 $this->assertAPISuccess($result);
6a488035
TO
154 }
155
6a488035
TO
156 /**
157 * check if required fields are not passed
158 */
159 function testRelationshipTypeDeleteWithoutRequired() {
160 $params = array(
161 'name_b_a' => 'Relation 2 delete without required',
162 'contact_type_b' => 'Individual',
163 'is_reserved' => 0,
164 'is_active' => 0,
165 );
166
d0e1eff2
CW
167 $result = $this->callAPIFailure('relationship_type', 'delete', $params);
168 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
6a488035
TO
169 }
170
171 /**
172 * check with incorrect required fields
173 */
174 function testRelationshipTypeDeleteWithIncorrectData() {
175 $params = array(
176 'id' => 'abcd',
177 'name_b_a' => 'Relation 2 delete with incorrect',
178 'description' => 'Testing relationship type',
179 'contact_type_a' => 'Individual',
180 'contact_type_b' => 'Individual',
181 'is_reserved' => 0,
182 'is_active' => 0,
6a488035 183 );
b2402735 184 $result = $this->callAPIFailure('relationship_type', 'delete', $params,
185 'Invalid value for relationship type ID'
186 );
6a488035
TO
187 }
188
189 /**
190 * check relationship type delete
191 */
192 function testRelationshipTypeDelete() {
b2402735 193 $id = $this->_relationshipTypeCreate();
6a488035
TO
194 // create sample relationship type.
195 $params = array(
b2402735 196 'id' => $id,
6a488035 197 );
b2402735 198 $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__, __FILE__);
199 $this->assertAPIDeleted('relationship_type', $id);
6a488035
TO
200 }
201
202 ///////////////// civicrm_relationship_type_update
203
204 /**
205 * check with empty array
206 */
207 function testRelationshipTypeUpdateEmpty() {
208 $params = array();
d0e1eff2
CW
209 $result = $this->callAPIFailure('relationship_type', 'create', $params);
210 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: name_a_b, name_b_a, contact_type_a, contact_type_b');
6a488035
TO
211 }
212
6a488035
TO
213 /**
214 * check with no contact type
215 */
216 function testRelationshipTypeUpdateWithoutContactType() {
217 // create sample relationship type.
218 $this->_relTypeID = $this->_relationshipTypeCreate(NULL);
219
220 $relTypeParams = array(
221 'id' => $this->_relTypeID,
222 'name_a_b' => 'Test 1',
223 'name_b_a' => 'Test 2',
224 'description' => 'Testing relationship type',
225 'is_reserved' => 1,
226 'is_active' => 0,
6a488035
TO
227 );
228
b2402735 229 $result = $this->callAPISuccess('relationship_type', 'create', $relTypeParams);
6a488035 230 $this->assertNotNull($result['id']);
6a488035
TO
231 // assertDBState compares expected values in $result to actual values in the DB
232 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $relTypeParams);
233 }
234
235 /**
236 * check with all parameters
237 */
238 function testRelationshipTypeUpdate() {
239 // create sample relationship type.
240 $this->_relTypeID = $this->_relationshipTypeCreate(NULL);
241
242 $params = array(
243 'id' => $this->_relTypeID,
244 'name_a_b' => 'Test 1 for update',
245 'name_b_a' => 'Test 2 for update',
246 'description' => 'SUNIL PAWAR relationship type',
247 'contact_type_a' => 'Individual',
248 'contact_type_b' => 'Individual',
249 'is_reserved' => 0,
250 'is_active' => 0,
251 'version' => $this->_apiversion,
252 );
253
21150aae 254 $result = civicrm_api('relationship_type', 'create', $params);
6a488035
TO
255 $this->assertNotNull($result['id']);
256 unset($params['version']);
257 // assertDBState compares expected values in $result to actual values in the DB
258 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
259 }
260
261 ///////////////// civicrm_relationship_types_get methods
262
263 /**
264 * check with empty array
265 */
266 function testRelationshipTypesGetEmptyParams() {
267 $firstRelTypeParams = array(
268 'name_a_b' => 'Relation 27 for create',
269 'name_b_a' => 'Relation 28 for create',
270 'description' => 'Testing relationship type',
271 'contact_type_a' => 'Individual',
272 'contact_type_b' => 'Organization',
273 'is_reserved' => 1,
274 'is_active' => 1,
275 'version' => $this->_apiversion,
276 );
277
278 $first = civicrm_api('RelationshipType', 'Create', $firstRelTypeParams);
279
280 $secondRelTypeParams = array(
281 'name_a_b' => 'Relation 25 for create',
282 'name_b_a' => 'Relation 26 for create',
283 'description' => 'Testing relationship type second',
284 'contact_type_a' => 'Individual',
285 'contact_type_b' => 'Organization',
286 'is_reserved' => 0,
287 'is_active' => 1,
288 'version' => $this->_apiversion,
289 );
290 $second = civicrm_api('RelationshipType', 'Create', $secondRelTypeParams);
291 $results = civicrm_api('relationship_type', 'get', array(
292 'version' => $this->_apiversion,
293 ));
294
295 $this->assertEquals(2, $results['count']);
296 $this->assertEquals(0, $results['is_error']);
297 }
298
299 /**
300 * check with params Not Array.
301 */
302 function testRelationshipTypesGetParamsNotArray() {
303
d0e1eff2 304 $results = $this->callAPIFailure('relationship_type', 'get', 'string');
6a488035
TO
305 }
306
307 /**
308 * check with valid params array.
309 */
310 function testRelationshipTypesGet() {
311 $firstRelTypeParams = array(
312 'name_a_b' => 'Relation 30 for create',
313 'name_b_a' => 'Relation 31 for create',
314 'description' => 'Testing relationship type',
315 'contact_type_a' => 'Individual',
316 'contact_type_b' => 'Organization',
317 'is_reserved' => 1,
318 'is_active' => 1,
319 'version' => $this->_apiversion,
320 );
321
322 $first = civicrm_api('RelationshipType', 'Create', $firstRelTypeParams);
323
324 $secondRelTypeParams = array(
325 'name_a_b' => 'Relation 32 for create',
326 'name_b_a' => 'Relation 33 for create',
327 'description' => 'Testing relationship type second',
328 'contact_type_a' => 'Individual',
329 'contact_type_b' => 'Organization',
330 'is_reserved' => 0,
331 'is_active' => 1,
332 'version' => $this->_apiversion,
333 );
334 $second = civicrm_api('RelationshipType', 'Create', $secondRelTypeParams);
335
336 $params = array(
337 'name_a_b' => 'Relation 32 for create',
338 'name_b_a' => 'Relation 33 for create',
339 'description' => 'Testing relationship type second',
340 'version' => $this->_apiversion,
341 );
342 $results = civicrm_api('relationship_type', 'get', $params);
343
344 $this->assertEquals(0, $results['is_error'], ' in line ' . __LINE__);
345 $this->assertEquals(1, $results['count'], ' in line ' . __LINE__);
346 $this->assertEquals(1, $results['values'][$results['id']]['is_active'], ' in line ' . __LINE__);
347 }
348
349 /**
350 * create relationship type.
351 */
352 function _relationshipTypeCreate($params = NULL) {
353 if (!is_array($params) || empty($params)) {
354 $params = array(
355 'name_a_b' => 'Relation 1 for create',
356 'name_b_a' => 'Relation 2 for create',
357 'description' => 'Testing relationship type',
358 'contact_type_a' => 'Individual',
359 'contact_type_b' => 'Organization',
360 'is_reserved' => 1,
361 'is_active' => 1,
362 'version' => API_LATEST_VERSION,
363 );
364 }
365
366 return $this->relationshipTypeCreate($params);
367 }
368}
369