Merge pull request #12093 from aniesshsethh/core_105
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTest.php
1 <?php
2 /**
3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 5 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2018 |
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 /**
29 * Class contains api test cases for "civicrm_relationship"
30 * @group headless
31 */
32 class api_v3_RelationshipTest extends CiviUnitTestCase {
33 protected $_apiversion = 3;
34 protected $_cId_a;
35 /**
36 * Second individual.
37 * @var integer
38 */
39 protected $_cId_a_2;
40 protected $_cId_b;
41 /**
42 * Second organization contact.
43 *
44 * @var int
45 */
46 protected $_cId_b2;
47 protected $_relTypeID;
48 protected $_ids = array();
49 protected $_customGroupId = NULL;
50 protected $_customFieldId = NULL;
51 protected $_params;
52
53 protected $_entity;
54
55 /**
56 * Set up function.
57 */
58 public function setUp() {
59 parent::setUp();
60 $this->_cId_a = $this->individualCreate();
61 $this->_cId_a_2 = $this->individualCreate(array(
62 'last_name' => 'c2',
63 'email' => 'c@w.com',
64 'contact_type' => 'Individual',
65 ));
66 $this->_cId_b = $this->organizationCreate();
67 $this->_cId_b2 = $this->organizationCreate(array('organization_name' => ' Org 2'));
68 $this->_entity = 'relationship';
69 //Create a relationship type.
70 $relTypeParams = array(
71 'name_a_b' => 'Relation 1 for delete',
72 'name_b_a' => 'Relation 2 for delete',
73 'description' => 'Testing relationship type',
74 'contact_type_a' => 'Individual',
75 'contact_type_b' => 'Organization',
76 'is_reserved' => 1,
77 'is_active' => 1,
78 );
79
80 $this->_relTypeID = $this->relationshipTypeCreate($relTypeParams);
81 $this->_params = array(
82 'contact_id_a' => $this->_cId_a,
83 'contact_id_b' => $this->_cId_b,
84 'relationship_type_id' => $this->_relTypeID,
85 'start_date' => '2008-12-20',
86 'is_active' => 1,
87 );
88
89 }
90
91 /**
92 * Tear down function.
93 *
94 * @throws \Exception
95 */
96 public function tearDown() {
97 $this->contactDelete($this->_cId_a);
98 $this->contactDelete($this->_cId_a_2);
99 $this->contactDelete($this->_cId_b);
100 $this->contactDelete($this->_cId_b2);
101 $this->quickCleanup(array('civicrm_relationship'), TRUE);
102 $this->relationshipTypeDelete($this->_relTypeID);
103 }
104
105 /**
106 * Check with empty array.
107 */
108 public function testRelationshipCreateEmpty() {
109 $this->callAPIFailure('relationship', 'create', array());
110 }
111
112 /**
113 * Test Current Employer is correctly set.
114 */
115 public function testCurrentEmployerRelationship() {
116 $employerRelationshipID = $this->callAPISuccessGetValue('RelationshipType', array(
117 'return' => "id",
118 'name_b_a' => "Employer Of",
119 ));
120 $employerRelationship = $this->callAPISuccess('Relationship', 'create', array(
121 'contact_id_a' => $this->_cId_a,
122 'contact_id_b' => $this->_cId_b,
123 'relationship_type_id' => $employerRelationshipID,
124 ));
125 $params = array($this->_cId_a => $this->_cId_b);
126 CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($params);
127
128 //Check if current employer is correctly set.
129 $employer = $this->callAPISuccessGetValue('Contact', array(
130 'return' => "current_employer",
131 'id' => $this->_cId_a,
132 ));
133 $organisation = $this->callAPISuccessGetValue('Contact', array(
134 'return' => "sort_name",
135 'id' => $this->_cId_b,
136 ));
137 $this->assertEquals($employer, $organisation);
138
139 //Update relationship type
140 $update = $this->callAPISuccess('Relationship', 'create', array(
141 'id' => $employerRelationship['id'],
142 'relationship_type_id' => $this->_relTypeID,
143 ));
144 $employeeContact = $this->callAPISuccessGetSingle('Contact', array(
145 'return' => array("current_employer"),
146 'id' => $this->_cId_a,
147 ));
148 //current employer should be removed.
149 $this->assertEmpty($employeeContact['current_employer']);
150 }
151
152 /**
153 * Check if required fields are not passed.
154 */
155 public function testRelationshipCreateWithoutRequired() {
156 $params = array(
157 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
158 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
159 'is_active' => 1,
160 );
161
162 $this->callAPIFailure('relationship', 'create', $params);
163 }
164
165 /**
166 * Check with incorrect required fields.
167 */
168 public function testRelationshipCreateWithIncorrectData() {
169
170 $params = array(
171 'contact_id_a' => $this->_cId_a,
172 'contact_id_b' => $this->_cId_b,
173 'relationship_type_id' => 'Breaking Relationship',
174 );
175
176 $this->callAPIFailure('relationship', 'create', $params);
177
178 //contact id is not an integer
179 $params = array(
180 'contact_id_a' => 'invalid',
181 'contact_id_b' => $this->_cId_b,
182 'relationship_type_id' => $this->_relTypeID,
183 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
184 'is_active' => 1,
185 );
186 $this->callAPIFailure('relationship', 'create', $params);
187
188 // Contact id does not exist.
189 $params['contact_id_a'] = 999;
190 $this->callAPIFailure('relationship', 'create', $params);
191
192 //invalid date
193 $params['contact_id_a'] = $this->_cId_a;
194 $params['start_date'] = array('d' => '1', 'M' => '1');
195 $this->callAPIFailure('relationship', 'create', $params);
196 }
197
198 /**
199 * Check relationship creation with invalid Relationship.
200 */
201 public function testRelationshipCreateInvalidRelationship() {
202 // Both have the contact type Individual.
203 $params = array(
204 'contact_id_a' => $this->_cId_a,
205 'contact_id_b' => $this->_cId_a,
206 'relationship_type_id' => $this->_relTypeID,
207 'start_date' => '2008-01-10',
208 'is_active' => 1,
209 );
210
211 $this->callAPIFailure('relationship', 'create', $params);
212
213 // both the contact of type Organization
214 $params = array(
215 'contact_id_a' => $this->_cId_b,
216 'contact_id_b' => $this->_cId_b,
217 'relationship_type_id' => $this->_relTypeID,
218 'start_date' => '2008-01-10',
219 'is_active' => 1,
220 );
221
222 $this->callAPIFailure('relationship', 'create', $params);
223 }
224
225 /**
226 * Check relationship already exists.
227 */
228 public function testRelationshipCreateAlreadyExists() {
229 $params = array(
230 'contact_id_a' => $this->_cId_a,
231 'contact_id_b' => $this->_cId_b,
232 'relationship_type_id' => $this->_relTypeID,
233 'start_date' => '2008-12-20',
234 'end_date' => NULL,
235 'is_active' => 1,
236 );
237 $relationship = $this->callAPISuccess('relationship', 'create', $params);
238
239 $params = array(
240 'contact_id_a' => $this->_cId_a,
241 'contact_id_b' => $this->_cId_b,
242 'relationship_type_id' => $this->_relTypeID,
243 'start_date' => '2008-12-20',
244 'is_active' => 1,
245 );
246 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
247
248 $params['id'] = $relationship['id'];
249 $this->callAPISuccess('relationship', 'delete', $params);
250 }
251
252 /**
253 * Check relationship already exists.
254 */
255 public function testRelationshipCreateUpdateAlreadyExists() {
256 $params = array(
257 'contact_id_a' => $this->_cId_a,
258 'contact_id_b' => $this->_cId_b,
259 'relationship_type_id' => $this->_relTypeID,
260 'start_date' => '2008-12-20',
261 'end_date' => NULL,
262 'is_active' => 1,
263
264 );
265 $relationship = $this->callAPISuccess('relationship', 'create', $params);
266 $params = array(
267 'id' => $relationship['id'],
268 'is_active' => 0,
269 'debug' => 1,
270 );
271 $this->callAPISuccess('relationship', 'create', $params);
272 $this->callAPISuccess('relationship', 'get', $params);
273 $params['id'] = $relationship['id'];
274 $this->callAPISuccess('relationship', 'delete', $params);
275 }
276
277 /**
278 * Check update doesn't reset stuff badly - CRM-11789.
279 */
280 public function testRelationshipCreateUpdateDoesNotMangle() {
281 $params = array(
282 'contact_id_a' => $this->_cId_a,
283 'contact_id_b' => $this->_cId_b,
284 'relationship_type_id' => $this->_relTypeID,
285 'start_date' => '2008-12-20',
286 'is_active' => 1,
287 'is_permission_a_b' => 1,
288 'description' => 'my desc',
289 );
290 $relationship = $this->callAPISuccess('relationship', 'create', $params);
291
292 $updateParams = array(
293 'id' => $relationship['id'],
294 'relationship_type_id' => $this->_relTypeID,
295 );
296 $this->callAPISuccess('relationship', 'create', $updateParams);
297
298 //make sure the orig params didn't get changed
299 $this->getAndCheck($params, $relationship['id'], 'relationship');
300
301 }
302
303
304 /**
305 * Check relationship creation.
306 */
307 public function testRelationshipCreate() {
308 $params = array(
309 'contact_id_a' => $this->_cId_a,
310 'contact_id_b' => $this->_cId_b,
311 'relationship_type_id' => $this->_relTypeID,
312 'start_date' => '2010-10-30',
313 'end_date' => '2010-12-30',
314 'is_active' => 1,
315 'note' => 'note',
316 );
317
318 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__, __FILE__);
319 $this->assertNotNull($result['id']);
320 $relationParams = array(
321 'id' => $result['id'],
322 );
323
324 // assertDBState compares expected values in $result to actual values in the DB
325 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
326 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
327 $values = $result['values'][$result['id']];
328 foreach ($params as $key => $value) {
329 if ($key == 'note') {
330 continue;
331 }
332 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
333 }
334 $params['id'] = $result['id'];
335 $this->callAPISuccess('relationship', 'delete', $params);
336 }
337
338 /**
339 * Ensure disabling works.
340 */
341 public function testRelationshipUpdate() {
342 $result = $this->callAPISuccess('relationship', 'create', $this->_params);
343 $relID = $result['id'];
344 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
345 $this->assertEquals($relID, $result['id']);
346
347 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
348
349 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
350 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
351 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
352 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
353 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
354 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
355 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
356 }
357
358 /**
359 * Check relationship creation.
360 */
361 public function testRelationshipCreateEmptyEndDate() {
362 $params = array(
363 'contact_id_a' => $this->_cId_a,
364 'contact_id_b' => $this->_cId_b,
365 'relationship_type_id' => $this->_relTypeID,
366 'start_date' => '2010-10-30',
367 'end_date' => '',
368 'is_active' => 1,
369 'note' => 'note',
370 );
371
372 $result = $this->callAPISuccess('relationship', 'create', $params);
373 $this->assertNotNull($result['id']);
374 $relationParams = array(
375 'id' => $result['id'],
376 );
377
378 // assertDBState compares expected values in $result to actual values in the DB
379 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
380 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
381 $values = $result['values'][$result['id']];
382 foreach ($params as $key => $value) {
383 if ($key == 'note') {
384 continue;
385 }
386 if ($key == 'end_date') {
387 $this->assertTrue(empty($values[$key]));
388 continue;
389 }
390 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
391 }
392 $params['id'] = $result['id'];
393 $this->callAPISuccess('relationship', 'delete', $params);
394 }
395
396 /**
397 * Check relationship creation with custom data.
398 */
399 public function testRelationshipCreateEditWithCustomData() {
400 $this->createCustomGroup();
401 $this->_ids = $this->createCustomField();
402 //few custom Values for comparing
403 $custom_params = array(
404 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
405 "custom_{$this->_ids[1]}" => 'Y',
406 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
407 "custom_{$this->_ids[3]}" => 'http://example.com',
408 );
409
410 $params = array(
411 'contact_id_a' => $this->_cId_a,
412 'contact_id_b' => $this->_cId_b,
413 'relationship_type_id' => $this->_relTypeID,
414 'start_date' => '2008-12-20',
415 'is_active' => 1,
416 );
417 $params = array_merge($params, $custom_params);
418 $result = $this->callAPISuccess('relationship', 'create', $params);
419
420 $relationParams = array(
421 'id' => $result['id'],
422 );
423 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
424
425 //Test Edit of custom field from the form.
426 $getParams = array('id' => $result['id']);
427 $updateParams = array_merge($getParams, array(
428 "custom_{$this->_ids[0]}" => 'Edited Text Value',
429 'relationship_type_id' => $this->_relTypeID . '_b_a',
430 'related_contact_id' => $this->_cId_a,
431 ));
432 $reln = new CRM_Contact_Form_Relationship();
433 $reln->_action = CRM_Core_Action::UPDATE;
434 $reln->_relationshipId = $result['id'];
435 $reln->submit($updateParams);
436
437 $check = $this->callAPISuccess('relationship', 'get', $getParams);
438 $this->assertEquals("Edited Text Value", $check['values'][$check['id']]["custom_{$this->_ids[0]}"]);
439
440 $params['id'] = $result['id'];
441 $this->callAPISuccess('relationship', 'delete', $params);
442 $this->relationshipTypeDelete($this->_relTypeID);
443 }
444
445 /**
446 * Check with complete array + custom field
447 * Note that the test is written on purpose without any
448 * variables specific to participant so it can be replicated into other entities
449 * and / or moved to the automated test suite
450 */
451 public function testGetWithCustom() {
452 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
453
454 $params = $this->_params;
455 $params['custom_' . $ids['custom_field_id']] = "custom string";
456
457 $result = $this->callAPISuccess($this->_entity, 'create', $params);
458 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
459
460 $getParams = array('id' => $result['id']);
461 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
462 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
463
464 $this->customFieldDelete($ids['custom_field_id']);
465 $this->customGroupDelete($ids['custom_group_id']);
466 }
467
468 /**
469 * @return mixed
470 */
471 public function createCustomGroup() {
472 $params = array(
473 'title' => 'Custom Group',
474 'extends' => array('Relationship'),
475 'weight' => 5,
476 'style' => 'Inline',
477 'is_active' => 1,
478 'max_multiple' => 0,
479 );
480 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
481 $this->_customGroupId = $customGroup['id'];
482 return $customGroup['id'];
483 }
484
485 /**
486 * @return array
487 */
488 public function createCustomField() {
489 $ids = array();
490 $params = array(
491 'custom_group_id' => $this->_customGroupId,
492 'label' => 'Enter text about relationship',
493 'html_type' => 'Text',
494 'data_type' => 'String',
495 'default_value' => 'xyz',
496 'weight' => 1,
497 'is_required' => 1,
498 'is_searchable' => 0,
499 'is_active' => 1,
500 );
501
502 $customField = $this->callAPISuccess('CustomField', 'create', $params);
503 $ids[] = $customField['id'];
504
505 $optionValue[] = array(
506 'label' => 'Red',
507 'value' => 'R',
508 'weight' => 1,
509 'is_active' => 1,
510 );
511 $optionValue[] = array(
512 'label' => 'Yellow',
513 'value' => 'Y',
514 'weight' => 2,
515 'is_active' => 1,
516 );
517 $optionValue[] = array(
518 'label' => 'Green',
519 'value' => 'G',
520 'weight' => 3,
521 'is_active' => 1,
522 );
523
524 $params = array(
525 'label' => 'Pick Color',
526 'html_type' => 'Select',
527 'data_type' => 'String',
528 'weight' => 2,
529 'is_required' => 1,
530 'is_searchable' => 0,
531 'is_active' => 1,
532 'option_values' => $optionValue,
533 'custom_group_id' => $this->_customGroupId,
534 );
535
536 $customField = $this->callAPISuccess('custom_field', 'create', $params);
537 $ids[] = $customField['id'];
538
539 $params = array(
540 'custom_group_id' => $this->_customGroupId,
541 'name' => 'test_date',
542 'label' => 'test_date',
543 'html_type' => 'Select Date',
544 'data_type' => 'Date',
545 'default_value' => '20090711',
546 'weight' => 3,
547 'is_required' => 1,
548 'is_searchable' => 0,
549 'is_active' => 1,
550 );
551
552 $customField = $this->callAPISuccess('custom_field', 'create', $params);
553
554 $ids[] = $customField['id'];
555 $params = array(
556 'custom_group_id' => $this->_customGroupId,
557 'name' => 'test_link',
558 'label' => 'test_link',
559 'html_type' => 'Link',
560 'data_type' => 'Link',
561 'default_value' => 'http://civicrm.org',
562 'weight' => 4,
563 'is_required' => 1,
564 'is_searchable' => 0,
565 'is_active' => 1,
566 );
567
568 $customField = $this->callAPISuccess('custom_field', 'create', $params);
569 $ids[] = $customField['id'];
570 return $ids;
571 }
572
573 /**
574 * Check with empty array.
575 */
576 public function testRelationshipDeleteEmpty() {
577 $this->callAPIFailure('relationship', 'delete', array(), 'Mandatory key(s) missing from params array: id');
578 }
579
580 /**
581 * Check if required fields are not passed.
582 */
583 public function testRelationshipDeleteWithoutRequired() {
584 $params = array(
585 'start_date' => '2008-12-20',
586 'end_date' => '2009-12-20',
587 'is_active' => 1,
588 );
589
590 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
591 }
592
593 /**
594 * Check with incorrect required fields.
595 */
596 public function testRelationshipDeleteWithIncorrectData() {
597 $params = array(
598 'contact_id_a' => $this->_cId_a,
599 'contact_id_b' => $this->_cId_b,
600 'relationship_type_id' => 'Breaking Relationship',
601 );
602
603 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
604
605 $params['id'] = "Invalid";
606 $this->callAPIFailure('relationship', 'delete', $params, 'id is not a valid integer');
607 }
608
609 /**
610 * Check relationship creation.
611 */
612 public function testRelationshipDelete() {
613 $params = array(
614 'contact_id_a' => $this->_cId_a,
615 'contact_id_b' => $this->_cId_b,
616 'relationship_type_id' => $this->_relTypeID,
617 'start_date' => '2008-12-20',
618 'is_active' => 1,
619 );
620
621 $result = $this->callAPISuccess('relationship', 'create', $params);
622 $params = array('id' => $result['id']);
623 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__, __FILE__);
624 $this->relationshipTypeDelete($this->_relTypeID);
625 }
626
627 ///////////////// civicrm_relationship_update methods
628
629 /**
630 * Check with empty array.
631 */
632 public function testRelationshipUpdateEmpty() {
633 $this->callAPIFailure('relationship', 'create', array(),
634 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
635 }
636
637 /**
638 * Check if required fields are not passed.
639 */
640
641 /**
642 * Check relationship update.
643 */
644 public function testRelationshipCreateDuplicate() {
645 $relParams = array(
646 'contact_id_a' => $this->_cId_a,
647 'contact_id_b' => $this->_cId_b,
648 'relationship_type_id' => $this->_relTypeID,
649 'start_date' => '20081214',
650 'end_date' => '20091214',
651 'is_active' => 1,
652 );
653
654 $result = $this->callAPISuccess('relationship', 'create', $relParams);
655
656 $this->assertNotNull($result['id']);
657
658 $params = array(
659 'contact_id_a' => $this->_cId_a,
660 'contact_id_b' => $this->_cId_b,
661 'relationship_type_id' => $this->_relTypeID,
662 'start_date' => '20081214',
663 'end_date' => '20091214',
664 'is_active' => 0,
665 );
666
667 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
668
669 $this->callAPISuccess('relationship', 'delete', array('id' => $result['id']));
670 $this->relationshipTypeDelete($this->_relTypeID);
671 }
672
673 /**
674 * CRM-13725 - Two relationships of same type with same start and end date
675 * should be OK if the custom field values differ.
676 */
677 public function testRelationshipCreateDuplicateWithCustomFields() {
678 $this->createCustomGroup();
679 $this->_ids = $this->createCustomField();
680
681 $custom_params_1 = array(
682 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
683 "custom_{$this->_ids[1]}" => 'Y',
684 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
685 "custom_{$this->_ids[3]}" => 'http://example.com',
686 );
687
688 $custom_params_2 = array(
689 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
690 "custom_{$this->_ids[1]}" => 'Y',
691 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
692 "custom_{$this->_ids[3]}" => 'http://example.org',
693 );
694
695 $params = array(
696 'contact_id_a' => $this->_cId_a,
697 'contact_id_b' => $this->_cId_b,
698 'relationship_type_id' => $this->_relTypeID,
699 'start_date' => '2008-12-20',
700 'is_active' => 1,
701 );
702
703 $params_1 = array_merge($params, $custom_params_1);
704 $params_2 = array_merge($params, $custom_params_2);
705
706 $result_1 = $this->callAPISuccess('relationship', 'create', $params_1);
707 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
708
709 $this->assertNotNull($result_2['id']);
710 $this->assertEquals(0, $result_2['is_error']);
711
712 $this->relationshipTypeDelete($this->_relTypeID);
713 }
714
715 /**
716 * CRM-13725 - Two relationships of same type with same start and end date
717 * should be OK if the custom field values differ. In this case, the
718 * existing relationship does not have custom values, but the new one
719 * does.
720 */
721 public function testRelationshipCreateDuplicateWithCustomFields2() {
722 $this->createCustomGroup();
723 $this->_ids = $this->createCustomField();
724
725 $custom_params_2 = array(
726 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
727 "custom_{$this->_ids[1]}" => 'Y',
728 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
729 "custom_{$this->_ids[3]}" => 'http://example.org',
730 );
731
732 $params_1 = array(
733 'contact_id_a' => $this->_cId_a,
734 'contact_id_b' => $this->_cId_b,
735 'relationship_type_id' => $this->_relTypeID,
736 'start_date' => '2008-12-20',
737 'is_active' => 1,
738 );
739
740 $params_2 = array_merge($params_1, $custom_params_2);
741
742 $this->callAPISuccess('relationship', 'create', $params_1);
743 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
744
745 $this->assertNotNull($result_2['id']);
746 $this->assertEquals(0, $result_2['is_error']);
747
748 $this->relationshipTypeDelete($this->_relTypeID);
749 }
750
751 /**
752 * CRM-13725 - Two relationships of same type with same start and end date
753 * should be OK if the custom field values differ. In this case, the
754 * existing relationship does have custom values, but the new one
755 * does not.
756 */
757 public function testRelationshipCreateDuplicateWithCustomFields3() {
758 $this->createCustomGroup();
759 $this->_ids = $this->createCustomField();
760
761 $custom_params_1 = array(
762 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
763 "custom_{$this->_ids[1]}" => 'Y',
764 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
765 "custom_{$this->_ids[3]}" => 'http://example.org',
766 );
767
768 $params_2 = array(
769 'contact_id_a' => $this->_cId_a,
770 'contact_id_b' => $this->_cId_b,
771 'relationship_type_id' => $this->_relTypeID,
772 'start_date' => '2008-12-20',
773 'is_active' => 1,
774 );
775
776 $params_1 = array_merge($params_2, $custom_params_1);
777
778 $this->callAPISuccess('relationship', 'create', $params_1);
779 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
780
781 $this->assertNotNull($result_2['id']);
782 $this->assertEquals(0, $result_2['is_error']);
783
784 $this->relationshipTypeDelete($this->_relTypeID);
785 }
786
787 /**
788 * Check with valid params array.
789 */
790 public function testRelationshipsGet() {
791 $relParams = array(
792 'contact_id_a' => $this->_cId_a,
793 'contact_id_b' => $this->_cId_b,
794 'relationship_type_id' => $this->_relTypeID,
795 'start_date' => '2011-01-01',
796 'end_date' => '2013-01-01',
797 'is_active' => 1,
798 );
799
800 $this->callAPISuccess('relationship', 'create', $relParams);
801
802 //get relationship
803 $params = array(
804 'contact_id' => $this->_cId_b,
805 );
806 $result = $this->callAPISuccess('relationship', 'get', $params);
807 $this->assertEquals($result['count'], 1);
808 $params = array(
809 'contact_id_a' => $this->_cId_a,
810 );
811 $result = $this->callAPISuccess('relationship', 'get', $params);
812 $this->assertEquals($result['count'], 1);
813 // contact_id_a is wrong so should be no matches
814 $params = array(
815 'contact_id_a' => $this->_cId_b,
816 );
817 $result = $this->callAPISuccess('relationship', 'get', $params);
818 $this->assertEquals($result['count'], 0);
819 }
820
821 /**
822 * Chain Relationship.get and to Contact.get.
823 */
824 public function testRelationshipGetWithChainedCall() {
825 // Create a relationship.
826 $createResult = $this->callAPISuccess('relationship', 'create', $this->_params);
827 $id = $createResult['id'];
828
829 // Try to retrieve it using chaining.
830 $params = array(
831 'relationship_type_id' => $this->_relTypeID,
832 'id' => $id,
833 'api.Contact.get' => array(
834 'id' => '$value.contact_id_b',
835 ),
836 );
837
838 $result = $this->callAPISuccess('relationship', 'get', $params);
839
840 $this->assertEquals(1, $result['count']);
841 $relationship = CRM_Utils_Array::first($result['values']);
842 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
843 $contact = CRM_Utils_Array::first($relationship['api.Contact.get']['values']);
844 $this->assertEquals($this->_cId_b, $contact['id']);
845 }
846
847 /**
848 * Chain Contact.get to Relationship.get and again to Contact.get.
849 */
850 public function testRelationshipGetInChainedCall() {
851 // Create a relationship.
852 $this->callAPISuccess('relationship', 'create', $this->_params);
853
854 // Try to retrieve it using chaining.
855 $params = array(
856 'id' => $this->_cId_a,
857 'api.Relationship.get' => array(
858 'relationship_type_id' => $this->_relTypeID,
859 'contact_id_a' => '$value.id',
860 'api.Contact.get' => array(
861 'id' => '$value.contact_id_b',
862 ),
863 ),
864 );
865
866 $result = $this->callAPISuccess('contact', 'get', $params);
867 $this->assertEquals(1, $result['count']);
868 $contact = CRM_Utils_Array::first($result['values']);
869 $this->assertEquals(1, $contact['api.Relationship.get']['count']);
870 $relationship = CRM_Utils_Array::first($contact['api.Relationship.get']['values']);
871 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
872 $contact = CRM_Utils_Array::first($relationship['api.Contact.get']['values']);
873 $this->assertEquals($this->_cId_b, $contact['id']);
874 }
875
876 /**
877 * Check with valid params array.
878 * (The get function will behave differently without 'contact_id' passed
879 */
880 public function testRelationshipsGetGeneric() {
881 $relParams = array(
882 'contact_id_a' => $this->_cId_a,
883 'contact_id_b' => $this->_cId_b,
884 'relationship_type_id' => $this->_relTypeID,
885 'start_date' => '2011-01-01',
886 'end_date' => '2013-01-01',
887 'is_active' => 1,
888 );
889
890 $this->callAPISuccess('relationship', 'create', $relParams);
891
892 //get relationship
893 $params = array(
894 'contact_id_b' => $this->_cId_b,
895 );
896 $this->callAPISuccess('relationship', 'get', $params);
897 }
898
899 /**
900 * Test retrieving only current relationships.
901 */
902 public function testGetIsCurrent() {
903 $rel2Params = array(
904 'contact_id_a' => $this->_cId_a,
905 'contact_id_b' => $this->_cId_b2,
906 'relationship_type_id' => $this->_relTypeID,
907 'start_date' => '2008-12-20',
908 'is_active' => 0,
909 );
910 $this->callAPISuccess('relationship', 'create', $rel2Params);
911 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
912
913 $getParams = array(
914 'filters' => array('is_current' => 1),
915 );
916 $description = "Demonstrates is_current filter.";
917 $subfile = 'filterIsCurrent';
918 //no relationship has been created
919 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
920 $this->assertEquals($result['count'], 1);
921 $this->AssertEquals($rel1['id'], $result['id']);
922
923 // now try not started
924 $rel2Params['is_active'] = 1;
925 $rel2Params['start_date'] = 'tomorrow';
926 $this->callAPISuccess('relationship', 'create', $rel2Params);
927 $result = $this->callAPISuccess('relationship', 'get', $getParams);
928 $this->assertEquals($result['count'], 1);
929 $this->AssertEquals($rel1['id'], $result['id']);
930
931 // now try finished
932 $rel2Params['is_active'] = 1;
933 $rel2Params['start_date'] = 'last week';
934 $rel2Params['end_date'] = 'yesterday';
935 $this->callAPISuccess('relationship', 'create', $rel2Params);
936 }
937
938 /**
939 * Test using various operators.
940 */
941 public function testGetTypeOperators() {
942 $relTypeParams = array(
943 'name_a_b' => 'Relation 3 for delete',
944 'name_b_a' => 'Relation 6 for delete',
945 'description' => 'Testing relationship type 2',
946 'contact_type_a' => 'Individual',
947 'contact_type_b' => 'Organization',
948 'is_reserved' => 1,
949 'is_active' => 1,
950 );
951 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
952 $relTypeParams = array(
953 'name_a_b' => 'Relation 8 for delete',
954 'name_b_a' => 'Relation 9 for delete',
955 'description' => 'Testing relationship type 7',
956 'contact_type_a' => 'Individual',
957 'contact_type_b' => 'Organization',
958 'is_reserved' => 1,
959 'is_active' => 1,
960 );
961 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
962
963 $relTypeParams = array(
964 'name_a_b' => 'Relation 6 for delete',
965 'name_b_a' => 'Relation 88for delete',
966 'description' => 'Testing relationship type 00',
967 'contact_type_a' => 'Individual',
968 'contact_type_b' => 'Organization',
969 'is_reserved' => 1,
970 'is_active' => 1,
971 );
972 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
973
974 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
975 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
976 array('relationship_type_id' => $relationType2)));
977 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
978 array('relationship_type_id' => $relationType3)));
979 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
980 array('relationship_type_id' => $relationType4)));
981
982 $getParams = array(
983 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
984 );
985
986 $description = "Demonstrates use of IN filter.";
987 $subfile = 'INRelationshipType';
988
989 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
990 $this->assertEquals($result['count'], 2);
991 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
992
993 $description = "Demonstrates use of NOT IN filter.";
994 $subfile = 'NotInRelationshipType';
995 $getParams = array(
996 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
997 );
998 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
999 $this->assertEquals($result['count'], 2);
1000 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
1001
1002 $description = "Demonstrates use of BETWEEN filter.";
1003 $subfile = 'BetweenRelationshipType';
1004 $getParams = array(
1005 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
1006 );
1007 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
1008 $this->assertEquals($result['count'], 3);
1009 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
1010
1011 $description = "Demonstrates use of Not BETWEEN filter.";
1012 $subfile = 'NotBetweenRelationshipType';
1013 $getParams = array(
1014 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
1015 );
1016 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
1017 $this->assertEquals($result['count'], 1);
1018 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
1019
1020 }
1021
1022 /**
1023 * Check with invalid relationshipType Id.
1024 */
1025 public function testRelationshipTypeAddInvalidId() {
1026 $relTypeParams = array(
1027 'id' => 'invalid',
1028 'name_a_b' => 'Relation 1 for delete',
1029 'name_b_a' => 'Relation 2 for delete',
1030 'contact_type_a' => 'Individual',
1031 'contact_type_b' => 'Organization',
1032 );
1033 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
1034 'id is not a valid integer');
1035 }
1036
1037 /**
1038 * Check with valid data with contact_b.
1039 */
1040 public function testGetRelationshipWithContactB() {
1041 $relParams = array(
1042 'contact_id_a' => $this->_cId_a,
1043 'contact_id_b' => $this->_cId_b,
1044 'relationship_type_id' => $this->_relTypeID,
1045 'start_date' => '2011-01-01',
1046 'end_date' => '2013-01-01',
1047 'is_active' => 1,
1048 );
1049
1050 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
1051
1052 $contacts = array(
1053 'contact_id' => $this->_cId_a,
1054 );
1055
1056 $result = $this->callAPISuccess('relationship', 'get', $contacts);
1057 $this->assertGreaterThan(0, $result['count']);
1058 $params = array(
1059 'id' => $relationship['id'],
1060 );
1061 $this->callAPISuccess('relationship', 'delete', $params);
1062 $this->relationshipTypeDelete($this->_relTypeID);
1063 }
1064
1065 /**
1066 * Check with valid data with relationshipTypes.
1067 */
1068 public function testGetRelationshipWithRelTypes() {
1069 $relParams = array(
1070 'contact_id_a' => $this->_cId_a,
1071 'contact_id_b' => $this->_cId_b,
1072 'relationship_type_id' => $this->_relTypeID,
1073 'start_date' => '2011-01-01',
1074 'end_date' => '2013-01-01',
1075 'is_active' => 1,
1076 );
1077
1078 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
1079
1080 $contact_a = array(
1081 'contact_id' => $this->_cId_a,
1082 );
1083 $this->callAPISuccess('relationship', 'get', $contact_a);
1084
1085 $params = array(
1086 'id' => $relationship['id'],
1087 );
1088 $this->callAPISuccess('relationship', 'delete', $params);
1089 $this->relationshipTypeDelete($this->_relTypeID);
1090 }
1091
1092 /**
1093 * Checks that passing in 'contact_id' + a relationship type
1094 * will filter by relationship type (relationships go in both directions)
1095 * as relationship api does a reciprocal check if contact_id provided
1096 *
1097 * We should get 1 result without or with correct relationship type id & 0 with
1098 * an incorrect one
1099 */
1100 public function testGetRelationshipByTypeReciprocal() {
1101 $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1102 $result = $this->callAPISuccess($this->_entity, 'get', array(
1103 'contact_id' => $this->_cId_a,
1104 'relationship_type_id' => $this->_relTypeID,
1105 ));
1106 $this->assertEquals(1, $result['count']);
1107 $result = $this->callAPISuccess($this->_entity, 'get', array(
1108 'contact_id' => $this->_cId_a,
1109 'relationship_type_id' => $this->_relTypeID + 1,
1110 ));
1111 $this->assertEquals(0, $result['count']);
1112 $this->callAPISuccess($this->_entity, 'delete', array('id' => $created['id']));
1113 }
1114
1115 /**
1116 * Checks that passing in 'contact_id_b' + a relationship type
1117 * will filter by relationship type for contact b
1118 *
1119 * We should get 1 result without or with correct relationship type id & 0 with
1120 * an incorrect one
1121 */
1122 public function testGetRelationshipByTypeDAO() {
1123 $this->_ids['relationship'] = $this->callAPISuccess($this->_entity, 'create', array('format.only_id' => TRUE) +
1124 $this->_params);
1125 $this->callAPISuccess($this->_entity, 'getcount', array(
1126 'contact_id_a' => $this->_cId_a,
1127 ),
1128 1);
1129 $result = $this->callAPISuccess($this->_entity, 'get', array(
1130 'contact_id_a' => $this->_cId_a,
1131 'relationship_type_id' => $this->_relTypeID,
1132 ));
1133 $this->assertEquals(1, $result['count']);
1134 $result = $this->callAPISuccess($this->_entity, 'get', array(
1135 'contact_id_a' => $this->_cId_a,
1136 'relationship_type_id' => $this->_relTypeID + 1,
1137 ));
1138 $this->assertEquals(0, $result['count']);
1139 }
1140
1141 /**
1142 * Checks that passing in 'contact_id_b' + a relationship type
1143 * will filter by relationship type for contact b
1144 *
1145 * We should get 1 result without or with correct relationship type id & 0 with
1146 * an incorrect one
1147 */
1148 public function testGetRelationshipByTypeArrayDAO() {
1149 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1150 $org3 = $this->organizationCreate();
1151 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1152 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1153
1154 // Relationship 2.
1155 $this->callAPISuccess($this->_entity, 'create',
1156 array_merge($this->_params, array(
1157 'relationship_type_id' => $relType2,
1158 'contact_id_b' => $this->_cId_b2,
1159 ))
1160 );
1161
1162 // Relationship 3.
1163 $this->callAPISuccess($this->_entity, 'create',
1164 array_merge($this->_params, array(
1165 'relationship_type_id' => $relType3,
1166 'contact_id_b' => $org3,
1167 ))
1168 );
1169
1170 $result = $this->callAPISuccess($this->_entity, 'get', array(
1171 'contact_id_a' => $this->_cId_a,
1172 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1173 ));
1174
1175 $this->assertEquals(2, $result['count']);
1176 foreach ($result['values'] as $key => $value) {
1177 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1178 }
1179 }
1180
1181 /**
1182 * Checks that passing in 'contact_id_b' + a relationship type
1183 * will filter by relationship type for contact b
1184 *
1185 * We should get 1 result without or with correct relationship type id & 0 with
1186 * an incorrect one
1187 */
1188 public function testGetRelationshipByTypeArrayReciprocal() {
1189 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1190 $org3 = $this->organizationCreate();
1191 // lets just assume built in ones aren't being messed with!
1192 $relType2 = 5;
1193 $relType3 = 6;
1194
1195 // Relationship 2.
1196 $this->callAPISuccess($this->_entity, 'create',
1197 array_merge($this->_params, array(
1198 'relationship_type_id' => $relType2,
1199 'contact_id_b' => $this->_cId_b2,
1200 ))
1201 );
1202
1203 // Relationship 3.
1204 $this->callAPISuccess($this->_entity, 'create',
1205 array_merge($this->_params, array(
1206 'relationship_type_id' => $relType3,
1207 'contact_id_b' => $org3,
1208 ))
1209 );
1210
1211 $result = $this->callAPISuccess($this->_entity, 'get', array(
1212 'contact_id' => $this->_cId_a,
1213 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1214 ));
1215
1216 $this->assertEquals(2, $result['count']);
1217 foreach ($result['values'] as $key => $value) {
1218 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1219 }
1220 }
1221
1222 /**
1223 * Test relationship get by membership type.
1224 *
1225 * Checks that passing in 'contact_id_b' + a relationship type
1226 * will filter by relationship type for contact b
1227 *
1228 * We should get 1 result without or with correct relationship type id & 0 with
1229 * an incorrect one
1230 */
1231 public function testGetRelationshipByMembershipTypeDAO() {
1232 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1233 $org3 = $this->organizationCreate();
1234
1235 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1236 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1237 $relType1 = 1;
1238 $memberType = $this->membershipTypeCreate(array(
1239 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1240 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1241 ));
1242
1243 // Relationship 2.
1244 $this->callAPISuccess($this->_entity, 'create',
1245 array_merge($this->_params, array(
1246 'relationship_type_id' => $relType2,
1247 'contact_id_b' => $this->_cId_b2,
1248 ))
1249 );
1250
1251 // Relationship 3.
1252 $this->callAPISuccess($this->_entity, 'create',
1253 array_merge($this->_params, array(
1254 'relationship_type_id' => $relType3,
1255 'contact_id_b' => $org3,
1256 ))
1257 );
1258
1259 // Relationship 4 with reversal.
1260 $this->callAPISuccess($this->_entity, 'create',
1261 array_merge($this->_params, array(
1262 'relationship_type_id' => $relType1,
1263 'contact_id_a' => $this->_cId_a,
1264 'contact_id_b' => $this->_cId_a_2,
1265 ))
1266 );
1267
1268 $result = $this->callAPISuccess($this->_entity, 'get', array(
1269 'contact_id_a' => $this->_cId_a,
1270 'membership_type_id' => $memberType,
1271 ));
1272 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1273 $this->assertEquals(1, $result['count']);
1274 foreach ($result['values'] as $key => $value) {
1275 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1276 }
1277 }
1278
1279 /**
1280 * Checks that passing in 'contact_id_b' + a relationship type
1281 * will filter by relationship type for contact b
1282 *
1283 * We should get 1 result without or with correct relationship type id & 0 with
1284 * an incorrect one
1285 */
1286 public function testGetRelationshipByMembershipTypeReciprocal() {
1287 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1288 $org3 = $this->organizationCreate();
1289
1290 // Let's just assume built in ones aren't being messed with!
1291 $relType2 = 5;
1292 $relType3 = 6;
1293 $relType1 = 1;
1294 $memberType = $this->membershipTypeCreate(array(
1295 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1296 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1297 ));
1298
1299 // Relationship 2.
1300 $this->callAPISuccess($this->_entity, 'create',
1301 array_merge($this->_params, array(
1302 'relationship_type_id' => $relType2,
1303 'contact_id_b' => $this->_cId_b2,
1304 ))
1305 );
1306
1307 // Relationship 4.
1308 $this->callAPISuccess($this->_entity, 'create',
1309 array_merge($this->_params, array(
1310 'relationship_type_id' => $relType3,
1311 'contact_id_b' => $org3,
1312 ))
1313 );
1314
1315 // Relationship 4 with reversal.
1316 $this->callAPISuccess($this->_entity, 'create',
1317 array_merge($this->_params, array(
1318 'relationship_type_id' => $relType1,
1319 'contact_id_a' => $this->_cId_a,
1320 'contact_id_b' => $this->_cId_a_2,
1321 ))
1322 );
1323
1324 $result = $this->callAPISuccess($this->_entity, 'get', array(
1325 'contact_id' => $this->_cId_a,
1326 'membership_type_id' => $memberType,
1327 ));
1328 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1329 $this->assertEquals(2, $result['count']);
1330
1331 foreach ($result['values'] as $key => $value) {
1332 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1333 }
1334 }
1335
1336 /**
1337 * Check for e-notices on enable & disable as reported in CRM-14350
1338 */
1339 public function testSetActive() {
1340 $relationship = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1341 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1342 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1343 }
1344
1345 /**
1346 * Test creating related memberships.
1347 */
1348 public function testCreateRelatedMembership() {
1349 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1350 'name' => 'Membership with Related',
1351 'member_of_contact_id' => 1,
1352 'financial_type_id' => 1,
1353 'minimum_fee' => 0.00,
1354 'duration_unit' => 'year',
1355 'duration_interval' => 1,
1356 'period_type' => 'rolling',
1357 'relationship_type_id' => $this->_relTypeID,
1358 'relationship_direction' => 'b_a',
1359 'visibility' => 'Public',
1360 'auto_renew' => 0,
1361 'is_active' => 1,
1362 'domain_id' => CRM_Core_Config::domainID(),
1363 ));
1364 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1365 'membership_type_id' => $relatedMembershipType['id'],
1366 'contact_id' => $this->_cId_b,
1367 ));
1368 $this->callAPISuccess('Relationship', 'create', array(
1369 'relationship_type_id' => $this->_relTypeID,
1370 'contact_id_a' => $this->_cId_a,
1371 'contact_id_b' => $this->_cId_b,
1372 ));
1373 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a));
1374 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
1375
1376 // Adding a relationship with a future start date should NOT create a membership
1377 $this->callAPISuccess('Relationship', 'create', array(
1378 'relationship_type_id' => $this->_relTypeID,
1379 'contact_id_a' => $this->_cId_a_2,
1380 'contact_id_b' => $this->_cId_b,
1381 'start_date' => 'now + 1 week',
1382 ));
1383 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2), 0);
1384
1385 // Deleting the organization should cause the related membership to be deleted.
1386 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b));
1387 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a), 0);
1388 }
1389
1390 }