Merge pull request #11154 from agileware/CRM-20421
[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
d6238393
JV
781 /**
782 * Chain Relationship.get and to Contact.get.
783 */
784 public function testRelationshipGetWithChainedCall() {
785 // Create a relationship.
a457852d 786 $createResult = $this->callAPISuccess('relationship', 'create', $this->_params);
d6238393
JV
787 $id = $createResult['id'];
788
789 // Try to retrieve it using chaining.
790 $params = array(
791 'relationship_type_id' => $this->_relTypeID,
792 'id' => $id,
793 'api.Contact.get' => array(
794 'id' => '$value.contact_id_b',
795 ),
796 );
797
798 $result = $this->callAPISuccess('relationship', 'get', $params);
799
800 $this->assertEquals(1, $result['count']);
801 $relationship = CRM_Utils_Array::first($result['values']);
802 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
803 $contact = CRM_Utils_Array::first($relationship['api.Contact.get']['values']);
804 $this->assertEquals($this->_cId_b, $contact['id']);
805 }
806
807 /**
808 * Chain Contact.get to Relationship.get and again to Contact.get.
809 */
810 public function testRelationshipGetInChainedCall() {
811 // Create a relationship.
a457852d 812 $this->callAPISuccess('relationship', 'create', $this->_params);
d6238393
JV
813
814 // Try to retrieve it using chaining.
815 $params = array(
816 'id' => $this->_cId_a,
817 'api.Relationship.get' => array(
818 'relationship_type_id' => $this->_relTypeID,
819 'contact_id_a' => '$value.id',
820 'api.Contact.get' => array(
821 'id' => '$value.contact_id_b',
822 ),
823 ),
824 );
825
826 $result = $this->callAPISuccess('contact', 'get', $params);
827 $this->assertEquals(1, $result['count']);
828 $contact = CRM_Utils_Array::first($result['values']);
829 $this->assertEquals(1, $contact['api.Relationship.get']['count']);
830 $relationship = CRM_Utils_Array::first($contact['api.Relationship.get']['values']);
831 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
832 $contact = CRM_Utils_Array::first($relationship['api.Contact.get']['values']);
833 $this->assertEquals($this->_cId_b, $contact['id']);
834 }
835
6a488035 836 /**
100fef9d 837 * Check with valid params array.
6a488035
TO
838 * (The get function will behave differently without 'contact_id' passed
839 */
00be9182 840 public function testRelationshipsGetGeneric() {
6a488035
TO
841 $relParams = array(
842 'contact_id_a' => $this->_cId_a,
843 'contact_id_b' => $this->_cId_b,
844 'relationship_type_id' => $this->_relTypeID,
845 'start_date' => '2011-01-01',
846 'end_date' => '2013-01-01',
847 'is_active' => 1,
6a488035
TO
848 );
849
b85df283 850 $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
851
852 //get relationship
853 $params = array(
854 'contact_id_b' => $this->_cId_b,
6a488035 855 );
b85df283 856 $this->callAPISuccess('relationship', 'get', $params);
6a488035
TO
857 }
858
b85df283
EM
859 /**
860 * Test retrieving only current relationships.
861 */
00be9182 862 public function testGetIsCurrent() {
6c6e6187 863 $rel2Params = array(
6a488035
TO
864 'contact_id_a' => $this->_cId_a,
865 'contact_id_b' => $this->_cId_b2,
866 'relationship_type_id' => $this->_relTypeID,
867 'start_date' => '2008-12-20',
868 'is_active' => 0,
6a488035 869 );
b85df283 870 $this->callAPISuccess('relationship', 'create', $rel2Params);
26dcc9eb 871 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
872
6a488035 873 $getParams = array(
21dfd5f5 874 'filters' => array('is_current' => 1),
6a488035 875 );
5c49fee0 876 $description = "Demonstrates is_current filter.";
6a488035
TO
877 $subfile = 'filterIsCurrent';
878 //no relationship has been created
26dcc9eb 879 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
880 $this->assertEquals($result['count'], 1);
881 $this->AssertEquals($rel1['id'], $result['id']);
882
883 // now try not started
6c6e6187
TO
884 $rel2Params['is_active'] = 1;
885 $rel2Params['start_date'] = 'tomorrow';
b85df283 886 $this->callAPISuccess('relationship', 'create', $rel2Params);
26dcc9eb 887 $result = $this->callAPISuccess('relationship', 'get', $getParams);
6a488035
TO
888 $this->assertEquals($result['count'], 1);
889 $this->AssertEquals($rel1['id'], $result['id']);
890
891 // now try finished
6c6e6187
TO
892 $rel2Params['is_active'] = 1;
893 $rel2Params['start_date'] = 'last week';
894 $rel2Params['end_date'] = 'yesterday';
b85df283 895 $this->callAPISuccess('relationship', 'create', $rel2Params);
6a488035 896 }
5896d037 897
408b79bf 898 /**
eceb18cc 899 * Test using various operators.
6a488035 900 */
00be9182 901 public function testGetTypeOperators() {
6a488035 902 $relTypeParams = array(
5896d037
TO
903 'name_a_b' => 'Relation 3 for delete',
904 'name_b_a' => 'Relation 6 for delete',
905 'description' => 'Testing relationship type 2',
906 'contact_type_a' => 'Individual',
907 'contact_type_b' => 'Organization',
908 'is_reserved' => 1,
909 'is_active' => 1,
6a488035
TO
910 );
911 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
912 $relTypeParams = array(
5896d037
TO
913 'name_a_b' => 'Relation 8 for delete',
914 'name_b_a' => 'Relation 9 for delete',
915 'description' => 'Testing relationship type 7',
916 'contact_type_a' => 'Individual',
917 'contact_type_b' => 'Organization',
918 'is_reserved' => 1,
919 'is_active' => 1,
6a488035
TO
920 );
921 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
922
923 $relTypeParams = array(
5896d037
TO
924 'name_a_b' => 'Relation 6 for delete',
925 'name_b_a' => 'Relation 88for delete',
926 'description' => 'Testing relationship type 00',
927 'contact_type_a' => 'Individual',
928 'contact_type_b' => 'Organization',
929 'is_reserved' => 1,
930 'is_active' => 1,
6a488035
TO
931 );
932 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
933
26dcc9eb 934 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
935 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
6c6e6187 936 array('relationship_type_id' => $relationType2)));
26dcc9eb 937 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
5896d037 938 array('relationship_type_id' => $relationType3)));
26dcc9eb 939 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
5896d037 940 array('relationship_type_id' => $relationType4)));
6a488035
TO
941
942 $getParams = array(
21dfd5f5 943 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
6a488035
TO
944 );
945
5c49fee0 946 $description = "Demonstrates use of IN filter.";
6a488035
TO
947 $subfile = 'INRelationshipType';
948
26dcc9eb 949 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
950 $this->assertEquals($result['count'], 2);
951 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
952
5c49fee0 953 $description = "Demonstrates use of NOT IN filter.";
6a488035
TO
954 $subfile = 'NotInRelationshipType';
955 $getParams = array(
21dfd5f5 956 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
6a488035 957 );
26dcc9eb 958 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
959 $this->assertEquals($result['count'], 2);
960 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
961
5c49fee0 962 $description = "Demonstrates use of BETWEEN filter.";
6a488035
TO
963 $subfile = 'BetweenRelationshipType';
964 $getParams = array(
21dfd5f5 965 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
6a488035 966 );
26dcc9eb 967 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
968 $this->assertEquals($result['count'], 3);
969 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
970
5c49fee0 971 $description = "Demonstrates use of Not BETWEEN filter.";
6a488035
TO
972 $subfile = 'NotBetweenRelationshipType';
973 $getParams = array(
21dfd5f5 974 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
6a488035 975 );
26dcc9eb 976 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 977 $this->assertEquals($result['count'], 1);
6c6e6187 978 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
6a488035
TO
979
980 }
5896d037 981
6a488035 982 /**
eceb18cc 983 * Check with invalid relationshipType Id.
6a488035 984 */
00be9182 985 public function testRelationshipTypeAddInvalidId() {
6a488035
TO
986 $relTypeParams = array(
987 'id' => 'invalid',
988 'name_a_b' => 'Relation 1 for delete',
989 'name_b_a' => 'Relation 2 for delete',
990 'contact_type_a' => 'Individual',
991 'contact_type_b' => 'Organization',
6a488035 992 );
b85df283 993 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
75638074 994 'id is not a valid integer');
6a488035
TO
995 }
996
6a488035 997 /**
fe482240 998 * Check with valid data with contact_b.
6a488035 999 */
00be9182 1000 public function testGetRelationshipWithContactB() {
6a488035
TO
1001 $relParams = array(
1002 'contact_id_a' => $this->_cId_a,
1003 'contact_id_b' => $this->_cId_b,
1004 'relationship_type_id' => $this->_relTypeID,
1005 'start_date' => '2011-01-01',
1006 'end_date' => '2013-01-01',
1007 'is_active' => 1,
6a488035
TO
1008 );
1009
26dcc9eb 1010 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
1011
1012 $contacts = array(
1013 'contact_id' => $this->_cId_a,
6a488035
TO
1014 );
1015
26dcc9eb 1016 $result = $this->callAPISuccess('relationship', 'get', $contacts);
75638074 1017 $this->assertGreaterThan(0, $result['count']);
6a488035
TO
1018 $params = array(
1019 'id' => $relationship['id'],
6a488035 1020 );
7c8ae32a 1021 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
1022 $this->relationshipTypeDelete($this->_relTypeID);
1023 }
1024
1025 /**
eceb18cc 1026 * Check with valid data with relationshipTypes.
6a488035 1027 */
00be9182 1028 public function testGetRelationshipWithRelTypes() {
6a488035
TO
1029 $relParams = array(
1030 'contact_id_a' => $this->_cId_a,
1031 'contact_id_b' => $this->_cId_b,
1032 'relationship_type_id' => $this->_relTypeID,
1033 'start_date' => '2011-01-01',
1034 'end_date' => '2013-01-01',
1035 'is_active' => 1,
6a488035
TO
1036 );
1037
26dcc9eb 1038 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
1039
1040 $contact_a = array(
1041 'contact_id' => $this->_cId_a,
6a488035 1042 );
b85df283 1043 $this->callAPISuccess('relationship', 'get', $contact_a);
6a488035
TO
1044
1045 $params = array(
1046 'id' => $relationship['id'],
6a488035 1047 );
b85df283 1048 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
1049 $this->relationshipTypeDelete($this->_relTypeID);
1050 }
75638074 1051
1052 /**
1053 * Checks that passing in 'contact_id' + a relationship type
1054 * will filter by relationship type (relationships go in both directions)
1055 * as relationship api does a reciprocal check if contact_id provided
1056 *
1057 * We should get 1 result without or with correct relationship type id & 0 with
1058 * an incorrect one
1059 */
00be9182 1060 public function testGetRelationshipByTypeReciprocal() {
75638074 1061 $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1062 $result = $this->callAPISuccess($this->_entity, 'get', array(
1063 'contact_id' => $this->_cId_a,
1064 'relationship_type_id' => $this->_relTypeID,
1065 ));
1066 $this->assertEquals(1, $result['count']);
1067 $result = $this->callAPISuccess($this->_entity, 'get', array(
1068 'contact_id' => $this->_cId_a,
1069 'relationship_type_id' => $this->_relTypeID + 1,
1070 ));
1071 $this->assertEquals(0, $result['count']);
1072 $this->callAPISuccess($this->_entity, 'delete', array('id' => $created['id']));
1073 }
1074
1075 /**
1076 * Checks that passing in 'contact_id_b' + a relationship type
1077 * will filter by relationship type for contact b
1078 *
1079 * We should get 1 result without or with correct relationship type id & 0 with
1080 * an incorrect one
1081 */
00be9182 1082 public function testGetRelationshipByTypeDAO() {
b85df283
EM
1083 $this->_ids['relationship'] = $this->callAPISuccess($this->_entity, 'create', array('format.only_id' => TRUE) +
1084 $this->_params);
1085 $this->callAPISuccess($this->_entity, 'getcount', array(
5896d037
TO
1086 'contact_id_a' => $this->_cId_a,
1087 ),
1088 1);
75638074 1089 $result = $this->callAPISuccess($this->_entity, 'get', array(
1090 'contact_id_a' => $this->_cId_a,
1091 'relationship_type_id' => $this->_relTypeID,
1092 ));
1093 $this->assertEquals(1, $result['count']);
1094 $result = $this->callAPISuccess($this->_entity, 'get', array(
1095 'contact_id_a' => $this->_cId_a,
1096 'relationship_type_id' => $this->_relTypeID + 1,
1097 ));
1098 $this->assertEquals(0, $result['count']);
1099 }
1100
1101 /**
1102 * Checks that passing in 'contact_id_b' + a relationship type
1103 * will filter by relationship type for contact b
1104 *
1105 * We should get 1 result without or with correct relationship type id & 0 with
1106 * an incorrect one
1107 */
00be9182 1108 public function testGetRelationshipByTypeArrayDAO() {
b85df283 1109 $this->callAPISuccess($this->_entity, 'create', $this->_params);
75638074 1110 $org3 = $this->organizationCreate();
1111 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1112 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1113
b85df283 1114 // Relationship 2.
75638074 1115 $this->callAPISuccess($this->_entity, 'create',
1116 array_merge($this->_params, array(
1117 'relationship_type_id' => $relType2,
21dfd5f5 1118 'contact_id_b' => $this->_cId_b2,
5896d037 1119 ))
75638074 1120 );
1121
b85df283 1122 // Relationship 3.
75638074 1123 $this->callAPISuccess($this->_entity, 'create',
1124 array_merge($this->_params, array(
1125 'relationship_type_id' => $relType3,
21dfd5f5 1126 'contact_id_b' => $org3,
5896d037 1127 ))
75638074 1128 );
1129
1130 $result = $this->callAPISuccess($this->_entity, 'get', array(
1131 'contact_id_a' => $this->_cId_a,
1132 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1133 ));
1134
1135 $this->assertEquals(2, $result['count']);
1136 foreach ($result['values'] as $key => $value) {
1137 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1138 }
1139 }
ac8aaa49 1140
1141 /**
1142 * Checks that passing in 'contact_id_b' + a relationship type
1143 * will filter by relationship type for contact b
1144 *
1145 * We should get 1 result without or with correct relationship type id & 0 with
1146 * an incorrect one
1147 */
00be9182 1148 public function testGetRelationshipByTypeArrayReciprocal() {
b85df283 1149 $this->callAPISuccess($this->_entity, 'create', $this->_params);
ac8aaa49 1150 $org3 = $this->organizationCreate();
b85df283
EM
1151 // lets just assume built in ones aren't being messed with!
1152 $relType2 = 5;
1153 $relType3 = 6;
ac8aaa49 1154
b85df283 1155 // Relationship 2.
ac8aaa49 1156 $this->callAPISuccess($this->_entity, 'create',
1157 array_merge($this->_params, array(
1158 'relationship_type_id' => $relType2,
21dfd5f5 1159 'contact_id_b' => $this->_cId_b2,
5896d037 1160 ))
ac8aaa49 1161 );
1162
b85df283 1163 // Relationship 3.
ac8aaa49 1164 $this->callAPISuccess($this->_entity, 'create',
1165 array_merge($this->_params, array(
1166 'relationship_type_id' => $relType3,
21dfd5f5 1167 'contact_id_b' => $org3,
5896d037 1168 ))
ac8aaa49 1169 );
1170
1171 $result = $this->callAPISuccess($this->_entity, 'get', array(
1172 'contact_id' => $this->_cId_a,
1173 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
1174 ));
1175
1176 $this->assertEquals(2, $result['count']);
1177 foreach ($result['values'] as $key => $value) {
1178 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
1179 }
1180 }
6a488035 1181
c9c41397 1182 /**
fe482240
EM
1183 * Test relationship get by membership type.
1184 *
c9c41397 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 testGetRelationshipByMembershipTypeDAO() {
b85df283 1192 $this->callAPISuccess($this->_entity, 'create', $this->_params);
c9c41397 1193 $org3 = $this->organizationCreate();
1194
1195 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1196 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1197 $relType1 = 1;
1198 $memberType = $this->membershipTypeCreate(array(
1199 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1200 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1201 ));
1202
b85df283 1203 // Relationship 2.
c9c41397 1204 $this->callAPISuccess($this->_entity, 'create',
1205 array_merge($this->_params, array(
1206 'relationship_type_id' => $relType2,
21dfd5f5 1207 'contact_id_b' => $this->_cId_b2,
5896d037 1208 ))
c9c41397 1209 );
1210
b85df283 1211 // Relationship 3.
c9c41397 1212 $this->callAPISuccess($this->_entity, 'create',
1213 array_merge($this->_params, array(
1214 'relationship_type_id' => $relType3,
21dfd5f5 1215 'contact_id_b' => $org3,
5896d037 1216 ))
c9c41397 1217 );
1218
b85df283 1219 // Relationship 4 with reversal.
c9c41397 1220 $this->callAPISuccess($this->_entity, 'create',
1221 array_merge($this->_params, array(
1222 'relationship_type_id' => $relType1,
1223 'contact_id_a' => $this->_cId_a,
21dfd5f5 1224 'contact_id_b' => $this->_cId_a_2,
5896d037 1225 ))
c9c41397 1226 );
1227
1228 $result = $this->callAPISuccess($this->_entity, 'get', array(
1229 'contact_id_a' => $this->_cId_a,
1230 'membership_type_id' => $memberType,
1231 ));
1232 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1233 $this->assertEquals(1, $result['count']);
1234 foreach ($result['values'] as $key => $value) {
1235 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1236 }
1237 }
1238
1239 /**
1240 * Checks that passing in 'contact_id_b' + a relationship type
1241 * will filter by relationship type for contact b
1242 *
1243 * We should get 1 result without or with correct relationship type id & 0 with
1244 * an incorrect one
1245 */
00be9182 1246 public function testGetRelationshipByMembershipTypeReciprocal() {
b85df283 1247 $this->callAPISuccess($this->_entity, 'create', $this->_params);
c9c41397 1248 $org3 = $this->organizationCreate();
1249
b85df283
EM
1250 // Let's just assume built in ones aren't being messed with!
1251 $relType2 = 5;
1252 $relType3 = 6;
c9c41397 1253 $relType1 = 1;
1254 $memberType = $this->membershipTypeCreate(array(
1255 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1256 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1257 ));
1258
b85df283 1259 // Relationship 2.
c9c41397 1260 $this->callAPISuccess($this->_entity, 'create',
1261 array_merge($this->_params, array(
1262 'relationship_type_id' => $relType2,
21dfd5f5 1263 'contact_id_b' => $this->_cId_b2,
5896d037 1264 ))
c9c41397 1265 );
1266
b85df283 1267 // Relationship 4.
c9c41397 1268 $this->callAPISuccess($this->_entity, 'create',
1269 array_merge($this->_params, array(
1270 'relationship_type_id' => $relType3,
21dfd5f5 1271 'contact_id_b' => $org3,
5896d037 1272 ))
c9c41397 1273 );
1274
b85df283 1275 // Relationship 4 with reversal.
c9c41397 1276 $this->callAPISuccess($this->_entity, 'create',
1277 array_merge($this->_params, array(
1278 'relationship_type_id' => $relType1,
1279 'contact_id_a' => $this->_cId_a,
21dfd5f5 1280 'contact_id_b' => $this->_cId_a_2,
5896d037 1281 ))
c9c41397 1282 );
1283
1284 $result = $this->callAPISuccess($this->_entity, 'get', array(
1285 'contact_id' => $this->_cId_a,
1286 'membership_type_id' => $memberType,
1287 ));
b85df283 1288 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
c9c41397 1289 $this->assertEquals(2, $result['count']);
1290
1291 foreach ($result['values'] as $key => $value) {
1292 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1293 }
1294 }
a7361ba3
EM
1295
1296 /**
b85df283 1297 * Check for e-notices on enable & disable as reported in CRM-14350
a7361ba3 1298 */
00be9182 1299 public function testSetActive() {
a7361ba3
EM
1300 $relationship = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1301 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1302 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1303 }
96025800 1304
7a44a255
EM
1305 /**
1306 * Test creating related memberships.
1307 */
1308 public function testCreateRelatedMembership() {
1309 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1310 'name' => 'Membership with Related',
1311 'member_of_contact_id' => 1,
1312 'financial_type_id' => 1,
1313 'minimum_fee' => 0.00,
1314 'duration_unit' => 'year',
1315 'duration_interval' => 1,
1316 'period_type' => 'rolling',
1317 'relationship_type_id' => $this->_relTypeID,
1318 'relationship_direction' => 'b_a',
1319 'visibility' => 'Public',
1320 'auto_renew' => 0,
1321 'is_active' => 1,
1322 'domain_id' => CRM_Core_Config::domainID(),
1323 ));
1324 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1325 'membership_type_id' => $relatedMembershipType['id'],
1326 'contact_id' => $this->_cId_b,
1327 ));
1328 $this->callAPISuccess('Relationship', 'create', array(
1329 'relationship_type_id' => $this->_relTypeID,
1330 'contact_id_a' => $this->_cId_a,
1331 'contact_id_b' => $this->_cId_b,
1332 ));
1333 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a));
1334 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
45089d88
CW
1335
1336 // Adding a relationship with a future start date should NOT create a membership
1337 $this->callAPISuccess('Relationship', 'create', array(
1338 'relationship_type_id' => $this->_relTypeID,
1339 'contact_id_a' => $this->_cId_a_2,
1340 'contact_id_b' => $this->_cId_b,
1341 'start_date' => 'now + 1 week',
1342 ));
1343 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2), 0);
1344
62118c6d
EM
1345 // Deleting the organization should cause the related membership to be deleted.
1346 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b));
1347 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a), 0);
7a44a255
EM
1348 }
1349
c9c41397 1350}