3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Class contains api test cases for "civicrm_relationship"
34 class api_v3_RelationshipTest
extends CiviUnitTestCase
{
35 protected $_apiversion;
38 protected $_cId_b2;// second org
39 protected $_relTypeID;
40 protected $_ids = array();
41 protected $_customGroupId = NULL;
42 protected $_customFieldId = NULL;
44 public $_eNoticeCompliant = FALSE;
48 'name' => 'Relationship Create',
49 'description' => 'Test all Relationship Create API methods.',
50 'group' => 'CiviCRM API Tests',
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',
70 'version' => $this->_apiversion
,
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',
79 'version' => $this->_apiversion
,
85 $this->quickCleanup(array('civicrm_relationship'));
86 $this->relationshipTypeDelete($this->_relTypeID
);
87 $this->contactDelete($this->_cId_a
);
88 $this->contactDelete($this->_cId_b
);
91 ///////////////// civicrm_relationship_create methods
94 * check with empty array
96 function testRelationshipCreateEmpty() {
97 $params = array('version' => $this->_apiversion
);
98 $result = civicrm_api('relationship', 'create', $params);
99 $this->assertEquals($result['is_error'], 1);
103 * check with No array
105 function testRelationshipCreateParamsNotArray() {
106 $params = 'relationship_type_id = 5';
107 $result = civicrm_api('relationship', 'create', $params);
108 $this->assertEquals($result['is_error'], 1);
112 * check if required fields are not passed
114 function testRelationshipCreateWithoutRequired() {
116 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
117 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
121 $result = civicrm_api('relationship', 'create', $params);
122 $this->assertEquals($result['is_error'], 1);
126 * check with incorrect required fields
128 function testRelationshipCreateWithIncorrectData() {
131 'contact_id_a' => $this->_cId_a
,
132 'contact_id_b' => $this->_cId_b
,
133 'relationship_type_id' => 'Breaking Relationship',
137 $result = civicrm_api('relationship', 'create', $params);
138 $this->assertEquals($result['is_error'], 1);
140 //contact id is not an integer
142 'contact_id_a' => 'invalid',
143 'contact_id_b' => $this->_cId_b
,
144 'relationship_type_id' => $this->_relTypeID
,
145 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
148 $result = civicrm_api('relationship', 'create', $params);
149 $this->assertEquals($result['is_error'], 1);
151 //contact id does not exists
152 $params['contact_id_a'] = 999;
153 $result = civicrm_api('relationship', 'create', $params);
154 $this->assertEquals($result['is_error'], 1);
157 $params['contact_id_a'] = $this->_cId_a
;
158 $params['start_date'] = array('d' => '1', 'M' => '1');
159 $result = civicrm_api('relationship', 'create', $params);
160 $this->assertEquals($result['is_error'], 1);
164 * check relationship creation with invalid Relationship
166 function testRelationshipCreatInvalidRelationship() {
167 // both the contact of type Individual
169 'contact_id_a' => $this->_cId_a
,
170 'contact_id_b' => $this->_cId_a
,
171 'relationship_type_id' => $this->_relTypeID
,
172 'start_date' => '2008-01-10',
177 $result = civicrm_api('relationship', 'create', $params);
178 $this->assertEquals($result['is_error'], 1);
180 // both the contact of type Organization
182 'contact_id_a' => $this->_cId_b
,
183 'contact_id_b' => $this->_cId_b
,
184 'relationship_type_id' => $this->_relTypeID
,
185 'start_date' => '2008-01-10',
190 $result = civicrm_api('relationship', 'create', $params);
191 $this->assertEquals($result['is_error'], 1);
195 * check relationship already exists
197 function testRelationshipCreateAlreadyExists() {
199 'contact_id_a' => $this->_cId_a
,
200 'contact_id_b' => $this->_cId_b
,
201 'relationship_type_id' => $this->_relTypeID
,
202 'start_date' => '2008-12-20', 'end_date' => NULL,
204 'version' => $this->_apiversion
,
206 $relationship = civicrm_api('relationship', 'create', $params);
209 'contact_id_a' => $this->_cId_a
,
210 'contact_id_b' => $this->_cId_b
,
211 'relationship_type_id' => $this->_relTypeID
,
212 'start_date' => '2008-12-20',
214 'version' => $this->_apiversion
,
216 $result = civicrm_api('relationship', 'create', $params);
218 $this->assertEquals($result['is_error'], 1);
219 $this->assertEquals($result['error_message'], 'Relationship already exists');
221 $params['id'] = $relationship['id'];
222 $result = civicrm_api('relationship', 'delete', $params);
226 * check relationship already exists
228 function testRelationshipCreateUpdateAlreadyExists() {
230 'contact_id_a' => $this->_cId_a
,
231 'contact_id_b' => $this->_cId_b
,
232 'relationship_type_id' => $this->_relTypeID
,
233 'start_date' => '2008-12-20',
236 'version' => $this->_apiversion
,
238 $relationship = civicrm_api('relationship', 'create', $params);
241 'id' => $relationship['id'],
243 'version' => $this->_apiversion
,
246 $result = civicrm_api('relationship', 'create', $params);
247 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
248 $result = civicrm_api('relationship', 'get', $params);
249 $this->assertEquals(0, $result['values'][$result['id']]['is_active'], 'in line ' . __LINE__
);
250 $params['id'] = $relationship['id'];
251 $result = civicrm_api('relationship', 'delete', $params);
255 * checkupdate doesn't reset stuff badly - CRM-11789
257 function testRelationshipCreateUpdateDoesntMangle() {
259 'contact_id_a' => $this->_cId_a
,
260 'contact_id_b' => $this->_cId_b
,
261 'relationship_type_id' => $this->_relTypeID
,
262 'start_date' => '2008-12-20',
265 'is_permission_a_b' => 1,
266 'description' => 'my desc',
267 'version' => $this->_apiversion
,
269 $relationship = civicrm_api('relationship', 'create', $params);
271 $updateparams = array(
272 'id' => $relationship['id'],
273 'version' => $this->_apiversion
,
274 'relationship_type_id' => $this->_relTypeID
,
276 $result = civicrm_api('relationship', 'create', $updateparams);
278 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
279 //make sure the orig params didn't get changed
280 $this->getAndCheck($params, $relationship['id'], 'relationship');
287 * check relationship creation
289 function testRelationshipCreate() {
291 'contact_id_a' => $this->_cId_a
,
292 'contact_id_b' => $this->_cId_b
,
293 'relationship_type_id' => $this->_relTypeID
,
294 'start_date' => '2010-10-30',
295 'end_date' => '2010-12-30',
298 'version' => $this->_apiversion
,
301 $result = civicrm_api('relationship', 'create', $params);
302 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
303 $this->assertEquals(0, $result['is_error'], 'in line ' . __LINE__
);
304 $this->assertNotNull($result['id'], 'in line ' . __LINE__
);
305 $relationParams = array(
306 'id' => $result['id'],
309 // assertDBState compares expected values in $result to actual values in the DB
310 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
311 $result = civicrm_api('relationship', 'get', array('version' => 3, 'id' => $result['id']));
312 $values = $result['values'][$result['id']];
313 foreach ($params as $key => $value) {
314 if ($key == 'version' ||
$key == 'note') {
317 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
319 $params['id'] = $result['id'];
320 civicrm_api('relationship', 'delete', $params);
324 * check relationship creation
326 function testRelationshipCreateEmptyEndDate() {
328 'contact_id_a' => $this->_cId_a
,
329 'contact_id_b' => $this->_cId_b
,
330 'relationship_type_id' => $this->_relTypeID
,
331 'start_date' => '2010-10-30',
335 'version' => $this->_apiversion
,
338 $result = civicrm_api('relationship', 'create', $params);
340 $this->assertEquals(0, $result['is_error'], 'in line ' . __LINE__
);
341 $this->assertNotNull($result['id'], 'in line ' . __LINE__
);
342 $relationParams = array(
343 'id' => $result['id'],
346 // assertDBState compares expected values in $result to actual values in the DB
347 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
348 $result = civicrm_api('relationship', 'get', array('version' => 3, 'id' => $result['id']));
349 $values = $result['values'][$result['id']];
350 foreach ($params as $key => $value) {
351 if ($key == 'version' ||
$key == 'note') {
354 if($key == 'end_date'){
355 $this->assertTrue(empty($values[$key]));
358 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
360 $params['id'] = $result['id'];
361 civicrm_api('relationship', 'delete', $params);
365 * check relationship creation with custom data
367 function testRelationshipCreateWithCustomData() {
368 $customGroup = $this->createCustomGroup();
369 $this->_ids
= $this->createCustomField();
370 //few custom Values for comparing
371 $custom_params = array(
372 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
373 "custom_{$this->_ids[1]}" => 'Y',
374 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
375 "custom_{$this->_ids[3]}" => 'http://example.com',
379 'contact_id_a' => $this->_cId_a
,
380 'contact_id_b' => $this->_cId_b
,
381 'relationship_type_id' => $this->_relTypeID
,
382 'start_date' => '2008-12-20',
384 'version' => $this->_apiversion
,
386 $params = array_merge($params, $custom_params);
387 $result = civicrm_api('relationship', 'create', $params);
389 $this->assertNotNull($result['id']);
390 $relationParams = array(
391 'id' => $result['id'],
393 // assertDBState compares expected values in $result to actual values in the DB
394 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
396 $params['id'] = $result['id'];
397 $result = civicrm_api('relationship', 'delete', $params);
398 $this->relationshipTypeDelete($this->_relTypeID
);
402 * check with complete array + custom field
403 * Note that the test is written on purpose without any
404 * variables specific to participant so it can be replicated into other entities
405 * and / or moved to the automated test suite
407 function testGetWithCustom() {
408 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
410 $params = $this->_params
;
411 $params['custom_' . $ids['custom_field_id']] = "custom string";
413 $result = civicrm_api($this->_entity
, 'create', $params);
414 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
416 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
417 $getParams = array('version' => 3, 'id' => $result['id']);
418 $check = civicrm_api($this->_entity
, 'get', $getParams);
419 $this->documentMe($getParams, $check, __FUNCTION__
, __FILE__
);
420 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
422 $this->customFieldDelete($ids['custom_field_id']);
423 $this->customGroupDelete($ids['custom_group_id']);
426 function createCustomGroup() {
428 'title' => 'Custom Group',
429 'extends' => array('Relationship'),
434 'version' => $this->_apiversion
,
436 $customGroup = civicrm_api('custom_group', 'create', $params);
437 $this->_customGroupId
= $customGroup['id'];
438 return $customGroup['id'];
441 function createCustomField() {
444 'custom_group_id' => $this->_customGroupId
,
445 'label' => 'Enter text about relationship',
446 'html_type' => 'Text',
447 'data_type' => 'String',
448 'default_value' => 'xyz',
451 'is_searchable' => 0,
453 'version' => $this->_apiversion
,
457 $result = civicrm_api('CustomField', 'create', $params);
460 $ids[] = $customField['result']['customFieldId'];
462 $optionValue[] = array(
468 $optionValue[] = array(
474 $optionValue[] = array(
482 'label' => 'Pick Color',
483 'html_type' => 'Select',
484 'data_type' => 'String',
487 'is_searchable' => 0,
489 'option_values' => $optionValue,
490 'custom_group_id' => $this->_customGroupId
,
491 'version' => $this->_apiversion
,
494 $customField = civicrm_api('custom_field', 'create', $params);
495 $ids[] = $customField['id'];
498 'custom_group_id' => $this->_customGroupId
,
499 'name' => 'test_date',
500 'label' => 'test_date',
501 'html_type' => 'Select Date',
502 'data_type' => 'Date',
503 'default_value' => '20090711',
506 'is_searchable' => 0,
508 'version' => $this->_apiversion
,
511 $customField = civicrm_api('custom_field', 'create', $params);
513 $ids[] = $customField['id'];
515 'custom_group_id' => $this->_customGroupId
,
516 'name' => 'test_link',
517 'label' => 'test_link',
518 'html_type' => 'Link',
519 'data_type' => 'Link',
520 'default_value' => 'http://civicrm.org',
523 'is_searchable' => 0,
525 'version' => $this->_apiversion
,
528 $customField = civicrm_api('custom_field', 'create', $params);
529 $ids[] = $customField['id'];
533 ///////////////// civicrm_relationship_delete methods
536 * check with empty array
538 function testRelationshipDeleteEmpty() {
539 $params = array('version' => $this->_apiversion
);
540 $result = civicrm_api('relationship', 'delete', $params);
541 $this->assertEquals($result['is_error'], 1);
542 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
546 * check with No array
548 function testRelationshipDeleteParamsNotArray() {
549 $params = 'relationship_type_id = 5';
550 $result = civicrm_api('relationship', 'delete', $params);
551 $this->assertEquals($result['is_error'], 1);
552 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
556 * check if required fields are not passed
558 function testRelationshipDeleteWithoutRequired() {
560 'start_date' => '2008-12-20',
561 'end_date' => '2009-12-20',
565 $result = civicrm_api('relationship', 'delete', $params);
566 $this->assertEquals($result['is_error'], 1);
567 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: version, id');
571 * check with incorrect required fields
573 function testRelationshipDeleteWithIncorrectData() {
575 'contact_id_a' => $this->_cId_a
,
576 'contact_id_b' => $this->_cId_b
,
577 'relationship_type_id' => 'Breaking Relationship',
578 'version' => $this->_apiversion
,
581 $result = civicrm_api('relationship', 'delete', $params);
582 $this->assertEquals($result['is_error'], 1, 'in line ' . __LINE__
);
583 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id', 'in line ' . __LINE__
);
585 $params['id'] = "Invalid";
586 $result = civicrm_api('relationship', 'delete', $params);
587 $this->assertEquals($result['is_error'], 1, 'in line ' . __LINE__
);
588 $this->assertEquals($result['error_message'], 'Invalid value for relationship ID', 'in line ' . __LINE__
);
592 * check relationship creation
594 function testRelationshipDelete() {
596 'contact_id_a' => $this->_cId_a
,
597 'contact_id_b' => $this->_cId_b
,
598 'relationship_type_id' => $this->_relTypeID
,
599 'start_date' => '2008-12-20',
601 'version' => $this->_apiversion
,
604 $result = civicrm_api('relationship', 'create', $params);
605 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
606 $this->assertNotNull($result['id']);
608 //Delete relationship
610 $params['id'] = $result['id'];
612 $result = civicrm_api('relationship', 'delete', $params);
613 $this->relationshipTypeDelete($this->_relTypeID
);
616 ///////////////// civicrm_relationship_update methods
619 * check with empty array
621 function testRelationshipUpdateEmpty() {
622 $params = array('version' => 3);
623 $result = civicrm_api('relationship', 'create', $params);
624 $this->assertEquals($result['is_error'], 1);
625 $this->assertEquals('Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id', $result['error_message'], 'In line ' . __LINE__
);
629 * check with No array
631 function testRelationshipUpdateParamsNotArray() {
632 $params = 'relationship_type_id = 5';
633 $result = civicrm_api('relationship', 'create', $params);
634 $this->assertEquals($result['is_error'], 1);
635 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
639 * check if required fields are not passed
643 * check relationship update
645 function testRelationshipCreateDuplicate() {
647 'contact_id_a' => $this->_cId_a
,
648 'contact_id_b' => $this->_cId_b
,
649 'relationship_type_id' => $this->_relTypeID
,
650 'start_date' => '20081214',
651 'end_date' => '20091214',
653 'version' => $this->_apiversion
,
656 $result = civicrm_api('relationship', 'create', $relParams);
658 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
659 $this->_relationID
= $result['id'];
662 'contact_id_a' => $this->_cId_a
,
663 'contact_id_b' => $this->_cId_b
,
664 'relationship_type_id' => $this->_relTypeID
,
665 'start_date' => '20081214',
666 'end_date' => '20091214', 'is_active' => 0,
667 'version' => $this->_apiversion
,
670 $result = civicrm_api('relationship', 'create', $params);
672 $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__
);
673 $this->assertEquals($result['error_message'], 'Relationship already exists', 'In line ' . __LINE__
);
675 //delete created relationship
677 'id' => $this->_relationID
,
678 'version' => $this->_apiversion
,
681 $result = civicrm_api('relationship', 'delete', $params);
682 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__
);
684 //delete created relationship type
685 $this->relationshipTypeDelete($this->_relTypeID
);
689 * check with valid params array.
691 function testRelationshipsGet() {
693 'contact_id_a' => $this->_cId_a
,
694 'contact_id_b' => $this->_cId_b
,
695 'relationship_type_id' => $this->_relTypeID
,
696 'start_date' => '2011-01-01',
697 'end_date' => '2013-01-01',
699 'version' => $this->_apiversion
,
702 $result = civicrm_api('relationship', 'create', $relParams);
706 'contact_id' => $this->_cId_b
,
707 'version' => $this->_apiversion
,
709 $result = civicrm_api('relationship', 'get', $params);
711 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
712 $this->assertEquals($result['count'], 1, 'in line ' . __LINE__
);
714 'contact_id_a' => $this->_cId_a
,
715 'version' => $this->_apiversion
,
717 $result = civicrm_api('relationship', 'get', $params);
718 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
719 $this->assertEquals($result['count'], 1, 'in line ' . __LINE__
);
720 // contact_id_a is wrong so should be no matches
722 'contact_id_a' => $this->_cId_b
,
723 'version' => $this->_apiversion
,
725 $result = civicrm_api('relationship', 'get', $params);
726 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
727 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__
);
731 * check with valid params array.
732 * (The get function will behave differently without 'contact_id' passed
734 function testRelationshipsGetGeneric() {
736 'contact_id_a' => $this->_cId_a
,
737 'contact_id_b' => $this->_cId_b
,
738 'relationship_type_id' => $this->_relTypeID
,
739 'start_date' => '2011-01-01',
740 'end_date' => '2013-01-01',
742 'version' => $this->_apiversion
,
745 $result = civicrm_api('relationship', 'create', $relParams);
749 'contact_id_b' => $this->_cId_b
,
750 'version' => $this->_apiversion
,
752 $result = civicrm_api('relationship', 'get', $params);
753 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
756 function testGetIsCurrent() {
758 'contact_id_a' => $this->_cId_a
,
759 'contact_id_b' => $this->_cId_b2
,
760 'relationship_type_id' => $this->_relTypeID
,
761 'start_date' => '2008-12-20',
763 'version' => $this->_apiversion
,
765 $rel2 = civicrm_api('relationship', 'create', $rel2Params);
766 $this->assertAPISuccess($rel2);
767 $rel1 = civicrm_api('relationship', 'create', $this->_params
);
768 $this->assertAPISuccess($rel1);
770 'version' => $this->_apiversion
,
771 'filters' => array('is_current' => 1)
773 $description = "demonstrates is_current filter";
774 $subfile = 'filterIsCurrent';
775 //no relationship has been created
776 $result = civicrm_api('relationship', 'get', $getParams);
777 $this->documentMe($getParams, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
778 $this->assertEquals($result['count'], 1);
779 $this->AssertEquals($rel1['id'], $result['id']);
781 // now try not started
782 $rel2Params['is_active'] =1;
783 $rel2Params['start_date'] ='tomorrow';
784 $rel2 = civicrm_api('relationship', 'create', $rel2Params);
785 $result = civicrm_api('relationship', 'get', $getParams);
786 $this->assertEquals($result['count'], 1);
787 $this->AssertEquals($rel1['id'], $result['id']);
790 $rel2Params['is_active'] =1;
791 $rel2Params['start_date'] ='last week';
792 $rel2Params['end_date'] ='yesterday';
793 $rel2 = civicrm_api('relationship', 'create', $rel2Params);
796 * Test using various operators
798 function testGetTypeOperators() {
799 $relTypeParams = array(
800 'name_a_b' => 'Relation 3 for delete',
801 'name_b_a' => 'Relation 6 for delete',
802 'description' => 'Testing relationship type 2',
803 'contact_type_a' => 'Individual',
804 'contact_type_b' => 'Organization',
807 'version' => $this->_apiversion
,
809 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
810 $relTypeParams = array(
811 'name_a_b' => 'Relation 8 for delete',
812 'name_b_a' => 'Relation 9 for delete',
813 'description' => 'Testing relationship type 7',
814 'contact_type_a' => 'Individual',
815 'contact_type_b' => 'Organization',
818 'version' => $this->_apiversion
,
820 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
822 $relTypeParams = array(
823 'name_a_b' => 'Relation 6 for delete',
824 'name_b_a' => 'Relation 88for delete',
825 'description' => 'Testing relationship type 00',
826 'contact_type_a' => 'Individual',
827 'contact_type_b' => 'Organization',
830 'version' => $this->_apiversion
,
832 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
834 $rel1 = civicrm_api('relationship', 'create', $this->_params
);
835 $this->assertAPISuccess($rel1);
836 $rel2 = civicrm_api('relationship', 'create', array_merge($this->_params
,
837 array('relationship_type_id' => $relationType2,)));
838 $this->assertAPISuccess($rel2);
839 $rel3 = civicrm_api('relationship', 'create', array_merge($this->_params
,
840 array('relationship_type_id' => $relationType3,)));
841 $this->assertAPISuccess($rel3);
842 $rel4 = civicrm_api('relationship', 'create', array_merge($this->_params
,
843 array('relationship_type_id' => $relationType4,)));
844 $this->assertAPISuccess($rel4);
847 'version' => $this->_apiversion
,
848 'relationship_type_id' => array('IN' => array($relationType2, $relationType3))
852 $description = "demonstrates use of IN filter";
853 $subfile = 'INRelationshipType';
855 $result = civicrm_api('relationship', 'get', $getParams);
856 $this->documentMe($getParams, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
857 $this->assertEquals($result['count'], 2);
858 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
860 $description = "demonstrates use of NOT IN filter";
861 $subfile = 'NotInRelationshipType';
863 'version' => $this->_apiversion
,
864 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3))
866 $result = civicrm_api('relationship', 'get', $getParams);
867 $this->documentMe($getParams, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
868 $this->assertEquals($result['count'], 2);
869 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
871 $description = "demonstrates use of BETWEEN filter";
872 $subfile = 'BetweenRelationshipType';
874 'version' => $this->_apiversion
,
875 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4))
877 $result = civicrm_api('relationship', 'get', $getParams);
878 $this->documentMe($getParams, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
879 $this->assertEquals($result['count'], 3);
880 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
882 $description = "demonstrates use of Not BETWEEN filter";
883 $subfile = 'NotBetweenRelationshipType';
885 'version' => $this->_apiversion
,
886 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4))
888 $result = civicrm_api('relationship', 'get', $getParams);
889 $this->documentMe($getParams, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
890 $this->assertEquals($result['count'], 1);
891 $this->AssertEquals(array($rel1['id'],), array_keys($result['values']));
895 * check with invalid relationshipType Id
897 function testRelationshipTypeAddInvalidId() {
898 $relTypeParams = array(
900 'name_a_b' => 'Relation 1 for delete',
901 'name_b_a' => 'Relation 2 for delete',
902 'contact_type_a' => 'Individual',
903 'contact_type_b' => 'Organization',
904 'version' => $this->_apiversion
,
906 $result = civicrm_api('relationship_type', 'create', $relTypeParams);
907 $this->assertEquals($result['is_error'], 1, 'in line ' . __LINE__
);
908 $this->assertEquals($result['error_message'], 'Invalid value for relationship type ID', 'in line ' . __LINE__
);
911 ///////////////// civicrm_get_relationships
914 * check with invalid data
916 function testGetRelationshipInvalidData() {
917 $contact_a = array('contact_id' => $this->_cId_a
);
918 $contact_b = array('contact_id' => $this->_cId_b
);
920 //no relationship has been created
921 $result = civicrm_api('relationship', 'get', $contact_a, $contact_b, NULL, 'asc');
922 $this->assertEquals($result['is_error'], 1);
926 * check with valid data with contact_b
928 function testGetRelationshipWithContactB() {
930 'contact_id_a' => $this->_cId_a
,
931 'contact_id_b' => $this->_cId_b
,
932 'relationship_type_id' => $this->_relTypeID
,
933 'start_date' => '2011-01-01',
934 'end_date' => '2013-01-01',
936 'version' => $this->_apiversion
,
939 $relationship = civicrm_api('relationship', 'create', $relParams);
942 'contact_id' => $this->_cId_a
,
943 'version' => $this->_apiversion
,
946 $result = civicrm_api('relationship', 'get', $contacts);
947 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__
);
948 $this->assertGreaterThan(0, $result['count'], 'in line ' . __LINE__
);
950 'id' => $relationship['id'],
951 'version' => $this->_apiversion
,
953 $result = civicrm_api('relationship', 'delete', $params);
954 $this->relationshipTypeDelete($this->_relTypeID
);
958 * check with valid data with relationshipTypes
960 function testGetRelationshipWithRelTypes() {
962 'contact_id_a' => $this->_cId_a
,
963 'contact_id_b' => $this->_cId_b
,
964 'relationship_type_id' => $this->_relTypeID
,
965 'start_date' => '2011-01-01',
966 'end_date' => '2013-01-01',
968 'version' => $this->_apiversion
,
971 $relationship = civicrm_api('relationship', 'create', $relParams);
974 'contact_id' => $this->_cId_a
,
975 'version' => $this->_apiversion
,
978 $result = civicrm_api('relationship', 'get', $contact_a);
980 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__
);
983 'id' => $relationship['id'],
984 'version' => $this->_apiversion
,
986 $result = civicrm_api('relationship', 'delete', $params);
987 $this->relationshipTypeDelete($this->_relTypeID
);