Merge pull request #11642 from JKingsnorth/CRM-21739
[civicrm-core.git] / tests / phpunit / api / v3 / AddressTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
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 +--------------------------------------------------------------------+
26 */
27
28/**
29 * Test APIv3 civicrm_activity_* functions
30 *
6c6e6187
TO
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
6a488035
TO
33 */
34
4cbe18b8
EM
35/**
36 * Class api_v3_AddressTest
acb109b7 37 * @group headless
4cbe18b8 38 */
6a488035 39class api_v3_AddressTest extends CiviUnitTestCase {
6c6e6187 40 protected $_apiversion = 3;
6a488035
TO
41 protected $_contactID;
42 protected $_locationType;
43 protected $_params;
b7c9bc4c 44
430ae6dd
TO
45 protected $_entity;
46
00be9182 47 public function setUp() {
6a488035
TO
48 $this->_entity = 'Address';
49 parent::setUp();
50
51 $this->_contactID = $this->organizationCreate();
52 $this->_locationType = $this->locationTypeCreate();
2683ce94 53 CRM_Core_PseudoConstant::flush();
6a488035
TO
54
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',
63 'city' => 'Brummen',
64 'is_primary' => 1,
6a488035
TO
65 );
66 }
67
00be9182 68 public function tearDown() {
6a488035
TO
69 $this->locationTypeDelete($this->_locationType->id);
70 $this->contactDelete($this->_contactID);
a41f119c 71 $this->quickCleanup(array('civicrm_address', 'civicrm_relationship'));
6a488035
TO
72 }
73
74 public function testCreateAddress() {
4e420887 75 $result = $this->callAPIAndDocument('address', 'create', $this->_params, __FUNCTION__, __FILE__);
ba4a1892
TM
76 $this->assertEquals(1, $result['count']);
77 $this->assertNotNull($result['values'][$result['id']]['id']);
6a488035
TO
78 $this->getAndCheck($this->_params, $result['id'], 'address');
79 }
4e420887 80
6a488035
TO
81 public function testCreateAddressParsing() {
82 $params = array(
6a488035
TO
83 'street_parsing' => 1,
84 'street_address' => '54A Excelsior Ave. Apt 1C',
85 'location_type_id' => $this->_locationType->id,
86 'contact_id' => $this->_contactID,
87 );
5896d037 88 $subfile = "AddressParse";
5c49fee0 89 $description = "Demonstrates Use of address parsing param.";
4e420887 90 $result = $this->callAPIAndDocument('address', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
ba4a1892
TM
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']);
4e420887 95 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
6a488035
TO
96
97 }
98
c490a46a 99 /**
fe482240 100 * Is_primary should be set as a default.
c490a46a 101 */
6a488035
TO
102 public function testCreateAddressTestDefaults() {
103 $params = $this->_params;
104 unset($params['is_primary']);
4e420887 105 $result = $this->callAPISuccess('address', 'create', $params);
ba4a1892
TM
106 $this->assertEquals(1, $result['count']);
107 $this->assertEquals(1, $result['values'][$result['id']]['is_primary']);
6a488035
TO
108 $this->getAndCheck($this->_params, $result['id'], 'address');
109 }
110
111 public function testCreateAddressTooLongSuffix() {
112 $params = $this->_params;
113 $params['street_number_suffix'] = 'really long string';
a41f119c
EM
114 $this->callAPIFailure('address', 'create', $params);
115 }
116
117 /**
eceb18cc 118 * Create an address with a master ID and ensure that a relationship is created.
a41f119c
EM
119 */
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'],
127 );
128 $this->callAPISuccess('address', 'create', array_merge($this->_params, $individualParams));
5896d037 129 $this->callAPISuccess('relationship', 'getcount', array(
92915c55
TO
130 'contact_id_a' => $individualID,
131 'contact_id_b' => $this->_contactID,
132 ));
a41f119c
EM
133 }
134
135 /**
eceb18cc 136 * Create an address with a master ID and ensure that a relationship is created.
6c6e6187 137 */
a41f119c
EM
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'],
144 );
145 $this->callAPISuccess('address', 'create', array_merge($this->_params, $individualParams));
5896d037 146 $this->callAPISuccess('relationship', 'getcount', array(
92915c55
TO
147 'contact_id_a' => $individualID,
148 'contact_id_b' => $this->_contactID,
149 ));
a41f119c
EM
150 }
151
152 /**
eceb18cc 153 * Create an address with a master ID and ensure that a relationship is created.
a41f119c
EM
154 */
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'],
163 ));
164 $individualAddress = $this->callAPISuccess('address', 'create', $individualParams);
165 $individualParams['master_id'] = $address2['id'];
166 $individualParams['id'] = $individualAddress['id'];
167 $this->callAPISuccess('address', 'create', $individualParams);
6c6e6187 168 $this->callAPISuccessGetCount('relationship', array('contact_id_a' => $individualID), 2);
a41f119c
EM
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);
5896d037 171 $this->callAPISuccessGetCount('relationship', array(
92915c55
TO
172 'contact_id_a' => $individualID,
173 'is_active' => TRUE,
174 'contact_id_b' => $organisation2ID,
175 ), 1);
a41f119c
EM
176
177 }
6a488035 178
c490a46a 179 /**
fe482240
EM
180 * Is_primary should be set as a default.
181 *
182 * ie. create the address, unset the params & recreate.
c490a46a 183 * is_primary should be 0 before & after the update. ie - having no other address
fe482240 184 * is_primary is invalid.
c490a46a 185 */
6a488035
TO
186 public function testCreateAddressTestDefaultWithID() {
187 $params = $this->_params;
188 $params['is_primary'] = 0;
4e420887 189 $result = $this->callAPISuccess('address', 'create', $params);
6a488035
TO
190 unset($params['is_primary']);
191 $params['id'] = $result['id'];
5896d037 192 $result = $this->callAPISuccess('address', 'create', $params);
a41f119c
EM
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']);
6a488035
TO
196 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__);
197 }
6a488035 198
a41f119c 199 /**
eceb18cc 200 * test address deletion.
a41f119c
EM
201 */
202 public function testDeleteAddress() {
203 //check there are no address to start with
4e420887 204 $get = $this->callAPISuccess('address', 'get', array(
205 'location_type_id' => $this->_locationType->id,
206 ));
a41f119c 207 $this->assertEquals(0, $get['count'], 'Contact already exists ');
6a488035
TO
208
209 //create one
4e420887 210 $create = $this->callAPISuccess('address', 'create', $this->_params);
6a488035 211
6c6e6187 212 $result = $this->callAPIAndDocument('address', 'delete', array('id' => $create['id']), __FUNCTION__, __FILE__);
ba4a1892 213 $this->assertEquals(1, $result['count']);
4e420887 214 $get = $this->callAPISuccess('address', 'get', array(
5896d037 215 'location_type_id' => $this->_locationType->id,
4e420887 216 ));
6a488035
TO
217 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
218 }
219
220 /**
221 * Test civicrm_address_get - success expected.
222 */
223 public function testGetAddress() {
4e420887 224 $address = $this->callAPISuccess('address', 'create', $this->_params);
6a488035
TO
225
226 $params = array(
227 'contact_id' => $this->_contactID,
228 'street_name' => $address['values'][$address['id']]['street_name'],
6a488035 229 );
4e420887 230 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__);
231 $this->callAPISuccess('Address', 'delete', array('id' => $result['id']));
a41f119c
EM
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']);
6a488035
TO
235 }
236
237 /**
238 * Test civicrm_address_get - success expected.
239 */
240 public function testGetSingleAddress() {
4e420887 241 $this->callAPISuccess('address', 'create', $this->_params);
6a488035
TO
242 $params = array(
243 'contact_id' => $this->_contactID,
6a488035 244 );
4e420887 245 $address = $this->callAPISuccess('Address', 'getsingle', ($params));
ba4a1892 246 $this->assertEquals($address['location_type_id'], $this->_params['location_type_id']);
4e420887 247 $this->callAPISuccess('address', 'delete', array('id' => $address['id']));
6a488035
TO
248 }
249
250 /**
251 * Test civicrm_address_get with sort option- success expected.
252 */
253 public function testGetAddressSort() {
4e420887 254 $create = $this->callAPISuccess('address', 'create', $this->_params);
7ecf6275 255 $this->callAPISuccess('address', 'create', array_merge($this->_params, array('street_address' => 'yzy')));
5896d037 256 $subfile = "AddressSort";
5c49fee0 257 $description = "Demonstrates Use of sort filter.";
5896d037 258 $params = array(
6a488035
TO
259 'options' => array(
260 'sort' => 'street_address DESC',
261 'limit' => 2,
262 ),
6a488035
TO
263 'sequential' => 1,
264 );
4e420887 265 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
7ecf6275
EM
266 $this->assertEquals(2, $result['count']);
267 $this->assertEquals('Ambachtstraat 23', $result['values'][1]['street_address']);
4e420887 268 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
6a488035
TO
269 }
270
271 /**
272 * Test civicrm_address_get with sort option- success expected.
273 */
274 public function testGetAddressLikeSuccess() {
4e420887 275 $this->callAPISuccess('address', 'create', $this->_params);
5896d037 276 $subfile = "AddressLike";
5c49fee0 277 $description = "Demonstrates Use of Like.";
5896d037
TO
278 $params = array(
279 'street_address' => array('LIKE' => '%mb%'),
6a488035
TO
280 'sequential' => 1,
281 );
4e420887 282 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
ba4a1892
TM
283 $this->assertEquals(1, $result['count']);
284 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address']);
4e420887 285 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
6a488035
TO
286 }
287
288 /**
289 * Test civicrm_address_get with sort option- success expected.
290 */
291 public function testGetAddressLikeFail() {
4e420887 292 $create = $this->callAPISuccess('address', 'create', $this->_params);
5896d037 293 $params = array(
7ecf6275 294 'street_address' => array('LIKE' => "'%xy%'"),
6a488035
TO
295 'sequential' => 1,
296 );
4e420887 297 $result = $this->callAPISuccess('Address', 'Get', ($params));
ba4a1892 298 $this->assertEquals(0, $result['count']);
4e420887 299 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
6a488035
TO
300 }
301
a41f119c 302 /**
a41f119c 303 */
00be9182 304 public function testGetWithCustom() {
6a488035
TO
305 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
306
307 $params = $this->_params;
308 $params['custom_' . $ids['custom_field_id']] = "custom string";
309
4e420887 310 $result = $this->callAPISuccess($this->_entity, 'create', $params);
6a488035 311
4e420887 312 $getParams = array('id' => $result['id'], 'return' => array('custom'));
313 $check = $this->callAPISuccess($this->_entity, 'get', $getParams);
6a488035
TO
314
315 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
316
317 $this->customFieldDelete($ids['custom_field_id']);
318 $this->customGroupDelete($ids['custom_group_id']);
4e420887 319 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
6a488035
TO
320 }
321
a41f119c 322 /**
a41f119c 323 */
6a488035
TO
324 public function testCreateAddressPrimaryHandlingChangeToPrimary() {
325 $params = $this->_params;
326 unset($params['is_primary']);
4e420887 327 $address1 = $this->callAPISuccess('address', 'create', $params);
a15773db 328 $this->assertApiSuccess($address1);
6a488035 329 //now we check & make sure it has been set to primary
4e420887 330 $check = $this->callAPISuccess('address', 'getcount', array(
5896d037
TO
331 'is_primary' => 1,
332 'id' => $address1['id'],
333 ));
6a488035 334 $this->assertEquals(1, $check);
4e420887 335 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
6a488035 336 }
a41f119c
EM
337
338 /**
a41f119c 339 */
6a488035 340 public function testCreateAddressPrimaryHandlingChangeExisting() {
4e420887 341 $address1 = $this->callAPISuccess('address', 'create', $this->_params);
a41f119c 342 $this->callAPISuccess('address', 'create', $this->_params);
4e420887 343 $check = $this->callAPISuccess('address', 'getcount', array(
5896d037
TO
344 'is_primary' => 1,
345 'contact_id' => $this->_contactID,
346 ));
6a488035 347 $this->assertEquals(1, $check);
4e420887 348 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
6a488035 349 }
96025800 350
a5ac91a8
SL
351 /**
352 * Test Creating address of same type alreay ind the database
f623557b
SL
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
a5ac91a8
SL
357 */
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',
364 'is_primary' => 0,
365 'is_billing' => 0,
366 'contact_id' => $this->_contactID,
367 ));
368 $check = $this->callAPISuccess('address', 'getcount', array(
369 'contact_id' => $this->_contactID,
370 'location_type_id' => $this->_locationType->id,
371 ));
372 $this->assertEquals(2, $check);
373 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
374 $this->callAPISuccess('address', 'delete', array('id' => $address2['id']));
375 }
376
6f736521
CW
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,
382 ),
383 ));
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',
389 ));
390 $this->assertEquals('Individual', $result['contact_id.contact_type']);
391 }
392
6a488035 393}