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