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