3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_activity_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
35 require_once 'CiviTest/CiviUnitTestCase.php';
38 * Class api_v3_AddressTest
40 class api_v3_AddressTest
extends CiviUnitTestCase
{
41 protected $_apiversion =3;
42 protected $_contactID;
43 protected $_locationType;
49 $this->_entity
= 'Address';
52 $this->_contactID
= $this->organizationCreate();
53 $this->_locationType
= $this->locationTypeCreate();
54 CRM_Core_PseudoConstant
::flush();
56 $this->_params
= array(
57 'contact_id' => $this->_contactID
,
58 'location_type_id' => $this->_locationType
->id
,
59 'street_name' => 'Ambachtstraat',
60 'street_number' => '23',
61 'street_address' => 'Ambachtstraat 23',
62 'postal_code' => '6971 BN',
63 'country_id' => '1152',
70 $this->locationTypeDelete($this->_locationType
->id
);
71 $this->contactDelete($this->_contactID
);
74 public function testCreateAddress() {
75 $result = $this->callAPIAndDocument('address', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
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');
81 public function testCreateAddressParsing() {
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 = $this->callAPIAndDocument('address', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
91 $this->assertEquals(54, $result['values'][$result['id']]['street_number'], 'In line ' . __LINE__
);
92 $this->assertEquals('A', $result['values'][$result['id']]['street_number_suffix'], 'In line ' . __LINE__
);
93 $this->assertEquals('Excelsior Ave.', $result['values'][$result['id']]['street_name'], 'In line ' . __LINE__
);
94 $this->assertEquals('Apt 1C', $result['values'][$result['id']]['street_unit'], 'In line ' . __LINE__
);
95 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
100 * is_primary should be set as a default
102 public function testCreateAddressTestDefaults() {
103 $params = $this->_params
;
104 unset($params['is_primary']);
105 $result = $this->callAPISuccess('address', 'create', $params);
106 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
107 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__
);
108 $this->getAndCheck($this->_params
, $result['id'], 'address');
111 public function testCreateAddressTooLongSuffix() {
112 $params = $this->_params
;
113 $params['street_number_suffix'] = 'really long string';
114 $result = $this->callAPIFailure('address', 'create', $params);
118 * is_primary shoule be set as a default. ie. create the address, unset the params & recreate.
119 * is_primary should be 0 before & after the update. ie - having no other address
120 * is_primary is invalid
122 public function testCreateAddressTestDefaultWithID() {
123 $params = $this->_params
;
124 $params['is_primary'] = 0;
125 $result = $this->callAPISuccess('address', 'create', $params);
126 unset($params['is_primary']);
127 $params['id'] = $result['id'];
128 $result = $this->callAPISuccess('address', 'create', $params);
129 $address = $this->callAPISuccess('address', 'get', array('contact_id' => $params['contact_id']));
130 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
131 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__
);
132 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__
);
134 public function testDeleteAddress() {
136 //check there are no addresss to start with
137 $get = $this->callAPISuccess('address', 'get', array(
138 'location_type_id' => $this->_locationType
->id
,
140 $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__
);
143 $create = $this->callAPISuccess('address', 'create', $this->_params
);
145 $result = $this->callAPIAndDocument('address', 'delete', array('id' => $create['id'],), __FUNCTION__
, __FILE__
);
146 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
147 $get = $this->callAPISuccess('address', 'get', array(
148 'location_type_id' => $this->_locationType
->id
,
150 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
154 * Test civicrm_address_get - success expected.
156 public function testGetAddress() {
157 $address = $this->callAPISuccess('address', 'create', $this->_params
);
160 'contact_id' => $this->_contactID
,
161 'street_name' => $address['values'][$address['id']]['street_name'],
163 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
);
164 $this->callAPISuccess('Address', 'delete', array('id' => $result['id']));
165 $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id'], 'In line ' . __LINE__
);
166 $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary'], 'In line ' . __LINE__
);
167 $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address'], 'In line ' . __LINE__
);
171 * Test civicrm_address_get - success expected.
173 public function testGetSingleAddress() {
174 $this->callAPISuccess('address', 'create', $this->_params
);
176 'contact_id' => $this->_contactID
,
178 $address = $this->callAPISuccess('Address', 'getsingle', ($params));
179 $this->assertEquals($address['location_type_id'], $this->_params
['location_type_id'], 'In line ' . __LINE__
);
180 $this->callAPISuccess('address', 'delete', array('id' => $address['id']));
184 * Test civicrm_address_get with sort option- success expected.
186 public function testGetAddressSort() {
187 $create = $this->callAPISuccess('address', 'create', $this->_params
);
188 $subfile = "AddressSort";
189 $description = "Demonstrates Use of sort filter";
192 'sort' => 'street_address DESC',
197 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
198 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
199 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__
);
200 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
204 * Test civicrm_address_get with sort option- success expected.
206 public function testGetAddressLikeSuccess() {
207 $this->callAPISuccess('address', 'create', $this->_params
);
208 $subfile = "AddressLike";
209 $description = "Demonstrates Use of Like";
210 $params = array('street_address' => array('LIKE' => '%mb%'),
213 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
214 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
215 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__
);
216 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
220 * Test civicrm_address_get with sort option- success expected.
222 public function testGetAddressLikeFail() {
223 $create = $this->callAPISuccess('address', 'create', $this->_params
);
224 $subfile = "AddressLike";
225 $description = "Demonstrates Use of Like";
226 $params = array('street_address' => array('LIKE' => "'%xy%'"),
229 $result = $this->callAPISuccess('Address', 'Get', ($params));
230 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__
);
231 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
234 function testGetWithCustom() {
235 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
237 $params = $this->_params
;
238 $params['custom_' . $ids['custom_field_id']] = "custom string";
240 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
242 $getParams = array('id' => $result['id'], 'return' => array('custom'));
243 $check = $this->callAPISuccess($this->_entity
, 'get', $getParams);
245 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
247 $this->customFieldDelete($ids['custom_field_id']);
248 $this->customGroupDelete($ids['custom_group_id']);
249 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
252 public function testCreateAddressPrimaryHandlingChangeToPrimary() {
253 $params = $this->_params
;
254 unset($params['is_primary']);
255 $address1 = $this->callAPISuccess('address', 'create', $params);
256 $this->assertApiSuccess($address1, 'In line ' . __LINE__
);
257 //now we check & make sure it has been set to primary
258 $check = $this->callAPISuccess('address', 'getcount', array(
260 'id' => $address1['id'],
262 $this->assertEquals(1, $check);
263 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
265 public function testCreateAddressPrimaryHandlingChangeExisting() {
266 $address1 = $this->callAPISuccess('address', 'create', $this->_params
);
267 $address2 = $this->callAPISuccess('address', 'create', $this->_params
);
268 $check = $this->callAPISuccess('address', 'getcount', array(
270 'contact_id' => $this->_contactID
,
272 $this->assertEquals(1, $check);
273 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));