Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-04-16-10-35-08
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTest.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
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. |
15 | |
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. |
20 | |
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 +--------------------------------------------------------------------+
28 */
29
30 require_once 'CiviTest/CiviUnitTestCase.php';
31
32 /**
33 * Class contains api test cases for "civicrm_relationship"
34 *
35 */
36 class api_v3_RelationshipTest extends CiviUnitTestCase {
37 protected $_apiversion;
38 protected $_cId_a;
39 protected $_cId_b;
40 protected $_cId_b2;// second org
41 protected $_relTypeID;
42 protected $_ids = array();
43 protected $_customGroupId = NULL;
44 protected $_customFieldId = NULL;
45 protected $_params;
46 public $_eNoticeCompliant = FALSE;
47 protected $_entity;
48 function get_info() {
49 return array(
50 'name' => 'Relationship Create',
51 'description' => 'Test all Relationship Create API methods.',
52 'group' => 'CiviCRM API Tests',
53 );
54 }
55
56 function setUp() {
57 parent::setUp();
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',
70 'is_reserved' => 1,
71 'is_active' => 1,
72 'version' => $this->_apiversion,
73 );
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',
80 'is_active' => 1,
81 'version' => $this->_apiversion,
82 );
83
84 }
85
86 function tearDown() {
87 $this->quickCleanup(array('civicrm_relationship'));
88 $this->relationshipTypeDelete($this->_relTypeID);
89 $this->contactDelete($this->_cId_a);
90 $this->contactDelete($this->_cId_b);
91 }
92
93 ///////////////// civicrm_relationship_create methods
94
95 /**
96 * check with empty array
97 */
98 function testRelationshipCreateEmpty() {
99 $params = array('version' => $this->_apiversion);
100 $result = civicrm_api('relationship', 'create', $params);
101 $this->assertEquals($result['is_error'], 1);
102 }
103
104 /**
105 * check with No array
106 */
107 function testRelationshipCreateParamsNotArray() {
108 $params = 'relationship_type_id = 5';
109 $result = civicrm_api('relationship', 'create', $params);
110 $this->assertEquals($result['is_error'], 1);
111 }
112
113 /**
114 * check if required fields are not passed
115 */
116 function testRelationshipCreateWithoutRequired() {
117 $params = array(
118 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
119 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
120 'is_active' => 1,
121 );
122
123 $result = civicrm_api('relationship', 'create', $params);
124 $this->assertEquals($result['is_error'], 1);
125 }
126
127 /**
128 * check with incorrect required fields
129 */
130 function testRelationshipCreateWithIncorrectData() {
131
132 $params = array(
133 'contact_id_a' => $this->_cId_a,
134 'contact_id_b' => $this->_cId_b,
135 'relationship_type_id' => 'Breaking Relationship',
136 'version' => 3,
137 );
138
139 $result = civicrm_api('relationship', 'create', $params);
140 $this->assertEquals($result['is_error'], 1);
141
142 //contact id is not an integer
143 $params = array(
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'),
148 'is_active' => 1,
149 );
150 $result = civicrm_api('relationship', 'create', $params);
151 $this->assertEquals($result['is_error'], 1);
152
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);
157
158 //invalid date
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);
163 }
164
165 /**
166 * check relationship creation with invalid Relationship
167 */
168 function testRelationshipCreatInvalidRelationship() {
169 // both the contact of type Individual
170 $params = array(
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',
175 'is_active' => 1,
176 'version' => 3,
177 );
178
179 $result = civicrm_api('relationship', 'create', $params);
180 $this->assertEquals($result['is_error'], 1);
181
182 // both the contact of type Organization
183 $params = array(
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',
188 'is_active' => 1,
189 'version' => 3,
190 );
191
192 $result = civicrm_api('relationship', 'create', $params);
193 $this->assertEquals($result['is_error'], 1);
194 }
195
196 /**
197 * check relationship already exists
198 */
199 function testRelationshipCreateAlreadyExists() {
200 $params = array(
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,
205 'is_active' => 1,
206 'version' => $this->_apiversion,
207 );
208 $relationship = civicrm_api('relationship', 'create', $params);
209
210 $params = array(
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',
215 'is_active' => 1,
216 'version' => $this->_apiversion,
217 );
218 $result = civicrm_api('relationship', 'create', $params);
219
220 $this->assertEquals($result['is_error'], 1);
221 $this->assertEquals($result['error_message'], 'Relationship already exists');
222
223 $params['id'] = $relationship['id'];
224 $result = civicrm_api('relationship', 'delete', $params);
225 }
226
227 /**
228 * check relationship already exists
229 */
230 function testRelationshipCreateUpdateAlreadyExists() {
231 $params = array(
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',
236 'end_date' => NULL,
237 'is_active' => 1,
238 'version' => $this->_apiversion,
239 );
240 $relationship = civicrm_api('relationship', 'create', $params);
241
242 $params = array(
243 'id' => $relationship['id'],
244 'is_active' => 0,
245 'version' => $this->_apiversion,
246 'debug' => 1,
247 );
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);
254 }
255
256 /**
257 * checkupdate doesn't reset stuff badly - CRM-11789
258 */
259 function testRelationshipCreateUpdateDoesntMangle() {
260 $params = array(
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',
265 'end_date' => NULL,
266 'is_active' => 1,
267 'is_permission_a_b' => 1,
268 'description' => 'my desc',
269 'version' => $this->_apiversion,
270 );
271 $relationship = civicrm_api('relationship', 'create', $params);
272
273 $updateparams = array(
274 'id' => $relationship['id'],
275 'version' => $this->_apiversion,
276 'relationship_type_id' => $this->_relTypeID,
277 );
278 $result = civicrm_api('relationship', 'create', $updateparams);
279
280 $this->assertAPISuccess($result, 'in line ' . __LINE__);
281 //make sure the orig params didn't get changed
282 $this->getAndCheck($params, $relationship['id'], 'relationship');
283
284 }
285
286
287
288 /**
289 * check relationship creation
290 */
291 function testRelationshipCreate() {
292 $params = array(
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',
298 'is_active' => 1,
299 'note' => 'note',
300 'version' => $this->_apiversion,
301 );
302
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'],
309 );
310
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') {
317 continue;
318 }
319 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
320 }
321 $params['id'] = $result['id'];
322 civicrm_api('relationship', 'delete', $params);
323 }
324
325 /**
326 * check relationship creation
327 */
328 function testRelationshipCreateEmptyEndDate() {
329 $params = array(
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',
334 'end_date' => '',
335 'is_active' => 1,
336 'note' => 'note',
337 'version' => $this->_apiversion,
338 );
339
340 $result = civicrm_api('relationship', 'create', $params);
341
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'],
346 );
347
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') {
354 continue;
355 }
356 if($key == 'end_date'){
357 $this->assertTrue(empty($values[$key]));
358 continue;
359 }
360 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
361 }
362 $params['id'] = $result['id'];
363 civicrm_api('relationship', 'delete', $params);
364 }
365
366 /**
367 * check relationship creation with custom data
368 */
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',
378 );
379
380 $params = array(
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',
385 'is_active' => 1,
386 'version' => $this->_apiversion,
387 );
388 $params = array_merge($params, $custom_params);
389 $result = civicrm_api('relationship', 'create', $params);
390
391 $this->assertNotNull($result['id']);
392 $relationParams = array(
393 'id' => $result['id'],
394 );
395 // assertDBState compares expected values in $result to actual values in the DB
396 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
397
398 $params['id'] = $result['id'];
399 $result = civicrm_api('relationship', 'delete', $params);
400 $this->relationshipTypeDelete($this->_relTypeID);
401 }
402
403 /**
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
408 */
409 function testGetWithCustom() {
410 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
411
412 $params = $this->_params;
413 $params['custom_' . $ids['custom_field_id']] = "custom string";
414
415 $result = civicrm_api($this->_entity, 'create', $params);
416 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
417
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__);
423
424 $this->customFieldDelete($ids['custom_field_id']);
425 $this->customGroupDelete($ids['custom_group_id']);
426 }
427
428 function createCustomGroup() {
429 $params = array(
430 'title' => 'Custom Group',
431 'extends' => array('Relationship'),
432 'weight' => 5,
433 'style' => 'Inline',
434 'is_active' => 1,
435 'max_multiple' => 0,
436 'version' => $this->_apiversion,
437 );
438 $customGroup = civicrm_api('custom_group', 'create', $params);
439 $this->_customGroupId = $customGroup['id'];
440 return $customGroup['id'];
441 }
442
443 function createCustomField() {
444 $ids = array();
445 $params = array(
446 'custom_group_id' => $this->_customGroupId,
447 'label' => 'Enter text about relationship',
448 'html_type' => 'Text',
449 'data_type' => 'String',
450 'default_value' => 'xyz',
451 'weight' => 1,
452 'is_required' => 1,
453 'is_searchable' => 0,
454 'is_active' => 1,
455 'version' => $this->_apiversion,
456 );
457
458
459 $result = civicrm_api('CustomField', 'create', $params);
460
461 $customField = NULL;
462 $ids[] = $customField['result']['customFieldId'];
463
464 $optionValue[] = array(
465 'label' => 'Red',
466 'value' => 'R',
467 'weight' => 1,
468 'is_active' => 1,
469 );
470 $optionValue[] = array(
471 'label' => 'Yellow',
472 'value' => 'Y',
473 'weight' => 2,
474 'is_active' => 1,
475 );
476 $optionValue[] = array(
477 'label' => 'Green',
478 'value' => 'G',
479 'weight' => 3,
480 'is_active' => 1,
481 );
482
483 $params = array(
484 'label' => 'Pick Color',
485 'html_type' => 'Select',
486 'data_type' => 'String',
487 'weight' => 2,
488 'is_required' => 1,
489 'is_searchable' => 0,
490 'is_active' => 1,
491 'option_values' => $optionValue,
492 'custom_group_id' => $this->_customGroupId,
493 'version' => $this->_apiversion,
494 );
495
496 $customField = civicrm_api('custom_field', 'create', $params);
497 $ids[] = $customField['id'];
498
499 $params = array(
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',
506 'weight' => 3,
507 'is_required' => 1,
508 'is_searchable' => 0,
509 'is_active' => 1,
510 'version' => $this->_apiversion,
511 );
512
513 $customField = civicrm_api('custom_field', 'create', $params);
514
515 $ids[] = $customField['id'];
516 $params = array(
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',
523 'weight' => 4,
524 'is_required' => 1,
525 'is_searchable' => 0,
526 'is_active' => 1,
527 'version' => $this->_apiversion,
528 );
529
530 $customField = civicrm_api('custom_field', 'create', $params);
531 $ids[] = $customField['id'];
532 return $ids;
533 }
534
535 ///////////////// civicrm_relationship_delete methods
536
537 /**
538 * check with empty array
539 */
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');
545 }
546
547 /**
548 * check with No array
549 */
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');
555 }
556
557 /**
558 * check if required fields are not passed
559 */
560 function testRelationshipDeleteWithoutRequired() {
561 $params = array(
562 'start_date' => '2008-12-20',
563 'end_date' => '2009-12-20',
564 'is_active' => 1,
565 );
566
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');
570 }
571
572 /**
573 * check with incorrect required fields
574 */
575 function testRelationshipDeleteWithIncorrectData() {
576 $params = array(
577 'contact_id_a' => $this->_cId_a,
578 'contact_id_b' => $this->_cId_b,
579 'relationship_type_id' => 'Breaking Relationship',
580 'version' => $this->_apiversion,
581 );
582
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__);
586
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__);
591 }
592
593 /**
594 * check relationship creation
595 */
596 function testRelationshipDelete() {
597 $params = array(
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',
602 'is_active' => 1,
603 'version' => $this->_apiversion,
604 );
605
606 $result = civicrm_api('relationship', 'create', $params);
607 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
608 $this->assertNotNull($result['id']);
609
610 //Delete relationship
611 $params = array();
612 $params['id'] = $result['id'];
613
614 $result = civicrm_api('relationship', 'delete', $params);
615 $this->relationshipTypeDelete($this->_relTypeID);
616 }
617
618 ///////////////// civicrm_relationship_update methods
619
620 /**
621 * check with empty array
622 */
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__);
628 }
629
630 /**
631 * check with No array
632 */
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__);
638 }
639
640 /**
641 * check if required fields are not passed
642 */
643
644 /**
645 * check relationship update
646 */
647 function testRelationshipCreateDuplicate() {
648 $relParams = array(
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',
654 'is_active' => 1,
655 'version' => $this->_apiversion,
656 );
657
658 $result = civicrm_api('relationship', 'create', $relParams);
659
660 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
661 $this->_relationID = $result['id'];
662
663 $params = array(
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,
670 );
671
672 $result = civicrm_api('relationship', 'create', $params);
673
674 $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__);
675 $this->assertEquals($result['error_message'], 'Relationship already exists', 'In line ' . __LINE__);
676
677 //delete created relationship
678 $params = array(
679 'id' => $this->_relationID,
680 'version' => $this->_apiversion,
681 );
682
683 $result = civicrm_api('relationship', 'delete', $params);
684 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__);
685
686 //delete created relationship type
687 $this->relationshipTypeDelete($this->_relTypeID);
688 }
689
690 /**
691 * check with valid params array.
692 */
693 function testRelationshipsGet() {
694 $relParams = array(
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',
700 'is_active' => 1,
701 'version' => $this->_apiversion,
702 );
703
704 $result = civicrm_api('relationship', 'create', $relParams);
705
706 //get relationship
707 $params = array(
708 'contact_id' => $this->_cId_b,
709 'version' => $this->_apiversion,
710 );
711 $result = civicrm_api('relationship', 'get', $params);
712
713 $this->assertAPISuccess($result, 'in line ' . __LINE__);
714 $this->assertEquals($result['count'], 1, 'in line ' . __LINE__);
715 $params = array(
716 'contact_id_a' => $this->_cId_a,
717 'version' => $this->_apiversion,
718 );
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
723 $params = array(
724 'contact_id_a' => $this->_cId_b,
725 'version' => $this->_apiversion,
726 );
727 $result = civicrm_api('relationship', 'get', $params);
728 $this->assertAPISuccess($result, 'in line ' . __LINE__);
729 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__);
730 }
731
732 /**
733 * check with valid params array.
734 * (The get function will behave differently without 'contact_id' passed
735 */
736 function testRelationshipsGetGeneric() {
737 $relParams = array(
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',
743 'is_active' => 1,
744 'version' => $this->_apiversion,
745 );
746
747 $result = civicrm_api('relationship', 'create', $relParams);
748
749 //get relationship
750 $params = array(
751 'contact_id_b' => $this->_cId_b,
752 'version' => $this->_apiversion,
753 );
754 $result = civicrm_api('relationship', 'get', $params);
755 $this->assertAPISuccess($result, 'in line ' . __LINE__);
756 }
757
758 function testGetIsCurrent() {
759 $rel2Params =array(
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',
764 'is_active' => 0,
765 'version' => $this->_apiversion,
766 );
767 $rel2 = civicrm_api('relationship', 'create', $rel2Params);
768 $this->assertAPISuccess($rel2);
769 $rel1 = civicrm_api('relationship', 'create', $this->_params);
770 $this->assertAPISuccess($rel1);
771 $getParams = array(
772 'version' => $this->_apiversion,
773 'filters' => array('is_current' => 1)
774 );
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']);
782
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']);
790
791 // now try finished
792 $rel2Params['is_active'] =1;
793 $rel2Params['start_date'] ='last week';
794 $rel2Params['end_date'] ='yesterday';
795 $rel2 = civicrm_api('relationship', 'create', $rel2Params);
796 }
797 /*
798 * Test using various operators
799 */
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',
807 'is_reserved' => 1,
808 'is_active' => 1,
809 'version' => $this->_apiversion,
810 );
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',
818 'is_reserved' => 1,
819 'is_active' => 1,
820 'version' => $this->_apiversion,
821 );
822 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
823
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',
830 'is_reserved' => 1,
831 'is_active' => 1,
832 'version' => $this->_apiversion,
833 );
834 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
835
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);
847
848 $getParams = array(
849 'version' => $this->_apiversion,
850 'relationship_type_id' => array('IN' => array($relationType2, $relationType3))
851 );
852
853
854 $description = "demonstrates use of IN filter";
855 $subfile = 'INRelationshipType';
856
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']));
861
862 $description = "demonstrates use of NOT IN filter";
863 $subfile = 'NotInRelationshipType';
864 $getParams = array(
865 'version' => $this->_apiversion,
866 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3))
867 );
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']));
872
873 $description = "demonstrates use of BETWEEN filter";
874 $subfile = 'BetweenRelationshipType';
875 $getParams = array(
876 'version' => $this->_apiversion,
877 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4))
878 );
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']));
883
884 $description = "demonstrates use of Not BETWEEN filter";
885 $subfile = 'NotBetweenRelationshipType';
886 $getParams = array(
887 'version' => $this->_apiversion,
888 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4))
889 );
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']));
894
895 }
896 /**
897 * check with invalid relationshipType Id
898 */
899 function testRelationshipTypeAddInvalidId() {
900 $relTypeParams = array(
901 'id' => 'invalid',
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,
907 );
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__);
911 }
912
913 ///////////////// civicrm_get_relationships
914
915 /**
916 * check with invalid data
917 */
918 function testGetRelationshipInvalidData() {
919 $contact_a = array('contact_id' => $this->_cId_a);
920 $contact_b = array('contact_id' => $this->_cId_b);
921
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);
925 }
926
927 /**
928 * check with valid data with contact_b
929 */
930 function testGetRelationshipWithContactB() {
931 $relParams = array(
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',
937 'is_active' => 1,
938 'version' => $this->_apiversion,
939 );
940
941 $relationship = civicrm_api('relationship', 'create', $relParams);
942
943 $contacts = array(
944 'contact_id' => $this->_cId_a,
945 'version' => $this->_apiversion,
946 );
947
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__);
951 $params = array(
952 'id' => $relationship['id'],
953 'version' => $this->_apiversion,
954 );
955 $result = civicrm_api('relationship', 'delete', $params);
956 $this->relationshipTypeDelete($this->_relTypeID);
957 }
958
959 /**
960 * check with valid data with relationshipTypes
961 */
962 function testGetRelationshipWithRelTypes() {
963 $relParams = array(
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',
969 'is_active' => 1,
970 'version' => $this->_apiversion,
971 );
972
973 $relationship = civicrm_api('relationship', 'create', $relParams);
974
975 $contact_a = array(
976 'contact_id' => $this->_cId_a,
977 'version' => $this->_apiversion,
978 );
979
980 $result = civicrm_api('relationship', 'get', $contact_a);
981
982 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__);
983
984 $params = array(
985 'id' => $relationship['id'],
986 'version' => $this->_apiversion,
987 );
988 $result = civicrm_api('relationship', 'delete', $params);
989 $this->relationshipTypeDelete($this->_relTypeID);
990 }
991 }
992