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