5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
31 * Test APIv3 civicrm_activity_* functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_Contact
37 require_once 'CiviTest/CiviUnitTestCase.php';
38 class api_v3_AddressTest
extends CiviUnitTestCase
{
39 protected $_apiversion;
40 protected $_contactID;
41 protected $_locationType;
43 public $_eNoticeCompliant = TRUE;
47 $this->_apiversion
= 3;
48 $this->_entity
= 'Address';
51 $this->_contactID
= $this->organizationCreate();
52 $this->_locationType
= $this->locationTypeCreate();
53 CRM_Core_PseudoConstant
::flush('locationType');
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',
65 'version' => $this->_apiversion
,
70 $this->locationTypeDelete($this->_locationType
->id
);
71 $this->contactDelete($this->_contactID
);
74 public function testCreateAddress() {
76 $result = civicrm_api('address', 'create', $this->_params
);
77 $this->documentMe($this->_params
, $result, __FUNCTION__
, __FILE__
);
78 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
79 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
80 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__
);
81 $this->getAndCheck($this->_params
, $result['id'], 'address');
83 public function testCreateAddressParsing() {
85 'version' => $this->_apiversion
,
86 'street_parsing' => 1,
87 'street_address' => '54A Excelsior Ave. Apt 1C',
88 'location_type_id' => $this->_locationType
->id
,
89 'contact_id' => $this->_contactID
,
91 $subfile = "AddressParse";
92 $description = "Demonstrates Use of address parsing param";
93 $result = civicrm_api('address', 'create', $params);
94 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
95 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
96 $this->assertEquals(54, $result['values'][$result['id']]['street_number'], 'In line ' . __LINE__
);
97 $this->assertEquals('A', $result['values'][$result['id']]['street_number_suffix'], 'In line ' . __LINE__
);
98 $this->assertEquals('Excelsior Ave.', $result['values'][$result['id']]['street_name'], 'In line ' . __LINE__
);
99 $this->assertEquals('Apt 1C', $result['values'][$result['id']]['street_unit'], 'In line ' . __LINE__
);
100 civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id']));
105 * is_primary should be set as a default
110 public function testCreateAddressTestDefaults() {
111 $params = $this->_params
;
112 unset($params['is_primary']);
113 $result = civicrm_api('address', 'create', $params);
114 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
115 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
116 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__
);
117 $this->getAndCheck($this->_params
, $result['id'], 'address');
120 public function testCreateAddressTooLongSuffix() {
121 $params = $this->_params
;
122 $params['street_number_suffix'] = 'really long string';
123 $result = civicrm_api('address', 'create', $params);
124 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
125 $this->assertEquals(2100, $result['error_code']);
128 * is_primary shoule be set as a default. ie. create the address, unset the params & recreate.
129 * is_primary should be 0 before & after the update. ie - having no other address
130 * is_primary is invalid
135 public function testCreateAddressTestDefaultWithID() {
136 $params = $this->_params
;
137 $params['is_primary'] = 0;
138 $result = civicrm_api('address', 'create', $params);
139 unset($params['is_primary']);
140 $params['id'] = $result['id'];
141 $result = civicrm_api('address', 'create', $params);
142 $address = civicrm_api('address', 'get', array('version' => 3, 'contact_id' => $params['contact_id']));
143 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
144 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
145 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__
);
146 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__
);
148 public function testDeleteAddress() {
150 //check there are no addresss to start with
151 $get = civicrm_api('address', 'get', array(
153 'location_type_id' => $this->_locationType
->id
,
155 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
156 $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__
);
159 $create = civicrm_api('address', 'create', $this->_params
);
161 $this->assertEquals(0, $create['is_error'], 'In line ' . __LINE__
);
163 $result = civicrm_api('address', 'delete', array('id' => $create['id'], 'version' => 3));
164 $this->documentMe($this->_params
, $result, __FUNCTION__
, __FILE__
);
165 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
166 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
167 $get = civicrm_api('address', 'get', array(
169 'location_type_id' => $this->_locationType
->id
,
171 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
172 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
176 * Test civicrm_address_get - success expected.
178 public function testGetAddress() {
179 $address = civicrm_api('address', 'create', $this->_params
);
180 $this->assertAPISuccess($address, 'In line ' . __LINE__
);
183 'contact_id' => $this->_contactID
,
184 'street_name' => $address['values'][$address['id']]['street_name'],
185 'version' => $this->_apiversion
,
187 $result = civicrm_api('Address', 'Get', ($params));
188 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
189 civicrm_api('Address', 'delete', array('version' => 3, 'id' => $result['id']));
190 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
191 $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id'], 'In line ' . __LINE__
);
192 $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary'], 'In line ' . __LINE__
);
193 $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address'], 'In line ' . __LINE__
);
197 * Test civicrm_address_get - success expected.
199 public function testGetSingleAddress() {
200 civicrm_api('address', 'create', $this->_params
);
202 'contact_id' => $this->_contactID
,
203 'version' => $this->_apiversion
,
205 $address = civicrm_api('Address', 'getsingle', ($params));
206 $this->assertEquals($address['location_type_id'], $this->_params
['location_type_id'], 'In line ' . __LINE__
);
207 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address['id']));
211 * Test civicrm_address_get with sort option- success expected.
213 public function testGetAddressSort() {
214 $create = civicrm_api('address', 'create', $this->_params
);
215 $subfile = "AddressSort";
216 $description = "Demonstrates Use of sort filter";
219 'sort' => 'street_address DESC',
222 'version' => $this->_apiversion
,
225 $result = civicrm_api('Address', 'Get', ($params));
226 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
227 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
228 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
229 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__
);
230 civicrm_api('address', 'delete', array('version' => 3, 'id' => $create['id']));
234 * Test civicrm_address_get with sort option- success expected.
236 public function testGetAddressLikeSuccess() {
237 civicrm_api('address', 'create', $this->_params
);
238 $subfile = "AddressLike";
239 $description = "Demonstrates Use of Like";
240 $params = array('street_address' => array('LIKE' => '%mb%'),
241 'version' => $this->_apiversion
,
244 $result = civicrm_api('Address', 'Get', ($params));
245 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
246 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
247 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
248 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__
);
249 civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id']));
253 * Test civicrm_address_get with sort option- success expected.
255 public function testGetAddressLikeFail() {
256 $create = civicrm_api('address', 'create', $this->_params
);
257 $subfile = "AddressLike";
258 $description = "Demonstrates Use of Like";
259 $params = array('street_address' => array('LIKE' => "'%xy%'"),
260 'version' => $this->_apiversion
,
263 $result = civicrm_api('Address', 'Get', ($params));
264 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
265 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__
);
266 civicrm_api('address', 'delete', array('version' => 3, 'id' => $create['id']));
269 function testGetWithCustom() {
270 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
272 $params = $this->_params
;
273 $params['custom_' . $ids['custom_field_id']] = "custom string";
275 $result = civicrm_api($this->_entity
, 'create', $params);
276 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
278 $getParams = array('version' => 3, 'id' => $result['id'], 'return' => array('custom'));
279 $check = civicrm_api($this->_entity
, 'get', $getParams);
281 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
283 $this->customFieldDelete($ids['custom_field_id']);
284 $this->customGroupDelete($ids['custom_group_id']);
285 civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id']));
288 public function testCreateAddressPrimaryHandlingChangeToPrimary() {
289 $params = $this->_params
;
290 unset($params['is_primary']);
291 $address1 = civicrm_api('address', 'create', $params);
292 $this->assertApiSuccess($address1, 'In line ' . __LINE__
);
293 //now we check & make sure it has been set to primary
294 $check = civicrm_api('address', 'getcount', array(
297 'id' => $address1['id'],
299 $this->assertEquals(1, $check);
300 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id']));
302 public function testCreateAddressPrimaryHandlingChangeExisting() {
303 $address1 = civicrm_api('address', 'create', $this->_params
);
304 $this->assertApiSuccess($address1, 'In line ' . __LINE__
);
305 $address2 = civicrm_api('address', 'create', $this->_params
);
306 $this->assertApiSuccess($address2, 'In line ' . __LINE__
);
307 $check = civicrm_api('address', 'getcount', array(
310 'contact_id' => $this->_contactID
,
312 $this->assertEquals(1, $check);
313 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id']));