Merge pull request #10092 from jitendrapurohit/CRM-20339
[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-2017 |
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 * Check with valid params array.
783 * (The get function will behave differently without 'contact_id' passed
784 */
785 public function testRelationshipsGetGeneric() {
786 $relParams = array(
787 'contact_id_a' => $this->_cId_a,
788 'contact_id_b' => $this->_cId_b,
789 'relationship_type_id' => $this->_relTypeID,
790 'start_date' => '2011-01-01',
791 'end_date' => '2013-01-01',
792 'is_active' => 1,
793 );
794
795 $this->callAPISuccess('relationship', 'create', $relParams);
796
797 //get relationship
798 $params = array(
799 'contact_id_b' => $this->_cId_b,
800 );
801 $this->callAPISuccess('relationship', 'get', $params);
802 }
803
804 /**
805 * Test retrieving only current relationships.
806 */
807 public function testGetIsCurrent() {
808 $rel2Params = array(
809 'contact_id_a' => $this->_cId_a,
810 'contact_id_b' => $this->_cId_b2,
811 'relationship_type_id' => $this->_relTypeID,
812 'start_date' => '2008-12-20',
813 'is_active' => 0,
814 );
815 $this->callAPISuccess('relationship', 'create', $rel2Params);
816 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
817
818 $getParams = array(
819 'filters' => array('is_current' => 1),
820 );
821 $description = "Demonstrates is_current filter.";
822 $subfile = 'filterIsCurrent';
823 //no relationship has been created
824 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
825 $this->assertEquals($result['count'], 1);
826 $this->AssertEquals($rel1['id'], $result['id']);
827
828 // now try not started
829 $rel2Params['is_active'] = 1;
830 $rel2Params['start_date'] = 'tomorrow';
831 $this->callAPISuccess('relationship', 'create', $rel2Params);
832 $result = $this->callAPISuccess('relationship', 'get', $getParams);
833 $this->assertEquals($result['count'], 1);
834 $this->AssertEquals($rel1['id'], $result['id']);
835
836 // now try finished
837 $rel2Params['is_active'] = 1;
838 $rel2Params['start_date'] = 'last week';
839 $rel2Params['end_date'] = 'yesterday';
840 $this->callAPISuccess('relationship', 'create', $rel2Params);
841 }
842
843 /**
844 * Test using various operators.
845 */
846 public function testGetTypeOperators() {
847 $relTypeParams = array(
848 'name_a_b' => 'Relation 3 for delete',
849 'name_b_a' => 'Relation 6 for delete',
850 'description' => 'Testing relationship type 2',
851 'contact_type_a' => 'Individual',
852 'contact_type_b' => 'Organization',
853 'is_reserved' => 1,
854 'is_active' => 1,
855 );
856 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
857 $relTypeParams = array(
858 'name_a_b' => 'Relation 8 for delete',
859 'name_b_a' => 'Relation 9 for delete',
860 'description' => 'Testing relationship type 7',
861 'contact_type_a' => 'Individual',
862 'contact_type_b' => 'Organization',
863 'is_reserved' => 1,
864 'is_active' => 1,
865 );
866 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
867
868 $relTypeParams = array(
869 'name_a_b' => 'Relation 6 for delete',
870 'name_b_a' => 'Relation 88for delete',
871 'description' => 'Testing relationship type 00',
872 'contact_type_a' => 'Individual',
873 'contact_type_b' => 'Organization',
874 'is_reserved' => 1,
875 'is_active' => 1,
876 );
877 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
878
879 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
880 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
881 array('relationship_type_id' => $relationType2)));
882 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
883 array('relationship_type_id' => $relationType3)));
884 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
885 array('relationship_type_id' => $relationType4)));
886
887 $getParams = array(
888 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
889 );
890
891 $description = "Demonstrates use of IN filter.";
892 $subfile = 'INRelationshipType';
893
894 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
895 $this->assertEquals($result['count'], 2);
896 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
897
898 $description = "Demonstrates use of NOT IN filter.";
899 $subfile = 'NotInRelationshipType';
900 $getParams = array(
901 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
902 );
903 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
904 $this->assertEquals($result['count'], 2);
905 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
906
907 $description = "Demonstrates use of BETWEEN filter.";
908 $subfile = 'BetweenRelationshipType';
909 $getParams = array(
910 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
911 );
912 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
913 $this->assertEquals($result['count'], 3);
914 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
915
916 $description = "Demonstrates use of Not BETWEEN filter.";
917 $subfile = 'NotBetweenRelationshipType';
918 $getParams = array(
919 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
920 );
921 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
922 $this->assertEquals($result['count'], 1);
923 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
924
925 }
926
927 /**
928 * Check with invalid relationshipType Id.
929 */
930 public function testRelationshipTypeAddInvalidId() {
931 $relTypeParams = array(
932 'id' => 'invalid',
933 'name_a_b' => 'Relation 1 for delete',
934 'name_b_a' => 'Relation 2 for delete',
935 'contact_type_a' => 'Individual',
936 'contact_type_b' => 'Organization',
937 );
938 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
939 'id is not a valid integer');
940 }
941
942 /**
943 * Check with valid data with contact_b.
944 */
945 public function testGetRelationshipWithContactB() {
946 $relParams = array(
947 'contact_id_a' => $this->_cId_a,
948 'contact_id_b' => $this->_cId_b,
949 'relationship_type_id' => $this->_relTypeID,
950 'start_date' => '2011-01-01',
951 'end_date' => '2013-01-01',
952 'is_active' => 1,
953 );
954
955 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
956
957 $contacts = array(
958 'contact_id' => $this->_cId_a,
959 );
960
961 $result = $this->callAPISuccess('relationship', 'get', $contacts);
962 $this->assertGreaterThan(0, $result['count']);
963 $params = array(
964 'id' => $relationship['id'],
965 );
966 $this->callAPISuccess('relationship', 'delete', $params);
967 $this->relationshipTypeDelete($this->_relTypeID);
968 }
969
970 /**
971 * Check with valid data with relationshipTypes.
972 */
973 public function testGetRelationshipWithRelTypes() {
974 $relParams = array(
975 'contact_id_a' => $this->_cId_a,
976 'contact_id_b' => $this->_cId_b,
977 'relationship_type_id' => $this->_relTypeID,
978 'start_date' => '2011-01-01',
979 'end_date' => '2013-01-01',
980 'is_active' => 1,
981 );
982
983 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
984
985 $contact_a = array(
986 'contact_id' => $this->_cId_a,
987 );
988 $this->callAPISuccess('relationship', 'get', $contact_a);
989
990 $params = array(
991 'id' => $relationship['id'],
992 );
993 $this->callAPISuccess('relationship', 'delete', $params);
994 $this->relationshipTypeDelete($this->_relTypeID);
995 }
996
997 /**
998 * Checks that passing in 'contact_id' + a relationship type
999 * will filter by relationship type (relationships go in both directions)
1000 * as relationship api does a reciprocal check if contact_id provided
1001 *
1002 * We should get 1 result without or with correct relationship type id & 0 with
1003 * an incorrect one
1004 */
1005 public function testGetRelationshipByTypeReciprocal() {
1006 $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1007 $result = $this->callAPISuccess($this->_entity, 'get', array(
1008 'contact_id' => $this->_cId_a,
1009 'relationship_type_id' => $this->_relTypeID,
1010 ));
1011 $this->assertEquals(1, $result['count']);
1012 $result = $this->callAPISuccess($this->_entity, 'get', array(
1013 'contact_id' => $this->_cId_a,
1014 'relationship_type_id' => $this->_relTypeID + 1,
1015 ));
1016 $this->assertEquals(0, $result['count']);
1017 $this->callAPISuccess($this->_entity, 'delete', array('id' => $created['id']));
1018 }
1019
1020 /**
1021 * Checks that passing in 'contact_id_b' + a relationship type
1022 * will filter by relationship type for contact b
1023 *
1024 * We should get 1 result without or with correct relationship type id & 0 with
1025 * an incorrect one
1026 */
1027 public function testGetRelationshipByTypeDAO() {
1028 $this->_ids['relationship'] = $this->callAPISuccess($this->_entity, 'create', array('format.only_id' => TRUE) +
1029 $this->_params);
1030 $this->callAPISuccess($this->_entity, 'getcount', array(
1031 'contact_id_a' => $this->_cId_a,
1032 ),
1033 1);
1034 $result = $this->callAPISuccess($this->_entity, 'get', array(
1035 'contact_id_a' => $this->_cId_a,
1036 'relationship_type_id' => $this->_relTypeID,
1037 ));
1038 $this->assertEquals(1, $result['count']);
1039 $result = $this->callAPISuccess($this->_entity, 'get', array(
1040 'contact_id_a' => $this->_cId_a,
1041 'relationship_type_id' => $this->_relTypeID + 1,
1042 ));
1043 $this->assertEquals(0, $result['count']);
1044 }
1045
1046 /**
1047 * Checks that passing in 'contact_id_b' + a relationship type
1048 * will filter by relationship type for contact b
1049 *
1050 * We should get 1 result without or with correct relationship type id & 0 with
1051 * an incorrect one
1052 */
1053 public function testGetRelationshipByTypeArrayDAO() {
1054 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1055 $org3 = $this->organizationCreate();
1056 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1057 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1058
1059 // Relationship 2.
1060 $this->callAPISuccess($this->_entity, 'create',
1061 array_merge($this->_params, array(
1062 'relationship_type_id' => $relType2,
1063 'contact_id_b' => $this->_cId_b2,
1064 ))
1065 );
1066
1067 // Relationship 3.
1068 $this->callAPISuccess($this->_entity, 'create',
1069 array_merge($this->_params, array(
1070 'relationship_type_id' => $relType3,
1071 'contact_id_b' => $org3,
1072 ))
1073 );
1074
1075 $result = $this->callAPISuccess($this->_entity, 'get', array(
1076 'contact_id_a' => $this->_cId_a,
1077 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1078 ));
1079
1080 $this->assertEquals(2, $result['count']);
1081 foreach ($result['values'] as $key => $value) {
1082 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1083 }
1084 }
1085
1086 /**
1087 * Checks that passing in 'contact_id_b' + a relationship type
1088 * will filter by relationship type for contact b
1089 *
1090 * We should get 1 result without or with correct relationship type id & 0 with
1091 * an incorrect one
1092 */
1093 public function testGetRelationshipByTypeArrayReciprocal() {
1094 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1095 $org3 = $this->organizationCreate();
1096 // lets just assume built in ones aren't being messed with!
1097 $relType2 = 5;
1098 $relType3 = 6;
1099
1100 // Relationship 2.
1101 $this->callAPISuccess($this->_entity, 'create',
1102 array_merge($this->_params, array(
1103 'relationship_type_id' => $relType2,
1104 'contact_id_b' => $this->_cId_b2,
1105 ))
1106 );
1107
1108 // Relationship 3.
1109 $this->callAPISuccess($this->_entity, 'create',
1110 array_merge($this->_params, array(
1111 'relationship_type_id' => $relType3,
1112 'contact_id_b' => $org3,
1113 ))
1114 );
1115
1116 $result = $this->callAPISuccess($this->_entity, 'get', array(
1117 'contact_id' => $this->_cId_a,
1118 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1119 ));
1120
1121 $this->assertEquals(2, $result['count']);
1122 foreach ($result['values'] as $key => $value) {
1123 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1124 }
1125 }
1126
1127 /**
1128 * Test relationship get by membership type.
1129 *
1130 * Checks that passing in 'contact_id_b' + a relationship type
1131 * will filter by relationship type for contact b
1132 *
1133 * We should get 1 result without or with correct relationship type id & 0 with
1134 * an incorrect one
1135 */
1136 public function testGetRelationshipByMembershipTypeDAO() {
1137 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1138 $org3 = $this->organizationCreate();
1139
1140 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1141 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1142 $relType1 = 1;
1143 $memberType = $this->membershipTypeCreate(array(
1144 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1145 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1146 ));
1147
1148 // Relationship 2.
1149 $this->callAPISuccess($this->_entity, 'create',
1150 array_merge($this->_params, array(
1151 'relationship_type_id' => $relType2,
1152 'contact_id_b' => $this->_cId_b2,
1153 ))
1154 );
1155
1156 // Relationship 3.
1157 $this->callAPISuccess($this->_entity, 'create',
1158 array_merge($this->_params, array(
1159 'relationship_type_id' => $relType3,
1160 'contact_id_b' => $org3,
1161 ))
1162 );
1163
1164 // Relationship 4 with reversal.
1165 $this->callAPISuccess($this->_entity, 'create',
1166 array_merge($this->_params, array(
1167 'relationship_type_id' => $relType1,
1168 'contact_id_a' => $this->_cId_a,
1169 'contact_id_b' => $this->_cId_a_2,
1170 ))
1171 );
1172
1173 $result = $this->callAPISuccess($this->_entity, 'get', array(
1174 'contact_id_a' => $this->_cId_a,
1175 'membership_type_id' => $memberType,
1176 ));
1177 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1178 $this->assertEquals(1, $result['count']);
1179 foreach ($result['values'] as $key => $value) {
1180 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1181 }
1182 }
1183
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 testGetRelationshipByMembershipTypeReciprocal() {
1192 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1193 $org3 = $this->organizationCreate();
1194
1195 // Let's just assume built in ones aren't being messed with!
1196 $relType2 = 5;
1197 $relType3 = 6;
1198 $relType1 = 1;
1199 $memberType = $this->membershipTypeCreate(array(
1200 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1201 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1202 ));
1203
1204 // Relationship 2.
1205 $this->callAPISuccess($this->_entity, 'create',
1206 array_merge($this->_params, array(
1207 'relationship_type_id' => $relType2,
1208 'contact_id_b' => $this->_cId_b2,
1209 ))
1210 );
1211
1212 // Relationship 4.
1213 $this->callAPISuccess($this->_entity, 'create',
1214 array_merge($this->_params, array(
1215 'relationship_type_id' => $relType3,
1216 'contact_id_b' => $org3,
1217 ))
1218 );
1219
1220 // Relationship 4 with reversal.
1221 $this->callAPISuccess($this->_entity, 'create',
1222 array_merge($this->_params, array(
1223 'relationship_type_id' => $relType1,
1224 'contact_id_a' => $this->_cId_a,
1225 'contact_id_b' => $this->_cId_a_2,
1226 ))
1227 );
1228
1229 $result = $this->callAPISuccess($this->_entity, 'get', array(
1230 'contact_id' => $this->_cId_a,
1231 'membership_type_id' => $memberType,
1232 ));
1233 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1234 $this->assertEquals(2, $result['count']);
1235
1236 foreach ($result['values'] as $key => $value) {
1237 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1238 }
1239 }
1240
1241 /**
1242 * Check for e-notices on enable & disable as reported in CRM-14350
1243 */
1244 public function testSetActive() {
1245 $relationship = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1246 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1247 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1248 }
1249
1250 /**
1251 * Test creating related memberships.
1252 */
1253 public function testCreateRelatedMembership() {
1254 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1255 'name' => 'Membership with Related',
1256 'member_of_contact_id' => 1,
1257 'financial_type_id' => 1,
1258 'minimum_fee' => 0.00,
1259 'duration_unit' => 'year',
1260 'duration_interval' => 1,
1261 'period_type' => 'rolling',
1262 'relationship_type_id' => $this->_relTypeID,
1263 'relationship_direction' => 'b_a',
1264 'visibility' => 'Public',
1265 'auto_renew' => 0,
1266 'is_active' => 1,
1267 'domain_id' => CRM_Core_Config::domainID(),
1268 ));
1269 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1270 'membership_type_id' => $relatedMembershipType['id'],
1271 'contact_id' => $this->_cId_b,
1272 ));
1273 $this->callAPISuccess('Relationship', 'create', array(
1274 'relationship_type_id' => $this->_relTypeID,
1275 'contact_id_a' => $this->_cId_a,
1276 'contact_id_b' => $this->_cId_b,
1277 ));
1278 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a));
1279 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
1280
1281 // Adding a relationship with a future start date should NOT create a membership
1282 $this->callAPISuccess('Relationship', 'create', array(
1283 'relationship_type_id' => $this->_relTypeID,
1284 'contact_id_a' => $this->_cId_a_2,
1285 'contact_id_b' => $this->_cId_b,
1286 'start_date' => 'now + 1 week',
1287 ));
1288 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2), 0);
1289
1290 // Deleting the organization should cause the related membership to be deleted.
1291 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b));
1292 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a), 0);
1293 }
1294
1295 }