Merge pull request #7797 from JKingsnorth/CRM-17977
[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-2016 |
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, 'Relationship already exists');
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 testRelationshipCreateWithCustomData() {
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 $params['id'] = $result['id'];
386 $this->callAPISuccess('relationship', 'delete', $params);
387 $this->relationshipTypeDelete($this->_relTypeID);
388 }
389
390 /**
391 * Check with complete array + custom field
392 * Note that the test is written on purpose without any
393 * variables specific to participant so it can be replicated into other entities
394 * and / or moved to the automated test suite
395 */
396 public function testGetWithCustom() {
397 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
398
399 $params = $this->_params;
400 $params['custom_' . $ids['custom_field_id']] = "custom string";
401
402 $result = $this->callAPISuccess($this->_entity, 'create', $params);
403 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
404
405 $getParams = array('id' => $result['id']);
406 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
407 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
408
409 $this->customFieldDelete($ids['custom_field_id']);
410 $this->customGroupDelete($ids['custom_group_id']);
411 }
412
413 /**
414 * @return mixed
415 */
416 public function createCustomGroup() {
417 $params = array(
418 'title' => 'Custom Group',
419 'extends' => array('Relationship'),
420 'weight' => 5,
421 'style' => 'Inline',
422 'is_active' => 1,
423 'max_multiple' => 0,
424 );
425 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
426 $this->_customGroupId = $customGroup['id'];
427 return $customGroup['id'];
428 }
429
430 /**
431 * @return array
432 */
433 public function createCustomField() {
434 $ids = array();
435 $params = array(
436 'custom_group_id' => $this->_customGroupId,
437 'label' => 'Enter text about relationship',
438 'html_type' => 'Text',
439 'data_type' => 'String',
440 'default_value' => 'xyz',
441 'weight' => 1,
442 'is_required' => 1,
443 'is_searchable' => 0,
444 'is_active' => 1,
445 );
446
447 $this->callAPISuccess('CustomField', 'create', $params);
448
449 $customField = NULL;
450 $ids[] = $customField['result']['customFieldId'];
451
452 $optionValue[] = array(
453 'label' => 'Red',
454 'value' => 'R',
455 'weight' => 1,
456 'is_active' => 1,
457 );
458 $optionValue[] = array(
459 'label' => 'Yellow',
460 'value' => 'Y',
461 'weight' => 2,
462 'is_active' => 1,
463 );
464 $optionValue[] = array(
465 'label' => 'Green',
466 'value' => 'G',
467 'weight' => 3,
468 'is_active' => 1,
469 );
470
471 $params = array(
472 'label' => 'Pick Color',
473 'html_type' => 'Select',
474 'data_type' => 'String',
475 'weight' => 2,
476 'is_required' => 1,
477 'is_searchable' => 0,
478 'is_active' => 1,
479 'option_values' => $optionValue,
480 'custom_group_id' => $this->_customGroupId,
481 );
482
483 $customField = $this->callAPISuccess('custom_field', 'create', $params);
484 $ids[] = $customField['id'];
485
486 $params = array(
487 'custom_group_id' => $this->_customGroupId,
488 'name' => 'test_date',
489 'label' => 'test_date',
490 'html_type' => 'Select Date',
491 'data_type' => 'Date',
492 'default_value' => '20090711',
493 'weight' => 3,
494 'is_required' => 1,
495 'is_searchable' => 0,
496 'is_active' => 1,
497 );
498
499 $customField = $this->callAPISuccess('custom_field', 'create', $params);
500
501 $ids[] = $customField['id'];
502 $params = array(
503 'custom_group_id' => $this->_customGroupId,
504 'name' => 'test_link',
505 'label' => 'test_link',
506 'html_type' => 'Link',
507 'data_type' => 'Link',
508 'default_value' => 'http://civicrm.org',
509 'weight' => 4,
510 'is_required' => 1,
511 'is_searchable' => 0,
512 'is_active' => 1,
513 );
514
515 $customField = $this->callAPISuccess('custom_field', 'create', $params);
516 $ids[] = $customField['id'];
517 return $ids;
518 }
519
520 /**
521 * Check with empty array.
522 */
523 public function testRelationshipDeleteEmpty() {
524 $this->callAPIFailure('relationship', 'delete', array(), 'Mandatory key(s) missing from params array: id');
525 }
526
527 /**
528 * Check if required fields are not passed.
529 */
530 public function testRelationshipDeleteWithoutRequired() {
531 $params = array(
532 'start_date' => '2008-12-20',
533 'end_date' => '2009-12-20',
534 'is_active' => 1,
535 );
536
537 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
538 }
539
540 /**
541 * Check with incorrect required fields.
542 */
543 public function testRelationshipDeleteWithIncorrectData() {
544 $params = array(
545 'contact_id_a' => $this->_cId_a,
546 'contact_id_b' => $this->_cId_b,
547 'relationship_type_id' => 'Breaking Relationship',
548 );
549
550 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
551
552 $params['id'] = "Invalid";
553 $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
554 }
555
556 /**
557 * Check relationship creation.
558 */
559 public function testRelationshipDelete() {
560 $params = array(
561 'contact_id_a' => $this->_cId_a,
562 'contact_id_b' => $this->_cId_b,
563 'relationship_type_id' => $this->_relTypeID,
564 'start_date' => '2008-12-20',
565 'is_active' => 1,
566 );
567
568 $result = $this->callAPISuccess('relationship', 'create', $params);
569 $params = array('id' => $result['id']);
570 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__, __FILE__);
571 $this->relationshipTypeDelete($this->_relTypeID);
572 }
573
574 ///////////////// civicrm_relationship_update methods
575
576 /**
577 * Check with empty array.
578 */
579 public function testRelationshipUpdateEmpty() {
580 $this->callAPIFailure('relationship', 'create', array(),
581 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
582 }
583
584 /**
585 * Check if required fields are not passed.
586 */
587
588 /**
589 * Check relationship update.
590 */
591 public function testRelationshipCreateDuplicate() {
592 $relParams = array(
593 'contact_id_a' => $this->_cId_a,
594 'contact_id_b' => $this->_cId_b,
595 'relationship_type_id' => $this->_relTypeID,
596 'start_date' => '20081214',
597 'end_date' => '20091214',
598 'is_active' => 1,
599 );
600
601 $result = $this->callAPISuccess('relationship', 'create', $relParams);
602
603 $this->assertNotNull($result['id']);
604
605 $params = 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' => 0,
612 );
613
614 $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
615
616 $this->callAPISuccess('relationship', 'delete', array('id' => $result['id']));
617 $this->relationshipTypeDelete($this->_relTypeID);
618 }
619
620 /**
621 * CRM-13725 - Two relationships of same type with same start and end date
622 * should be OK if the custom field values differ.
623 */
624 public function testRelationshipCreateDuplicateWithCustomFields() {
625 $this->createCustomGroup();
626 $this->_ids = $this->createCustomField();
627
628 $custom_params_1 = array(
629 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
630 "custom_{$this->_ids[1]}" => 'Y',
631 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
632 "custom_{$this->_ids[3]}" => 'http://example.com',
633 );
634
635 $custom_params_2 = array(
636 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
637 "custom_{$this->_ids[1]}" => 'Y',
638 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
639 "custom_{$this->_ids[3]}" => 'http://example.org',
640 );
641
642 $params = array(
643 'contact_id_a' => $this->_cId_a,
644 'contact_id_b' => $this->_cId_b,
645 'relationship_type_id' => $this->_relTypeID,
646 'start_date' => '2008-12-20',
647 'is_active' => 1,
648 );
649
650 $params_1 = array_merge($params, $custom_params_1);
651 $params_2 = array_merge($params, $custom_params_2);
652
653 $result_1 = $this->callAPISuccess('relationship', 'create', $params_1);
654 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
655
656 $this->assertNotNull($result_2['id']);
657 $this->assertEquals(0, $result_2['is_error']);
658
659 $this->relationshipTypeDelete($this->_relTypeID);
660 }
661
662 /**
663 * CRM-13725 - Two relationships of same type with same start and end date
664 * should be OK if the custom field values differ. In this case, the
665 * existing relationship does not have custom values, but the new one
666 * does.
667 */
668 public function testRelationshipCreateDuplicateWithCustomFields2() {
669 $this->createCustomGroup();
670 $this->_ids = $this->createCustomField();
671
672 $custom_params_2 = array(
673 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
674 "custom_{$this->_ids[1]}" => 'Y',
675 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
676 "custom_{$this->_ids[3]}" => 'http://example.org',
677 );
678
679 $params_1 = array(
680 'contact_id_a' => $this->_cId_a,
681 'contact_id_b' => $this->_cId_b,
682 'relationship_type_id' => $this->_relTypeID,
683 'start_date' => '2008-12-20',
684 'is_active' => 1,
685 );
686
687 $params_2 = array_merge($params_1, $custom_params_2);
688
689 $this->callAPISuccess('relationship', 'create', $params_1);
690 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
691
692 $this->assertNotNull($result_2['id']);
693 $this->assertEquals(0, $result_2['is_error']);
694
695 $this->relationshipTypeDelete($this->_relTypeID);
696 }
697
698 /**
699 * CRM-13725 - Two relationships of same type with same start and end date
700 * should be OK if the custom field values differ. In this case, the
701 * existing relationship does have custom values, but the new one
702 * does not.
703 */
704 public function testRelationshipCreateDuplicateWithCustomFields3() {
705 $this->createCustomGroup();
706 $this->_ids = $this->createCustomField();
707
708 $custom_params_1 = array(
709 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
710 "custom_{$this->_ids[1]}" => 'Y',
711 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
712 "custom_{$this->_ids[3]}" => 'http://example.org',
713 );
714
715 $params_2 = array(
716 'contact_id_a' => $this->_cId_a,
717 'contact_id_b' => $this->_cId_b,
718 'relationship_type_id' => $this->_relTypeID,
719 'start_date' => '2008-12-20',
720 'is_active' => 1,
721 );
722
723 $params_1 = array_merge($params_2, $custom_params_1);
724
725 $this->callAPISuccess('relationship', 'create', $params_1);
726 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
727
728 $this->assertNotNull($result_2['id']);
729 $this->assertEquals(0, $result_2['is_error']);
730
731 $this->relationshipTypeDelete($this->_relTypeID);
732 }
733
734 /**
735 * Check with valid params array.
736 */
737 public function testRelationshipsGet() {
738 $relParams = array(
739 'contact_id_a' => $this->_cId_a,
740 'contact_id_b' => $this->_cId_b,
741 'relationship_type_id' => $this->_relTypeID,
742 'start_date' => '2011-01-01',
743 'end_date' => '2013-01-01',
744 'is_active' => 1,
745 );
746
747 $this->callAPISuccess('relationship', 'create', $relParams);
748
749 //get relationship
750 $params = array(
751 'contact_id' => $this->_cId_b,
752 );
753 $result = $this->callAPISuccess('relationship', 'get', $params);
754 $this->assertEquals($result['count'], 1);
755 $params = array(
756 'contact_id_a' => $this->_cId_a,
757 );
758 $result = $this->callAPISuccess('relationship', 'get', $params);
759 $this->assertEquals($result['count'], 1);
760 // contact_id_a is wrong so should be no matches
761 $params = array(
762 'contact_id_a' => $this->_cId_b,
763 );
764 $result = $this->callAPISuccess('relationship', 'get', $params);
765 $this->assertEquals($result['count'], 0);
766 }
767
768 /**
769 * Check with valid params array.
770 * (The get function will behave differently without 'contact_id' passed
771 */
772 public function testRelationshipsGetGeneric() {
773 $relParams = array(
774 'contact_id_a' => $this->_cId_a,
775 'contact_id_b' => $this->_cId_b,
776 'relationship_type_id' => $this->_relTypeID,
777 'start_date' => '2011-01-01',
778 'end_date' => '2013-01-01',
779 'is_active' => 1,
780 );
781
782 $this->callAPISuccess('relationship', 'create', $relParams);
783
784 //get relationship
785 $params = array(
786 'contact_id_b' => $this->_cId_b,
787 );
788 $this->callAPISuccess('relationship', 'get', $params);
789 }
790
791 /**
792 * Test retrieving only current relationships.
793 */
794 public function testGetIsCurrent() {
795 $rel2Params = array(
796 'contact_id_a' => $this->_cId_a,
797 'contact_id_b' => $this->_cId_b2,
798 'relationship_type_id' => $this->_relTypeID,
799 'start_date' => '2008-12-20',
800 'is_active' => 0,
801 );
802 $this->callAPISuccess('relationship', 'create', $rel2Params);
803 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
804
805 $getParams = array(
806 'filters' => array('is_current' => 1),
807 );
808 $description = "Demonstrates is_current filter.";
809 $subfile = 'filterIsCurrent';
810 //no relationship has been created
811 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
812 $this->assertEquals($result['count'], 1);
813 $this->AssertEquals($rel1['id'], $result['id']);
814
815 // now try not started
816 $rel2Params['is_active'] = 1;
817 $rel2Params['start_date'] = 'tomorrow';
818 $this->callAPISuccess('relationship', 'create', $rel2Params);
819 $result = $this->callAPISuccess('relationship', 'get', $getParams);
820 $this->assertEquals($result['count'], 1);
821 $this->AssertEquals($rel1['id'], $result['id']);
822
823 // now try finished
824 $rel2Params['is_active'] = 1;
825 $rel2Params['start_date'] = 'last week';
826 $rel2Params['end_date'] = 'yesterday';
827 $this->callAPISuccess('relationship', 'create', $rel2Params);
828 }
829
830 /**
831 * Test using various operators.
832 */
833 public function testGetTypeOperators() {
834 $relTypeParams = array(
835 'name_a_b' => 'Relation 3 for delete',
836 'name_b_a' => 'Relation 6 for delete',
837 'description' => 'Testing relationship type 2',
838 'contact_type_a' => 'Individual',
839 'contact_type_b' => 'Organization',
840 'is_reserved' => 1,
841 'is_active' => 1,
842 );
843 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
844 $relTypeParams = array(
845 'name_a_b' => 'Relation 8 for delete',
846 'name_b_a' => 'Relation 9 for delete',
847 'description' => 'Testing relationship type 7',
848 'contact_type_a' => 'Individual',
849 'contact_type_b' => 'Organization',
850 'is_reserved' => 1,
851 'is_active' => 1,
852 );
853 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
854
855 $relTypeParams = array(
856 'name_a_b' => 'Relation 6 for delete',
857 'name_b_a' => 'Relation 88for delete',
858 'description' => 'Testing relationship type 00',
859 'contact_type_a' => 'Individual',
860 'contact_type_b' => 'Organization',
861 'is_reserved' => 1,
862 'is_active' => 1,
863 );
864 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
865
866 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
867 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
868 array('relationship_type_id' => $relationType2)));
869 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
870 array('relationship_type_id' => $relationType3)));
871 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
872 array('relationship_type_id' => $relationType4)));
873
874 $getParams = array(
875 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
876 );
877
878 $description = "Demonstrates use of IN filter.";
879 $subfile = 'INRelationshipType';
880
881 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
882 $this->assertEquals($result['count'], 2);
883 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
884
885 $description = "Demonstrates use of NOT IN filter.";
886 $subfile = 'NotInRelationshipType';
887 $getParams = array(
888 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
889 );
890 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
891 $this->assertEquals($result['count'], 2);
892 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
893
894 $description = "Demonstrates use of BETWEEN filter.";
895 $subfile = 'BetweenRelationshipType';
896 $getParams = array(
897 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
898 );
899 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
900 $this->assertEquals($result['count'], 3);
901 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
902
903 $description = "Demonstrates use of Not BETWEEN filter.";
904 $subfile = 'NotBetweenRelationshipType';
905 $getParams = array(
906 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
907 );
908 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
909 $this->assertEquals($result['count'], 1);
910 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
911
912 }
913
914 /**
915 * Check with invalid relationshipType Id.
916 */
917 public function testRelationshipTypeAddInvalidId() {
918 $relTypeParams = array(
919 'id' => 'invalid',
920 'name_a_b' => 'Relation 1 for delete',
921 'name_b_a' => 'Relation 2 for delete',
922 'contact_type_a' => 'Individual',
923 'contact_type_b' => 'Organization',
924 );
925 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
926 'id is not a valid integer');
927 }
928
929 /**
930 * Check with valid data with contact_b.
931 */
932 public function testGetRelationshipWithContactB() {
933 $relParams = array(
934 'contact_id_a' => $this->_cId_a,
935 'contact_id_b' => $this->_cId_b,
936 'relationship_type_id' => $this->_relTypeID,
937 'start_date' => '2011-01-01',
938 'end_date' => '2013-01-01',
939 'is_active' => 1,
940 );
941
942 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
943
944 $contacts = array(
945 'contact_id' => $this->_cId_a,
946 );
947
948 $result = $this->callAPISuccess('relationship', 'get', $contacts);
949 $this->assertGreaterThan(0, $result['count']);
950 $params = array(
951 'id' => $relationship['id'],
952 );
953 $this->callAPISuccess('relationship', 'delete', $params);
954 $this->relationshipTypeDelete($this->_relTypeID);
955 }
956
957 /**
958 * Check with valid data with relationshipTypes.
959 */
960 public function testGetRelationshipWithRelTypes() {
961 $relParams = array(
962 'contact_id_a' => $this->_cId_a,
963 'contact_id_b' => $this->_cId_b,
964 'relationship_type_id' => $this->_relTypeID,
965 'start_date' => '2011-01-01',
966 'end_date' => '2013-01-01',
967 'is_active' => 1,
968 );
969
970 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
971
972 $contact_a = array(
973 'contact_id' => $this->_cId_a,
974 );
975 $this->callAPISuccess('relationship', 'get', $contact_a);
976
977 $params = array(
978 'id' => $relationship['id'],
979 );
980 $this->callAPISuccess('relationship', 'delete', $params);
981 $this->relationshipTypeDelete($this->_relTypeID);
982 }
983
984 /**
985 * Checks that passing in 'contact_id' + a relationship type
986 * will filter by relationship type (relationships go in both directions)
987 * as relationship api does a reciprocal check if contact_id provided
988 *
989 * We should get 1 result without or with correct relationship type id & 0 with
990 * an incorrect one
991 */
992 public function testGetRelationshipByTypeReciprocal() {
993 $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
994 $result = $this->callAPISuccess($this->_entity, 'get', array(
995 'contact_id' => $this->_cId_a,
996 'relationship_type_id' => $this->_relTypeID,
997 ));
998 $this->assertEquals(1, $result['count']);
999 $result = $this->callAPISuccess($this->_entity, 'get', array(
1000 'contact_id' => $this->_cId_a,
1001 'relationship_type_id' => $this->_relTypeID + 1,
1002 ));
1003 $this->assertEquals(0, $result['count']);
1004 $this->callAPISuccess($this->_entity, 'delete', array('id' => $created['id']));
1005 }
1006
1007 /**
1008 * Checks that passing in 'contact_id_b' + a relationship type
1009 * will filter by relationship type for contact b
1010 *
1011 * We should get 1 result without or with correct relationship type id & 0 with
1012 * an incorrect one
1013 */
1014 public function testGetRelationshipByTypeDAO() {
1015 $this->_ids['relationship'] = $this->callAPISuccess($this->_entity, 'create', array('format.only_id' => TRUE) +
1016 $this->_params);
1017 $this->callAPISuccess($this->_entity, 'getcount', array(
1018 'contact_id_a' => $this->_cId_a,
1019 ),
1020 1);
1021 $result = $this->callAPISuccess($this->_entity, 'get', array(
1022 'contact_id_a' => $this->_cId_a,
1023 'relationship_type_id' => $this->_relTypeID,
1024 ));
1025 $this->assertEquals(1, $result['count']);
1026 $result = $this->callAPISuccess($this->_entity, 'get', array(
1027 'contact_id_a' => $this->_cId_a,
1028 'relationship_type_id' => $this->_relTypeID + 1,
1029 ));
1030 $this->assertEquals(0, $result['count']);
1031 }
1032
1033 /**
1034 * Checks that passing in 'contact_id_b' + a relationship type
1035 * will filter by relationship type for contact b
1036 *
1037 * We should get 1 result without or with correct relationship type id & 0 with
1038 * an incorrect one
1039 */
1040 public function testGetRelationshipByTypeArrayDAO() {
1041 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1042 $org3 = $this->organizationCreate();
1043 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1044 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1045
1046 // Relationship 2.
1047 $this->callAPISuccess($this->_entity, 'create',
1048 array_merge($this->_params, array(
1049 'relationship_type_id' => $relType2,
1050 'contact_id_b' => $this->_cId_b2,
1051 ))
1052 );
1053
1054 // Relationship 3.
1055 $this->callAPISuccess($this->_entity, 'create',
1056 array_merge($this->_params, array(
1057 'relationship_type_id' => $relType3,
1058 'contact_id_b' => $org3,
1059 ))
1060 );
1061
1062 $result = $this->callAPISuccess($this->_entity, 'get', array(
1063 'contact_id_a' => $this->_cId_a,
1064 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1065 ));
1066
1067 $this->assertEquals(2, $result['count']);
1068 foreach ($result['values'] as $key => $value) {
1069 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1070 }
1071 }
1072
1073 /**
1074 * Checks that passing in 'contact_id_b' + a relationship type
1075 * will filter by relationship type for contact b
1076 *
1077 * We should get 1 result without or with correct relationship type id & 0 with
1078 * an incorrect one
1079 */
1080 public function testGetRelationshipByTypeArrayReciprocal() {
1081 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1082 $org3 = $this->organizationCreate();
1083 // lets just assume built in ones aren't being messed with!
1084 $relType2 = 5;
1085 $relType3 = 6;
1086
1087 // Relationship 2.
1088 $this->callAPISuccess($this->_entity, 'create',
1089 array_merge($this->_params, array(
1090 'relationship_type_id' => $relType2,
1091 'contact_id_b' => $this->_cId_b2,
1092 ))
1093 );
1094
1095 // Relationship 3.
1096 $this->callAPISuccess($this->_entity, 'create',
1097 array_merge($this->_params, array(
1098 'relationship_type_id' => $relType3,
1099 'contact_id_b' => $org3,
1100 ))
1101 );
1102
1103 $result = $this->callAPISuccess($this->_entity, 'get', array(
1104 'contact_id' => $this->_cId_a,
1105 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1106 ));
1107
1108 $this->assertEquals(2, $result['count']);
1109 foreach ($result['values'] as $key => $value) {
1110 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1111 }
1112 }
1113
1114 /**
1115 * Test relationship get by membership type.
1116 *
1117 * Checks that passing in 'contact_id_b' + a relationship type
1118 * will filter by relationship type for contact b
1119 *
1120 * We should get 1 result without or with correct relationship type id & 0 with
1121 * an incorrect one
1122 */
1123 public function testGetRelationshipByMembershipTypeDAO() {
1124 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1125 $org3 = $this->organizationCreate();
1126
1127 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1128 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1129 $relType1 = 1;
1130 $memberType = $this->membershipTypeCreate(array(
1131 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1132 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1133 ));
1134
1135 // Relationship 2.
1136 $this->callAPISuccess($this->_entity, 'create',
1137 array_merge($this->_params, array(
1138 'relationship_type_id' => $relType2,
1139 'contact_id_b' => $this->_cId_b2,
1140 ))
1141 );
1142
1143 // Relationship 3.
1144 $this->callAPISuccess($this->_entity, 'create',
1145 array_merge($this->_params, array(
1146 'relationship_type_id' => $relType3,
1147 'contact_id_b' => $org3,
1148 ))
1149 );
1150
1151 // Relationship 4 with reversal.
1152 $this->callAPISuccess($this->_entity, 'create',
1153 array_merge($this->_params, array(
1154 'relationship_type_id' => $relType1,
1155 'contact_id_a' => $this->_cId_a,
1156 'contact_id_b' => $this->_cId_a_2,
1157 ))
1158 );
1159
1160 $result = $this->callAPISuccess($this->_entity, 'get', array(
1161 'contact_id_a' => $this->_cId_a,
1162 'membership_type_id' => $memberType,
1163 ));
1164 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1165 $this->assertEquals(1, $result['count']);
1166 foreach ($result['values'] as $key => $value) {
1167 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1168 }
1169 }
1170
1171 /**
1172 * Checks that passing in 'contact_id_b' + a relationship type
1173 * will filter by relationship type for contact b
1174 *
1175 * We should get 1 result without or with correct relationship type id & 0 with
1176 * an incorrect one
1177 */
1178 public function testGetRelationshipByMembershipTypeReciprocal() {
1179 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1180 $org3 = $this->organizationCreate();
1181
1182 // Let's just assume built in ones aren't being messed with!
1183 $relType2 = 5;
1184 $relType3 = 6;
1185 $relType1 = 1;
1186 $memberType = $this->membershipTypeCreate(array(
1187 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1188 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1189 ));
1190
1191 // Relationship 2.
1192 $this->callAPISuccess($this->_entity, 'create',
1193 array_merge($this->_params, array(
1194 'relationship_type_id' => $relType2,
1195 'contact_id_b' => $this->_cId_b2,
1196 ))
1197 );
1198
1199 // Relationship 4.
1200 $this->callAPISuccess($this->_entity, 'create',
1201 array_merge($this->_params, array(
1202 'relationship_type_id' => $relType3,
1203 'contact_id_b' => $org3,
1204 ))
1205 );
1206
1207 // Relationship 4 with reversal.
1208 $this->callAPISuccess($this->_entity, 'create',
1209 array_merge($this->_params, array(
1210 'relationship_type_id' => $relType1,
1211 'contact_id_a' => $this->_cId_a,
1212 'contact_id_b' => $this->_cId_a_2,
1213 ))
1214 );
1215
1216 $result = $this->callAPISuccess($this->_entity, 'get', array(
1217 'contact_id' => $this->_cId_a,
1218 'membership_type_id' => $memberType,
1219 ));
1220 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1221 $this->assertEquals(2, $result['count']);
1222
1223 foreach ($result['values'] as $key => $value) {
1224 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1225 }
1226 }
1227
1228 /**
1229 * Check for e-notices on enable & disable as reported in CRM-14350
1230 */
1231 public function testSetActive() {
1232 $relationship = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1233 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1234 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1235 }
1236
1237 /**
1238 * Test creating related memberships.
1239 */
1240 public function testCreateRelatedMembership() {
1241 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1242 'name' => 'Membership with Related',
1243 'member_of_contact_id' => 1,
1244 'financial_type_id' => 1,
1245 'minimum_fee' => 0.00,
1246 'duration_unit' => 'year',
1247 'duration_interval' => 1,
1248 'period_type' => 'rolling',
1249 'relationship_type_id' => $this->_relTypeID,
1250 'relationship_direction' => 'b_a',
1251 'visibility' => 'Public',
1252 'auto_renew' => 0,
1253 'is_active' => 1,
1254 'domain_id' => CRM_Core_Config::domainID(),
1255 ));
1256 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1257 'membership_type_id' => $relatedMembershipType['id'],
1258 'contact_id' => $this->_cId_b,
1259 ));
1260 $this->callAPISuccess('Relationship', 'create', array(
1261 'relationship_type_id' => $this->_relTypeID,
1262 'contact_id_a' => $this->_cId_a,
1263 'contact_id_b' => $this->_cId_b,
1264 ));
1265 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a));
1266 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
1267
1268 // Adding a relationship with a future start date should NOT create a membership
1269 $this->callAPISuccess('Relationship', 'create', array(
1270 'relationship_type_id' => $this->_relTypeID,
1271 'contact_id_a' => $this->_cId_a_2,
1272 'contact_id_b' => $this->_cId_b,
1273 'start_date' => 'now + 1 week',
1274 ));
1275 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2), 0);
1276
1277 // Deleting the organization should cause the related membership to be deleted.
1278 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b));
1279 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a), 0);
1280 }
1281
1282 }