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