d18e60861b13b2697b017fa8cce910686db877ef
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * Test APIv3 civicrm_activity_* functions
32 * @package CiviCRM_APIv3
33 * @subpackage API_Contact
36 require_once 'CiviTest/CiviUnitTestCase.php';
37 class api_v3_AddressTest
extends CiviUnitTestCase
{
38 protected $_apiversion;
39 protected $_contactID;
40 protected $_locationType;
42 public $_eNoticeCompliant = TRUE;
46 $this->_apiversion
= 3;
47 $this->_entity
= 'Address';
50 $this->_contactID
= $this->organizationCreate();
51 $this->_locationType
= $this->locationTypeCreate();
52 CRM_Core_PseudoConstant
::flush();
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',
64 'version' => $this->_apiversion
,
69 $this->locationTypeDelete($this->_locationType
->id
);
70 $this->contactDelete($this->_contactID
);
73 public function testCreateAddress() {
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');
82 public function testCreateAddressParsing() {
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
,
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']));
104 * is_primary should be set as a default
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');
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']);
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
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__
);
147 public function testDeleteAddress() {
149 //check there are no addresss to start with
150 $get = civicrm_api('address', 'get', array(
152 'location_type_id' => $this->_locationType
->id
,
154 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
155 $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__
);
158 $create = civicrm_api('address', 'create', $this->_params
);
160 $this->assertEquals(0, $create['is_error'], 'In line ' . __LINE__
);
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(
168 'location_type_id' => $this->_locationType
->id
,
170 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
171 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
175 * Test civicrm_address_get - success expected.
177 public function testGetAddress() {
178 $address = civicrm_api('address', 'create', $this->_params
);
179 $this->assertAPISuccess($address, 'In line ' . __LINE__
);
182 'contact_id' => $this->_contactID
,
183 'street_name' => $address['values'][$address['id']]['street_name'],
184 'version' => $this->_apiversion
,
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__
);
196 * Test civicrm_address_get - success expected.
198 public function testGetSingleAddress() {
199 civicrm_api('address', 'create', $this->_params
);
201 'contact_id' => $this->_contactID
,
202 'version' => $this->_apiversion
,
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']));
210 * Test civicrm_address_get with sort option- success expected.
212 public function testGetAddressSort() {
213 $create = civicrm_api('address', 'create', $this->_params
);
214 $subfile = "AddressSort";
215 $description = "Demonstrates Use of sort filter";
218 'sort' => 'street_address DESC',
221 'version' => $this->_apiversion
,
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']));
233 * Test civicrm_address_get with sort option- success expected.
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
,
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']));
252 * Test civicrm_address_get with sort option- success expected.
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
,
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']));
268 function testGetWithCustom() {
269 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
271 $params = $this->_params
;
272 $params['custom_' . $ids['custom_field_id']] = "custom string";
274 $result = civicrm_api($this->_entity
, 'create', $params);
275 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
277 $getParams = array('version' => 3, 'id' => $result['id'], 'return' => array('custom'));
278 $check = civicrm_api($this->_entity
, 'get', $getParams);
280 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
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']));
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(
296 'id' => $address1['id'],
298 $this->assertEquals(1, $check);
299 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id']));
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(
309 'contact_id' => $this->_contactID
,
311 $this->assertEquals(1, $check);
312 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id']));