3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_activity_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
36 * Class api_v3_AddressTest
39 class api_v3_AddressTest
extends CiviUnitTestCase
{
40 protected $_apiversion = 3;
41 protected $_contactID;
42 protected $_locationType;
47 public function setUp() {
48 $this->_entity
= 'Address';
51 $this->_contactID
= $this->organizationCreate();
52 $this->_locationType
= $this->locationTypeCreate();
53 CRM_Core_PseudoConstant
::flush();
55 $this->_params
= array(
56 'contact_id' => $this->_contactID
,
57 'location_type_id' => $this->_locationType
->id
,
58 'street_name' => 'Ambachtstraat',
59 'street_number' => '23',
60 'street_address' => 'Ambachtstraat 23',
61 'postal_code' => '6971 BN',
62 'country_id' => '1152',
68 public function tearDown() {
69 $this->locationTypeDelete($this->_locationType
->id
);
70 $this->contactDelete($this->_contactID
);
71 $this->quickCleanup(array('civicrm_address', 'civicrm_relationship'));
74 public function testCreateAddress() {
75 $result = $this->callAPIAndDocument('address', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
76 $this->assertEquals(1, $result['count']);
77 $this->assertNotNull($result['values'][$result['id']]['id']);
78 $this->getAndCheck($this->_params
, $result['id'], 'address');
81 public function testCreateAddressParsing() {
83 'street_parsing' => 1,
84 'street_address' => '54A Excelsior Ave. Apt 1C',
85 'location_type_id' => $this->_locationType
->id
,
86 'contact_id' => $this->_contactID
,
88 $subfile = "AddressParse";
89 $description = "Demonstrates Use of address parsing param.";
90 $result = $this->callAPIAndDocument('address', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
91 $this->assertEquals(54, $result['values'][$result['id']]['street_number']);
92 $this->assertEquals('A', $result['values'][$result['id']]['street_number_suffix']);
93 $this->assertEquals('Excelsior Ave.', $result['values'][$result['id']]['street_name']);
94 $this->assertEquals('Apt 1C', $result['values'][$result['id']]['street_unit']);
95 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
100 * Is_primary should be set as a default.
102 public function testCreateAddressTestDefaults() {
103 $params = $this->_params
;
104 unset($params['is_primary']);
105 $result = $this->callAPISuccess('address', 'create', $params);
106 $this->assertEquals(1, $result['count']);
107 $this->assertEquals(1, $result['values'][$result['id']]['is_primary']);
108 $this->getAndCheck($this->_params
, $result['id'], 'address');
111 public function testCreateAddressTooLongSuffix() {
112 $params = $this->_params
;
113 $params['street_number_suffix'] = 'really long string';
114 $this->callAPIFailure('address', 'create', $params);
118 * Create an address with a master ID and ensure that a relationship is created.
120 public function testCreateAddressWithMasterRelationshipHousehold() {
121 $householdID = $this->householdCreate();
122 $address = $this->callAPISuccess('address', 'create', array_merge($this->_params
, $this->_params
, array('contact_id' => $householdID)));
123 $individualID = $this->individualCreate();
124 $individualParams = array(
125 'contact_id' => $individualID,
126 'master_id' => $address['id'],
128 $this->callAPISuccess('address', 'create', array_merge($this->_params
, $individualParams));
129 $this->callAPISuccess('relationship', 'getcount', array(
130 'contact_id_a' => $individualID,
131 'contact_id_b' => $this->_contactID
,
136 * Create an address with a master ID and ensure that a relationship is created.
138 public function testCreateAddressWithMasterRelationshipOrganization() {
139 $address = $this->callAPISuccess('address', 'create', $this->_params
);
140 $individualID = $this->individualCreate();
141 $individualParams = array(
142 'contact_id' => $individualID,
143 'master_id' => $address['id'],
145 $this->callAPISuccess('address', 'create', array_merge($this->_params
, $individualParams));
146 $this->callAPISuccess('relationship', 'getcount', array(
147 'contact_id_a' => $individualID,
148 'contact_id_b' => $this->_contactID
,
153 * Create an address with a master ID and ensure that a relationship is created.
155 public function testCreateAddressWithMasterRelationshipChangingOrganization() {
156 $address = $this->callAPISuccess('address', 'create', $this->_params
);
157 $organisation2ID = $this->organizationCreate();
158 $address2 = $this->callAPISuccess('address', 'create', array_merge($this->_params
, array('contact_id' => $organisation2ID)));
159 $individualID = $this->individualCreate();
160 $individualParams = array_merge($this->_params
, array(
161 'contact_id' => $individualID,
162 'master_id' => $address['id'],
164 $individualAddress = $this->callAPISuccess('address', 'create', $individualParams);
165 $individualParams['master_id'] = $address2['id'];
166 $individualParams['id'] = $individualAddress['id'];
167 $this->callAPISuccess('address', 'create', $individualParams);
168 $this->callAPISuccessGetCount('relationship', array('contact_id_a' => $individualID), 2);
169 $this->markTestIncomplete('Remainder of test checks that employer relationship is disabled when new one is created but turns out to be not happening - by design?');
170 $this->callAPISuccessGetCount('relationship', array('contact_id_a' => $individualID, 'is_active' => FALSE), 1);
171 $this->callAPISuccessGetCount('relationship', array(
172 'contact_id_a' => $individualID,
174 'contact_id_b' => $organisation2ID,
180 * Is_primary should be set as a default.
182 * ie. create the address, unset the params & recreate.
183 * is_primary should be 0 before & after the update. ie - having no other address
184 * is_primary is invalid.
186 public function testCreateAddressTestDefaultWithID() {
187 $params = $this->_params
;
188 $params['is_primary'] = 0;
189 $result = $this->callAPISuccess('address', 'create', $params);
190 unset($params['is_primary']);
191 $params['id'] = $result['id'];
192 $result = $this->callAPISuccess('address', 'create', $params);
193 $this->callAPISuccess('address', 'get', array('contact_id' => $params['contact_id']));
194 $this->assertEquals(1, $result['count']);
195 $this->assertEquals(1, $result['values'][$result['id']]['is_primary']);
196 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__
);
200 * test address deletion.
202 public function testDeleteAddress() {
203 //check there are no address to start with
204 $get = $this->callAPISuccess('address', 'get', array(
205 'location_type_id' => $this->_locationType
->id
,
207 $this->assertEquals(0, $get['count'], 'Contact already exists ');
210 $create = $this->callAPISuccess('address', 'create', $this->_params
);
212 $result = $this->callAPIAndDocument('address', 'delete', array('id' => $create['id']), __FUNCTION__
, __FILE__
);
213 $this->assertEquals(1, $result['count']);
214 $get = $this->callAPISuccess('address', 'get', array(
215 'location_type_id' => $this->_locationType
->id
,
217 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
221 * Test civicrm_address_get - success expected.
223 public function testGetAddress() {
224 $address = $this->callAPISuccess('address', 'create', $this->_params
);
227 'contact_id' => $this->_contactID
,
228 'street_name' => $address['values'][$address['id']]['street_name'],
230 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
);
231 $this->callAPISuccess('Address', 'delete', array('id' => $result['id']));
232 $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id']);
233 $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary']);
234 $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address']);
238 * Test civicrm_address_get - success expected.
240 public function testGetSingleAddress() {
241 $this->callAPISuccess('address', 'create', $this->_params
);
243 'contact_id' => $this->_contactID
,
245 $address = $this->callAPISuccess('Address', 'getsingle', ($params));
246 $this->assertEquals($address['location_type_id'], $this->_params
['location_type_id']);
247 $this->callAPISuccess('address', 'delete', array('id' => $address['id']));
251 * Test civicrm_address_get with sort option- success expected.
253 public function testGetAddressSort() {
254 $create = $this->callAPISuccess('address', 'create', $this->_params
);
255 $this->callAPISuccess('address', 'create', array_merge($this->_params
, array('street_address' => 'yzy')));
256 $subfile = "AddressSort";
257 $description = "Demonstrates Use of sort filter.";
260 'sort' => 'street_address DESC',
265 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
266 $this->assertEquals(2, $result['count']);
267 $this->assertEquals('Ambachtstraat 23', $result['values'][1]['street_address']);
268 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
272 * Test civicrm_address_get with sort option- success expected.
274 public function testGetAddressLikeSuccess() {
275 $this->callAPISuccess('address', 'create', $this->_params
);
276 $subfile = "AddressLike";
277 $description = "Demonstrates Use of Like.";
279 'street_address' => array('LIKE' => '%mb%'),
282 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
283 $this->assertEquals(1, $result['count']);
284 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address']);
285 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
289 * Test civicrm_address_get with sort option- success expected.
291 public function testGetAddressLikeFail() {
292 $create = $this->callAPISuccess('address', 'create', $this->_params
);
294 'street_address' => array('LIKE' => "'%xy%'"),
297 $result = $this->callAPISuccess('Address', 'Get', ($params));
298 $this->assertEquals(0, $result['count']);
299 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
304 public function testGetWithCustom() {
305 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
307 $params = $this->_params
;
308 $params['custom_' . $ids['custom_field_id']] = "custom string";
310 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
312 $getParams = array('id' => $result['id'], 'return' => array('custom'));
313 $check = $this->callAPISuccess($this->_entity
, 'get', $getParams);
315 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
317 $this->customFieldDelete($ids['custom_field_id']);
318 $this->customGroupDelete($ids['custom_group_id']);
319 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
324 public function testCreateAddressPrimaryHandlingChangeToPrimary() {
325 $params = $this->_params
;
326 unset($params['is_primary']);
327 $address1 = $this->callAPISuccess('address', 'create', $params);
328 $this->assertApiSuccess($address1);
329 //now we check & make sure it has been set to primary
330 $check = $this->callAPISuccess('address', 'getcount', array(
332 'id' => $address1['id'],
334 $this->assertEquals(1, $check);
335 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
340 public function testCreateAddressPrimaryHandlingChangeExisting() {
341 $address1 = $this->callAPISuccess('address', 'create', $this->_params
);
342 $this->callAPISuccess('address', 'create', $this->_params
);
343 $check = $this->callAPISuccess('address', 'getcount', array(
345 'contact_id' => $this->_contactID
,
347 $this->assertEquals(1, $check);
348 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
352 * Test Creating address of same type alreay ind the database
353 * This is legacy API v3 behaviour and not correct behaviour
354 * however we are too far down the path wiwth v3 to fix this
355 * @link https://chat.civicrm.org/civicrm/pl/zcq3jkg69jdt5g4aqze6bbe9pc
356 * @todo vis this in v4 api
358 public function testCreateDuplicateLocationTypes() {
359 $address1 = $this->callAPISuccess('address', 'create', $this->_params
);
360 $address2 = $this->callAPISuccess('address', 'create', array(
361 'location_type_id' => $this->_locationType
->id
,
362 'street_address' => '1600 Pensilvania Avenue',
363 'city' => 'Washington DC',
366 'contact_id' => $this->_contactID
,
368 $check = $this->callAPISuccess('address', 'getcount', array(
369 'contact_id' => $this->_contactID
,
370 'location_type_id' => $this->_locationType
->id
,
372 $this->assertEquals(2, $check);
373 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
374 $this->callAPISuccess('address', 'delete', array('id' => $address2['id']));
377 public function testGetWithJoin() {
378 $cid = $this->individualCreate(array(
379 'api.Address.create' => array(
380 'street_address' => __FUNCTION__
,
381 'location_type_id' => $this->_locationType
->id
,
384 $result = $this->callAPISuccess('address', 'getsingle', array(
385 'check_permissions' => TRUE,
386 'contact_id' => $cid,
387 'street_address' => __FUNCTION__
,
388 'return' => 'contact_id.contact_type',
390 $this->assertEquals('Individual', $result['contact_id.contact_type']);