Commit | Line | Data |
---|---|---|
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 | ||
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; | |
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->_apiversion = 3; | |
79 | $this->_cId_a = $this->individualCreate(NULL); | |
80 | $this->_cId_b = $this->organizationCreate(NULL); | |
81 | } | |
82 | ||
83 | function tearDown() { | |
84 | ||
85 | $tablesToTruncate = array( | |
86 | 'civicrm_contact', | |
87 | 'civicrm_relationship_type', | |
88 | ); | |
89 | $this->quickCleanup($tablesToTruncate); | |
90 | } | |
91 | ||
92 | ///////////////// civicrm_relationship_type_add methods | |
93 | ||
94 | /** | |
95 | * check with no name | |
96 | */ | |
97 | function testRelationshipTypeCreateWithoutName() { | |
98 | $relTypeParams = array( | |
99 | 'contact_type_a' => 'Individual', | |
100 | 'contact_type_b' => 'Organization', | |
101 | 'version' => $this->_apiversion, | |
102 | ); | |
103 | $result = civicrm_api('relationship_type', 'create', $relTypeParams); | |
104 | ||
105 | $this->assertEquals($result['is_error'], 1); | |
106 | $this->assertEquals($result['error_message'], | |
107 | 'Mandatory key(s) missing from params array: name_a_b, name_b_a' | |
108 | ); | |
109 | } | |
110 | ||
111 | /** | |
112 | * check with no contact type | |
113 | */ | |
114 | function testRelationshipTypeCreateWithoutContactType() { | |
115 | $relTypeParams = array( | |
116 | 'name_a_b' => 'Relation 1 without contact type', | |
117 | 'name_b_a' => 'Relation 2 without contact type', | |
118 | 'version' => $this->_apiversion, | |
119 | ); | |
120 | $result = civicrm_api('relationship_type', 'create', $relTypeParams); | |
121 | ||
122 | $this->assertEquals($result['is_error'], 1); | |
123 | $this->assertEquals($result['error_message'], | |
124 | 'Mandatory key(s) missing from params array: contact_type_a, contact_type_b' | |
125 | ); | |
126 | } | |
127 | ||
128 | /** | |
129 | * create relationship type | |
130 | */ | |
131 | function testRelationshipTypeCreate() { | |
132 | $params = array( | |
133 | 'name_a_b' => 'Relation 1 for relationship type create', | |
134 | 'name_b_a' => 'Relation 2 for relationship type create', | |
135 | 'contact_type_a' => 'Individual', | |
136 | 'contact_type_b' => 'Organization', | |
137 | 'is_reserved' => 1, | |
138 | 'is_active' => 1, | |
139 | 'version' => $this->_apiversion, | |
140 | 'sequential' => 1, | |
141 | ); | |
142 | $result = civicrm_api('relationship_type', 'create', $params); | |
143 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
144 | $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__); | |
145 | unset($params['version']); | |
146 | unset($params['sequential']); | |
147 | //assertDBState compares expected values in $result to actual values in the DB | |
148 | $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params); | |
149 | } | |
150 | ||
151 | /** | |
152 | * Test using example code | |
153 | */ | |
154 | function testRelationshipTypeCreateExample() { | |
155 | require_once 'api/v3/examples/RelationshipTypeCreate.php'; | |
156 | $result = relationship_type_create_example(); | |
157 | $expectedResult = relationship_type_create_expectedresult(); | |
158 | $this->assertEquals($result['is_error'], 0); | |
159 | } | |
160 | ||
161 | ///////////////// civicrm_relationship_type_delete methods | |
162 | ||
163 | /** | |
164 | * check with empty array | |
165 | */ | |
166 | function testRelationshipTypeDeleteEmpty() { | |
167 | $params = array(); | |
168 | $result = civicrm_api('relationship_type', 'delete', $params); | |
169 | ||
170 | $this->assertEquals($result['is_error'], 1); | |
171 | } | |
172 | ||
173 | /** | |
174 | * check with No array | |
175 | */ | |
176 | function testRelationshipTypeDeleteParamsNotArray() { | |
177 | $params = 'name_a_b = Test1'; | |
178 | $result = civicrm_api('relationship_type', 'delete', $params); | |
179 | ||
180 | $this->assertEquals($result['is_error'], 1); | |
181 | } | |
182 | ||
183 | /** | |
184 | * check if required fields are not passed | |
185 | */ | |
186 | function testRelationshipTypeDeleteWithoutRequired() { | |
187 | $params = array( | |
188 | 'name_b_a' => 'Relation 2 delete without required', | |
189 | 'contact_type_b' => 'Individual', | |
190 | 'is_reserved' => 0, | |
191 | 'is_active' => 0, | |
192 | ); | |
193 | ||
194 | $result = civicrm_api('relationship_type', 'delete', $params); | |
195 | ||
196 | $this->assertEquals($result['is_error'], 1); | |
197 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: version, id'); | |
198 | } | |
199 | ||
200 | /** | |
201 | * check with incorrect required fields | |
202 | */ | |
203 | function testRelationshipTypeDeleteWithIncorrectData() { | |
204 | $params = array( | |
205 | 'id' => 'abcd', | |
206 | 'name_b_a' => 'Relation 2 delete with incorrect', | |
207 | 'description' => 'Testing relationship type', | |
208 | 'contact_type_a' => 'Individual', | |
209 | 'contact_type_b' => 'Individual', | |
210 | 'is_reserved' => 0, | |
211 | 'is_active' => 0, | |
212 | 'version' => $this->_apiversion, | |
213 | ); | |
214 | ||
215 | $result = civicrm_api('relationship_type', 'delete', $params); | |
216 | ||
217 | $this->assertEquals($result['is_error'], 1); | |
218 | $this->assertEquals($result['error_message'], 'Invalid value for relationship type ID'); | |
219 | } | |
220 | ||
221 | /** | |
222 | * check relationship type delete | |
223 | */ | |
224 | function testRelationshipTypeDelete() { | |
225 | $rel = $this->_relationshipTypeCreate(); | |
226 | // create sample relationship type. | |
227 | $params = array( | |
228 | 'id' => $rel, | |
229 | 'version' => $this->_apiversion, | |
230 | ); | |
231 | $result = civicrm_api('relationship_type', 'delete', $params); | |
232 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
233 | $this->assertEquals($result['is_error'], 0); | |
234 | } | |
235 | ||
236 | ///////////////// civicrm_relationship_type_update | |
237 | ||
238 | /** | |
239 | * check with empty array | |
240 | */ | |
241 | function testRelationshipTypeUpdateEmpty() { | |
242 | $params = array(); | |
243 | $result = civicrm_api('relationship_type', 'create', $params); | |
244 | ||
245 | $this->assertEquals($result['is_error'], 1); | |
246 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: version, name_a_b, name_b_a, contact_type_a, contact_type_b'); | |
247 | } | |
248 | ||
249 | /** | |
250 | * check with No array | |
251 | */ | |
252 | function testRelationshipTypeUpdateParamsNotArray() { | |
253 | $params = 'name_a_b = Relation 1'; | |
254 | $result = civicrm_api('relationship_type', 'create', $params); | |
255 | ||
256 | $this->assertEquals($result['is_error'], 1); | |
257 | $this->assertEquals($result['error_message'], 'Input variable `params` is not an array'); | |
258 | } | |
259 | ||
260 | /** | |
261 | * check with no contact type | |
262 | */ | |
263 | function testRelationshipTypeUpdateWithoutContactType() { | |
264 | // create sample relationship type. | |
265 | $this->_relTypeID = $this->_relationshipTypeCreate(NULL); | |
266 | ||
267 | $relTypeParams = array( | |
268 | 'id' => $this->_relTypeID, | |
269 | 'name_a_b' => 'Test 1', | |
270 | 'name_b_a' => 'Test 2', | |
271 | 'description' => 'Testing relationship type', | |
272 | 'is_reserved' => 1, | |
273 | 'is_active' => 0, | |
274 | 'version' => $this->_apiversion, | |
275 | ); | |
276 | ||
277 | $result = civicrm_api('relationship_type', 'create', $relTypeParams); | |
278 | $this->assertNotNull($result['id']); | |
279 | unset($relTypeParams['version']); | |
280 | // assertDBState compares expected values in $result to actual values in the DB | |
281 | $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $relTypeParams); | |
282 | } | |
283 | ||
284 | /** | |
285 | * check with all parameters | |
286 | */ | |
287 | function testRelationshipTypeUpdate() { | |
288 | // create sample relationship type. | |
289 | $this->_relTypeID = $this->_relationshipTypeCreate(NULL); | |
290 | ||
291 | $params = array( | |
292 | 'id' => $this->_relTypeID, | |
293 | 'name_a_b' => 'Test 1 for update', | |
294 | 'name_b_a' => 'Test 2 for update', | |
295 | 'description' => 'SUNIL PAWAR relationship type', | |
296 | 'contact_type_a' => 'Individual', | |
297 | 'contact_type_b' => 'Individual', | |
298 | 'is_reserved' => 0, | |
299 | 'is_active' => 0, | |
300 | 'version' => $this->_apiversion, | |
301 | ); | |
302 | ||
21150aae | 303 | $result = civicrm_api('relationship_type', 'create', $params); |
6a488035 TO |
304 | $this->assertNotNull($result['id']); |
305 | unset($params['version']); | |
306 | // assertDBState compares expected values in $result to actual values in the DB | |
307 | $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params); | |
308 | } | |
309 | ||
310 | ///////////////// civicrm_relationship_types_get methods | |
311 | ||
312 | /** | |
313 | * check with empty array | |
314 | */ | |
315 | function testRelationshipTypesGetEmptyParams() { | |
316 | $firstRelTypeParams = array( | |
317 | 'name_a_b' => 'Relation 27 for create', | |
318 | 'name_b_a' => 'Relation 28 for create', | |
319 | 'description' => 'Testing relationship type', | |
320 | 'contact_type_a' => 'Individual', | |
321 | 'contact_type_b' => 'Organization', | |
322 | 'is_reserved' => 1, | |
323 | 'is_active' => 1, | |
324 | 'version' => $this->_apiversion, | |
325 | ); | |
326 | ||
327 | $first = civicrm_api('RelationshipType', 'Create', $firstRelTypeParams); | |
328 | ||
329 | $secondRelTypeParams = array( | |
330 | 'name_a_b' => 'Relation 25 for create', | |
331 | 'name_b_a' => 'Relation 26 for create', | |
332 | 'description' => 'Testing relationship type second', | |
333 | 'contact_type_a' => 'Individual', | |
334 | 'contact_type_b' => 'Organization', | |
335 | 'is_reserved' => 0, | |
336 | 'is_active' => 1, | |
337 | 'version' => $this->_apiversion, | |
338 | ); | |
339 | $second = civicrm_api('RelationshipType', 'Create', $secondRelTypeParams); | |
340 | $results = civicrm_api('relationship_type', 'get', array( | |
341 | 'version' => $this->_apiversion, | |
342 | )); | |
343 | ||
344 | $this->assertEquals(2, $results['count']); | |
345 | $this->assertEquals(0, $results['is_error']); | |
346 | } | |
347 | ||
348 | /** | |
349 | * check with params Not Array. | |
350 | */ | |
351 | function testRelationshipTypesGetParamsNotArray() { | |
352 | ||
353 | $results = civicrm_api('relationship_type', 'get', 'string'); | |
354 | $this->assertEquals(1, $results['is_error']); | |
355 | } | |
356 | ||
357 | /** | |
358 | * check with valid params array. | |
359 | */ | |
360 | function testRelationshipTypesGet() { | |
361 | $firstRelTypeParams = array( | |
362 | 'name_a_b' => 'Relation 30 for create', | |
363 | 'name_b_a' => 'Relation 31 for create', | |
364 | 'description' => 'Testing relationship type', | |
365 | 'contact_type_a' => 'Individual', | |
366 | 'contact_type_b' => 'Organization', | |
367 | 'is_reserved' => 1, | |
368 | 'is_active' => 1, | |
369 | 'version' => $this->_apiversion, | |
370 | ); | |
371 | ||
372 | $first = civicrm_api('RelationshipType', 'Create', $firstRelTypeParams); | |
373 | ||
374 | $secondRelTypeParams = array( | |
375 | 'name_a_b' => 'Relation 32 for create', | |
376 | 'name_b_a' => 'Relation 33 for create', | |
377 | 'description' => 'Testing relationship type second', | |
378 | 'contact_type_a' => 'Individual', | |
379 | 'contact_type_b' => 'Organization', | |
380 | 'is_reserved' => 0, | |
381 | 'is_active' => 1, | |
382 | 'version' => $this->_apiversion, | |
383 | ); | |
384 | $second = civicrm_api('RelationshipType', 'Create', $secondRelTypeParams); | |
385 | ||
386 | $params = array( | |
387 | 'name_a_b' => 'Relation 32 for create', | |
388 | 'name_b_a' => 'Relation 33 for create', | |
389 | 'description' => 'Testing relationship type second', | |
390 | 'version' => $this->_apiversion, | |
391 | ); | |
392 | $results = civicrm_api('relationship_type', 'get', $params); | |
393 | ||
394 | $this->assertEquals(0, $results['is_error'], ' in line ' . __LINE__); | |
395 | $this->assertEquals(1, $results['count'], ' in line ' . __LINE__); | |
396 | $this->assertEquals(1, $results['values'][$results['id']]['is_active'], ' in line ' . __LINE__); | |
397 | } | |
398 | ||
399 | /** | |
400 | * create relationship type. | |
401 | */ | |
402 | function _relationshipTypeCreate($params = NULL) { | |
403 | if (!is_array($params) || empty($params)) { | |
404 | $params = array( | |
405 | 'name_a_b' => 'Relation 1 for create', | |
406 | 'name_b_a' => 'Relation 2 for create', | |
407 | 'description' => 'Testing relationship type', | |
408 | 'contact_type_a' => 'Individual', | |
409 | 'contact_type_b' => 'Organization', | |
410 | 'is_reserved' => 1, | |
411 | 'is_active' => 1, | |
412 | 'version' => API_LATEST_VERSION, | |
413 | ); | |
414 | } | |
415 | ||
416 | return $this->relationshipTypeCreate($params); | |
417 | } | |
418 | } | |
419 |