Merge pull request #10030 from eileenmcnaughton/test
[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 */
00d84e9b 359 public function testRelationshipCreateEditWithCustomData() {
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
00d84e9b
JP
385 //Test Edit of custom field from the form.
386 $getParams = array('id' => $result['id']);
387 $updateParams = array_merge($getParams, array(
388 "custom_{$this->_ids[0]}" => 'Edited Text Value',
389 'relationship_type_id' => $this->_relTypeID . '_b_a',
390 'related_contact_id' => $this->_cId_a,
391 ));
392 $reln = new CRM_Contact_Form_Relationship();
393 $reln->_action = CRM_Core_Action::UPDATE;
394 $reln->_relationshipId = $result['id'];
395 $reln->submit($updateParams);
396
397 $check = $this->callAPISuccess('relationship', 'get', $getParams);
398 $this->assertEquals("Edited Text Value", $check['values'][$check['id']]["custom_{$this->_ids[0]}"]);
399
6a488035 400 $params['id'] = $result['id'];
b85df283 401 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
402 $this->relationshipTypeDelete($this->_relTypeID);
403 }
404
405 /**
100fef9d 406 * Check with complete array + custom field
6a488035
TO
407 * Note that the test is written on purpose without any
408 * variables specific to participant so it can be replicated into other entities
409 * and / or moved to the automated test suite
410 */
00be9182 411 public function testGetWithCustom() {
6a488035
TO
412 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
413
414 $params = $this->_params;
415 $params['custom_' . $ids['custom_field_id']] = "custom string";
416
26dcc9eb 417 $result = $this->callAPISuccess($this->_entity, 'create', $params);
6a488035
TO
418 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
419
b2402735 420 $getParams = array('id' => $result['id']);
26dcc9eb 421 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
6a488035
TO
422 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
423
424 $this->customFieldDelete($ids['custom_field_id']);
425 $this->customGroupDelete($ids['custom_group_id']);
426 }
427
4cbe18b8
EM
428 /**
429 * @return mixed
430 */
00be9182 431 public function createCustomGroup() {
6a488035
TO
432 $params = array(
433 'title' => 'Custom Group',
434 'extends' => array('Relationship'),
435 'weight' => 5,
436 'style' => 'Inline',
437 'is_active' => 1,
438 'max_multiple' => 0,
6a488035 439 );
26dcc9eb 440 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
6a488035
TO
441 $this->_customGroupId = $customGroup['id'];
442 return $customGroup['id'];
443 }
444
4cbe18b8
EM
445 /**
446 * @return array
447 */
00be9182 448 public function createCustomField() {
6a488035
TO
449 $ids = array();
450 $params = array(
451 'custom_group_id' => $this->_customGroupId,
452 'label' => 'Enter text about relationship',
453 'html_type' => 'Text',
454 'data_type' => 'String',
455 'default_value' => 'xyz',
456 'weight' => 1,
457 'is_required' => 1,
458 'is_searchable' => 0,
459 'is_active' => 1,
6a488035
TO
460 );
461
00d84e9b
JP
462 $customField = $this->callAPISuccess('CustomField', 'create', $params);
463 $ids[] = $customField['id'];
6a488035
TO
464
465 $optionValue[] = array(
466 'label' => 'Red',
467 'value' => 'R',
468 'weight' => 1,
469 'is_active' => 1,
470 );
471 $optionValue[] = array(
472 'label' => 'Yellow',
473 'value' => 'Y',
474 'weight' => 2,
475 'is_active' => 1,
476 );
477 $optionValue[] = array(
478 'label' => 'Green',
479 'value' => 'G',
480 'weight' => 3,
481 'is_active' => 1,
482 );
483
484 $params = array(
485 'label' => 'Pick Color',
486 'html_type' => 'Select',
487 'data_type' => 'String',
488 'weight' => 2,
489 'is_required' => 1,
490 'is_searchable' => 0,
491 'is_active' => 1,
492 'option_values' => $optionValue,
493 'custom_group_id' => $this->_customGroupId,
6a488035
TO
494 );
495
26dcc9eb 496 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035
TO
497 $ids[] = $customField['id'];
498
499 $params = array(
500 'custom_group_id' => $this->_customGroupId,
501 'name' => 'test_date',
502 'label' => 'test_date',
503 'html_type' => 'Select Date',
504 'data_type' => 'Date',
505 'default_value' => '20090711',
506 'weight' => 3,
507 'is_required' => 1,
508 'is_searchable' => 0,
509 'is_active' => 1,
6a488035
TO
510 );
511
26dcc9eb 512 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035
TO
513
514 $ids[] = $customField['id'];
515 $params = array(
516 'custom_group_id' => $this->_customGroupId,
517 'name' => 'test_link',
518 'label' => 'test_link',
519 'html_type' => 'Link',
520 'data_type' => 'Link',
521 'default_value' => 'http://civicrm.org',
522 'weight' => 4,
523 'is_required' => 1,
524 'is_searchable' => 0,
525 'is_active' => 1,
6a488035
TO
526 );
527
26dcc9eb 528 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035
TO
529 $ids[] = $customField['id'];
530 return $ids;
531 }
532
6a488035 533 /**
eceb18cc 534 * Check with empty array.
6a488035 535 */
00be9182 536 public function testRelationshipDeleteEmpty() {
b85df283 537 $this->callAPIFailure('relationship', 'delete', array(), 'Mandatory key(s) missing from params array: id');
6a488035
TO
538 }
539
6a488035 540 /**
eceb18cc 541 * Check if required fields are not passed.
6a488035 542 */
00be9182 543 public function testRelationshipDeleteWithoutRequired() {
6a488035
TO
544 $params = array(
545 'start_date' => '2008-12-20',
546 'end_date' => '2009-12-20',
547 'is_active' => 1,
548 );
549
b85df283 550 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
6a488035
TO
551 }
552
553 /**
eceb18cc 554 * Check with incorrect required fields.
6a488035 555 */
00be9182 556 public function testRelationshipDeleteWithIncorrectData() {
6a488035
TO
557 $params = array(
558 'contact_id_a' => $this->_cId_a,
559 'contact_id_b' => $this->_cId_b,
560 'relationship_type_id' => 'Breaking Relationship',
6a488035
TO
561 );
562
b85df283 563 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
6a488035
TO
564
565 $params['id'] = "Invalid";
4f94e3fa 566 $this->callAPIFailure('relationship', 'delete', $params, 'id is not a valid integer');
6a488035
TO
567 }
568
569 /**
eceb18cc 570 * Check relationship creation.
6a488035 571 */
00be9182 572 public function testRelationshipDelete() {
6a488035
TO
573 $params = array(
574 'contact_id_a' => $this->_cId_a,
575 'contact_id_b' => $this->_cId_b,
576 'relationship_type_id' => $this->_relTypeID,
577 'start_date' => '2008-12-20',
578 'is_active' => 1,
6a488035
TO
579 );
580
26dcc9eb 581 $result = $this->callAPISuccess('relationship', 'create', $params);
7a44a255 582 $params = array('id' => $result['id']);
b85df283 583 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
584 $this->relationshipTypeDelete($this->_relTypeID);
585 }
586
587 ///////////////// civicrm_relationship_update methods
588
589 /**
eceb18cc 590 * Check with empty array.
6a488035 591 */
00be9182 592 public function testRelationshipUpdateEmpty() {
b85df283 593 $this->callAPIFailure('relationship', 'create', array(),
75638074 594 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
6a488035
TO
595 }
596
6a488035 597 /**
eceb18cc 598 * Check if required fields are not passed.
6a488035
TO
599 */
600
601 /**
eceb18cc 602 * Check relationship update.
6a488035 603 */
00be9182 604 public function testRelationshipCreateDuplicate() {
6a488035
TO
605 $relParams = array(
606 'contact_id_a' => $this->_cId_a,
607 'contact_id_b' => $this->_cId_b,
608 'relationship_type_id' => $this->_relTypeID,
609 'start_date' => '20081214',
610 'end_date' => '20091214',
611 'is_active' => 1,
6a488035
TO
612 );
613
26dcc9eb 614 $result = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035 615
75638074 616 $this->assertNotNull($result['id']);
6a488035
TO
617
618 $params = array(
6a488035
TO
619 'contact_id_a' => $this->_cId_a,
620 'contact_id_b' => $this->_cId_b,
621 'relationship_type_id' => $this->_relTypeID,
622 'start_date' => '20081214',
b2402735 623 'end_date' => '20091214',
624 'is_active' => 0,
6a488035
TO
625 );
626
4f94e3fa 627 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
6a488035 628
7c8ae32a 629 $this->callAPISuccess('relationship', 'delete', array('id' => $result['id']));
6a488035
TO
630 $this->relationshipTypeDelete($this->_relTypeID);
631 }
632
69f20614
JV
633 /**
634 * CRM-13725 - Two relationships of same type with same start and end date
635 * should be OK if the custom field values differ.
636 */
637 public function testRelationshipCreateDuplicateWithCustomFields() {
638 $this->createCustomGroup();
639 $this->_ids = $this->createCustomField();
640
641 $custom_params_1 = array(
642 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
643 "custom_{$this->_ids[1]}" => 'Y',
644 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
645 "custom_{$this->_ids[3]}" => 'http://example.com',
646 );
647
648 $custom_params_2 = array(
649 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
650 "custom_{$this->_ids[1]}" => 'Y',
651 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
652 "custom_{$this->_ids[3]}" => 'http://example.org',
653 );
654
655 $params = array(
656 'contact_id_a' => $this->_cId_a,
657 'contact_id_b' => $this->_cId_b,
658 'relationship_type_id' => $this->_relTypeID,
659 'start_date' => '2008-12-20',
660 'is_active' => 1,
661 );
662
663 $params_1 = array_merge($params, $custom_params_1);
664 $params_2 = array_merge($params, $custom_params_2);
665
666 $result_1 = $this->callAPISuccess('relationship', 'create', $params_1);
667 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
668
669 $this->assertNotNull($result_2['id']);
670 $this->assertEquals(0, $result_2['is_error']);
671
672 $this->relationshipTypeDelete($this->_relTypeID);
673 }
674
a12d81e1
JV
675 /**
676 * CRM-13725 - Two relationships of same type with same start and end date
677 * should be OK if the custom field values differ. In this case, the
678 * existing relationship does not have custom values, but the new one
679 * does.
680 */
681 public function testRelationshipCreateDuplicateWithCustomFields2() {
682 $this->createCustomGroup();
683 $this->_ids = $this->createCustomField();
684
685 $custom_params_2 = array(
686 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
687 "custom_{$this->_ids[1]}" => 'Y',
688 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
689 "custom_{$this->_ids[3]}" => 'http://example.org',
690 );
691
692 $params_1 = array(
693 'contact_id_a' => $this->_cId_a,
694 'contact_id_b' => $this->_cId_b,
695 'relationship_type_id' => $this->_relTypeID,
696 'start_date' => '2008-12-20',
697 'is_active' => 1,
698 );
699
700 $params_2 = array_merge($params_1, $custom_params_2);
701
702 $this->callAPISuccess('relationship', 'create', $params_1);
703 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
704
705 $this->assertNotNull($result_2['id']);
706 $this->assertEquals(0, $result_2['is_error']);
707
708 $this->relationshipTypeDelete($this->_relTypeID);
709 }
710
711 /**
712 * CRM-13725 - Two relationships of same type with same start and end date
713 * should be OK if the custom field values differ. In this case, the
714 * existing relationship does have custom values, but the new one
715 * does not.
716 */
717 public function testRelationshipCreateDuplicateWithCustomFields3() {
718 $this->createCustomGroup();
719 $this->_ids = $this->createCustomField();
720
721 $custom_params_1 = array(
722 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
723 "custom_{$this->_ids[1]}" => 'Y',
724 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
725 "custom_{$this->_ids[3]}" => 'http://example.org',
726 );
727
728 $params_2 = array(
729 'contact_id_a' => $this->_cId_a,
730 'contact_id_b' => $this->_cId_b,
731 'relationship_type_id' => $this->_relTypeID,
732 'start_date' => '2008-12-20',
733 'is_active' => 1,
734 );
735
736 $params_1 = array_merge($params_2, $custom_params_1);
737
738 $this->callAPISuccess('relationship', 'create', $params_1);
739 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
740
741 $this->assertNotNull($result_2['id']);
742 $this->assertEquals(0, $result_2['is_error']);
743
744 $this->relationshipTypeDelete($this->_relTypeID);
745 }
746
6a488035 747 /**
100fef9d 748 * Check with valid params array.
6a488035 749 */
00be9182 750 public function testRelationshipsGet() {
6a488035
TO
751 $relParams = array(
752 'contact_id_a' => $this->_cId_a,
753 'contact_id_b' => $this->_cId_b,
754 'relationship_type_id' => $this->_relTypeID,
755 'start_date' => '2011-01-01',
756 'end_date' => '2013-01-01',
757 'is_active' => 1,
6a488035
TO
758 );
759
b85df283 760 $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
761
762 //get relationship
763 $params = array(
764 'contact_id' => $this->_cId_b,
6a488035 765 );
26dcc9eb 766 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 767 $this->assertEquals($result['count'], 1);
6a488035
TO
768 $params = array(
769 'contact_id_a' => $this->_cId_a,
6a488035 770 );
26dcc9eb 771 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 772 $this->assertEquals($result['count'], 1);
6a488035
TO
773 // contact_id_a is wrong so should be no matches
774 $params = array(
775 'contact_id_a' => $this->_cId_b,
6a488035 776 );
26dcc9eb 777 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 778 $this->assertEquals($result['count'], 0);
6a488035
TO
779 }
780
781 /**
100fef9d 782 * Check with valid params array.
6a488035
TO
783 * (The get function will behave differently without 'contact_id' passed
784 */
00be9182 785 public function testRelationshipsGetGeneric() {
6a488035
TO
786 $relParams = array(
787 'contact_id_a' => $this->_cId_a,
788 'contact_id_b' => $this->_cId_b,
789 'relationship_type_id' => $this->_relTypeID,
790 'start_date' => '2011-01-01',
791 'end_date' => '2013-01-01',
792 'is_active' => 1,
6a488035
TO
793 );
794
b85df283 795 $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
796
797 //get relationship
798 $params = array(
799 'contact_id_b' => $this->_cId_b,
6a488035 800 );
b85df283 801 $this->callAPISuccess('relationship', 'get', $params);
6a488035
TO
802 }
803
b85df283
EM
804 /**
805 * Test retrieving only current relationships.
806 */
00be9182 807 public function testGetIsCurrent() {
6c6e6187 808 $rel2Params = array(
6a488035
TO
809 'contact_id_a' => $this->_cId_a,
810 'contact_id_b' => $this->_cId_b2,
811 'relationship_type_id' => $this->_relTypeID,
812 'start_date' => '2008-12-20',
813 'is_active' => 0,
6a488035 814 );
b85df283 815 $this->callAPISuccess('relationship', 'create', $rel2Params);
26dcc9eb 816 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
817
6a488035 818 $getParams = array(
21dfd5f5 819 'filters' => array('is_current' => 1),
6a488035 820 );
5c49fee0 821 $description = "Demonstrates is_current filter.";
6a488035
TO
822 $subfile = 'filterIsCurrent';
823 //no relationship has been created
26dcc9eb 824 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
825 $this->assertEquals($result['count'], 1);
826 $this->AssertEquals($rel1['id'], $result['id']);
827
828 // now try not started
6c6e6187
TO
829 $rel2Params['is_active'] = 1;
830 $rel2Params['start_date'] = 'tomorrow';
b85df283 831 $this->callAPISuccess('relationship', 'create', $rel2Params);
26dcc9eb 832 $result = $this->callAPISuccess('relationship', 'get', $getParams);
6a488035
TO
833 $this->assertEquals($result['count'], 1);
834 $this->AssertEquals($rel1['id'], $result['id']);
835
836 // now try finished
6c6e6187
TO
837 $rel2Params['is_active'] = 1;
838 $rel2Params['start_date'] = 'last week';
839 $rel2Params['end_date'] = 'yesterday';
b85df283 840 $this->callAPISuccess('relationship', 'create', $rel2Params);
6a488035 841 }
5896d037 842
408b79bf 843 /**
eceb18cc 844 * Test using various operators.
6a488035 845 */
00be9182 846 public function testGetTypeOperators() {
6a488035 847 $relTypeParams = array(
5896d037
TO
848 'name_a_b' => 'Relation 3 for delete',
849 'name_b_a' => 'Relation 6 for delete',
850 'description' => 'Testing relationship type 2',
851 'contact_type_a' => 'Individual',
852 'contact_type_b' => 'Organization',
853 'is_reserved' => 1,
854 'is_active' => 1,
6a488035
TO
855 );
856 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
857 $relTypeParams = array(
5896d037
TO
858 'name_a_b' => 'Relation 8 for delete',
859 'name_b_a' => 'Relation 9 for delete',
860 'description' => 'Testing relationship type 7',
861 'contact_type_a' => 'Individual',
862 'contact_type_b' => 'Organization',
863 'is_reserved' => 1,
864 'is_active' => 1,
6a488035
TO
865 );
866 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
867
868 $relTypeParams = array(
5896d037
TO
869 'name_a_b' => 'Relation 6 for delete',
870 'name_b_a' => 'Relation 88for delete',
871 'description' => 'Testing relationship type 00',
872 'contact_type_a' => 'Individual',
873 'contact_type_b' => 'Organization',
874 'is_reserved' => 1,
875 'is_active' => 1,
6a488035
TO
876 );
877 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
878
26dcc9eb 879 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
880 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
6c6e6187 881 array('relationship_type_id' => $relationType2)));
26dcc9eb 882 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
5896d037 883 array('relationship_type_id' => $relationType3)));
26dcc9eb 884 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
5896d037 885 array('relationship_type_id' => $relationType4)));
6a488035
TO
886
887 $getParams = array(
21dfd5f5 888 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
6a488035
TO
889 );
890
5c49fee0 891 $description = "Demonstrates use of IN filter.";
6a488035
TO
892 $subfile = 'INRelationshipType';
893
26dcc9eb 894 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
895 $this->assertEquals($result['count'], 2);
896 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
897
5c49fee0 898 $description = "Demonstrates use of NOT IN filter.";
6a488035
TO
899 $subfile = 'NotInRelationshipType';
900 $getParams = array(
21dfd5f5 901 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
6a488035 902 );
26dcc9eb 903 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
904 $this->assertEquals($result['count'], 2);
905 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
906
5c49fee0 907 $description = "Demonstrates use of BETWEEN filter.";
6a488035
TO
908 $subfile = 'BetweenRelationshipType';
909 $getParams = array(
21dfd5f5 910 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
6a488035 911 );
26dcc9eb 912 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
913 $this->assertEquals($result['count'], 3);
914 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
915
5c49fee0 916 $description = "Demonstrates use of Not BETWEEN filter.";
6a488035
TO
917 $subfile = 'NotBetweenRelationshipType';
918 $getParams = array(
21dfd5f5 919 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
6a488035 920 );
26dcc9eb 921 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 922 $this->assertEquals($result['count'], 1);
6c6e6187 923 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
6a488035
TO
924
925 }
5896d037 926
6a488035 927 /**
eceb18cc 928 * Check with invalid relationshipType Id.
6a488035 929 */
00be9182 930 public function testRelationshipTypeAddInvalidId() {
6a488035
TO
931 $relTypeParams = array(
932 'id' => 'invalid',
933 'name_a_b' => 'Relation 1 for delete',
934 'name_b_a' => 'Relation 2 for delete',
935 'contact_type_a' => 'Individual',
936 'contact_type_b' => 'Organization',
6a488035 937 );
b85df283 938 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
75638074 939 'id is not a valid integer');
6a488035
TO
940 }
941
6a488035 942 /**
fe482240 943 * Check with valid data with contact_b.
6a488035 944 */
00be9182 945 public function testGetRelationshipWithContactB() {
6a488035
TO
946 $relParams = array(
947 'contact_id_a' => $this->_cId_a,
948 'contact_id_b' => $this->_cId_b,
949 'relationship_type_id' => $this->_relTypeID,
950 'start_date' => '2011-01-01',
951 'end_date' => '2013-01-01',
952 'is_active' => 1,
6a488035
TO
953 );
954
26dcc9eb 955 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
956
957 $contacts = array(
958 'contact_id' => $this->_cId_a,
6a488035
TO
959 );
960
26dcc9eb 961 $result = $this->callAPISuccess('relationship', 'get', $contacts);
75638074 962 $this->assertGreaterThan(0, $result['count']);
6a488035
TO
963 $params = array(
964 'id' => $relationship['id'],
6a488035 965 );
7c8ae32a 966 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
967 $this->relationshipTypeDelete($this->_relTypeID);
968 }
969
970 /**
eceb18cc 971 * Check with valid data with relationshipTypes.
6a488035 972 */
00be9182 973 public function testGetRelationshipWithRelTypes() {
6a488035
TO
974 $relParams = array(
975 'contact_id_a' => $this->_cId_a,
976 'contact_id_b' => $this->_cId_b,
977 'relationship_type_id' => $this->_relTypeID,
978 'start_date' => '2011-01-01',
979 'end_date' => '2013-01-01',
980 'is_active' => 1,
6a488035
TO
981 );
982
26dcc9eb 983 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
984
985 $contact_a = array(
986 'contact_id' => $this->_cId_a,
6a488035 987 );
b85df283 988 $this->callAPISuccess('relationship', 'get', $contact_a);
6a488035
TO
989
990 $params = array(
991 'id' => $relationship['id'],
6a488035 992 );
b85df283 993 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
994 $this->relationshipTypeDelete($this->_relTypeID);
995 }
75638074 996
997 /**
998 * Checks that passing in 'contact_id' + a relationship type
999 * will filter by relationship type (relationships go in both directions)
1000 * as relationship api does a reciprocal check if contact_id provided
1001 *
1002 * We should get 1 result without or with correct relationship type id & 0 with
1003 * an incorrect one
1004 */
00be9182 1005 public function testGetRelationshipByTypeReciprocal() {
75638074 1006 $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1007 $result = $this->callAPISuccess($this->_entity, 'get', array(
1008 'contact_id' => $this->_cId_a,
1009 'relationship_type_id' => $this->_relTypeID,
1010 ));
1011 $this->assertEquals(1, $result['count']);
1012 $result = $this->callAPISuccess($this->_entity, 'get', array(
1013 'contact_id' => $this->_cId_a,
1014 'relationship_type_id' => $this->_relTypeID + 1,
1015 ));
1016 $this->assertEquals(0, $result['count']);
1017 $this->callAPISuccess($this->_entity, 'delete', array('id' => $created['id']));
1018 }
1019
1020 /**
1021 * Checks that passing in 'contact_id_b' + a relationship type
1022 * will filter by relationship type for contact b
1023 *
1024 * We should get 1 result without or with correct relationship type id & 0 with
1025 * an incorrect one
1026 */
00be9182 1027 public function testGetRelationshipByTypeDAO() {
b85df283
EM
1028 $this->_ids['relationship'] = $this->callAPISuccess($this->_entity, 'create', array('format.only_id' => TRUE) +
1029 $this->_params);
1030 $this->callAPISuccess($this->_entity, 'getcount', array(
5896d037
TO
1031 'contact_id_a' => $this->_cId_a,
1032 ),
1033 1);
75638074 1034 $result = $this->callAPISuccess($this->_entity, 'get', array(
1035 'contact_id_a' => $this->_cId_a,
1036 'relationship_type_id' => $this->_relTypeID,
1037 ));
1038 $this->assertEquals(1, $result['count']);
1039 $result = $this->callAPISuccess($this->_entity, 'get', array(
1040 'contact_id_a' => $this->_cId_a,
1041 'relationship_type_id' => $this->_relTypeID + 1,
1042 ));
1043 $this->assertEquals(0, $result['count']);
1044 }
1045
1046 /**
1047 * Checks that passing in 'contact_id_b' + a relationship type
1048 * will filter by relationship type for contact b
1049 *
1050 * We should get 1 result without or with correct relationship type id & 0 with
1051 * an incorrect one
1052 */
00be9182 1053 public function testGetRelationshipByTypeArrayDAO() {
b85df283 1054 $this->callAPISuccess($this->_entity, 'create', $this->_params);
75638074 1055 $org3 = $this->organizationCreate();
1056 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1057 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1058
b85df283 1059 // Relationship 2.
75638074 1060 $this->callAPISuccess($this->_entity, 'create',
1061 array_merge($this->_params, array(
1062 'relationship_type_id' => $relType2,
21dfd5f5 1063 'contact_id_b' => $this->_cId_b2,
5896d037 1064 ))
75638074 1065 );
1066
b85df283 1067 // Relationship 3.
75638074 1068 $this->callAPISuccess($this->_entity, 'create',
1069 array_merge($this->_params, array(
1070 'relationship_type_id' => $relType3,
21dfd5f5 1071 'contact_id_b' => $org3,
5896d037 1072 ))
75638074 1073 );
1074
1075 $result = $this->callAPISuccess($this->_entity, 'get', array(
1076 'contact_id_a' => $this->_cId_a,
1077 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1078 ));
1079
1080 $this->assertEquals(2, $result['count']);
1081 foreach ($result['values'] as $key => $value) {
1082 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1083 }
1084 }
ac8aaa49 1085
1086 /**
1087 * Checks that passing in 'contact_id_b' + a relationship type
1088 * will filter by relationship type for contact b
1089 *
1090 * We should get 1 result without or with correct relationship type id & 0 with
1091 * an incorrect one
1092 */
00be9182 1093 public function testGetRelationshipByTypeArrayReciprocal() {
b85df283 1094 $this->callAPISuccess($this->_entity, 'create', $this->_params);
ac8aaa49 1095 $org3 = $this->organizationCreate();
b85df283
EM
1096 // lets just assume built in ones aren't being messed with!
1097 $relType2 = 5;
1098 $relType3 = 6;
ac8aaa49 1099
b85df283 1100 // Relationship 2.
ac8aaa49 1101 $this->callAPISuccess($this->_entity, 'create',
1102 array_merge($this->_params, array(
1103 'relationship_type_id' => $relType2,
21dfd5f5 1104 'contact_id_b' => $this->_cId_b2,
5896d037 1105 ))
ac8aaa49 1106 );
1107
b85df283 1108 // Relationship 3.
ac8aaa49 1109 $this->callAPISuccess($this->_entity, 'create',
1110 array_merge($this->_params, array(
1111 'relationship_type_id' => $relType3,
21dfd5f5 1112 'contact_id_b' => $org3,
5896d037 1113 ))
ac8aaa49 1114 );
1115
1116 $result = $this->callAPISuccess($this->_entity, 'get', array(
1117 'contact_id' => $this->_cId_a,
1118 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1119 ));
1120
1121 $this->assertEquals(2, $result['count']);
1122 foreach ($result['values'] as $key => $value) {
1123 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1124 }
1125 }
6a488035 1126
c9c41397 1127 /**
fe482240
EM
1128 * Test relationship get by membership type.
1129 *
c9c41397 1130 * Checks that passing in 'contact_id_b' + a relationship type
1131 * will filter by relationship type for contact b
1132 *
1133 * We should get 1 result without or with correct relationship type id & 0 with
1134 * an incorrect one
1135 */
00be9182 1136 public function testGetRelationshipByMembershipTypeDAO() {
b85df283 1137 $this->callAPISuccess($this->_entity, 'create', $this->_params);
c9c41397 1138 $org3 = $this->organizationCreate();
1139
1140 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1141 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1142 $relType1 = 1;
1143 $memberType = $this->membershipTypeCreate(array(
1144 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1145 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1146 ));
1147
b85df283 1148 // Relationship 2.
c9c41397 1149 $this->callAPISuccess($this->_entity, 'create',
1150 array_merge($this->_params, array(
1151 'relationship_type_id' => $relType2,
21dfd5f5 1152 'contact_id_b' => $this->_cId_b2,
5896d037 1153 ))
c9c41397 1154 );
1155
b85df283 1156 // Relationship 3.
c9c41397 1157 $this->callAPISuccess($this->_entity, 'create',
1158 array_merge($this->_params, array(
1159 'relationship_type_id' => $relType3,
21dfd5f5 1160 'contact_id_b' => $org3,
5896d037 1161 ))
c9c41397 1162 );
1163
b85df283 1164 // Relationship 4 with reversal.
c9c41397 1165 $this->callAPISuccess($this->_entity, 'create',
1166 array_merge($this->_params, array(
1167 'relationship_type_id' => $relType1,
1168 'contact_id_a' => $this->_cId_a,
21dfd5f5 1169 'contact_id_b' => $this->_cId_a_2,
5896d037 1170 ))
c9c41397 1171 );
1172
1173 $result = $this->callAPISuccess($this->_entity, 'get', array(
1174 'contact_id_a' => $this->_cId_a,
1175 'membership_type_id' => $memberType,
1176 ));
1177 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1178 $this->assertEquals(1, $result['count']);
1179 foreach ($result['values'] as $key => $value) {
1180 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1181 }
1182 }
1183
1184 /**
1185 * Checks that passing in 'contact_id_b' + a relationship type
1186 * will filter by relationship type for contact b
1187 *
1188 * We should get 1 result without or with correct relationship type id & 0 with
1189 * an incorrect one
1190 */
00be9182 1191 public function testGetRelationshipByMembershipTypeReciprocal() {
b85df283 1192 $this->callAPISuccess($this->_entity, 'create', $this->_params);
c9c41397 1193 $org3 = $this->organizationCreate();
1194
b85df283
EM
1195 // Let's just assume built in ones aren't being messed with!
1196 $relType2 = 5;
1197 $relType3 = 6;
c9c41397 1198 $relType1 = 1;
1199 $memberType = $this->membershipTypeCreate(array(
1200 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1201 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1202 ));
1203
b85df283 1204 // Relationship 2.
c9c41397 1205 $this->callAPISuccess($this->_entity, 'create',
1206 array_merge($this->_params, array(
1207 'relationship_type_id' => $relType2,
21dfd5f5 1208 'contact_id_b' => $this->_cId_b2,
5896d037 1209 ))
c9c41397 1210 );
1211
b85df283 1212 // Relationship 4.
c9c41397 1213 $this->callAPISuccess($this->_entity, 'create',
1214 array_merge($this->_params, array(
1215 'relationship_type_id' => $relType3,
21dfd5f5 1216 'contact_id_b' => $org3,
5896d037 1217 ))
c9c41397 1218 );
1219
b85df283 1220 // Relationship 4 with reversal.
c9c41397 1221 $this->callAPISuccess($this->_entity, 'create',
1222 array_merge($this->_params, array(
1223 'relationship_type_id' => $relType1,
1224 'contact_id_a' => $this->_cId_a,
21dfd5f5 1225 'contact_id_b' => $this->_cId_a_2,
5896d037 1226 ))
c9c41397 1227 );
1228
1229 $result = $this->callAPISuccess($this->_entity, 'get', array(
1230 'contact_id' => $this->_cId_a,
1231 'membership_type_id' => $memberType,
1232 ));
b85df283 1233 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
c9c41397 1234 $this->assertEquals(2, $result['count']);
1235
1236 foreach ($result['values'] as $key => $value) {
1237 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1238 }
1239 }
a7361ba3
EM
1240
1241 /**
b85df283 1242 * Check for e-notices on enable & disable as reported in CRM-14350
a7361ba3 1243 */
00be9182 1244 public function testSetActive() {
a7361ba3
EM
1245 $relationship = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1246 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1247 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1248 }
96025800 1249
7a44a255
EM
1250 /**
1251 * Test creating related memberships.
1252 */
1253 public function testCreateRelatedMembership() {
1254 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1255 'name' => 'Membership with Related',
1256 'member_of_contact_id' => 1,
1257 'financial_type_id' => 1,
1258 'minimum_fee' => 0.00,
1259 'duration_unit' => 'year',
1260 'duration_interval' => 1,
1261 'period_type' => 'rolling',
1262 'relationship_type_id' => $this->_relTypeID,
1263 'relationship_direction' => 'b_a',
1264 'visibility' => 'Public',
1265 'auto_renew' => 0,
1266 'is_active' => 1,
1267 'domain_id' => CRM_Core_Config::domainID(),
1268 ));
1269 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1270 'membership_type_id' => $relatedMembershipType['id'],
1271 'contact_id' => $this->_cId_b,
1272 ));
1273 $this->callAPISuccess('Relationship', 'create', array(
1274 'relationship_type_id' => $this->_relTypeID,
1275 'contact_id_a' => $this->_cId_a,
1276 'contact_id_b' => $this->_cId_b,
1277 ));
1278 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a));
1279 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
45089d88
CW
1280
1281 // Adding a relationship with a future start date should NOT create a membership
1282 $this->callAPISuccess('Relationship', 'create', array(
1283 'relationship_type_id' => $this->_relTypeID,
1284 'contact_id_a' => $this->_cId_a_2,
1285 'contact_id_b' => $this->_cId_b,
1286 'start_date' => 'now + 1 week',
1287 ));
1288 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2), 0);
1289
62118c6d
EM
1290 // Deleting the organization should cause the related membership to be deleted.
1291 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b));
1292 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a), 0);
7a44a255
EM
1293 }
1294
c9c41397 1295}