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