d18e60861b13b2697b017fa8cce910686db877ef
[civicrm-core.git] / tests / phpunit / api / v3 / AddressTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * Test APIv3 civicrm_activity_* functions
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_Contact
34 */
35
36 require_once 'CiviTest/CiviUnitTestCase.php';
37 class api_v3_AddressTest extends CiviUnitTestCase {
38 protected $_apiversion;
39 protected $_contactID;
40 protected $_locationType;
41 protected $_params;
42 public $_eNoticeCompliant = TRUE;
43 protected $_entity;
44
45 function setUp() {
46 $this->_apiversion = 3;
47 $this->_entity = 'Address';
48 parent::setUp();
49
50 $this->_contactID = $this->organizationCreate();
51 $this->_locationType = $this->locationTypeCreate();
52 CRM_Core_PseudoConstant::flush();
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,
64 'version' => $this->_apiversion,
65 );
66 }
67
68 function tearDown() {
69 $this->locationTypeDelete($this->_locationType->id);
70 $this->contactDelete($this->_contactID);
71 }
72
73 public function testCreateAddress() {
74
75 $result = civicrm_api('address', 'create', $this->_params);
76 $this->documentMe($this->_params, $result, __FUNCTION__, __FILE__);
77 $this->assertAPISuccess($result, 'In line ' . __LINE__);
78 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
79 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__);
80 $this->getAndCheck($this->_params, $result['id'], 'address');
81 }
82 public function testCreateAddressParsing() {
83 $params = array(
84 'version' => $this->_apiversion,
85 'street_parsing' => 1,
86 'street_address' => '54A Excelsior Ave. Apt 1C',
87 'location_type_id' => $this->_locationType->id,
88 'contact_id' => $this->_contactID,
89 );
90 $subfile = "AddressParse";
91 $description = "Demonstrates Use of address parsing param";
92 $result = civicrm_api('address', 'create', $params);
93 $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile);
94 $this->assertAPISuccess($result, 'In line ' . __LINE__);
95 $this->assertEquals(54, $result['values'][$result['id']]['street_number'], 'In line ' . __LINE__);
96 $this->assertEquals('A', $result['values'][$result['id']]['street_number_suffix'], 'In line ' . __LINE__);
97 $this->assertEquals('Excelsior Ave.', $result['values'][$result['id']]['street_name'], 'In line ' . __LINE__);
98 $this->assertEquals('Apt 1C', $result['values'][$result['id']]['street_unit'], 'In line ' . __LINE__);
99 civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id']));
100
101 }
102
103 /*
104 * is_primary should be set as a default
105 */
106
107
108
109 public function testCreateAddressTestDefaults() {
110 $params = $this->_params;
111 unset($params['is_primary']);
112 $result = civicrm_api('address', 'create', $params);
113 $this->assertAPISuccess($result, 'In line ' . __LINE__);
114 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
115 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__);
116 $this->getAndCheck($this->_params, $result['id'], 'address');
117 }
118
119 public function testCreateAddressTooLongSuffix() {
120 $params = $this->_params;
121 $params['street_number_suffix'] = 'really long string';
122 $result = civicrm_api('address', 'create', $params);
123 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
124 $this->assertEquals(2100, $result['error_code']);
125 }
126 /*
127 * is_primary shoule be set as a default. ie. create the address, unset the params & recreate.
128 * is_primary should be 0 before & after the update. ie - having no other address
129 * is_primary is invalid
130 */
131
132
133
134 public function testCreateAddressTestDefaultWithID() {
135 $params = $this->_params;
136 $params['is_primary'] = 0;
137 $result = civicrm_api('address', 'create', $params);
138 unset($params['is_primary']);
139 $params['id'] = $result['id'];
140 $result = civicrm_api('address', 'create', $params);
141 $address = civicrm_api('address', 'get', array('version' => 3, 'contact_id' => $params['contact_id']));
142 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
143 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
144 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__);
145 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__);
146 }
147 public function testDeleteAddress() {
148
149 //check there are no addresss to start with
150 $get = civicrm_api('address', 'get', array(
151 'version' => 3,
152 'location_type_id' => $this->_locationType->id,
153 ));
154 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__);
155 $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__);
156
157 //create one
158 $create = civicrm_api('address', 'create', $this->_params);
159
160 $this->assertEquals(0, $create['is_error'], 'In line ' . __LINE__);
161
162 $result = civicrm_api('address', 'delete', array('id' => $create['id'], 'version' => 3));
163 $this->documentMe($this->_params, $result, __FUNCTION__, __FILE__);
164 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
165 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
166 $get = civicrm_api('address', 'get', array(
167 'version' => 3,
168 'location_type_id' => $this->_locationType->id,
169 ));
170 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__);
171 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
172 }
173
174 /**
175 * Test civicrm_address_get - success expected.
176 */
177 public function testGetAddress() {
178 $address = civicrm_api('address', 'create', $this->_params);
179 $this->assertAPISuccess($address, 'In line ' . __LINE__);
180
181 $params = array(
182 'contact_id' => $this->_contactID,
183 'street_name' => $address['values'][$address['id']]['street_name'],
184 'version' => $this->_apiversion,
185 );
186 $result = civicrm_api('Address', 'Get', ($params));
187 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
188 civicrm_api('Address', 'delete', array('version' => 3, 'id' => $result['id']));
189 $this->assertAPISuccess($result, 'In line ' . __LINE__);
190 $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id'], 'In line ' . __LINE__);
191 $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary'], 'In line ' . __LINE__);
192 $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address'], 'In line ' . __LINE__);
193 }
194
195 /**
196 * Test civicrm_address_get - success expected.
197 */
198 public function testGetSingleAddress() {
199 civicrm_api('address', 'create', $this->_params);
200 $params = array(
201 'contact_id' => $this->_contactID,
202 'version' => $this->_apiversion,
203 );
204 $address = civicrm_api('Address', 'getsingle', ($params));
205 $this->assertEquals($address['location_type_id'], $this->_params['location_type_id'], 'In line ' . __LINE__);
206 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address['id']));
207 }
208
209 /**
210 * Test civicrm_address_get with sort option- success expected.
211 */
212 public function testGetAddressSort() {
213 $create = civicrm_api('address', 'create', $this->_params);
214 $subfile = "AddressSort";
215 $description = "Demonstrates Use of sort filter";
216 $params = array(
217 'options' => array(
218 'sort' => 'street_address DESC',
219 'limit' => 2,
220 ),
221 'version' => $this->_apiversion,
222 'sequential' => 1,
223 );
224 $result = civicrm_api('Address', 'Get', ($params));
225 $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile);
226 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
227 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__);
228 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__);
229 civicrm_api('address', 'delete', array('version' => 3, 'id' => $create['id']));
230 }
231
232 /**
233 * Test civicrm_address_get with sort option- success expected.
234 */
235 public function testGetAddressLikeSuccess() {
236 civicrm_api('address', 'create', $this->_params);
237 $subfile = "AddressLike";
238 $description = "Demonstrates Use of Like";
239 $params = array('street_address' => array('LIKE' => '%mb%'),
240 'version' => $this->_apiversion,
241 'sequential' => 1,
242 );
243 $result = civicrm_api('Address', 'Get', ($params));
244 $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile);
245 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
246 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
247 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__);
248 civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id']));
249 }
250
251 /**
252 * Test civicrm_address_get with sort option- success expected.
253 */
254 public function testGetAddressLikeFail() {
255 $create = civicrm_api('address', 'create', $this->_params);
256 $subfile = "AddressLike";
257 $description = "Demonstrates Use of Like";
258 $params = array('street_address' => array('LIKE' => "'%xy%'"),
259 'version' => $this->_apiversion,
260 'sequential' => 1,
261 );
262 $result = civicrm_api('Address', 'Get', ($params));
263 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
264 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__);
265 civicrm_api('address', 'delete', array('version' => 3, 'id' => $create['id']));
266 }
267
268 function testGetWithCustom() {
269 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
270
271 $params = $this->_params;
272 $params['custom_' . $ids['custom_field_id']] = "custom string";
273
274 $result = civicrm_api($this->_entity, 'create', $params);
275 $this->assertAPISuccess($result, ' in line ' . __LINE__);
276
277 $getParams = array('version' => 3, 'id' => $result['id'], 'return' => array('custom'));
278 $check = civicrm_api($this->_entity, 'get', $getParams);
279
280 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
281
282 $this->customFieldDelete($ids['custom_field_id']);
283 $this->customGroupDelete($ids['custom_group_id']);
284 civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id']));
285 }
286
287 public function testCreateAddressPrimaryHandlingChangeToPrimary() {
288 $params = $this->_params;
289 unset($params['is_primary']);
290 $address1 = civicrm_api('address', 'create', $params);
291 $this->assertApiSuccess($address1, 'In line ' . __LINE__);
292 //now we check & make sure it has been set to primary
293 $check = civicrm_api('address', 'getcount', array(
294 'version' => 3,
295 'is_primary' => 1,
296 'id' => $address1['id'],
297 ));
298 $this->assertEquals(1, $check);
299 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id']));
300 }
301 public function testCreateAddressPrimaryHandlingChangeExisting() {
302 $address1 = civicrm_api('address', 'create', $this->_params);
303 $this->assertApiSuccess($address1, 'In line ' . __LINE__);
304 $address2 = civicrm_api('address', 'create', $this->_params);
305 $this->assertApiSuccess($address2, 'In line ' . __LINE__);
306 $check = civicrm_api('address', 'getcount', array(
307 'version' => 3,
308 'is_primary' => 1,
309 'contact_id' => $this->_contactID,
310 ));
311 $this->assertEquals(1, $check);
312 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id']));
313 }
314 }
315