Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | ||
30 | /** | |
31 | * Class contains api test cases for "civicrm_relationship" | |
32 | * | |
33 | */ | |
34 | class api_v3_RelationshipTest extends CiviUnitTestCase { | |
35 | protected $_apiversion; | |
36 | protected $_cId_a; | |
37 | protected $_cId_b; | |
38 | protected $_cId_b2;// second org | |
39 | protected $_relTypeID; | |
40 | protected $_ids = array(); | |
41 | protected $_customGroupId = NULL; | |
42 | protected $_customFieldId = NULL; | |
43 | protected $_params; | |
26dcc9eb | 44 | public $_eNoticeCompliant = TRUE; |
6a488035 TO |
45 | protected $_entity; |
46 | function get_info() { | |
47 | return array( | |
48 | 'name' => 'Relationship Create', | |
49 | 'description' => 'Test all Relationship Create API methods.', | |
50 | 'group' => 'CiviCRM API Tests', | |
51 | ); | |
52 | } | |
53 | ||
54 | function setUp() { | |
55 | parent::setUp(); | |
56 | $this->_apiversion = 3; | |
57 | $this->_cId_a = $this->individualCreate(NULL); | |
58 | $this->_cId_b = $this->organizationCreate(); | |
59 | $this->_cId_b2 = $this->organizationCreate(array('organization_name' => ' Org 2')); | |
60 | $this->_entity = 'relationship'; | |
61 | //Create a relationship type | |
62 | $relTypeParams = array( | |
63 | 'name_a_b' => 'Relation 1 for delete', | |
64 | 'name_b_a' => 'Relation 2 for delete', | |
65 | 'description' => 'Testing relationship type', | |
66 | 'contact_type_a' => 'Individual', | |
67 | 'contact_type_b' => 'Organization', | |
68 | 'is_reserved' => 1, | |
69 | 'is_active' => 1, | |
70 | 'version' => $this->_apiversion, | |
71 | ); | |
72 | $this->_relTypeID = $this->relationshipTypeCreate($relTypeParams); | |
73 | $this->_params = array( | |
74 | 'contact_id_a' => $this->_cId_a, | |
75 | 'contact_id_b' => $this->_cId_b, | |
76 | 'relationship_type_id' => $this->_relTypeID, | |
77 | 'start_date' => '2008-12-20', | |
78 | 'is_active' => 1, | |
79 | 'version' => $this->_apiversion, | |
80 | ); | |
81 | ||
82 | } | |
83 | ||
84 | function tearDown() { | |
26dcc9eb | 85 | $this->quickCleanup(array('civicrm_relationship'), TRUE); |
6a488035 TO |
86 | $this->relationshipTypeDelete($this->_relTypeID); |
87 | $this->contactDelete($this->_cId_a); | |
88 | $this->contactDelete($this->_cId_b); | |
89 | } | |
90 | ||
91 | ///////////////// civicrm_relationship_create methods | |
92 | ||
93 | /** | |
94 | * check with empty array | |
95 | */ | |
96 | function testRelationshipCreateEmpty() { | |
97 | $params = array('version' => $this->_apiversion); | |
d0e1eff2 | 98 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
99 | } |
100 | ||
101 | /** | |
102 | * check with No array | |
103 | */ | |
104 | function testRelationshipCreateParamsNotArray() { | |
105 | $params = 'relationship_type_id = 5'; | |
d0e1eff2 | 106 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
107 | } |
108 | ||
109 | /** | |
110 | * check if required fields are not passed | |
111 | */ | |
112 | function testRelationshipCreateWithoutRequired() { | |
113 | $params = array( | |
114 | 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'), | |
115 | 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'), | |
116 | 'is_active' => 1, | |
117 | ); | |
118 | ||
d0e1eff2 | 119 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
120 | } |
121 | ||
122 | /** | |
123 | * check with incorrect required fields | |
124 | */ | |
125 | function testRelationshipCreateWithIncorrectData() { | |
126 | ||
127 | $params = array( | |
128 | 'contact_id_a' => $this->_cId_a, | |
129 | 'contact_id_b' => $this->_cId_b, | |
130 | 'relationship_type_id' => 'Breaking Relationship', | |
131 | 'version' => 3, | |
132 | ); | |
133 | ||
d0e1eff2 | 134 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
135 | |
136 | //contact id is not an integer | |
137 | $params = array( | |
138 | 'contact_id_a' => 'invalid', | |
139 | 'contact_id_b' => $this->_cId_b, | |
140 | 'relationship_type_id' => $this->_relTypeID, | |
141 | 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'), | |
142 | 'is_active' => 1, | |
143 | ); | |
d0e1eff2 | 144 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
145 | |
146 | //contact id does not exists | |
147 | $params['contact_id_a'] = 999; | |
d0e1eff2 | 148 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
149 | |
150 | //invalid date | |
151 | $params['contact_id_a'] = $this->_cId_a; | |
152 | $params['start_date'] = array('d' => '1', 'M' => '1'); | |
d0e1eff2 | 153 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
154 | } |
155 | ||
156 | /** | |
157 | * check relationship creation with invalid Relationship | |
158 | */ | |
159 | function testRelationshipCreatInvalidRelationship() { | |
160 | // both the contact of type Individual | |
161 | $params = array( | |
162 | 'contact_id_a' => $this->_cId_a, | |
163 | 'contact_id_b' => $this->_cId_a, | |
164 | 'relationship_type_id' => $this->_relTypeID, | |
165 | 'start_date' => '2008-01-10', | |
166 | 'is_active' => 1, | |
167 | 'version' => 3, | |
168 | ); | |
169 | ||
d0e1eff2 | 170 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
171 | |
172 | // both the contact of type Organization | |
173 | $params = array( | |
174 | 'contact_id_a' => $this->_cId_b, | |
175 | 'contact_id_b' => $this->_cId_b, | |
176 | 'relationship_type_id' => $this->_relTypeID, | |
177 | 'start_date' => '2008-01-10', | |
178 | 'is_active' => 1, | |
179 | 'version' => 3, | |
180 | ); | |
181 | ||
d0e1eff2 | 182 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
183 | } |
184 | ||
185 | /** | |
186 | * check relationship already exists | |
187 | */ | |
188 | function testRelationshipCreateAlreadyExists() { | |
189 | $params = array( | |
190 | 'contact_id_a' => $this->_cId_a, | |
191 | 'contact_id_b' => $this->_cId_b, | |
192 | 'relationship_type_id' => $this->_relTypeID, | |
193 | 'start_date' => '2008-12-20', 'end_date' => NULL, | |
194 | 'is_active' => 1, | |
195 | 'version' => $this->_apiversion, | |
196 | ); | |
197 | $relationship = civicrm_api('relationship', 'create', $params); | |
198 | ||
199 | $params = array( | |
200 | 'contact_id_a' => $this->_cId_a, | |
201 | 'contact_id_b' => $this->_cId_b, | |
202 | 'relationship_type_id' => $this->_relTypeID, | |
203 | 'start_date' => '2008-12-20', | |
204 | 'is_active' => 1, | |
6a488035 | 205 | ); |
d0e1eff2 | 206 | $result = $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
207 | $this->assertEquals($result['error_message'], 'Relationship already exists'); |
208 | ||
209 | $params['id'] = $relationship['id']; | |
210 | $result = civicrm_api('relationship', 'delete', $params); | |
211 | } | |
212 | ||
213 | /** | |
214 | * check relationship already exists | |
215 | */ | |
216 | function testRelationshipCreateUpdateAlreadyExists() { | |
217 | $params = array( | |
218 | 'contact_id_a' => $this->_cId_a, | |
219 | 'contact_id_b' => $this->_cId_b, | |
220 | 'relationship_type_id' => $this->_relTypeID, | |
221 | 'start_date' => '2008-12-20', | |
222 | 'end_date' => NULL, | |
223 | 'is_active' => 1, | |
6a488035 | 224 | |
26dcc9eb | 225 | ); |
226 | $relationship = $this->callAPISuccess('relationship', 'create', $params); | |
6a488035 TO |
227 | $params = array( |
228 | 'id' => $relationship['id'], | |
229 | 'is_active' => 0, | |
6a488035 TO |
230 | 'debug' => 1, |
231 | ); | |
26dcc9eb | 232 | $result = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 | 233 | $this->assertAPISuccess($result, 'in line ' . __LINE__); |
26dcc9eb | 234 | $result = $this->callAPISuccess('relationship', 'get', $params); |
6a488035 | 235 | $params['id'] = $relationship['id']; |
26dcc9eb | 236 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
237 | } |
238 | ||
239 | /** | |
240 | * checkupdate doesn't reset stuff badly - CRM-11789 | |
241 | */ | |
242 | function testRelationshipCreateUpdateDoesntMangle() { | |
243 | $params = array( | |
244 | 'contact_id_a' => $this->_cId_a, | |
245 | 'contact_id_b' => $this->_cId_b, | |
246 | 'relationship_type_id' => $this->_relTypeID, | |
247 | 'start_date' => '2008-12-20', | |
6a488035 TO |
248 | 'is_active' => 1, |
249 | 'is_permission_a_b' => 1, | |
250 | 'description' => 'my desc', | |
251 | 'version' => $this->_apiversion, | |
252 | ); | |
26dcc9eb | 253 | $relationship = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 TO |
254 | |
255 | $updateparams = array( | |
256 | 'id' => $relationship['id'], | |
257 | 'version' => $this->_apiversion, | |
258 | 'relationship_type_id' => $this->_relTypeID, | |
259 | ); | |
26dcc9eb | 260 | $result = $this->callAPISuccess('relationship', 'create', $updateparams); |
6a488035 | 261 | |
6a488035 TO |
262 | //make sure the orig params didn't get changed |
263 | $this->getAndCheck($params, $relationship['id'], 'relationship'); | |
264 | ||
265 | } | |
266 | ||
267 | ||
268 | ||
269 | /** | |
270 | * check relationship creation | |
271 | */ | |
272 | function testRelationshipCreate() { | |
273 | $params = array( | |
274 | 'contact_id_a' => $this->_cId_a, | |
275 | 'contact_id_b' => $this->_cId_b, | |
276 | 'relationship_type_id' => $this->_relTypeID, | |
277 | 'start_date' => '2010-10-30', | |
278 | 'end_date' => '2010-12-30', | |
279 | 'is_active' => 1, | |
280 | 'note' => 'note', | |
6a488035 TO |
281 | ); |
282 | ||
26dcc9eb | 283 | $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
284 | $this->assertNotNull($result['id'], 'in line ' . __LINE__); |
285 | $relationParams = array( | |
286 | 'id' => $result['id'], | |
287 | ); | |
288 | ||
289 | // assertDBState compares expected values in $result to actual values in the DB | |
290 | $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams); | |
26dcc9eb | 291 | $result = $this->callAPISuccess('relationship', 'get', array('version' => 3, 'id' => $result['id'])); |
6a488035 TO |
292 | $values = $result['values'][$result['id']]; |
293 | foreach ($params as $key => $value) { | |
294 | if ($key == 'version' || $key == 'note') { | |
295 | continue; | |
296 | } | |
297 | $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__); | |
298 | } | |
299 | $params['id'] = $result['id']; | |
26dcc9eb | 300 | $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
301 | } |
302 | ||
303 | /** | |
304 | * check relationship creation | |
305 | */ | |
306 | function testRelationshipCreateEmptyEndDate() { | |
307 | $params = array( | |
308 | 'contact_id_a' => $this->_cId_a, | |
309 | 'contact_id_b' => $this->_cId_b, | |
310 | 'relationship_type_id' => $this->_relTypeID, | |
311 | 'start_date' => '2010-10-30', | |
312 | 'end_date' => '', | |
313 | 'is_active' => 1, | |
314 | 'note' => 'note', | |
315 | 'version' => $this->_apiversion, | |
316 | ); | |
317 | ||
26dcc9eb | 318 | $result = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 TO |
319 | $this->assertNotNull($result['id'], 'in line ' . __LINE__); |
320 | $relationParams = array( | |
321 | 'id' => $result['id'], | |
322 | ); | |
323 | ||
324 | // assertDBState compares expected values in $result to actual values in the DB | |
325 | $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams); | |
26dcc9eb | 326 | $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id'])); |
6a488035 TO |
327 | $values = $result['values'][$result['id']]; |
328 | foreach ($params as $key => $value) { | |
329 | if ($key == 'version' || $key == 'note') { | |
330 | continue; | |
331 | } | |
332 | if($key == 'end_date'){ | |
333 | $this->assertTrue(empty($values[$key])); | |
334 | continue; | |
335 | } | |
336 | $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__); | |
337 | } | |
338 | $params['id'] = $result['id']; | |
26dcc9eb | 339 | $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
340 | } |
341 | ||
342 | /** | |
343 | * check relationship creation with custom data | |
344 | */ | |
345 | function testRelationshipCreateWithCustomData() { | |
346 | $customGroup = $this->createCustomGroup(); | |
347 | $this->_ids = $this->createCustomField(); | |
348 | //few custom Values for comparing | |
349 | $custom_params = array( | |
350 | "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship', | |
351 | "custom_{$this->_ids[1]}" => 'Y', | |
352 | "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00', | |
353 | "custom_{$this->_ids[3]}" => 'http://example.com', | |
354 | ); | |
355 | ||
356 | $params = array( | |
357 | 'contact_id_a' => $this->_cId_a, | |
358 | 'contact_id_b' => $this->_cId_b, | |
359 | 'relationship_type_id' => $this->_relTypeID, | |
360 | 'start_date' => '2008-12-20', | |
361 | 'is_active' => 1, | |
362 | 'version' => $this->_apiversion, | |
363 | ); | |
364 | $params = array_merge($params, $custom_params); | |
26dcc9eb | 365 | $result = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 | 366 | |
6a488035 TO |
367 | $relationParams = array( |
368 | 'id' => $result['id'], | |
369 | ); | |
370 | // assertDBState compares expected values in $result to actual values in the DB | |
371 | $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams); | |
372 | ||
373 | $params['id'] = $result['id']; | |
26dcc9eb | 374 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
375 | $this->relationshipTypeDelete($this->_relTypeID); |
376 | } | |
377 | ||
378 | /** | |
379 | * check with complete array + custom field | |
380 | * Note that the test is written on purpose without any | |
381 | * variables specific to participant so it can be replicated into other entities | |
382 | * and / or moved to the automated test suite | |
383 | */ | |
384 | function testGetWithCustom() { | |
385 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); | |
386 | ||
387 | $params = $this->_params; | |
388 | $params['custom_' . $ids['custom_field_id']] = "custom string"; | |
389 | ||
26dcc9eb | 390 | $result = $this->callAPISuccess($this->_entity, 'create', $params); |
6a488035 TO |
391 | $this->assertEquals($result['id'], $result['values'][$result['id']]['id']); |
392 | ||
6a488035 | 393 | $getParams = array('version' => 3, 'id' => $result['id']); |
26dcc9eb | 394 | $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__); |
6a488035 TO |
395 | $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); |
396 | ||
397 | $this->customFieldDelete($ids['custom_field_id']); | |
398 | $this->customGroupDelete($ids['custom_group_id']); | |
399 | } | |
400 | ||
401 | function createCustomGroup() { | |
402 | $params = array( | |
403 | 'title' => 'Custom Group', | |
404 | 'extends' => array('Relationship'), | |
405 | 'weight' => 5, | |
406 | 'style' => 'Inline', | |
407 | 'is_active' => 1, | |
408 | 'max_multiple' => 0, | |
409 | 'version' => $this->_apiversion, | |
410 | ); | |
26dcc9eb | 411 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
6a488035 TO |
412 | $this->_customGroupId = $customGroup['id']; |
413 | return $customGroup['id']; | |
414 | } | |
415 | ||
416 | function createCustomField() { | |
417 | $ids = array(); | |
418 | $params = array( | |
419 | 'custom_group_id' => $this->_customGroupId, | |
420 | 'label' => 'Enter text about relationship', | |
421 | 'html_type' => 'Text', | |
422 | 'data_type' => 'String', | |
423 | 'default_value' => 'xyz', | |
424 | 'weight' => 1, | |
425 | 'is_required' => 1, | |
426 | 'is_searchable' => 0, | |
427 | 'is_active' => 1, | |
428 | 'version' => $this->_apiversion, | |
429 | ); | |
430 | ||
431 | ||
26dcc9eb | 432 | $result = $this->callAPISuccess('CustomField', 'create', $params); |
6a488035 TO |
433 | |
434 | $customField = NULL; | |
435 | $ids[] = $customField['result']['customFieldId']; | |
436 | ||
437 | $optionValue[] = array( | |
438 | 'label' => 'Red', | |
439 | 'value' => 'R', | |
440 | 'weight' => 1, | |
441 | 'is_active' => 1, | |
442 | ); | |
443 | $optionValue[] = array( | |
444 | 'label' => 'Yellow', | |
445 | 'value' => 'Y', | |
446 | 'weight' => 2, | |
447 | 'is_active' => 1, | |
448 | ); | |
449 | $optionValue[] = array( | |
450 | 'label' => 'Green', | |
451 | 'value' => 'G', | |
452 | 'weight' => 3, | |
453 | 'is_active' => 1, | |
454 | ); | |
455 | ||
456 | $params = array( | |
457 | 'label' => 'Pick Color', | |
458 | 'html_type' => 'Select', | |
459 | 'data_type' => 'String', | |
460 | 'weight' => 2, | |
461 | 'is_required' => 1, | |
462 | 'is_searchable' => 0, | |
463 | 'is_active' => 1, | |
464 | 'option_values' => $optionValue, | |
465 | 'custom_group_id' => $this->_customGroupId, | |
466 | 'version' => $this->_apiversion, | |
467 | ); | |
468 | ||
26dcc9eb | 469 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
470 | $ids[] = $customField['id']; |
471 | ||
472 | $params = array( | |
473 | 'custom_group_id' => $this->_customGroupId, | |
474 | 'name' => 'test_date', | |
475 | 'label' => 'test_date', | |
476 | 'html_type' => 'Select Date', | |
477 | 'data_type' => 'Date', | |
478 | 'default_value' => '20090711', | |
479 | 'weight' => 3, | |
480 | 'is_required' => 1, | |
481 | 'is_searchable' => 0, | |
482 | 'is_active' => 1, | |
483 | 'version' => $this->_apiversion, | |
484 | ); | |
485 | ||
26dcc9eb | 486 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
487 | |
488 | $ids[] = $customField['id']; | |
489 | $params = array( | |
490 | 'custom_group_id' => $this->_customGroupId, | |
491 | 'name' => 'test_link', | |
492 | 'label' => 'test_link', | |
493 | 'html_type' => 'Link', | |
494 | 'data_type' => 'Link', | |
495 | 'default_value' => 'http://civicrm.org', | |
496 | 'weight' => 4, | |
497 | 'is_required' => 1, | |
498 | 'is_searchable' => 0, | |
499 | 'is_active' => 1, | |
500 | 'version' => $this->_apiversion, | |
501 | ); | |
502 | ||
26dcc9eb | 503 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
504 | $ids[] = $customField['id']; |
505 | return $ids; | |
506 | } | |
507 | ||
508 | ///////////////// civicrm_relationship_delete methods | |
509 | ||
510 | /** | |
511 | * check with empty array | |
512 | */ | |
513 | function testRelationshipDeleteEmpty() { | |
514 | $params = array('version' => $this->_apiversion); | |
d0e1eff2 | 515 | $result = $this->callAPIFailure('relationship', 'delete', $params); |
6a488035 TO |
516 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id'); |
517 | } | |
518 | ||
6a488035 TO |
519 | /** |
520 | * check if required fields are not passed | |
521 | */ | |
522 | function testRelationshipDeleteWithoutRequired() { | |
523 | $params = array( | |
524 | 'start_date' => '2008-12-20', | |
525 | 'end_date' => '2009-12-20', | |
526 | 'is_active' => 1, | |
527 | ); | |
528 | ||
d0e1eff2 CW |
529 | $result = $this->callAPIFailure('relationship', 'delete', $params); |
530 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id'); | |
6a488035 TO |
531 | } |
532 | ||
533 | /** | |
534 | * check with incorrect required fields | |
535 | */ | |
536 | function testRelationshipDeleteWithIncorrectData() { | |
537 | $params = array( | |
538 | 'contact_id_a' => $this->_cId_a, | |
539 | 'contact_id_b' => $this->_cId_b, | |
540 | 'relationship_type_id' => 'Breaking Relationship', | |
541 | 'version' => $this->_apiversion, | |
542 | ); | |
543 | ||
d0e1eff2 | 544 | $result = $this->callAPIFailure('relationship', 'delete', $params); |
6a488035 TO |
545 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id', 'in line ' . __LINE__); |
546 | ||
547 | $params['id'] = "Invalid"; | |
d0e1eff2 | 548 | $result = $this->callAPIFailure('relationship', 'delete', $params); |
6a488035 TO |
549 | $this->assertEquals($result['error_message'], 'Invalid value for relationship ID', 'in line ' . __LINE__); |
550 | } | |
551 | ||
552 | /** | |
553 | * check relationship creation | |
554 | */ | |
555 | function testRelationshipDelete() { | |
556 | $params = array( | |
557 | 'contact_id_a' => $this->_cId_a, | |
558 | 'contact_id_b' => $this->_cId_b, | |
559 | 'relationship_type_id' => $this->_relTypeID, | |
560 | 'start_date' => '2008-12-20', | |
561 | 'is_active' => 1, | |
562 | 'version' => $this->_apiversion, | |
563 | ); | |
564 | ||
26dcc9eb | 565 | $result = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 TO |
566 | |
567 | //Delete relationship | |
568 | $params = array(); | |
569 | $params['id'] = $result['id']; | |
570 | ||
26dcc9eb | 571 | $result = $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
572 | $this->relationshipTypeDelete($this->_relTypeID); |
573 | } | |
574 | ||
575 | ///////////////// civicrm_relationship_update methods | |
576 | ||
577 | /** | |
578 | * check with empty array | |
579 | */ | |
580 | function testRelationshipUpdateEmpty() { | |
d0e1eff2 | 581 | $result = $this->callAPIFailure('relationship', 'create', array()); |
6a488035 TO |
582 | $this->assertEquals('Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id', $result['error_message'], 'In line ' . __LINE__); |
583 | } | |
584 | ||
6a488035 TO |
585 | /** |
586 | * check if required fields are not passed | |
587 | */ | |
588 | ||
589 | /** | |
590 | * check relationship update | |
591 | */ | |
82a0ae6b | 592 | function testRelationshipCreateDuplicate() { |
6a488035 TO |
593 | $relParams = array( |
594 | 'contact_id_a' => $this->_cId_a, | |
595 | 'contact_id_b' => $this->_cId_b, | |
596 | 'relationship_type_id' => $this->_relTypeID, | |
597 | 'start_date' => '20081214', | |
598 | 'end_date' => '20091214', | |
599 | 'is_active' => 1, | |
6a488035 TO |
600 | ); |
601 | ||
26dcc9eb | 602 | $result = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
603 | |
604 | $this->assertNotNull($result['id'], 'In line ' . __LINE__); | |
605 | $this->_relationID = $result['id']; | |
606 | ||
607 | $params = array( | |
6a488035 TO |
608 | 'contact_id_a' => $this->_cId_a, |
609 | 'contact_id_b' => $this->_cId_b, | |
610 | 'relationship_type_id' => $this->_relTypeID, | |
611 | 'start_date' => '20081214', | |
612 | 'end_date' => '20091214', 'is_active' => 0, | |
613 | 'version' => $this->_apiversion, | |
614 | ); | |
615 | ||
d0e1eff2 | 616 | $result = $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
617 | $this->assertEquals($result['error_message'], 'Relationship already exists', 'In line ' . __LINE__); |
618 | ||
619 | //delete created relationship | |
620 | $params = array( | |
621 | 'id' => $this->_relationID, | |
622 | 'version' => $this->_apiversion, | |
623 | ); | |
624 | ||
625 | $result = civicrm_api('relationship', 'delete', $params); | |
791c263c | 626 | $this->assertAPISuccess($result, 'in line ' . __LINE__); |
6a488035 TO |
627 | |
628 | //delete created relationship type | |
629 | $this->relationshipTypeDelete($this->_relTypeID); | |
630 | } | |
631 | ||
632 | /** | |
633 | * check with valid params array. | |
634 | */ | |
635 | function testRelationshipsGet() { | |
636 | $relParams = array( | |
637 | 'contact_id_a' => $this->_cId_a, | |
638 | 'contact_id_b' => $this->_cId_b, | |
639 | 'relationship_type_id' => $this->_relTypeID, | |
640 | 'start_date' => '2011-01-01', | |
641 | 'end_date' => '2013-01-01', | |
642 | 'is_active' => 1, | |
643 | 'version' => $this->_apiversion, | |
644 | ); | |
645 | ||
646 | $result = civicrm_api('relationship', 'create', $relParams); | |
647 | ||
648 | //get relationship | |
649 | $params = array( | |
650 | 'contact_id' => $this->_cId_b, | |
6a488035 | 651 | ); |
26dcc9eb | 652 | $result = $this->callAPISuccess('relationship', 'get', $params); |
6a488035 TO |
653 | $this->assertEquals($result['count'], 1, 'in line ' . __LINE__); |
654 | $params = array( | |
655 | 'contact_id_a' => $this->_cId_a, | |
6a488035 | 656 | ); |
26dcc9eb | 657 | $result = $this->callAPISuccess('relationship', 'get', $params); |
6a488035 TO |
658 | $this->assertEquals($result['count'], 1, 'in line ' . __LINE__); |
659 | // contact_id_a is wrong so should be no matches | |
660 | $params = array( | |
661 | 'contact_id_a' => $this->_cId_b, | |
662 | 'version' => $this->_apiversion, | |
663 | ); | |
26dcc9eb | 664 | $result = $this->callAPISuccess('relationship', 'get', $params); |
6a488035 TO |
665 | $this->assertEquals($result['count'], 0, 'in line ' . __LINE__); |
666 | } | |
667 | ||
668 | /** | |
669 | * check with valid params array. | |
670 | * (The get function will behave differently without 'contact_id' passed | |
671 | */ | |
672 | function testRelationshipsGetGeneric() { | |
673 | $relParams = array( | |
674 | 'contact_id_a' => $this->_cId_a, | |
675 | 'contact_id_b' => $this->_cId_b, | |
676 | 'relationship_type_id' => $this->_relTypeID, | |
677 | 'start_date' => '2011-01-01', | |
678 | 'end_date' => '2013-01-01', | |
679 | 'is_active' => 1, | |
680 | 'version' => $this->_apiversion, | |
681 | ); | |
682 | ||
26dcc9eb | 683 | $result = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
684 | |
685 | //get relationship | |
686 | $params = array( | |
687 | 'contact_id_b' => $this->_cId_b, | |
6a488035 | 688 | ); |
26dcc9eb | 689 | $result = $this->callAPISuccess('relationship', 'get', $params); |
6a488035 TO |
690 | } |
691 | ||
692 | function testGetIsCurrent() { | |
693 | $rel2Params =array( | |
694 | 'contact_id_a' => $this->_cId_a, | |
695 | 'contact_id_b' => $this->_cId_b2, | |
696 | 'relationship_type_id' => $this->_relTypeID, | |
697 | 'start_date' => '2008-12-20', | |
698 | 'is_active' => 0, | |
6a488035 | 699 | ); |
26dcc9eb | 700 | $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); |
701 | $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params); | |
702 | ||
6a488035 | 703 | $getParams = array( |
6a488035 TO |
704 | 'filters' => array('is_current' => 1) |
705 | ); | |
706 | $description = "demonstrates is_current filter"; | |
707 | $subfile = 'filterIsCurrent'; | |
708 | //no relationship has been created | |
26dcc9eb | 709 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
710 | $this->assertEquals($result['count'], 1); |
711 | $this->AssertEquals($rel1['id'], $result['id']); | |
712 | ||
713 | // now try not started | |
714 | $rel2Params['is_active'] =1; | |
715 | $rel2Params['start_date'] ='tomorrow'; | |
26dcc9eb | 716 | $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); |
717 | $result = $this->callAPISuccess('relationship', 'get', $getParams); | |
6a488035 TO |
718 | $this->assertEquals($result['count'], 1); |
719 | $this->AssertEquals($rel1['id'], $result['id']); | |
720 | ||
721 | // now try finished | |
722 | $rel2Params['is_active'] =1; | |
723 | $rel2Params['start_date'] ='last week'; | |
724 | $rel2Params['end_date'] ='yesterday'; | |
26dcc9eb | 725 | $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); |
6a488035 TO |
726 | } |
727 | /* | |
728 | * Test using various operators | |
729 | */ | |
730 | function testGetTypeOperators() { | |
731 | $relTypeParams = array( | |
732 | 'name_a_b' => 'Relation 3 for delete', | |
733 | 'name_b_a' => 'Relation 6 for delete', | |
734 | 'description' => 'Testing relationship type 2', | |
735 | 'contact_type_a' => 'Individual', | |
736 | 'contact_type_b' => 'Organization', | |
737 | 'is_reserved' => 1, | |
738 | 'is_active' => 1, | |
739 | 'version' => $this->_apiversion, | |
740 | ); | |
741 | $relationType2 = $this->relationshipTypeCreate($relTypeParams); | |
742 | $relTypeParams = array( | |
743 | 'name_a_b' => 'Relation 8 for delete', | |
744 | 'name_b_a' => 'Relation 9 for delete', | |
745 | 'description' => 'Testing relationship type 7', | |
746 | 'contact_type_a' => 'Individual', | |
747 | 'contact_type_b' => 'Organization', | |
748 | 'is_reserved' => 1, | |
749 | 'is_active' => 1, | |
750 | 'version' => $this->_apiversion, | |
751 | ); | |
752 | $relationType3 = $this->relationshipTypeCreate($relTypeParams); | |
753 | ||
754 | $relTypeParams = array( | |
755 | 'name_a_b' => 'Relation 6 for delete', | |
756 | 'name_b_a' => 'Relation 88for delete', | |
757 | 'description' => 'Testing relationship type 00', | |
758 | 'contact_type_a' => 'Individual', | |
759 | 'contact_type_b' => 'Organization', | |
760 | 'is_reserved' => 1, | |
761 | 'is_active' => 1, | |
762 | 'version' => $this->_apiversion, | |
763 | ); | |
764 | $relationType4 = $this->relationshipTypeCreate($relTypeParams); | |
765 | ||
26dcc9eb | 766 | $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params); |
767 | $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, | |
6a488035 | 768 | array('relationship_type_id' => $relationType2,))); |
26dcc9eb | 769 | $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, |
6a488035 | 770 | array('relationship_type_id' => $relationType3,))); |
26dcc9eb | 771 | $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, |
6a488035 | 772 | array('relationship_type_id' => $relationType4,))); |
6a488035 TO |
773 | |
774 | $getParams = array( | |
26dcc9eb | 775 | 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)) |
6a488035 TO |
776 | ); |
777 | ||
6a488035 TO |
778 | $description = "demonstrates use of IN filter"; |
779 | $subfile = 'INRelationshipType'; | |
780 | ||
26dcc9eb | 781 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
782 | $this->assertEquals($result['count'], 2); |
783 | $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values'])); | |
784 | ||
785 | $description = "demonstrates use of NOT IN filter"; | |
786 | $subfile = 'NotInRelationshipType'; | |
787 | $getParams = array( | |
6a488035 TO |
788 | 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)) |
789 | ); | |
26dcc9eb | 790 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
791 | $this->assertEquals($result['count'], 2); |
792 | $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values'])); | |
793 | ||
794 | $description = "demonstrates use of BETWEEN filter"; | |
795 | $subfile = 'BetweenRelationshipType'; | |
796 | $getParams = array( | |
6a488035 TO |
797 | 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)) |
798 | ); | |
26dcc9eb | 799 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
800 | $this->assertEquals($result['count'], 3); |
801 | $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values'])); | |
802 | ||
803 | $description = "demonstrates use of Not BETWEEN filter"; | |
804 | $subfile = 'NotBetweenRelationshipType'; | |
805 | $getParams = array( | |
806 | 'version' => $this->_apiversion, | |
807 | 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)) | |
808 | ); | |
26dcc9eb | 809 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
810 | $this->assertEquals($result['count'], 1); |
811 | $this->AssertEquals(array($rel1['id'],), array_keys($result['values'])); | |
812 | ||
813 | } | |
814 | /** | |
815 | * check with invalid relationshipType Id | |
816 | */ | |
817 | function testRelationshipTypeAddInvalidId() { | |
818 | $relTypeParams = array( | |
819 | 'id' => 'invalid', | |
820 | 'name_a_b' => 'Relation 1 for delete', | |
821 | 'name_b_a' => 'Relation 2 for delete', | |
822 | 'contact_type_a' => 'Individual', | |
823 | 'contact_type_b' => 'Organization', | |
6a488035 | 824 | ); |
d0e1eff2 CW |
825 | $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams); |
826 | $this->assertEquals('id is not a valid integer', $result['error_message'], 'in line ' . __LINE__); | |
6a488035 TO |
827 | } |
828 | ||
6a488035 TO |
829 | /** |
830 | * check with valid data with contact_b | |
831 | */ | |
832 | function testGetRelationshipWithContactB() { | |
833 | $relParams = array( | |
834 | 'contact_id_a' => $this->_cId_a, | |
835 | 'contact_id_b' => $this->_cId_b, | |
836 | 'relationship_type_id' => $this->_relTypeID, | |
837 | 'start_date' => '2011-01-01', | |
838 | 'end_date' => '2013-01-01', | |
839 | 'is_active' => 1, | |
6a488035 TO |
840 | ); |
841 | ||
26dcc9eb | 842 | $relationship = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
843 | |
844 | $contacts = array( | |
845 | 'contact_id' => $this->_cId_a, | |
6a488035 TO |
846 | ); |
847 | ||
26dcc9eb | 848 | $result = $this->callAPISuccess('relationship', 'get', $contacts); |
6a488035 TO |
849 | $this->assertGreaterThan(0, $result['count'], 'in line ' . __LINE__); |
850 | $params = array( | |
851 | 'id' => $relationship['id'], | |
6a488035 | 852 | ); |
26dcc9eb | 853 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
854 | $this->relationshipTypeDelete($this->_relTypeID); |
855 | } | |
856 | ||
857 | /** | |
858 | * check with valid data with relationshipTypes | |
859 | */ | |
860 | function testGetRelationshipWithRelTypes() { | |
861 | $relParams = array( | |
862 | 'contact_id_a' => $this->_cId_a, | |
863 | 'contact_id_b' => $this->_cId_b, | |
864 | 'relationship_type_id' => $this->_relTypeID, | |
865 | 'start_date' => '2011-01-01', | |
866 | 'end_date' => '2013-01-01', | |
867 | 'is_active' => 1, | |
6a488035 TO |
868 | ); |
869 | ||
26dcc9eb | 870 | $relationship = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
871 | |
872 | $contact_a = array( | |
873 | 'contact_id' => $this->_cId_a, | |
6a488035 | 874 | ); |
26dcc9eb | 875 | $result = $this->callAPISuccess('relationship', 'get', $contact_a); |
6a488035 TO |
876 | |
877 | $params = array( | |
878 | 'id' => $relationship['id'], | |
6a488035 | 879 | ); |
26dcc9eb | 880 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
881 | $this->relationshipTypeDelete($this->_relTypeID); |
882 | } | |
883 | } | |
884 |