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