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