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