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';
36 class api_v3_AddressTest
extends CiviUnitTestCase
{
37 protected $_apiversion =3;
38 protected $_contactID;
39 protected $_locationType;
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',
66 $this->locationTypeDelete($this->_locationType
->id
);
67 $this->contactDelete($this->_contactID
);
70 public function testCreateAddress() {
71 $result = $this->callAPIAndDocument('address', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
72 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
73 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__
);
74 $this->getAndCheck($this->_params
, $result['id'], 'address');
77 public function testCreateAddressParsing() {
79 'street_parsing' => 1,
80 'street_address' => '54A Excelsior Ave. Apt 1C',
81 'location_type_id' => $this->_locationType
->id
,
82 'contact_id' => $this->_contactID
,
84 $subfile = "AddressParse";
85 $description = "Demonstrates Use of address parsing param";
86 $result = $this->callAPIAndDocument('address', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
87 $this->assertEquals(54, $result['values'][$result['id']]['street_number'], 'In line ' . __LINE__
);
88 $this->assertEquals('A', $result['values'][$result['id']]['street_number_suffix'], 'In line ' . __LINE__
);
89 $this->assertEquals('Excelsior Ave.', $result['values'][$result['id']]['street_name'], 'In line ' . __LINE__
);
90 $this->assertEquals('Apt 1C', $result['values'][$result['id']]['street_unit'], 'In line ' . __LINE__
);
91 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
96 * is_primary should be set as a default
101 public function testCreateAddressTestDefaults() {
102 $params = $this->_params
;
103 unset($params['is_primary']);
104 $result = $this->callAPISuccess('address', 'create', $params);
105 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
106 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__
);
107 $this->getAndCheck($this->_params
, $result['id'], 'address');
110 public function testCreateAddressTooLongSuffix() {
111 $params = $this->_params
;
112 $params['street_number_suffix'] = 'really long string';
113 $result = $this->callAPIFailure('address', 'create', $params);
116 * is_primary shoule be set as a default. ie. create the address, unset the params & recreate.
117 * is_primary should be 0 before & after the update. ie - having no other address
118 * is_primary is invalid
123 public function testCreateAddressTestDefaultWithID() {
124 $params = $this->_params
;
125 $params['is_primary'] = 0;
126 $result = $this->callAPISuccess('address', 'create', $params);
127 unset($params['is_primary']);
128 $params['id'] = $result['id'];
129 $result = $this->callAPISuccess('address', 'create', $params);
130 $address = $this->callAPISuccess('address', 'get', array('contact_id' => $params['contact_id']));
131 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
132 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__
);
133 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__
);
135 public function testDeleteAddress() {
137 //check there are no addresss to start with
138 $get = $this->callAPISuccess('address', 'get', array(
139 'location_type_id' => $this->_locationType
->id
,
141 $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__
);
144 $create = $this->callAPISuccess('address', 'create', $this->_params
);
146 $result = $this->callAPIAndDocument('address', 'delete', array('id' => $create['id'],), __FUNCTION__
, __FILE__
);
147 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
148 $get = $this->callAPISuccess('address', 'get', array(
149 'location_type_id' => $this->_locationType
->id
,
151 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
155 * Test civicrm_address_get - success expected.
157 public function testGetAddress() {
158 $address = $this->callAPISuccess('address', 'create', $this->_params
);
161 'contact_id' => $this->_contactID
,
162 'street_name' => $address['values'][$address['id']]['street_name'],
164 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
);
165 $this->callAPISuccess('Address', 'delete', array('id' => $result['id']));
166 $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id'], 'In line ' . __LINE__
);
167 $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary'], 'In line ' . __LINE__
);
168 $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address'], 'In line ' . __LINE__
);
172 * Test civicrm_address_get - success expected.
174 public function testGetSingleAddress() {
175 $this->callAPISuccess('address', 'create', $this->_params
);
177 'contact_id' => $this->_contactID
,
179 $address = $this->callAPISuccess('Address', 'getsingle', ($params));
180 $this->assertEquals($address['location_type_id'], $this->_params
['location_type_id'], 'In line ' . __LINE__
);
181 $this->callAPISuccess('address', 'delete', array('id' => $address['id']));
185 * Test civicrm_address_get with sort option- success expected.
187 public function testGetAddressSort() {
188 $create = $this->callAPISuccess('address', 'create', $this->_params
);
189 $subfile = "AddressSort";
190 $description = "Demonstrates Use of sort filter";
193 'sort' => 'street_address DESC',
198 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
199 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
200 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__
);
201 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
205 * Test civicrm_address_get with sort option- success expected.
207 public function testGetAddressLikeSuccess() {
208 $this->callAPISuccess('address', 'create', $this->_params
);
209 $subfile = "AddressLike";
210 $description = "Demonstrates Use of Like";
211 $params = array('street_address' => array('LIKE' => '%mb%'),
214 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
215 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
216 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__
);
217 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
221 * Test civicrm_address_get with sort option- success expected.
223 public function testGetAddressLikeFail() {
224 $create = $this->callAPISuccess('address', 'create', $this->_params
);
225 $subfile = "AddressLike";
226 $description = "Demonstrates Use of Like";
227 $params = array('street_address' => array('LIKE' => "'%xy%'"),
230 $result = $this->callAPISuccess('Address', 'Get', ($params));
231 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__
);
232 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
235 function testGetWithCustom() {
236 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
238 $params = $this->_params
;
239 $params['custom_' . $ids['custom_field_id']] = "custom string";
241 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
243 $getParams = array('id' => $result['id'], 'return' => array('custom'));
244 $check = $this->callAPISuccess($this->_entity
, 'get', $getParams);
246 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
248 $this->customFieldDelete($ids['custom_field_id']);
249 $this->customGroupDelete($ids['custom_group_id']);
250 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
253 public function testCreateAddressPrimaryHandlingChangeToPrimary() {
254 $params = $this->_params
;
255 unset($params['is_primary']);
256 $address1 = $this->callAPISuccess('address', 'create', $params);
257 $this->assertApiSuccess($address1, 'In line ' . __LINE__
);
258 //now we check & make sure it has been set to primary
259 $check = $this->callAPISuccess('address', 'getcount', array(
261 'id' => $address1['id'],
263 $this->assertEquals(1, $check);
264 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
266 public function testCreateAddressPrimaryHandlingChangeExisting() {
267 $address1 = $this->callAPISuccess('address', 'create', $this->_params
);
268 $address2 = $this->callAPISuccess('address', 'create', $this->_params
);
269 $check = $this->callAPISuccess('address', 'getcount', array(
271 'contact_id' => $this->_contactID
,
273 $this->assertEquals(1, $check);
274 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));