Merge pull request #7656 from totten/master-civi-ns
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTest.php
CommitLineData
6a488035 1<?php
7fbb4198 2/**
5896d037 3 * +--------------------------------------------------------------------+
81621fee 4 * | CiviCRM version 4.7 |
5896d037 5 * +--------------------------------------------------------------------+
e7112fa7 6 * | Copyright CiviCRM LLC (c) 2004-2015 |
5896d037
TO
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 * +--------------------------------------------------------------------+
6c6e6187 26 */
6a488035
TO
27
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Class contains api test cases for "civicrm_relationship"
6a488035
TO
32 */
33class api_v3_RelationshipTest extends CiviUnitTestCase {
b2402735 34 protected $_apiversion = 3;
6a488035 35 protected $_cId_a;
c9c41397 36 /**
eceb18cc 37 * Second individual.
c9c41397 38 * @var integer
39 */
40 protected $_cId_a_2;
6a488035 41 protected $_cId_b;
b85df283
EM
42 /**
43 * Second organization contact.
44 *
45 * @var int
46 */
47 protected $_cId_b2;
6a488035
TO
48 protected $_relTypeID;
49 protected $_ids = array();
50 protected $_customGroupId = NULL;
51 protected $_customFieldId = NULL;
52 protected $_params;
b7c9bc4c 53
6a488035 54 protected $_entity;
4cbe18b8 55
b85df283
EM
56 /**
57 * Set up function.
58 */
00be9182 59 public function setUp() {
6a488035 60 parent::setUp();
c9c41397 61 $this->_cId_a = $this->individualCreate();
5896d037 62 $this->_cId_a_2 = $this->individualCreate(array(
92915c55
TO
63 'last_name' => 'c2',
64 'email' => 'c@w.com',
65 'contact_type' => 'Individual',
66 ));
c9c41397 67 $this->_cId_b = $this->organizationCreate();
68 $this->_cId_b2 = $this->organizationCreate(array('organization_name' => ' Org 2'));
69 $this->_entity = 'relationship';
b85df283 70 //Create a relationship type.
6a488035
TO
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,
6a488035 79 );
b2402735 80
6a488035
TO
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,
6a488035
TO
88 );
89
90 }
91
b85df283
EM
92 /**
93 * Tear down function.
94 *
95 * @throws \Exception
96 */
00be9182 97 public function tearDown() {
6a488035 98 $this->contactDelete($this->_cId_a);
c9c41397 99 $this->contactDelete($this->_cId_a_2);
6a488035 100 $this->contactDelete($this->_cId_b);
75638074 101 $this->contactDelete($this->_cId_b2);
102 $this->quickCleanup(array('civicrm_relationship'), TRUE);
103 $this->relationshipTypeDelete($this->_relTypeID);
6a488035
TO
104 }
105
6a488035 106 /**
eceb18cc 107 * Check with empty array.
6a488035 108 */
00be9182 109 public function testRelationshipCreateEmpty() {
b2402735 110 $this->callAPIFailure('relationship', 'create', array());
6a488035
TO
111 }
112
113 /**
eceb18cc 114 * Check if required fields are not passed.
6a488035 115 */
00be9182 116 public function testRelationshipCreateWithoutRequired() {
6a488035
TO
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
d0e1eff2 123 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
124 }
125
126 /**
eceb18cc 127 * Check with incorrect required fields.
6a488035 128 */
00be9182 129 public function testRelationshipCreateWithIncorrectData() {
6a488035
TO
130
131 $params = array(
132 'contact_id_a' => $this->_cId_a,
133 'contact_id_b' => $this->_cId_b,
134 'relationship_type_id' => 'Breaking Relationship',
6a488035
TO
135 );
136
d0e1eff2 137 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
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 );
d0e1eff2 147 $this->callAPIFailure('relationship', 'create', $params);
6a488035 148
b85df283 149 // Contact id does not exist.
6a488035 150 $params['contact_id_a'] = 999;
d0e1eff2 151 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
152
153 //invalid date
154 $params['contact_id_a'] = $this->_cId_a;
155 $params['start_date'] = array('d' => '1', 'M' => '1');
d0e1eff2 156 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
157 }
158
159 /**
eceb18cc 160 * Check relationship creation with invalid Relationship.
6a488035 161 */
b85df283
EM
162 public function testRelationshipCreateInvalidRelationship() {
163 // Both have the contact type Individual.
6a488035
TO
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,
6a488035
TO
170 );
171
d0e1eff2 172 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
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,
6a488035
TO
181 );
182
d0e1eff2 183 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
184 }
185
186 /**
eceb18cc 187 * Check relationship already exists.
6a488035 188 */
00be9182 189 public function testRelationshipCreateAlreadyExists() {
6a488035
TO
190 $params = array(
191 'contact_id_a' => $this->_cId_a,
192 'contact_id_b' => $this->_cId_b,
193 'relationship_type_id' => $this->_relTypeID,
6c6e6187 194 'start_date' => '2008-12-20',
5896d037 195 'end_date' => NULL,
6a488035 196 'is_active' => 1,
6a488035 197 );
b2402735 198 $relationship = $this->callAPISuccess('relationship', 'create', $params);
6a488035
TO
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,
6a488035 206 );
b85df283 207 $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
6a488035
TO
208
209 $params['id'] = $relationship['id'];
b85df283 210 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
211 }
212
213 /**
eceb18cc 214 * Check relationship already exists.
6a488035 215 */
00be9182 216 public function testRelationshipCreateUpdateAlreadyExists() {
6a488035
TO
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,
6a488035 224
26dcc9eb 225 );
226 $relationship = $this->callAPISuccess('relationship', 'create', $params);
6a488035
TO
227 $params = array(
228 'id' => $relationship['id'],
229 'is_active' => 0,
6a488035
TO
230 'debug' => 1,
231 );
b85df283
EM
232 $this->callAPISuccess('relationship', 'create', $params);
233 $this->callAPISuccess('relationship', 'get', $params);
6a488035 234 $params['id'] = $relationship['id'];
b85df283 235 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
236 }
237
238 /**
b85df283 239 * Check update doesn't reset stuff badly - CRM-11789.
6a488035 240 */
b85df283 241 public function testRelationshipCreateUpdateDoesNotMangle() {
6a488035
TO
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',
6a488035
TO
247 'is_active' => 1,
248 'is_permission_a_b' => 1,
249 'description' => 'my desc',
6a488035 250 );
26dcc9eb 251 $relationship = $this->callAPISuccess('relationship', 'create', $params);
6a488035 252
b85df283 253 $updateParams = array(
6a488035 254 'id' => $relationship['id'],
6a488035
TO
255 'relationship_type_id' => $this->_relTypeID,
256 );
b85df283 257 $this->callAPISuccess('relationship', 'create', $updateParams);
6a488035 258
6a488035
TO
259 //make sure the orig params didn't get changed
260 $this->getAndCheck($params, $relationship['id'], 'relationship');
261
262 }
263
264
6a488035 265 /**
eceb18cc 266 * Check relationship creation.
6a488035 267 */
00be9182 268 public function testRelationshipCreate() {
6a488035
TO
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',
6a488035
TO
277 );
278
26dcc9eb 279 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__, __FILE__);
75638074 280 $this->assertNotNull($result['id']);
6a488035
TO
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);
b2402735 287 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
6a488035
TO
288 $values = $result['values'][$result['id']];
289 foreach ($params as $key => $value) {
b2402735 290 if ($key == 'note') {
6a488035
TO
291 continue;
292 }
75638074 293 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
6a488035
TO
294 }
295 $params['id'] = $result['id'];
26dcc9eb 296 $this->callAPISuccess('relationship', 'delete', $params);
6a488035 297 }
5896d037 298
49f8272d 299 /**
eceb18cc 300 * Ensure disabling works.
49f8272d 301 */
00be9182 302 public function testRelationshipUpdate() {
49f8272d
E
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']);
936e43d6 307
49f8272d 308 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
936e43d6 309
49f8272d
E
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));
936e43d6 313 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
49f8272d
E
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 }
5896d037 318
6a488035 319 /**
eceb18cc 320 * Check relationship creation.
6a488035 321 */
00be9182 322 public function testRelationshipCreateEmptyEndDate() {
6a488035
TO
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',
6a488035
TO
331 );
332
26dcc9eb 333 $result = $this->callAPISuccess('relationship', 'create', $params);
75638074 334 $this->assertNotNull($result['id']);
6a488035
TO
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);
26dcc9eb 341 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
6a488035
TO
342 $values = $result['values'][$result['id']];
343 foreach ($params as $key => $value) {
b2402735 344 if ($key == 'note') {
6a488035
TO
345 continue;
346 }
5896d037 347 if ($key == 'end_date') {
6a488035
TO
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'];
26dcc9eb 354 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
355 }
356
357 /**
eceb18cc 358 * Check relationship creation with custom data.
6a488035 359 */
00be9182 360 public function testRelationshipCreateWithCustomData() {
b85df283 361 $this->createCustomGroup();
6a488035
TO
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,
6a488035
TO
377 );
378 $params = array_merge($params, $custom_params);
26dcc9eb 379 $result = $this->callAPISuccess('relationship', 'create', $params);
6a488035 380
6a488035
TO
381 $relationParams = array(
382 'id' => $result['id'],
383 );
6a488035
TO
384 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
385
386 $params['id'] = $result['id'];
b85df283 387 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
388 $this->relationshipTypeDelete($this->_relTypeID);
389 }
390
391 /**
100fef9d 392 * Check with complete array + custom field
6a488035
TO
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 */
00be9182 397 public function testGetWithCustom() {
6a488035
TO
398 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
399
400 $params = $this->_params;
401 $params['custom_' . $ids['custom_field_id']] = "custom string";
402
26dcc9eb 403 $result = $this->callAPISuccess($this->_entity, 'create', $params);
6a488035
TO
404 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
405
b2402735 406 $getParams = array('id' => $result['id']);
26dcc9eb 407 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
6a488035
TO
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
4cbe18b8
EM
414 /**
415 * @return mixed
416 */
00be9182 417 public function createCustomGroup() {
6a488035
TO
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,
6a488035 425 );
26dcc9eb 426 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
6a488035
TO
427 $this->_customGroupId = $customGroup['id'];
428 return $customGroup['id'];
429 }
430
4cbe18b8
EM
431 /**
432 * @return array
433 */
00be9182 434 public function createCustomField() {
6a488035
TO
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,
6a488035
TO
446 );
447
b85df283 448 $this->callAPISuccess('CustomField', 'create', $params);
6a488035
TO
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,
6a488035
TO
482 );
483
26dcc9eb 484 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035
TO
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,
6a488035
TO
498 );
499
26dcc9eb 500 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035
TO
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,
6a488035
TO
514 );
515
26dcc9eb 516 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035
TO
517 $ids[] = $customField['id'];
518 return $ids;
519 }
520
6a488035 521 /**
eceb18cc 522 * Check with empty array.
6a488035 523 */
00be9182 524 public function testRelationshipDeleteEmpty() {
b85df283 525 $this->callAPIFailure('relationship', 'delete', array(), 'Mandatory key(s) missing from params array: id');
6a488035
TO
526 }
527
6a488035 528 /**
eceb18cc 529 * Check if required fields are not passed.
6a488035 530 */
00be9182 531 public function testRelationshipDeleteWithoutRequired() {
6a488035
TO
532 $params = array(
533 'start_date' => '2008-12-20',
534 'end_date' => '2009-12-20',
535 'is_active' => 1,
536 );
537
b85df283 538 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
6a488035
TO
539 }
540
541 /**
eceb18cc 542 * Check with incorrect required fields.
6a488035 543 */
00be9182 544 public function testRelationshipDeleteWithIncorrectData() {
6a488035
TO
545 $params = array(
546 'contact_id_a' => $this->_cId_a,
547 'contact_id_b' => $this->_cId_b,
548 'relationship_type_id' => 'Breaking Relationship',
6a488035
TO
549 );
550
b85df283 551 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
6a488035
TO
552
553 $params['id'] = "Invalid";
b85df283 554 $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
6a488035
TO
555 }
556
557 /**
eceb18cc 558 * Check relationship creation.
6a488035 559 */
00be9182 560 public function testRelationshipDelete() {
6a488035
TO
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,
6a488035
TO
567 );
568
26dcc9eb 569 $result = $this->callAPISuccess('relationship', 'create', $params);
7a44a255 570 $params = array('id' => $result['id']);
b85df283 571 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
572 $this->relationshipTypeDelete($this->_relTypeID);
573 }
574
575 ///////////////// civicrm_relationship_update methods
576
577 /**
eceb18cc 578 * Check with empty array.
6a488035 579 */
00be9182 580 public function testRelationshipUpdateEmpty() {
b85df283 581 $this->callAPIFailure('relationship', 'create', array(),
75638074 582 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
6a488035
TO
583 }
584
6a488035 585 /**
eceb18cc 586 * Check if required fields are not passed.
6a488035
TO
587 */
588
589 /**
eceb18cc 590 * Check relationship update.
6a488035 591 */
00be9182 592 public function testRelationshipCreateDuplicate() {
6a488035
TO
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,
6a488035
TO
600 );
601
26dcc9eb 602 $result = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035 603
75638074 604 $this->assertNotNull($result['id']);
6a488035
TO
605
606 $params = array(
6a488035
TO
607 'contact_id_a' => $this->_cId_a,
608 'contact_id_b' => $this->_cId_b,
609 'relationship_type_id' => $this->_relTypeID,
610 'start_date' => '20081214',
b2402735 611 'end_date' => '20091214',
612 'is_active' => 0,
6a488035
TO
613 );
614
b85df283 615 $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
6a488035 616
7c8ae32a 617 $this->callAPISuccess('relationship', 'delete', array('id' => $result['id']));
6a488035
TO
618 $this->relationshipTypeDelete($this->_relTypeID);
619 }
620
69f20614
JV
621 /**
622 * CRM-13725 - Two relationships of same type with same start and end date
623 * should be OK if the custom field values differ.
624 */
625 public function testRelationshipCreateDuplicateWithCustomFields() {
626 $this->createCustomGroup();
627 $this->_ids = $this->createCustomField();
628
629 $custom_params_1 = array(
630 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
631 "custom_{$this->_ids[1]}" => 'Y',
632 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
633 "custom_{$this->_ids[3]}" => 'http://example.com',
634 );
635
636 $custom_params_2 = array(
637 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
638 "custom_{$this->_ids[1]}" => 'Y',
639 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
640 "custom_{$this->_ids[3]}" => 'http://example.org',
641 );
642
643 $params = array(
644 'contact_id_a' => $this->_cId_a,
645 'contact_id_b' => $this->_cId_b,
646 'relationship_type_id' => $this->_relTypeID,
647 'start_date' => '2008-12-20',
648 'is_active' => 1,
649 );
650
651 $params_1 = array_merge($params, $custom_params_1);
652 $params_2 = array_merge($params, $custom_params_2);
653
654 $result_1 = $this->callAPISuccess('relationship', 'create', $params_1);
655 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
656
657 $this->assertNotNull($result_2['id']);
658 $this->assertEquals(0, $result_2['is_error']);
659
660 $this->relationshipTypeDelete($this->_relTypeID);
661 }
662
a12d81e1
JV
663 /**
664 * CRM-13725 - Two relationships of same type with same start and end date
665 * should be OK if the custom field values differ. In this case, the
666 * existing relationship does not have custom values, but the new one
667 * does.
668 */
669 public function testRelationshipCreateDuplicateWithCustomFields2() {
670 $this->createCustomGroup();
671 $this->_ids = $this->createCustomField();
672
673 $custom_params_2 = array(
674 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
675 "custom_{$this->_ids[1]}" => 'Y',
676 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
677 "custom_{$this->_ids[3]}" => 'http://example.org',
678 );
679
680 $params_1 = array(
681 'contact_id_a' => $this->_cId_a,
682 'contact_id_b' => $this->_cId_b,
683 'relationship_type_id' => $this->_relTypeID,
684 'start_date' => '2008-12-20',
685 'is_active' => 1,
686 );
687
688 $params_2 = array_merge($params_1, $custom_params_2);
689
690 $this->callAPISuccess('relationship', 'create', $params_1);
691 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
692
693 $this->assertNotNull($result_2['id']);
694 $this->assertEquals(0, $result_2['is_error']);
695
696 $this->relationshipTypeDelete($this->_relTypeID);
697 }
698
699 /**
700 * CRM-13725 - Two relationships of same type with same start and end date
701 * should be OK if the custom field values differ. In this case, the
702 * existing relationship does have custom values, but the new one
703 * does not.
704 */
705 public function testRelationshipCreateDuplicateWithCustomFields3() {
706 $this->createCustomGroup();
707 $this->_ids = $this->createCustomField();
708
709 $custom_params_1 = array(
710 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
711 "custom_{$this->_ids[1]}" => 'Y',
712 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
713 "custom_{$this->_ids[3]}" => 'http://example.org',
714 );
715
716 $params_2 = array(
717 'contact_id_a' => $this->_cId_a,
718 'contact_id_b' => $this->_cId_b,
719 'relationship_type_id' => $this->_relTypeID,
720 'start_date' => '2008-12-20',
721 'is_active' => 1,
722 );
723
724 $params_1 = array_merge($params_2, $custom_params_1);
725
726 $this->callAPISuccess('relationship', 'create', $params_1);
727 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
728
729 $this->assertNotNull($result_2['id']);
730 $this->assertEquals(0, $result_2['is_error']);
731
732 $this->relationshipTypeDelete($this->_relTypeID);
733 }
734
6a488035 735 /**
100fef9d 736 * Check with valid params array.
6a488035 737 */
00be9182 738 public function testRelationshipsGet() {
6a488035
TO
739 $relParams = array(
740 'contact_id_a' => $this->_cId_a,
741 'contact_id_b' => $this->_cId_b,
742 'relationship_type_id' => $this->_relTypeID,
743 'start_date' => '2011-01-01',
744 'end_date' => '2013-01-01',
745 'is_active' => 1,
6a488035
TO
746 );
747
b85df283 748 $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
749
750 //get relationship
751 $params = array(
752 'contact_id' => $this->_cId_b,
6a488035 753 );
26dcc9eb 754 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 755 $this->assertEquals($result['count'], 1);
6a488035
TO
756 $params = array(
757 'contact_id_a' => $this->_cId_a,
6a488035 758 );
26dcc9eb 759 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 760 $this->assertEquals($result['count'], 1);
6a488035
TO
761 // contact_id_a is wrong so should be no matches
762 $params = array(
763 'contact_id_a' => $this->_cId_b,
6a488035 764 );
26dcc9eb 765 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 766 $this->assertEquals($result['count'], 0);
6a488035
TO
767 }
768
769 /**
100fef9d 770 * Check with valid params array.
6a488035
TO
771 * (The get function will behave differently without 'contact_id' passed
772 */
00be9182 773 public function testRelationshipsGetGeneric() {
6a488035
TO
774 $relParams = array(
775 'contact_id_a' => $this->_cId_a,
776 'contact_id_b' => $this->_cId_b,
777 'relationship_type_id' => $this->_relTypeID,
778 'start_date' => '2011-01-01',
779 'end_date' => '2013-01-01',
780 'is_active' => 1,
6a488035
TO
781 );
782
b85df283 783 $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
784
785 //get relationship
786 $params = array(
787 'contact_id_b' => $this->_cId_b,
6a488035 788 );
b85df283 789 $this->callAPISuccess('relationship', 'get', $params);
6a488035
TO
790 }
791
b85df283
EM
792 /**
793 * Test retrieving only current relationships.
794 */
00be9182 795 public function testGetIsCurrent() {
6c6e6187 796 $rel2Params = array(
6a488035
TO
797 'contact_id_a' => $this->_cId_a,
798 'contact_id_b' => $this->_cId_b2,
799 'relationship_type_id' => $this->_relTypeID,
800 'start_date' => '2008-12-20',
801 'is_active' => 0,
6a488035 802 );
b85df283 803 $this->callAPISuccess('relationship', 'create', $rel2Params);
26dcc9eb 804 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
805
6a488035 806 $getParams = array(
21dfd5f5 807 'filters' => array('is_current' => 1),
6a488035 808 );
5c49fee0 809 $description = "Demonstrates is_current filter.";
6a488035
TO
810 $subfile = 'filterIsCurrent';
811 //no relationship has been created
26dcc9eb 812 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
813 $this->assertEquals($result['count'], 1);
814 $this->AssertEquals($rel1['id'], $result['id']);
815
816 // now try not started
6c6e6187
TO
817 $rel2Params['is_active'] = 1;
818 $rel2Params['start_date'] = 'tomorrow';
b85df283 819 $this->callAPISuccess('relationship', 'create', $rel2Params);
26dcc9eb 820 $result = $this->callAPISuccess('relationship', 'get', $getParams);
6a488035
TO
821 $this->assertEquals($result['count'], 1);
822 $this->AssertEquals($rel1['id'], $result['id']);
823
824 // now try finished
6c6e6187
TO
825 $rel2Params['is_active'] = 1;
826 $rel2Params['start_date'] = 'last week';
827 $rel2Params['end_date'] = 'yesterday';
b85df283 828 $this->callAPISuccess('relationship', 'create', $rel2Params);
6a488035 829 }
5896d037 830
408b79bf 831 /**
eceb18cc 832 * Test using various operators.
6a488035 833 */
00be9182 834 public function testGetTypeOperators() {
6a488035 835 $relTypeParams = array(
5896d037
TO
836 'name_a_b' => 'Relation 3 for delete',
837 'name_b_a' => 'Relation 6 for delete',
838 'description' => 'Testing relationship type 2',
839 'contact_type_a' => 'Individual',
840 'contact_type_b' => 'Organization',
841 'is_reserved' => 1,
842 'is_active' => 1,
6a488035
TO
843 );
844 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
845 $relTypeParams = array(
5896d037
TO
846 'name_a_b' => 'Relation 8 for delete',
847 'name_b_a' => 'Relation 9 for delete',
848 'description' => 'Testing relationship type 7',
849 'contact_type_a' => 'Individual',
850 'contact_type_b' => 'Organization',
851 'is_reserved' => 1,
852 'is_active' => 1,
6a488035
TO
853 );
854 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
855
856 $relTypeParams = array(
5896d037
TO
857 'name_a_b' => 'Relation 6 for delete',
858 'name_b_a' => 'Relation 88for delete',
859 'description' => 'Testing relationship type 00',
860 'contact_type_a' => 'Individual',
861 'contact_type_b' => 'Organization',
862 'is_reserved' => 1,
863 'is_active' => 1,
6a488035
TO
864 );
865 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
866
26dcc9eb 867 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
868 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
6c6e6187 869 array('relationship_type_id' => $relationType2)));
26dcc9eb 870 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
5896d037 871 array('relationship_type_id' => $relationType3)));
26dcc9eb 872 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
5896d037 873 array('relationship_type_id' => $relationType4)));
6a488035
TO
874
875 $getParams = array(
21dfd5f5 876 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
6a488035
TO
877 );
878
5c49fee0 879 $description = "Demonstrates use of IN filter.";
6a488035
TO
880 $subfile = 'INRelationshipType';
881
26dcc9eb 882 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
883 $this->assertEquals($result['count'], 2);
884 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
885
5c49fee0 886 $description = "Demonstrates use of NOT IN filter.";
6a488035
TO
887 $subfile = 'NotInRelationshipType';
888 $getParams = array(
21dfd5f5 889 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
6a488035 890 );
26dcc9eb 891 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
892 $this->assertEquals($result['count'], 2);
893 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
894
5c49fee0 895 $description = "Demonstrates use of BETWEEN filter.";
6a488035
TO
896 $subfile = 'BetweenRelationshipType';
897 $getParams = array(
21dfd5f5 898 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
6a488035 899 );
26dcc9eb 900 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
901 $this->assertEquals($result['count'], 3);
902 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
903
5c49fee0 904 $description = "Demonstrates use of Not BETWEEN filter.";
6a488035
TO
905 $subfile = 'NotBetweenRelationshipType';
906 $getParams = array(
21dfd5f5 907 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
6a488035 908 );
26dcc9eb 909 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 910 $this->assertEquals($result['count'], 1);
6c6e6187 911 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
6a488035
TO
912
913 }
5896d037 914
6a488035 915 /**
eceb18cc 916 * Check with invalid relationshipType Id.
6a488035 917 */
00be9182 918 public function testRelationshipTypeAddInvalidId() {
6a488035
TO
919 $relTypeParams = array(
920 'id' => 'invalid',
921 'name_a_b' => 'Relation 1 for delete',
922 'name_b_a' => 'Relation 2 for delete',
923 'contact_type_a' => 'Individual',
924 'contact_type_b' => 'Organization',
6a488035 925 );
b85df283 926 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
75638074 927 'id is not a valid integer');
6a488035
TO
928 }
929
6a488035 930 /**
fe482240 931 * Check with valid data with contact_b.
6a488035 932 */
00be9182 933 public function testGetRelationshipWithContactB() {
6a488035
TO
934 $relParams = array(
935 'contact_id_a' => $this->_cId_a,
936 'contact_id_b' => $this->_cId_b,
937 'relationship_type_id' => $this->_relTypeID,
938 'start_date' => '2011-01-01',
939 'end_date' => '2013-01-01',
940 'is_active' => 1,
6a488035
TO
941 );
942
26dcc9eb 943 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
944
945 $contacts = array(
946 'contact_id' => $this->_cId_a,
6a488035
TO
947 );
948
26dcc9eb 949 $result = $this->callAPISuccess('relationship', 'get', $contacts);
75638074 950 $this->assertGreaterThan(0, $result['count']);
6a488035
TO
951 $params = array(
952 'id' => $relationship['id'],
6a488035 953 );
7c8ae32a 954 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
955 $this->relationshipTypeDelete($this->_relTypeID);
956 }
957
958 /**
eceb18cc 959 * Check with valid data with relationshipTypes.
6a488035 960 */
00be9182 961 public function testGetRelationshipWithRelTypes() {
6a488035
TO
962 $relParams = array(
963 'contact_id_a' => $this->_cId_a,
964 'contact_id_b' => $this->_cId_b,
965 'relationship_type_id' => $this->_relTypeID,
966 'start_date' => '2011-01-01',
967 'end_date' => '2013-01-01',
968 'is_active' => 1,
6a488035
TO
969 );
970
26dcc9eb 971 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
972
973 $contact_a = array(
974 'contact_id' => $this->_cId_a,
6a488035 975 );
b85df283 976 $this->callAPISuccess('relationship', 'get', $contact_a);
6a488035
TO
977
978 $params = array(
979 'id' => $relationship['id'],
6a488035 980 );
b85df283 981 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
982 $this->relationshipTypeDelete($this->_relTypeID);
983 }
75638074 984
985 /**
986 * Checks that passing in 'contact_id' + a relationship type
987 * will filter by relationship type (relationships go in both directions)
988 * as relationship api does a reciprocal check if contact_id provided
989 *
990 * We should get 1 result without or with correct relationship type id & 0 with
991 * an incorrect one
992 */
00be9182 993 public function testGetRelationshipByTypeReciprocal() {
75638074 994 $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
995 $result = $this->callAPISuccess($this->_entity, 'get', array(
996 'contact_id' => $this->_cId_a,
997 'relationship_type_id' => $this->_relTypeID,
998 ));
999 $this->assertEquals(1, $result['count']);
1000 $result = $this->callAPISuccess($this->_entity, 'get', array(
1001 'contact_id' => $this->_cId_a,
1002 'relationship_type_id' => $this->_relTypeID + 1,
1003 ));
1004 $this->assertEquals(0, $result['count']);
1005 $this->callAPISuccess($this->_entity, 'delete', array('id' => $created['id']));
1006 }
1007
1008 /**
1009 * Checks that passing in 'contact_id_b' + a relationship type
1010 * will filter by relationship type for contact b
1011 *
1012 * We should get 1 result without or with correct relationship type id & 0 with
1013 * an incorrect one
1014 */
00be9182 1015 public function testGetRelationshipByTypeDAO() {
b85df283
EM
1016 $this->_ids['relationship'] = $this->callAPISuccess($this->_entity, 'create', array('format.only_id' => TRUE) +
1017 $this->_params);
1018 $this->callAPISuccess($this->_entity, 'getcount', array(
5896d037
TO
1019 'contact_id_a' => $this->_cId_a,
1020 ),
1021 1);
75638074 1022 $result = $this->callAPISuccess($this->_entity, 'get', array(
1023 'contact_id_a' => $this->_cId_a,
1024 'relationship_type_id' => $this->_relTypeID,
1025 ));
1026 $this->assertEquals(1, $result['count']);
1027 $result = $this->callAPISuccess($this->_entity, 'get', array(
1028 'contact_id_a' => $this->_cId_a,
1029 'relationship_type_id' => $this->_relTypeID + 1,
1030 ));
1031 $this->assertEquals(0, $result['count']);
1032 }
1033
1034 /**
1035 * Checks that passing in 'contact_id_b' + a relationship type
1036 * will filter by relationship type for contact b
1037 *
1038 * We should get 1 result without or with correct relationship type id & 0 with
1039 * an incorrect one
1040 */
00be9182 1041 public function testGetRelationshipByTypeArrayDAO() {
b85df283 1042 $this->callAPISuccess($this->_entity, 'create', $this->_params);
75638074 1043 $org3 = $this->organizationCreate();
1044 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1045 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1046
b85df283 1047 // Relationship 2.
75638074 1048 $this->callAPISuccess($this->_entity, 'create',
1049 array_merge($this->_params, array(
1050 'relationship_type_id' => $relType2,
21dfd5f5 1051 'contact_id_b' => $this->_cId_b2,
5896d037 1052 ))
75638074 1053 );
1054
b85df283 1055 // Relationship 3.
75638074 1056 $this->callAPISuccess($this->_entity, 'create',
1057 array_merge($this->_params, array(
1058 'relationship_type_id' => $relType3,
21dfd5f5 1059 'contact_id_b' => $org3,
5896d037 1060 ))
75638074 1061 );
1062
1063 $result = $this->callAPISuccess($this->_entity, 'get', array(
1064 'contact_id_a' => $this->_cId_a,
1065 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1066 ));
1067
1068 $this->assertEquals(2, $result['count']);
1069 foreach ($result['values'] as $key => $value) {
1070 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1071 }
1072 }
ac8aaa49 1073
1074 /**
1075 * Checks that passing in 'contact_id_b' + a relationship type
1076 * will filter by relationship type for contact b
1077 *
1078 * We should get 1 result without or with correct relationship type id & 0 with
1079 * an incorrect one
1080 */
00be9182 1081 public function testGetRelationshipByTypeArrayReciprocal() {
b85df283 1082 $this->callAPISuccess($this->_entity, 'create', $this->_params);
ac8aaa49 1083 $org3 = $this->organizationCreate();
b85df283
EM
1084 // lets just assume built in ones aren't being messed with!
1085 $relType2 = 5;
1086 $relType3 = 6;
ac8aaa49 1087
b85df283 1088 // Relationship 2.
ac8aaa49 1089 $this->callAPISuccess($this->_entity, 'create',
1090 array_merge($this->_params, array(
1091 'relationship_type_id' => $relType2,
21dfd5f5 1092 'contact_id_b' => $this->_cId_b2,
5896d037 1093 ))
ac8aaa49 1094 );
1095
b85df283 1096 // Relationship 3.
ac8aaa49 1097 $this->callAPISuccess($this->_entity, 'create',
1098 array_merge($this->_params, array(
1099 'relationship_type_id' => $relType3,
21dfd5f5 1100 'contact_id_b' => $org3,
5896d037 1101 ))
ac8aaa49 1102 );
1103
1104 $result = $this->callAPISuccess($this->_entity, 'get', array(
1105 'contact_id' => $this->_cId_a,
1106 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1107 ));
1108
1109 $this->assertEquals(2, $result['count']);
1110 foreach ($result['values'] as $key => $value) {
1111 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1112 }
1113 }
6a488035 1114
c9c41397 1115 /**
fe482240
EM
1116 * Test relationship get by membership type.
1117 *
c9c41397 1118 * Checks that passing in 'contact_id_b' + a relationship type
1119 * will filter by relationship type for contact b
1120 *
1121 * We should get 1 result without or with correct relationship type id & 0 with
1122 * an incorrect one
1123 */
00be9182 1124 public function testGetRelationshipByMembershipTypeDAO() {
b85df283 1125 $this->callAPISuccess($this->_entity, 'create', $this->_params);
c9c41397 1126 $org3 = $this->organizationCreate();
1127
1128 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1129 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1130 $relType1 = 1;
1131 $memberType = $this->membershipTypeCreate(array(
1132 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1133 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1134 ));
1135
b85df283 1136 // Relationship 2.
c9c41397 1137 $this->callAPISuccess($this->_entity, 'create',
1138 array_merge($this->_params, array(
1139 'relationship_type_id' => $relType2,
21dfd5f5 1140 'contact_id_b' => $this->_cId_b2,
5896d037 1141 ))
c9c41397 1142 );
1143
b85df283 1144 // Relationship 3.
c9c41397 1145 $this->callAPISuccess($this->_entity, 'create',
1146 array_merge($this->_params, array(
1147 'relationship_type_id' => $relType3,
21dfd5f5 1148 'contact_id_b' => $org3,
5896d037 1149 ))
c9c41397 1150 );
1151
b85df283 1152 // Relationship 4 with reversal.
c9c41397 1153 $this->callAPISuccess($this->_entity, 'create',
1154 array_merge($this->_params, array(
1155 'relationship_type_id' => $relType1,
1156 'contact_id_a' => $this->_cId_a,
21dfd5f5 1157 'contact_id_b' => $this->_cId_a_2,
5896d037 1158 ))
c9c41397 1159 );
1160
1161 $result = $this->callAPISuccess($this->_entity, 'get', array(
1162 'contact_id_a' => $this->_cId_a,
1163 'membership_type_id' => $memberType,
1164 ));
1165 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1166 $this->assertEquals(1, $result['count']);
1167 foreach ($result['values'] as $key => $value) {
1168 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1169 }
1170 }
1171
1172 /**
1173 * Checks that passing in 'contact_id_b' + a relationship type
1174 * will filter by relationship type for contact b
1175 *
1176 * We should get 1 result without or with correct relationship type id & 0 with
1177 * an incorrect one
1178 */
00be9182 1179 public function testGetRelationshipByMembershipTypeReciprocal() {
b85df283 1180 $this->callAPISuccess($this->_entity, 'create', $this->_params);
c9c41397 1181 $org3 = $this->organizationCreate();
1182
b85df283
EM
1183 // Let's just assume built in ones aren't being messed with!
1184 $relType2 = 5;
1185 $relType3 = 6;
c9c41397 1186 $relType1 = 1;
1187 $memberType = $this->membershipTypeCreate(array(
1188 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1189 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1190 ));
1191
b85df283 1192 // Relationship 2.
c9c41397 1193 $this->callAPISuccess($this->_entity, 'create',
1194 array_merge($this->_params, array(
1195 'relationship_type_id' => $relType2,
21dfd5f5 1196 'contact_id_b' => $this->_cId_b2,
5896d037 1197 ))
c9c41397 1198 );
1199
b85df283 1200 // Relationship 4.
c9c41397 1201 $this->callAPISuccess($this->_entity, 'create',
1202 array_merge($this->_params, array(
1203 'relationship_type_id' => $relType3,
21dfd5f5 1204 'contact_id_b' => $org3,
5896d037 1205 ))
c9c41397 1206 );
1207
b85df283 1208 // Relationship 4 with reversal.
c9c41397 1209 $this->callAPISuccess($this->_entity, 'create',
1210 array_merge($this->_params, array(
1211 'relationship_type_id' => $relType1,
1212 'contact_id_a' => $this->_cId_a,
21dfd5f5 1213 'contact_id_b' => $this->_cId_a_2,
5896d037 1214 ))
c9c41397 1215 );
1216
1217 $result = $this->callAPISuccess($this->_entity, 'get', array(
1218 'contact_id' => $this->_cId_a,
1219 'membership_type_id' => $memberType,
1220 ));
b85df283 1221 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
c9c41397 1222 $this->assertEquals(2, $result['count']);
1223
1224 foreach ($result['values'] as $key => $value) {
1225 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1226 }
1227 }
a7361ba3
EM
1228
1229 /**
b85df283 1230 * Check for e-notices on enable & disable as reported in CRM-14350
a7361ba3 1231 */
00be9182 1232 public function testSetActive() {
a7361ba3
EM
1233 $relationship = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1234 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1235 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1236 }
96025800 1237
7a44a255
EM
1238 /**
1239 * Test creating related memberships.
1240 */
1241 public function testCreateRelatedMembership() {
1242 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1243 'name' => 'Membership with Related',
1244 'member_of_contact_id' => 1,
1245 'financial_type_id' => 1,
1246 'minimum_fee' => 0.00,
1247 'duration_unit' => 'year',
1248 'duration_interval' => 1,
1249 'period_type' => 'rolling',
1250 'relationship_type_id' => $this->_relTypeID,
1251 'relationship_direction' => 'b_a',
1252 'visibility' => 'Public',
1253 'auto_renew' => 0,
1254 'is_active' => 1,
1255 'domain_id' => CRM_Core_Config::domainID(),
1256 ));
1257 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1258 'membership_type_id' => $relatedMembershipType['id'],
1259 'contact_id' => $this->_cId_b,
1260 ));
1261 $this->callAPISuccess('Relationship', 'create', array(
1262 'relationship_type_id' => $this->_relTypeID,
1263 'contact_id_a' => $this->_cId_a,
1264 'contact_id_b' => $this->_cId_b,
1265 ));
1266 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a));
1267 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
45089d88
CW
1268
1269 // Adding a relationship with a future start date should NOT create a membership
1270 $this->callAPISuccess('Relationship', 'create', array(
1271 'relationship_type_id' => $this->_relTypeID,
1272 'contact_id_a' => $this->_cId_a_2,
1273 'contact_id_b' => $this->_cId_b,
1274 'start_date' => 'now + 1 week',
1275 ));
1276 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2), 0);
1277
62118c6d
EM
1278 // Deleting the organization should cause the related membership to be deleted.
1279 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b));
1280 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a), 0);
7a44a255
EM
1281 }
1282
c9c41397 1283}