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