5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
30 require_once 'CiviTest/CiviUnitTestCase.php';
33 * Class contains api test cases for "civicrm_relationship"
36 class api_v3_RelationshipTest
extends CiviUnitTestCase
{
37 protected $_apiversion;
40 protected $_cId_b2;// second org
41 protected $_relTypeID;
42 protected $_ids = array();
43 protected $_customGroupId = NULL;
44 protected $_customFieldId = NULL;
46 public $_eNoticeCompliant = FALSE;
50 'name' => 'Relationship Create',
51 'description' => 'Test all Relationship Create API methods.',
52 'group' => 'CiviCRM API Tests',
58 $this->_apiversion
= 3;
59 $this->_cId_a
= $this->individualCreate(NULL);
60 $this->_cId_b
= $this->organizationCreate();
61 $this->_cId_b2
= $this->organizationCreate(array('organization_name' => ' Org 2'));
62 $this->_entity
= 'relationship';
63 //Create a relationship type
64 $relTypeParams = array(
65 'name_a_b' => 'Relation 1 for delete',
66 'name_b_a' => 'Relation 2 for delete',
67 'description' => 'Testing relationship type',
68 'contact_type_a' => 'Individual',
69 'contact_type_b' => 'Organization',
72 'version' => $this->_apiversion
,
74 $this->_relTypeID
= $this->relationshipTypeCreate($relTypeParams);
75 $this->_params
= array(
76 'contact_id_a' => $this->_cId_a
,
77 'contact_id_b' => $this->_cId_b
,
78 'relationship_type_id' => $this->_relTypeID
,
79 'start_date' => '2008-12-20',
81 'version' => $this->_apiversion
,
87 $this->quickCleanup(array('civicrm_relationship'));
88 $this->relationshipTypeDelete($this->_relTypeID
);
89 $this->contactDelete($this->_cId_a
);
90 $this->contactDelete($this->_cId_b
);
93 ///////////////// civicrm_relationship_create methods
96 * check with empty array
98 function testRelationshipCreateEmpty() {
99 $params = array('version' => $this->_apiversion
);
100 $result = civicrm_api('relationship', 'create', $params);
101 $this->assertEquals($result['is_error'], 1);
105 * check with No array
107 function testRelationshipCreateParamsNotArray() {
108 $params = 'relationship_type_id = 5';
109 $result = civicrm_api('relationship', 'create', $params);
110 $this->assertEquals($result['is_error'], 1);
114 * check if required fields are not passed
116 function testRelationshipCreateWithoutRequired() {
118 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
119 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
123 $result = civicrm_api('relationship', 'create', $params);
124 $this->assertEquals($result['is_error'], 1);
128 * check with incorrect required fields
130 function testRelationshipCreateWithIncorrectData() {
133 'contact_id_a' => $this->_cId_a
,
134 'contact_id_b' => $this->_cId_b
,
135 'relationship_type_id' => 'Breaking Relationship',
139 $result = civicrm_api('relationship', 'create', $params);
140 $this->assertEquals($result['is_error'], 1);
142 //contact id is not an integer
144 'contact_id_a' => 'invalid',
145 'contact_id_b' => $this->_cId_b
,
146 'relationship_type_id' => $this->_relTypeID
,
147 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
150 $result = civicrm_api('relationship', 'create', $params);
151 $this->assertEquals($result['is_error'], 1);
153 //contact id does not exists
154 $params['contact_id_a'] = 999;
155 $result = civicrm_api('relationship', 'create', $params);
156 $this->assertEquals($result['is_error'], 1);
159 $params['contact_id_a'] = $this->_cId_a
;
160 $params['start_date'] = array('d' => '1', 'M' => '1');
161 $result = civicrm_api('relationship', 'create', $params);
162 $this->assertEquals($result['is_error'], 1);
166 * check relationship creation with invalid Relationship
168 function testRelationshipCreatInvalidRelationship() {
169 // both the contact of type Individual
171 'contact_id_a' => $this->_cId_a
,
172 'contact_id_b' => $this->_cId_a
,
173 'relationship_type_id' => $this->_relTypeID
,
174 'start_date' => '2008-01-10',
179 $result = civicrm_api('relationship', 'create', $params);
180 $this->assertEquals($result['is_error'], 1);
182 // both the contact of type Organization
184 'contact_id_a' => $this->_cId_b
,
185 'contact_id_b' => $this->_cId_b
,
186 'relationship_type_id' => $this->_relTypeID
,
187 'start_date' => '2008-01-10',
192 $result = civicrm_api('relationship', 'create', $params);
193 $this->assertEquals($result['is_error'], 1);
197 * check relationship already exists
199 function testRelationshipCreateAlreadyExists() {
201 'contact_id_a' => $this->_cId_a
,
202 'contact_id_b' => $this->_cId_b
,
203 'relationship_type_id' => $this->_relTypeID
,
204 'start_date' => '2008-12-20', 'end_date' => NULL,
206 'version' => $this->_apiversion
,
208 $relationship = civicrm_api('relationship', 'create', $params);
211 'contact_id_a' => $this->_cId_a
,
212 'contact_id_b' => $this->_cId_b
,
213 'relationship_type_id' => $this->_relTypeID
,
214 'start_date' => '2008-12-20',
216 'version' => $this->_apiversion
,
218 $result = civicrm_api('relationship', 'create', $params);
220 $this->assertEquals($result['is_error'], 1);
221 $this->assertEquals($result['error_message'], 'Relationship already exists');
223 $params['id'] = $relationship['id'];
224 $result = civicrm_api('relationship', 'delete', $params);
228 * check relationship already exists
230 function testRelationshipCreateUpdateAlreadyExists() {
232 'contact_id_a' => $this->_cId_a
,
233 'contact_id_b' => $this->_cId_b
,
234 'relationship_type_id' => $this->_relTypeID
,
235 'start_date' => '2008-12-20',
238 'version' => $this->_apiversion
,
240 $relationship = civicrm_api('relationship', 'create', $params);
243 'id' => $relationship['id'],
245 'version' => $this->_apiversion
,
248 $result = civicrm_api('relationship', 'create', $params);
249 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
250 $result = civicrm_api('relationship', 'get', $params);
251 $this->assertEquals(0, $result['values'][$result['id']]['is_active'], 'in line ' . __LINE__
);
252 $params['id'] = $relationship['id'];
253 $result = civicrm_api('relationship', 'delete', $params);
257 * checkupdate doesn't reset stuff badly - CRM-11789
259 function testRelationshipCreateUpdateDoesntMangle() {
261 'contact_id_a' => $this->_cId_a
,
262 'contact_id_b' => $this->_cId_b
,
263 'relationship_type_id' => $this->_relTypeID
,
264 'start_date' => '2008-12-20',
267 'is_permission_a_b' => 1,
268 'description' => 'my desc',
269 'version' => $this->_apiversion
,
271 $relationship = civicrm_api('relationship', 'create', $params);
273 $updateparams = array(
274 'id' => $relationship['id'],
275 'version' => $this->_apiversion
,
276 'relationship_type_id' => $this->_relTypeID
,
278 $result = civicrm_api('relationship', 'create', $updateparams);
280 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
281 //make sure the orig params didn't get changed
282 $this->getAndCheck($params, $relationship['id'], 'relationship');
289 * check relationship creation
291 function testRelationshipCreate() {
293 'contact_id_a' => $this->_cId_a
,
294 'contact_id_b' => $this->_cId_b
,
295 'relationship_type_id' => $this->_relTypeID
,
296 'start_date' => '2010-10-30',
297 'end_date' => '2010-12-30',
300 'version' => $this->_apiversion
,
303 $result = civicrm_api('relationship', 'create', $params);
304 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
305 $this->assertEquals(0, $result['is_error'], 'in line ' . __LINE__
);
306 $this->assertNotNull($result['id'], 'in line ' . __LINE__
);
307 $relationParams = array(
308 'id' => $result['id'],
311 // assertDBState compares expected values in $result to actual values in the DB
312 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
313 $result = civicrm_api('relationship', 'get', array('version' => 3, 'id' => $result['id']));
314 $values = $result['values'][$result['id']];
315 foreach ($params as $key => $value) {
316 if ($key == 'version' ||
$key == 'note') {
319 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
321 $params['id'] = $result['id'];
322 civicrm_api('relationship', 'delete', $params);
326 * check relationship creation
328 function testRelationshipCreateEmptyEndDate() {
330 'contact_id_a' => $this->_cId_a
,
331 'contact_id_b' => $this->_cId_b
,
332 'relationship_type_id' => $this->_relTypeID
,
333 'start_date' => '2010-10-30',
337 'version' => $this->_apiversion
,
340 $result = civicrm_api('relationship', 'create', $params);
342 $this->assertEquals(0, $result['is_error'], 'in line ' . __LINE__
);
343 $this->assertNotNull($result['id'], 'in line ' . __LINE__
);
344 $relationParams = array(
345 'id' => $result['id'],
348 // assertDBState compares expected values in $result to actual values in the DB
349 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
350 $result = civicrm_api('relationship', 'get', array('version' => 3, 'id' => $result['id']));
351 $values = $result['values'][$result['id']];
352 foreach ($params as $key => $value) {
353 if ($key == 'version' ||
$key == 'note') {
356 if($key == 'end_date'){
357 $this->assertTrue(empty($values[$key]));
360 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
362 $params['id'] = $result['id'];
363 civicrm_api('relationship', 'delete', $params);
367 * check relationship creation with custom data
369 function testRelationshipCreateWithCustomData() {
370 $customGroup = $this->createCustomGroup();
371 $this->_ids
= $this->createCustomField();
372 //few custom Values for comparing
373 $custom_params = array(
374 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
375 "custom_{$this->_ids[1]}" => 'Y',
376 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
377 "custom_{$this->_ids[3]}" => 'http://example.com',
381 'contact_id_a' => $this->_cId_a
,
382 'contact_id_b' => $this->_cId_b
,
383 'relationship_type_id' => $this->_relTypeID
,
384 'start_date' => '2008-12-20',
386 'version' => $this->_apiversion
,
388 $params = array_merge($params, $custom_params);
389 $result = civicrm_api('relationship', 'create', $params);
391 $this->assertNotNull($result['id']);
392 $relationParams = array(
393 'id' => $result['id'],
395 // assertDBState compares expected values in $result to actual values in the DB
396 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
398 $params['id'] = $result['id'];
399 $result = civicrm_api('relationship', 'delete', $params);
400 $this->relationshipTypeDelete($this->_relTypeID
);
404 * check with complete array + custom field
405 * Note that the test is written on purpose without any
406 * variables specific to participant so it can be replicated into other entities
407 * and / or moved to the automated test suite
409 function testGetWithCustom() {
410 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
412 $params = $this->_params
;
413 $params['custom_' . $ids['custom_field_id']] = "custom string";
415 $result = civicrm_api($this->_entity
, 'create', $params);
416 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
418 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
419 $getParams = array('version' => 3, 'id' => $result['id']);
420 $check = civicrm_api($this->_entity
, 'get', $getParams);
421 $this->documentMe($getParams, $check, __FUNCTION__
, __FILE__
);
422 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
424 $this->customFieldDelete($ids['custom_field_id']);
425 $this->customGroupDelete($ids['custom_group_id']);
428 function createCustomGroup() {
430 'title' => 'Custom Group',
431 'extends' => array('Relationship'),
436 'version' => $this->_apiversion
,
438 $customGroup = civicrm_api('custom_group', 'create', $params);
439 $this->_customGroupId
= $customGroup['id'];
440 return $customGroup['id'];
443 function createCustomField() {
446 'custom_group_id' => $this->_customGroupId
,
447 'label' => 'Enter text about relationship',
448 'html_type' => 'Text',
449 'data_type' => 'String',
450 'default_value' => 'xyz',
453 'is_searchable' => 0,
455 'version' => $this->_apiversion
,
459 $result = civicrm_api('CustomField', 'create', $params);
462 $ids[] = $customField['result']['customFieldId'];
464 $optionValue[] = array(
470 $optionValue[] = array(
476 $optionValue[] = array(
484 'label' => 'Pick Color',
485 'html_type' => 'Select',
486 'data_type' => 'String',
489 'is_searchable' => 0,
491 'option_values' => $optionValue,
492 'custom_group_id' => $this->_customGroupId
,
493 'version' => $this->_apiversion
,
496 $customField = civicrm_api('custom_field', 'create', $params);
497 $ids[] = $customField['id'];
500 'custom_group_id' => $this->_customGroupId
,
501 'name' => 'test_date',
502 'label' => 'test_date',
503 'html_type' => 'Select Date',
504 'data_type' => 'Date',
505 'default_value' => '20090711',
508 'is_searchable' => 0,
510 'version' => $this->_apiversion
,
513 $customField = civicrm_api('custom_field', 'create', $params);
515 $ids[] = $customField['id'];
517 'custom_group_id' => $this->_customGroupId
,
518 'name' => 'test_link',
519 'label' => 'test_link',
520 'html_type' => 'Link',
521 'data_type' => 'Link',
522 'default_value' => 'http://civicrm.org',
525 'is_searchable' => 0,
527 'version' => $this->_apiversion
,
530 $customField = civicrm_api('custom_field', 'create', $params);
531 $ids[] = $customField['id'];
535 ///////////////// civicrm_relationship_delete methods
538 * check with empty array
540 function testRelationshipDeleteEmpty() {
541 $params = array('version' => $this->_apiversion
);
542 $result = civicrm_api('relationship', 'delete', $params);
543 $this->assertEquals($result['is_error'], 1);
544 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
548 * check with No array
550 function testRelationshipDeleteParamsNotArray() {
551 $params = 'relationship_type_id = 5';
552 $result = civicrm_api('relationship', 'delete', $params);
553 $this->assertEquals($result['is_error'], 1);
554 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
558 * check if required fields are not passed
560 function testRelationshipDeleteWithoutRequired() {
562 'start_date' => '2008-12-20',
563 'end_date' => '2009-12-20',
567 $result = civicrm_api('relationship', 'delete', $params);
568 $this->assertEquals($result['is_error'], 1);
569 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: version, id');
573 * check with incorrect required fields
575 function testRelationshipDeleteWithIncorrectData() {
577 'contact_id_a' => $this->_cId_a
,
578 'contact_id_b' => $this->_cId_b
,
579 'relationship_type_id' => 'Breaking Relationship',
580 'version' => $this->_apiversion
,
583 $result = civicrm_api('relationship', 'delete', $params);
584 $this->assertEquals($result['is_error'], 1, 'in line ' . __LINE__
);
585 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id', 'in line ' . __LINE__
);
587 $params['id'] = "Invalid";
588 $result = civicrm_api('relationship', 'delete', $params);
589 $this->assertEquals($result['is_error'], 1, 'in line ' . __LINE__
);
590 $this->assertEquals($result['error_message'], 'Invalid value for relationship ID', 'in line ' . __LINE__
);
594 * check relationship creation
596 function testRelationshipDelete() {
598 'contact_id_a' => $this->_cId_a
,
599 'contact_id_b' => $this->_cId_b
,
600 'relationship_type_id' => $this->_relTypeID
,
601 'start_date' => '2008-12-20',
603 'version' => $this->_apiversion
,
606 $result = civicrm_api('relationship', 'create', $params);
607 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
608 $this->assertNotNull($result['id']);
610 //Delete relationship
612 $params['id'] = $result['id'];
614 $result = civicrm_api('relationship', 'delete', $params);
615 $this->relationshipTypeDelete($this->_relTypeID
);
618 ///////////////// civicrm_relationship_update methods
621 * check with empty array
623 function testRelationshipUpdateEmpty() {
624 $params = array('version' => 3);
625 $result = civicrm_api('relationship', 'create', $params);
626 $this->assertEquals($result['is_error'], 1);
627 $this->assertEquals('Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id', $result['error_message'], 'In line ' . __LINE__
);
631 * check with No array
633 function testRelationshipUpdateParamsNotArray() {
634 $params = 'relationship_type_id = 5';
635 $result = civicrm_api('relationship', 'create', $params);
636 $this->assertEquals($result['is_error'], 1);
637 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
641 * check if required fields are not passed
645 * check relationship update
647 function testRelationshipCreateDuplicate() {
649 'contact_id_a' => $this->_cId_a
,
650 'contact_id_b' => $this->_cId_b
,
651 'relationship_type_id' => $this->_relTypeID
,
652 'start_date' => '20081214',
653 'end_date' => '20091214',
655 'version' => $this->_apiversion
,
658 $result = civicrm_api('relationship', 'create', $relParams);
660 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
661 $this->_relationID
= $result['id'];
664 'contact_id_a' => $this->_cId_a
,
665 'contact_id_b' => $this->_cId_b
,
666 'relationship_type_id' => $this->_relTypeID
,
667 'start_date' => '20081214',
668 'end_date' => '20091214', 'is_active' => 0,
669 'version' => $this->_apiversion
,
672 $result = civicrm_api('relationship', 'create', $params);
674 $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__
);
675 $this->assertEquals($result['error_message'], 'Relationship already exists', 'In line ' . __LINE__
);
677 //delete created relationship
679 'id' => $this->_relationID
,
680 'version' => $this->_apiversion
,
683 $result = civicrm_api('relationship', 'delete', $params);
684 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__
);
686 //delete created relationship type
687 $this->relationshipTypeDelete($this->_relTypeID
);
691 * check with valid params array.
693 function testRelationshipsGet() {
695 'contact_id_a' => $this->_cId_a
,
696 'contact_id_b' => $this->_cId_b
,
697 'relationship_type_id' => $this->_relTypeID
,
698 'start_date' => '2011-01-01',
699 'end_date' => '2013-01-01',
701 'version' => $this->_apiversion
,
704 $result = civicrm_api('relationship', 'create', $relParams);
708 'contact_id' => $this->_cId_b
,
709 'version' => $this->_apiversion
,
711 $result = civicrm_api('relationship', 'get', $params);
713 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
714 $this->assertEquals($result['count'], 1, 'in line ' . __LINE__
);
716 'contact_id_a' => $this->_cId_a
,
717 'version' => $this->_apiversion
,
719 $result = civicrm_api('relationship', 'get', $params);
720 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
721 $this->assertEquals($result['count'], 1, 'in line ' . __LINE__
);
722 // contact_id_a is wrong so should be no matches
724 'contact_id_a' => $this->_cId_b
,
725 'version' => $this->_apiversion
,
727 $result = civicrm_api('relationship', 'get', $params);
728 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
729 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__
);
733 * check with valid params array.
734 * (The get function will behave differently without 'contact_id' passed
736 function testRelationshipsGetGeneric() {
738 'contact_id_a' => $this->_cId_a
,
739 'contact_id_b' => $this->_cId_b
,
740 'relationship_type_id' => $this->_relTypeID
,
741 'start_date' => '2011-01-01',
742 'end_date' => '2013-01-01',
744 'version' => $this->_apiversion
,
747 $result = civicrm_api('relationship', 'create', $relParams);
751 'contact_id_b' => $this->_cId_b
,
752 'version' => $this->_apiversion
,
754 $result = civicrm_api('relationship', 'get', $params);
755 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
758 function testGetIsCurrent() {
760 'contact_id_a' => $this->_cId_a
,
761 'contact_id_b' => $this->_cId_b2
,
762 'relationship_type_id' => $this->_relTypeID
,
763 'start_date' => '2008-12-20',
765 'version' => $this->_apiversion
,
767 $rel2 = civicrm_api('relationship', 'create', $rel2Params);
768 $this->assertAPISuccess($rel2);
769 $rel1 = civicrm_api('relationship', 'create', $this->_params
);
770 $this->assertAPISuccess($rel1);
772 'version' => $this->_apiversion
,
773 'filters' => array('is_current' => 1)
775 $description = "demonstrates is_current filter";
776 $subfile = 'filterIsCurrent';
777 //no relationship has been created
778 $result = civicrm_api('relationship', 'get', $getParams);
779 $this->documentMe($getParams, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
780 $this->assertEquals($result['count'], 1);
781 $this->AssertEquals($rel1['id'], $result['id']);
783 // now try not started
784 $rel2Params['is_active'] =1;
785 $rel2Params['start_date'] ='tomorrow';
786 $rel2 = civicrm_api('relationship', 'create', $rel2Params);
787 $result = civicrm_api('relationship', 'get', $getParams);
788 $this->assertEquals($result['count'], 1);
789 $this->AssertEquals($rel1['id'], $result['id']);
792 $rel2Params['is_active'] =1;
793 $rel2Params['start_date'] ='last week';
794 $rel2Params['end_date'] ='yesterday';
795 $rel2 = civicrm_api('relationship', 'create', $rel2Params);
798 * Test using various operators
800 function testGetTypeOperators() {
801 $relTypeParams = array(
802 'name_a_b' => 'Relation 3 for delete',
803 'name_b_a' => 'Relation 6 for delete',
804 'description' => 'Testing relationship type 2',
805 'contact_type_a' => 'Individual',
806 'contact_type_b' => 'Organization',
809 'version' => $this->_apiversion
,
811 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
812 $relTypeParams = array(
813 'name_a_b' => 'Relation 8 for delete',
814 'name_b_a' => 'Relation 9 for delete',
815 'description' => 'Testing relationship type 7',
816 'contact_type_a' => 'Individual',
817 'contact_type_b' => 'Organization',
820 'version' => $this->_apiversion
,
822 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
824 $relTypeParams = array(
825 'name_a_b' => 'Relation 6 for delete',
826 'name_b_a' => 'Relation 88for delete',
827 'description' => 'Testing relationship type 00',
828 'contact_type_a' => 'Individual',
829 'contact_type_b' => 'Organization',
832 'version' => $this->_apiversion
,
834 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
836 $rel1 = civicrm_api('relationship', 'create', $this->_params
);
837 $this->assertAPISuccess($rel1);
838 $rel2 = civicrm_api('relationship', 'create', array_merge($this->_params
,
839 array('relationship_type_id' => $relationType2,)));
840 $this->assertAPISuccess($rel2);
841 $rel3 = civicrm_api('relationship', 'create', array_merge($this->_params
,
842 array('relationship_type_id' => $relationType3,)));
843 $this->assertAPISuccess($rel3);
844 $rel4 = civicrm_api('relationship', 'create', array_merge($this->_params
,
845 array('relationship_type_id' => $relationType4,)));
846 $this->assertAPISuccess($rel4);
849 'version' => $this->_apiversion
,
850 'relationship_type_id' => array('IN' => array($relationType2, $relationType3))
854 $description = "demonstrates use of IN filter";
855 $subfile = 'INRelationshipType';
857 $result = civicrm_api('relationship', 'get', $getParams);
858 $this->documentMe($getParams, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
859 $this->assertEquals($result['count'], 2);
860 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
862 $description = "demonstrates use of NOT IN filter";
863 $subfile = 'NotInRelationshipType';
865 'version' => $this->_apiversion
,
866 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3))
868 $result = civicrm_api('relationship', 'get', $getParams);
869 $this->documentMe($getParams, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
870 $this->assertEquals($result['count'], 2);
871 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
873 $description = "demonstrates use of BETWEEN filter";
874 $subfile = 'BetweenRelationshipType';
876 'version' => $this->_apiversion
,
877 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4))
879 $result = civicrm_api('relationship', 'get', $getParams);
880 $this->documentMe($getParams, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
881 $this->assertEquals($result['count'], 3);
882 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
884 $description = "demonstrates use of Not BETWEEN filter";
885 $subfile = 'NotBetweenRelationshipType';
887 'version' => $this->_apiversion
,
888 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4))
890 $result = civicrm_api('relationship', 'get', $getParams);
891 $this->documentMe($getParams, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
892 $this->assertEquals($result['count'], 1);
893 $this->AssertEquals(array($rel1['id'],), array_keys($result['values']));
897 * check with invalid relationshipType Id
899 function testRelationshipTypeAddInvalidId() {
900 $relTypeParams = array(
902 'name_a_b' => 'Relation 1 for delete',
903 'name_b_a' => 'Relation 2 for delete',
904 'contact_type_a' => 'Individual',
905 'contact_type_b' => 'Organization',
906 'version' => $this->_apiversion
,
908 $result = civicrm_api('relationship_type', 'create', $relTypeParams);
909 $this->assertEquals($result['is_error'], 1, 'in line ' . __LINE__
);
910 $this->assertEquals($result['error_message'], 'Invalid value for relationship type ID', 'in line ' . __LINE__
);
913 ///////////////// civicrm_get_relationships
916 * check with invalid data
918 function testGetRelationshipInvalidData() {
919 $contact_a = array('contact_id' => $this->_cId_a
);
920 $contact_b = array('contact_id' => $this->_cId_b
);
922 //no relationship has been created
923 $result = civicrm_api('relationship', 'get', $contact_a, $contact_b, NULL, 'asc');
924 $this->assertEquals($result['is_error'], 1);
928 * check with valid data with contact_b
930 function testGetRelationshipWithContactB() {
932 'contact_id_a' => $this->_cId_a
,
933 'contact_id_b' => $this->_cId_b
,
934 'relationship_type_id' => $this->_relTypeID
,
935 'start_date' => '2011-01-01',
936 'end_date' => '2013-01-01',
938 'version' => $this->_apiversion
,
941 $relationship = civicrm_api('relationship', 'create', $relParams);
944 'contact_id' => $this->_cId_a
,
945 'version' => $this->_apiversion
,
948 $result = civicrm_api('relationship', 'get', $contacts);
949 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__
);
950 $this->assertGreaterThan(0, $result['count'], 'in line ' . __LINE__
);
952 'id' => $relationship['id'],
953 'version' => $this->_apiversion
,
955 $result = civicrm_api('relationship', 'delete', $params);
956 $this->relationshipTypeDelete($this->_relTypeID
);
960 * check with valid data with relationshipTypes
962 function testGetRelationshipWithRelTypes() {
964 'contact_id_a' => $this->_cId_a
,
965 'contact_id_b' => $this->_cId_b
,
966 'relationship_type_id' => $this->_relTypeID
,
967 'start_date' => '2011-01-01',
968 'end_date' => '2013-01-01',
970 'version' => $this->_apiversion
,
973 $relationship = civicrm_api('relationship', 'create', $relParams);
976 'contact_id' => $this->_cId_a
,
977 'version' => $this->_apiversion
,
980 $result = civicrm_api('relationship', 'get', $contact_a);
982 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__
);
985 'id' => $relationship['id'],
986 'version' => $this->_apiversion
,
988 $result = civicrm_api('relationship', 'delete', $params);
989 $this->relationshipTypeDelete($this->_relTypeID
);