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