Merge pull request #1005 from GiantRobot/CRM-12804
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTest.php
1 <?php
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;
44 public $_eNoticeCompliant = FALSE;
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() {
85 $this->quickCleanup(array('civicrm_relationship'));
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);
98 $this->callAPIFailure('relationship', 'create', $params);
99 }
100
101 /**
102 * check with No array
103 */
104 function testRelationshipCreateParamsNotArray() {
105 $params = 'relationship_type_id = 5';
106 $this->callAPIFailure('relationship', 'create', $params);
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
119 $this->callAPIFailure('relationship', 'create', $params);
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
134 $this->callAPIFailure('relationship', 'create', $params);
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 );
144 $this->callAPIFailure('relationship', 'create', $params);
145
146 //contact id does not exists
147 $params['contact_id_a'] = 999;
148 $this->callAPIFailure('relationship', 'create', $params);
149
150 //invalid date
151 $params['contact_id_a'] = $this->_cId_a;
152 $params['start_date'] = array('d' => '1', 'M' => '1');
153 $this->callAPIFailure('relationship', 'create', $params);
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
170 $this->callAPIFailure('relationship', 'create', $params);
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
182 $this->callAPIFailure('relationship', 'create', $params);
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,
205 );
206 $result = $this->callAPIFailure('relationship', 'create', $params);
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,
224 'version' => $this->_apiversion,
225 );
226 $relationship = civicrm_api('relationship', 'create', $params);
227
228 $params = array(
229 'id' => $relationship['id'],
230 'is_active' => 0,
231 'version' => $this->_apiversion,
232 'debug' => 1,
233 );
234 $result = civicrm_api('relationship', 'create', $params);
235 $this->assertAPISuccess($result, 'in line ' . __LINE__);
236 $result = civicrm_api('relationship', 'get', $params);
237 $this->assertEquals(0, $result['values'][$result['id']]['is_active'], 'in line ' . __LINE__);
238 $params['id'] = $relationship['id'];
239 $result = civicrm_api('relationship', 'delete', $params);
240 }
241
242 /**
243 * checkupdate doesn't reset stuff badly - CRM-11789
244 */
245 function testRelationshipCreateUpdateDoesntMangle() {
246 $params = array(
247 'contact_id_a' => $this->_cId_a,
248 'contact_id_b' => $this->_cId_b,
249 'relationship_type_id' => $this->_relTypeID,
250 'start_date' => '2008-12-20',
251 'end_date' => NULL,
252 'is_active' => 1,
253 'is_permission_a_b' => 1,
254 'description' => 'my desc',
255 'version' => $this->_apiversion,
256 );
257 $relationship = civicrm_api('relationship', 'create', $params);
258
259 $updateparams = array(
260 'id' => $relationship['id'],
261 'version' => $this->_apiversion,
262 'relationship_type_id' => $this->_relTypeID,
263 );
264 $result = civicrm_api('relationship', 'create', $updateparams);
265
266 $this->assertAPISuccess($result, 'in line ' . __LINE__);
267 //make sure the orig params didn't get changed
268 $this->getAndCheck($params, $relationship['id'], 'relationship');
269
270 }
271
272
273
274 /**
275 * check relationship creation
276 */
277 function testRelationshipCreate() {
278 $params = array(
279 'contact_id_a' => $this->_cId_a,
280 'contact_id_b' => $this->_cId_b,
281 'relationship_type_id' => $this->_relTypeID,
282 'start_date' => '2010-10-30',
283 'end_date' => '2010-12-30',
284 'is_active' => 1,
285 'note' => 'note',
286 'version' => $this->_apiversion,
287 );
288
289 $result = civicrm_api('relationship', 'create', $params);
290 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
291 $this->assertEquals(0, $result['is_error'], 'in line ' . __LINE__);
292 $this->assertNotNull($result['id'], 'in line ' . __LINE__);
293 $relationParams = array(
294 'id' => $result['id'],
295 );
296
297 // assertDBState compares expected values in $result to actual values in the DB
298 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
299 $result = civicrm_api('relationship', 'get', array('version' => 3, 'id' => $result['id']));
300 $values = $result['values'][$result['id']];
301 foreach ($params as $key => $value) {
302 if ($key == 'version' || $key == 'note') {
303 continue;
304 }
305 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
306 }
307 $params['id'] = $result['id'];
308 civicrm_api('relationship', 'delete', $params);
309 }
310
311 /**
312 * check relationship creation
313 */
314 function testRelationshipCreateEmptyEndDate() {
315 $params = array(
316 'contact_id_a' => $this->_cId_a,
317 'contact_id_b' => $this->_cId_b,
318 'relationship_type_id' => $this->_relTypeID,
319 'start_date' => '2010-10-30',
320 'end_date' => '',
321 'is_active' => 1,
322 'note' => 'note',
323 'version' => $this->_apiversion,
324 );
325
326 $result = civicrm_api('relationship', 'create', $params);
327
328 $this->assertEquals(0, $result['is_error'], 'in line ' . __LINE__);
329 $this->assertNotNull($result['id'], 'in line ' . __LINE__);
330 $relationParams = array(
331 'id' => $result['id'],
332 );
333
334 // assertDBState compares expected values in $result to actual values in the DB
335 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
336 $result = civicrm_api('relationship', 'get', array('version' => 3, 'id' => $result['id']));
337 $values = $result['values'][$result['id']];
338 foreach ($params as $key => $value) {
339 if ($key == 'version' || $key == 'note') {
340 continue;
341 }
342 if($key == 'end_date'){
343 $this->assertTrue(empty($values[$key]));
344 continue;
345 }
346 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
347 }
348 $params['id'] = $result['id'];
349 civicrm_api('relationship', 'delete', $params);
350 }
351
352 /**
353 * check relationship creation with custom data
354 */
355 function testRelationshipCreateWithCustomData() {
356 $customGroup = $this->createCustomGroup();
357 $this->_ids = $this->createCustomField();
358 //few custom Values for comparing
359 $custom_params = array(
360 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
361 "custom_{$this->_ids[1]}" => 'Y',
362 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
363 "custom_{$this->_ids[3]}" => 'http://example.com',
364 );
365
366 $params = array(
367 'contact_id_a' => $this->_cId_a,
368 'contact_id_b' => $this->_cId_b,
369 'relationship_type_id' => $this->_relTypeID,
370 'start_date' => '2008-12-20',
371 'is_active' => 1,
372 'version' => $this->_apiversion,
373 );
374 $params = array_merge($params, $custom_params);
375 $result = civicrm_api('relationship', 'create', $params);
376
377 $this->assertNotNull($result['id']);
378 $relationParams = array(
379 'id' => $result['id'],
380 );
381 // assertDBState compares expected values in $result to actual values in the DB
382 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
383
384 $params['id'] = $result['id'];
385 $result = civicrm_api('relationship', 'delete', $params);
386 $this->relationshipTypeDelete($this->_relTypeID);
387 }
388
389 /**
390 * check with complete array + custom field
391 * Note that the test is written on purpose without any
392 * variables specific to participant so it can be replicated into other entities
393 * and / or moved to the automated test suite
394 */
395 function testGetWithCustom() {
396 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
397
398 $params = $this->_params;
399 $params['custom_' . $ids['custom_field_id']] = "custom string";
400
401 $result = civicrm_api($this->_entity, 'create', $params);
402 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
403
404 $this->assertAPISuccess($result, ' in line ' . __LINE__);
405 $getParams = array('version' => 3, 'id' => $result['id']);
406 $check = civicrm_api($this->_entity, 'get', $getParams);
407 $this->documentMe($getParams, $check, __FUNCTION__, __FILE__);
408 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
409
410 $this->customFieldDelete($ids['custom_field_id']);
411 $this->customGroupDelete($ids['custom_group_id']);
412 }
413
414 function createCustomGroup() {
415 $params = array(
416 'title' => 'Custom Group',
417 'extends' => array('Relationship'),
418 'weight' => 5,
419 'style' => 'Inline',
420 'is_active' => 1,
421 'max_multiple' => 0,
422 'version' => $this->_apiversion,
423 );
424 $customGroup = civicrm_api('custom_group', 'create', $params);
425 $this->_customGroupId = $customGroup['id'];
426 return $customGroup['id'];
427 }
428
429 function createCustomField() {
430 $ids = array();
431 $params = array(
432 'custom_group_id' => $this->_customGroupId,
433 'label' => 'Enter text about relationship',
434 'html_type' => 'Text',
435 'data_type' => 'String',
436 'default_value' => 'xyz',
437 'weight' => 1,
438 'is_required' => 1,
439 'is_searchable' => 0,
440 'is_active' => 1,
441 'version' => $this->_apiversion,
442 );
443
444
445 $result = civicrm_api('CustomField', 'create', $params);
446
447 $customField = NULL;
448 $ids[] = $customField['result']['customFieldId'];
449
450 $optionValue[] = array(
451 'label' => 'Red',
452 'value' => 'R',
453 'weight' => 1,
454 'is_active' => 1,
455 );
456 $optionValue[] = array(
457 'label' => 'Yellow',
458 'value' => 'Y',
459 'weight' => 2,
460 'is_active' => 1,
461 );
462 $optionValue[] = array(
463 'label' => 'Green',
464 'value' => 'G',
465 'weight' => 3,
466 'is_active' => 1,
467 );
468
469 $params = array(
470 'label' => 'Pick Color',
471 'html_type' => 'Select',
472 'data_type' => 'String',
473 'weight' => 2,
474 'is_required' => 1,
475 'is_searchable' => 0,
476 'is_active' => 1,
477 'option_values' => $optionValue,
478 'custom_group_id' => $this->_customGroupId,
479 'version' => $this->_apiversion,
480 );
481
482 $customField = civicrm_api('custom_field', 'create', $params);
483 $ids[] = $customField['id'];
484
485 $params = array(
486 'custom_group_id' => $this->_customGroupId,
487 'name' => 'test_date',
488 'label' => 'test_date',
489 'html_type' => 'Select Date',
490 'data_type' => 'Date',
491 'default_value' => '20090711',
492 'weight' => 3,
493 'is_required' => 1,
494 'is_searchable' => 0,
495 'is_active' => 1,
496 'version' => $this->_apiversion,
497 );
498
499 $customField = civicrm_api('custom_field', 'create', $params);
500
501 $ids[] = $customField['id'];
502 $params = array(
503 'custom_group_id' => $this->_customGroupId,
504 'name' => 'test_link',
505 'label' => 'test_link',
506 'html_type' => 'Link',
507 'data_type' => 'Link',
508 'default_value' => 'http://civicrm.org',
509 'weight' => 4,
510 'is_required' => 1,
511 'is_searchable' => 0,
512 'is_active' => 1,
513 'version' => $this->_apiversion,
514 );
515
516 $customField = civicrm_api('custom_field', 'create', $params);
517 $ids[] = $customField['id'];
518 return $ids;
519 }
520
521 ///////////////// civicrm_relationship_delete methods
522
523 /**
524 * check with empty array
525 */
526 function testRelationshipDeleteEmpty() {
527 $params = array('version' => $this->_apiversion);
528 $result = $this->callAPIFailure('relationship', 'delete', $params);
529 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
530 }
531
532 /**
533 * check if required fields are not passed
534 */
535 function testRelationshipDeleteWithoutRequired() {
536 $params = array(
537 'start_date' => '2008-12-20',
538 'end_date' => '2009-12-20',
539 'is_active' => 1,
540 );
541
542 $result = $this->callAPIFailure('relationship', 'delete', $params);
543 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
544 }
545
546 /**
547 * check with incorrect required fields
548 */
549 function testRelationshipDeleteWithIncorrectData() {
550 $params = array(
551 'contact_id_a' => $this->_cId_a,
552 'contact_id_b' => $this->_cId_b,
553 'relationship_type_id' => 'Breaking Relationship',
554 'version' => $this->_apiversion,
555 );
556
557 $result = $this->callAPIFailure('relationship', 'delete', $params);
558 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id', 'in line ' . __LINE__);
559
560 $params['id'] = "Invalid";
561 $result = $this->callAPIFailure('relationship', 'delete', $params);
562 $this->assertEquals($result['error_message'], 'Invalid value for relationship ID', 'in line ' . __LINE__);
563 }
564
565 /**
566 * check relationship creation
567 */
568 function testRelationshipDelete() {
569 $params = array(
570 'contact_id_a' => $this->_cId_a,
571 'contact_id_b' => $this->_cId_b,
572 'relationship_type_id' => $this->_relTypeID,
573 'start_date' => '2008-12-20',
574 'is_active' => 1,
575 'version' => $this->_apiversion,
576 );
577
578 $result = civicrm_api('relationship', 'create', $params);
579 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
580 $this->assertNotNull($result['id']);
581
582 //Delete relationship
583 $params = array();
584 $params['id'] = $result['id'];
585
586 $result = civicrm_api('relationship', 'delete', $params);
587 $this->relationshipTypeDelete($this->_relTypeID);
588 }
589
590 ///////////////// civicrm_relationship_update methods
591
592 /**
593 * check with empty array
594 */
595 function testRelationshipUpdateEmpty() {
596 $result = $this->callAPIFailure('relationship', 'create', array());
597 $this->assertEquals('Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id', $result['error_message'], 'In line ' . __LINE__);
598 }
599
600 /**
601 * check if required fields are not passed
602 */
603
604 /**
605 * check relationship update
606 */
607 function testRelationshipCreateDuplicate() {
608 $relParams = array(
609 'contact_id_a' => $this->_cId_a,
610 'contact_id_b' => $this->_cId_b,
611 'relationship_type_id' => $this->_relTypeID,
612 'start_date' => '20081214',
613 'end_date' => '20091214',
614 'is_active' => 1,
615 'version' => $this->_apiversion,
616 );
617
618 $result = civicrm_api('relationship', 'create', $relParams);
619
620 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
621 $this->_relationID = $result['id'];
622
623 $params = array(
624 'contact_id_a' => $this->_cId_a,
625 'contact_id_b' => $this->_cId_b,
626 'relationship_type_id' => $this->_relTypeID,
627 'start_date' => '20081214',
628 'end_date' => '20091214', 'is_active' => 0,
629 'version' => $this->_apiversion,
630 );
631
632 $result = $this->callAPIFailure('relationship', 'create', $params);
633 $this->assertEquals($result['error_message'], 'Relationship already exists', 'In line ' . __LINE__);
634
635 //delete created relationship
636 $params = array(
637 'id' => $this->_relationID,
638 'version' => $this->_apiversion,
639 );
640
641 $result = civicrm_api('relationship', 'delete', $params);
642 $this->assertAPISuccess($result, 'in line ' . __LINE__);
643
644 //delete created relationship type
645 $this->relationshipTypeDelete($this->_relTypeID);
646 }
647
648 /**
649 * check with valid params array.
650 */
651 function testRelationshipsGet() {
652 $relParams = array(
653 'contact_id_a' => $this->_cId_a,
654 'contact_id_b' => $this->_cId_b,
655 'relationship_type_id' => $this->_relTypeID,
656 'start_date' => '2011-01-01',
657 'end_date' => '2013-01-01',
658 'is_active' => 1,
659 'version' => $this->_apiversion,
660 );
661
662 $result = civicrm_api('relationship', 'create', $relParams);
663
664 //get relationship
665 $params = array(
666 'contact_id' => $this->_cId_b,
667 'version' => $this->_apiversion,
668 );
669 $result = civicrm_api('relationship', 'get', $params);
670
671 $this->assertAPISuccess($result, 'in line ' . __LINE__);
672 $this->assertEquals($result['count'], 1, 'in line ' . __LINE__);
673 $params = array(
674 'contact_id_a' => $this->_cId_a,
675 'version' => $this->_apiversion,
676 );
677 $result = civicrm_api('relationship', 'get', $params);
678 $this->assertAPISuccess($result, 'in line ' . __LINE__);
679 $this->assertEquals($result['count'], 1, 'in line ' . __LINE__);
680 // contact_id_a is wrong so should be no matches
681 $params = array(
682 'contact_id_a' => $this->_cId_b,
683 'version' => $this->_apiversion,
684 );
685 $result = civicrm_api('relationship', 'get', $params);
686 $this->assertAPISuccess($result, 'in line ' . __LINE__);
687 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__);
688 }
689
690 /**
691 * check with valid params array.
692 * (The get function will behave differently without 'contact_id' passed
693 */
694 function testRelationshipsGetGeneric() {
695 $relParams = array(
696 'contact_id_a' => $this->_cId_a,
697 'contact_id_b' => $this->_cId_b,
698 'relationship_type_id' => $this->_relTypeID,
699 'start_date' => '2011-01-01',
700 'end_date' => '2013-01-01',
701 'is_active' => 1,
702 'version' => $this->_apiversion,
703 );
704
705 $result = civicrm_api('relationship', 'create', $relParams);
706
707 //get relationship
708 $params = array(
709 'contact_id_b' => $this->_cId_b,
710 'version' => $this->_apiversion,
711 );
712 $result = civicrm_api('relationship', 'get', $params);
713 $this->assertAPISuccess($result, 'in line ' . __LINE__);
714 }
715
716 function testGetIsCurrent() {
717 $rel2Params =array(
718 'contact_id_a' => $this->_cId_a,
719 'contact_id_b' => $this->_cId_b2,
720 'relationship_type_id' => $this->_relTypeID,
721 'start_date' => '2008-12-20',
722 'is_active' => 0,
723 'version' => $this->_apiversion,
724 );
725 $rel2 = civicrm_api('relationship', 'create', $rel2Params);
726 $this->assertAPISuccess($rel2);
727 $rel1 = civicrm_api('relationship', 'create', $this->_params);
728 $this->assertAPISuccess($rel1);
729 $getParams = array(
730 'version' => $this->_apiversion,
731 'filters' => array('is_current' => 1)
732 );
733 $description = "demonstrates is_current filter";
734 $subfile = 'filterIsCurrent';
735 //no relationship has been created
736 $result = civicrm_api('relationship', 'get', $getParams);
737 $this->documentMe($getParams, $result, __FUNCTION__, __FILE__, $description, $subfile);
738 $this->assertEquals($result['count'], 1);
739 $this->AssertEquals($rel1['id'], $result['id']);
740
741 // now try not started
742 $rel2Params['is_active'] =1;
743 $rel2Params['start_date'] ='tomorrow';
744 $rel2 = civicrm_api('relationship', 'create', $rel2Params);
745 $result = civicrm_api('relationship', 'get', $getParams);
746 $this->assertEquals($result['count'], 1);
747 $this->AssertEquals($rel1['id'], $result['id']);
748
749 // now try finished
750 $rel2Params['is_active'] =1;
751 $rel2Params['start_date'] ='last week';
752 $rel2Params['end_date'] ='yesterday';
753 $rel2 = civicrm_api('relationship', 'create', $rel2Params);
754 }
755 /*
756 * Test using various operators
757 */
758 function testGetTypeOperators() {
759 $relTypeParams = array(
760 'name_a_b' => 'Relation 3 for delete',
761 'name_b_a' => 'Relation 6 for delete',
762 'description' => 'Testing relationship type 2',
763 'contact_type_a' => 'Individual',
764 'contact_type_b' => 'Organization',
765 'is_reserved' => 1,
766 'is_active' => 1,
767 'version' => $this->_apiversion,
768 );
769 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
770 $relTypeParams = array(
771 'name_a_b' => 'Relation 8 for delete',
772 'name_b_a' => 'Relation 9 for delete',
773 'description' => 'Testing relationship type 7',
774 'contact_type_a' => 'Individual',
775 'contact_type_b' => 'Organization',
776 'is_reserved' => 1,
777 'is_active' => 1,
778 'version' => $this->_apiversion,
779 );
780 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
781
782 $relTypeParams = array(
783 'name_a_b' => 'Relation 6 for delete',
784 'name_b_a' => 'Relation 88for delete',
785 'description' => 'Testing relationship type 00',
786 'contact_type_a' => 'Individual',
787 'contact_type_b' => 'Organization',
788 'is_reserved' => 1,
789 'is_active' => 1,
790 'version' => $this->_apiversion,
791 );
792 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
793
794 $rel1 = civicrm_api('relationship', 'create', $this->_params);
795 $this->assertAPISuccess($rel1);
796 $rel2 = civicrm_api('relationship', 'create', array_merge($this->_params,
797 array('relationship_type_id' => $relationType2,)));
798 $this->assertAPISuccess($rel2);
799 $rel3 = civicrm_api('relationship', 'create', array_merge($this->_params,
800 array('relationship_type_id' => $relationType3,)));
801 $this->assertAPISuccess($rel3);
802 $rel4 = civicrm_api('relationship', 'create', array_merge($this->_params,
803 array('relationship_type_id' => $relationType4,)));
804 $this->assertAPISuccess($rel4);
805
806 $getParams = array(
807 'version' => $this->_apiversion,
808 'relationship_type_id' => array('IN' => array($relationType2, $relationType3))
809 );
810
811 $description = "demonstrates use of IN filter";
812 $subfile = 'INRelationshipType';
813
814 $result = civicrm_api('relationship', 'get', $getParams);
815 $this->documentMe($getParams, $result, __FUNCTION__, __FILE__, $description, $subfile);
816 $this->assertEquals($result['count'], 2);
817 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
818
819 $description = "demonstrates use of NOT IN filter";
820 $subfile = 'NotInRelationshipType';
821 $getParams = array(
822 'version' => $this->_apiversion,
823 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3))
824 );
825 $result = civicrm_api('relationship', 'get', $getParams);
826 $this->documentMe($getParams, $result, __FUNCTION__, __FILE__, $description, $subfile);
827 $this->assertEquals($result['count'], 2);
828 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
829
830 $description = "demonstrates use of BETWEEN filter";
831 $subfile = 'BetweenRelationshipType';
832 $getParams = array(
833 'version' => $this->_apiversion,
834 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4))
835 );
836 $result = civicrm_api('relationship', 'get', $getParams);
837 $this->documentMe($getParams, $result, __FUNCTION__, __FILE__, $description, $subfile);
838 $this->assertEquals($result['count'], 3);
839 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
840
841 $description = "demonstrates use of Not BETWEEN filter";
842 $subfile = 'NotBetweenRelationshipType';
843 $getParams = array(
844 'version' => $this->_apiversion,
845 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4))
846 );
847 $result = civicrm_api('relationship', 'get', $getParams);
848 $this->documentMe($getParams, $result, __FUNCTION__, __FILE__, $description, $subfile);
849 $this->assertEquals($result['count'], 1);
850 $this->AssertEquals(array($rel1['id'],), array_keys($result['values']));
851
852 }
853 /**
854 * check with invalid relationshipType Id
855 */
856 function testRelationshipTypeAddInvalidId() {
857 $relTypeParams = array(
858 'id' => 'invalid',
859 'name_a_b' => 'Relation 1 for delete',
860 'name_b_a' => 'Relation 2 for delete',
861 'contact_type_a' => 'Individual',
862 'contact_type_b' => 'Organization',
863 );
864 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams);
865 $this->assertEquals('id is not a valid integer', $result['error_message'], 'in line ' . __LINE__);
866 }
867
868 ///////////////// civicrm_get_relationships
869
870 /**
871 * check with invalid data
872 */
873 function testGetRelationshipInvalidData() {
874 $contact_a = array('contact_id' => $this->_cId_a);
875 $contact_b = array('contact_id' => $this->_cId_b);
876
877 //no relationship has been created
878 $result = civicrm_api('relationship', 'get', $contact_a, $contact_b, NULL, 'asc');
879 $this->assertAPIFailure($result);
880 }
881
882 /**
883 * check with valid data with contact_b
884 */
885 function testGetRelationshipWithContactB() {
886 $relParams = array(
887 'contact_id_a' => $this->_cId_a,
888 'contact_id_b' => $this->_cId_b,
889 'relationship_type_id' => $this->_relTypeID,
890 'start_date' => '2011-01-01',
891 'end_date' => '2013-01-01',
892 'is_active' => 1,
893 'version' => $this->_apiversion,
894 );
895
896 $relationship = civicrm_api('relationship', 'create', $relParams);
897
898 $contacts = array(
899 'contact_id' => $this->_cId_a,
900 'version' => $this->_apiversion,
901 );
902
903 $result = civicrm_api('relationship', 'get', $contacts);
904 $this->assertAPISuccess($result, 'in line ' . __LINE__);
905 $this->assertGreaterThan(0, $result['count'], 'in line ' . __LINE__);
906 $params = array(
907 'id' => $relationship['id'],
908 'version' => $this->_apiversion,
909 );
910 $result = civicrm_api('relationship', 'delete', $params);
911 $this->relationshipTypeDelete($this->_relTypeID);
912 }
913
914 /**
915 * check with valid data with relationshipTypes
916 */
917 function testGetRelationshipWithRelTypes() {
918 $relParams = array(
919 'contact_id_a' => $this->_cId_a,
920 'contact_id_b' => $this->_cId_b,
921 'relationship_type_id' => $this->_relTypeID,
922 'start_date' => '2011-01-01',
923 'end_date' => '2013-01-01',
924 'is_active' => 1,
925 'version' => $this->_apiversion,
926 );
927
928 $relationship = civicrm_api('relationship', 'create', $relParams);
929
930 $contact_a = array(
931 'contact_id' => $this->_cId_a,
932 'version' => $this->_apiversion,
933 );
934
935 $result = civicrm_api('relationship', 'get', $contact_a);
936
937 $this->assertAPISuccess($result, 'in line ' . __LINE__);
938
939 $params = array(
940 'id' => $relationship['id'],
941 'version' => $this->_apiversion,
942 );
943 $result = civicrm_api('relationship', 'delete', $params);
944 $this->relationshipTypeDelete($this->_relTypeID);
945 }
946 }
947