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