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