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;
43 protected $_entity; function setUp() {
44 $this->_apiversion
= 3;
45 $this->_entity
= 'Address';
48 $this->_contactID
= $this->organizationCreate();
49 $this->_locationType
= $this->locationTypeCreate();
50 CRM_Core_PseudoConstant
::flush();
52 $this->_params
= array(
53 'contact_id' => $this->_contactID
,
54 'location_type_id' => $this->_locationType
->id
,
55 'street_name' => 'Ambachtstraat',
56 'street_number' => '23',
57 'street_address' => 'Ambachtstraat 23',
58 'postal_code' => '6971 BN',
59 'country_id' => '1152',
62 'version' => $this->_apiversion
,
67 $this->locationTypeDelete($this->_locationType
->id
);
68 $this->contactDelete($this->_contactID
);
71 public function testCreateAddress() {
73 $result = civicrm_api('address', 'create', $this->_params
);
74 $this->documentMe($this->_params
, $result, __FUNCTION__
, __FILE__
);
75 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
76 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
77 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__
);
78 $this->getAndCheck($this->_params
, $result['id'], 'address');
80 public function testCreateAddressParsing() {
82 'version' => $this->_apiversion
,
83 'street_parsing' => 1,
84 'street_address' => '54A Excelsior Ave. Apt 1C',
85 'location_type_id' => $this->_locationType
->id
,
86 'contact_id' => $this->_contactID
,
88 $subfile = "AddressParse";
89 $description = "Demonstrates Use of address parsing param";
90 $result = civicrm_api('address', 'create', $params);
91 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
92 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
93 $this->assertEquals(54, $result['values'][$result['id']]['street_number'], 'In line ' . __LINE__
);
94 $this->assertEquals('A', $result['values'][$result['id']]['street_number_suffix'], 'In line ' . __LINE__
);
95 $this->assertEquals('Excelsior Ave.', $result['values'][$result['id']]['street_name'], 'In line ' . __LINE__
);
96 $this->assertEquals('Apt 1C', $result['values'][$result['id']]['street_unit'], 'In line ' . __LINE__
);
97 civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id']));
102 * is_primary should be set as a default
107 public function testCreateAddressTestDefaults() {
108 $params = $this->_params
;
109 unset($params['is_primary']);
110 $result = civicrm_api('address', 'create', $params);
111 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
112 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
113 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__
);
114 $this->getAndCheck($this->_params
, $result['id'], 'address');
117 public function testCreateAddressTooLongSuffix() {
118 $params = $this->_params
;
119 $params['street_number_suffix'] = 'really long string';
120 $result = civicrm_api('address', 'create', $params);
121 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
122 $this->assertEquals(2100, $result['error_code']);
125 * is_primary shoule be set as a default. ie. create the address, unset the params & recreate.
126 * is_primary should be 0 before & after the update. ie - having no other address
127 * is_primary is invalid
132 public function testCreateAddressTestDefaultWithID() {
133 $params = $this->_params
;
134 $params['is_primary'] = 0;
135 $result = civicrm_api('address', 'create', $params);
136 unset($params['is_primary']);
137 $params['id'] = $result['id'];
138 $result = civicrm_api('address', 'create', $params);
139 $address = civicrm_api('address', 'get', array('version' => 3, 'contact_id' => $params['contact_id']));
140 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
141 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
142 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__
);
143 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__
);
145 public function testDeleteAddress() {
147 //check there are no addresss to start with
148 $get = civicrm_api('address', 'get', array(
150 'location_type_id' => $this->_locationType
->id
,
152 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
153 $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__
);
156 $create = civicrm_api('address', 'create', $this->_params
);
158 $this->assertEquals(0, $create['is_error'], 'In line ' . __LINE__
);
160 $result = civicrm_api('address', 'delete', array('id' => $create['id'], 'version' => 3));
161 $this->documentMe($this->_params
, $result, __FUNCTION__
, __FILE__
);
162 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
163 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
164 $get = civicrm_api('address', 'get', array(
166 'location_type_id' => $this->_locationType
->id
,
168 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
169 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
173 * Test civicrm_address_get - success expected.
175 public function testGetAddress() {
176 $address = civicrm_api('address', 'create', $this->_params
);
177 $this->assertAPISuccess($address, 'In line ' . __LINE__
);
180 'contact_id' => $this->_contactID
,
181 'street_name' => $address['values'][$address['id']]['street_name'],
182 'version' => $this->_apiversion
,
184 $result = civicrm_api('Address', 'Get', ($params));
185 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
186 civicrm_api('Address', 'delete', array('version' => 3, 'id' => $result['id']));
187 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
188 $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id'], 'In line ' . __LINE__
);
189 $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary'], 'In line ' . __LINE__
);
190 $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address'], 'In line ' . __LINE__
);
194 * Test civicrm_address_get - success expected.
196 public function testGetSingleAddress() {
197 civicrm_api('address', 'create', $this->_params
);
199 'contact_id' => $this->_contactID
,
200 'version' => $this->_apiversion
,
202 $address = civicrm_api('Address', 'getsingle', ($params));
203 $this->assertEquals($address['location_type_id'], $this->_params
['location_type_id'], 'In line ' . __LINE__
);
204 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address['id']));
208 * Test civicrm_address_get with sort option- success expected.
210 public function testGetAddressSort() {
211 $create = civicrm_api('address', 'create', $this->_params
);
212 $subfile = "AddressSort";
213 $description = "Demonstrates Use of sort filter";
216 'sort' => 'street_address DESC',
219 'version' => $this->_apiversion
,
222 $result = civicrm_api('Address', 'Get', ($params));
223 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
224 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
225 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
226 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__
);
227 civicrm_api('address', 'delete', array('version' => 3, 'id' => $create['id']));
231 * Test civicrm_address_get with sort option- success expected.
233 public function testGetAddressLikeSuccess() {
234 civicrm_api('address', 'create', $this->_params
);
235 $subfile = "AddressLike";
236 $description = "Demonstrates Use of Like";
237 $params = array('street_address' => array('LIKE' => '%mb%'),
238 'version' => $this->_apiversion
,
241 $result = civicrm_api('Address', 'Get', ($params));
242 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
243 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
244 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
245 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__
);
246 civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id']));
250 * Test civicrm_address_get with sort option- success expected.
252 public function testGetAddressLikeFail() {
253 $create = civicrm_api('address', 'create', $this->_params
);
254 $subfile = "AddressLike";
255 $description = "Demonstrates Use of Like";
256 $params = array('street_address' => array('LIKE' => "'%xy%'"),
257 'version' => $this->_apiversion
,
260 $result = civicrm_api('Address', 'Get', ($params));
261 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
262 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__
);
263 civicrm_api('address', 'delete', array('version' => 3, 'id' => $create['id']));
266 function testGetWithCustom() {
267 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
269 $params = $this->_params
;
270 $params['custom_' . $ids['custom_field_id']] = "custom string";
272 $result = civicrm_api($this->_entity
, 'create', $params);
273 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
275 $getParams = array('version' => 3, 'id' => $result['id'], 'return' => array('custom'));
276 $check = civicrm_api($this->_entity
, 'get', $getParams);
278 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
280 $this->customFieldDelete($ids['custom_field_id']);
281 $this->customGroupDelete($ids['custom_group_id']);
282 civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id']));
285 public function testCreateAddressPrimaryHandlingChangeToPrimary() {
286 $params = $this->_params
;
287 unset($params['is_primary']);
288 $address1 = civicrm_api('address', 'create', $params);
289 $this->assertApiSuccess($address1, 'In line ' . __LINE__
);
290 //now we check & make sure it has been set to primary
291 $check = civicrm_api('address', 'getcount', array(
294 'id' => $address1['id'],
296 $this->assertEquals(1, $check);
297 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id']));
299 public function testCreateAddressPrimaryHandlingChangeExisting() {
300 $address1 = civicrm_api('address', 'create', $this->_params
);
301 $this->assertApiSuccess($address1, 'In line ' . __LINE__
);
302 $address2 = civicrm_api('address', 'create', $this->_params
);
303 $this->assertApiSuccess($address2, 'In line ' . __LINE__
);
304 $check = civicrm_api('address', 'getcount', array(
307 'contact_id' => $this->_contactID
,
309 $this->assertEquals(1, $check);
310 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id']));