3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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;
48 public function setUp() {
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',
69 public function tearDown() {
70 $this->locationTypeDelete($this->_locationType
->id
);
71 $this->contactDelete($this->_contactID
);
72 $this->quickCleanup(array('civicrm_address', 'civicrm_relationship'));
75 public function testCreateAddress() {
76 $result = $this->callAPIAndDocument('address', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
77 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
78 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__
);
79 $this->getAndCheck($this->_params
, $result['id'], 'address');
82 public function testCreateAddressParsing() {
84 'street_parsing' => 1,
85 'street_address' => '54A Excelsior Ave. Apt 1C',
86 'location_type_id' => $this->_locationType
->id
,
87 'contact_id' => $this->_contactID
,
89 $subfile = "AddressParse";
90 $description = "Demonstrates Use of address parsing param";
91 $result = $this->callAPIAndDocument('address', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
92 $this->assertEquals(54, $result['values'][$result['id']]['street_number'], 'In line ' . __LINE__
);
93 $this->assertEquals('A', $result['values'][$result['id']]['street_number_suffix'], 'In line ' . __LINE__
);
94 $this->assertEquals('Excelsior Ave.', $result['values'][$result['id']]['street_name'], 'In line ' . __LINE__
);
95 $this->assertEquals('Apt 1C', $result['values'][$result['id']]['street_unit'], 'In line ' . __LINE__
);
96 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
101 * Is_primary should be set as a default
103 public function testCreateAddressTestDefaults() {
104 $params = $this->_params
;
105 unset($params['is_primary']);
106 $result = $this->callAPISuccess('address', 'create', $params);
107 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
108 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__
);
109 $this->getAndCheck($this->_params
, $result['id'], 'address');
112 public function testCreateAddressTooLongSuffix() {
113 $params = $this->_params
;
114 $params['street_number_suffix'] = 'really long string';
115 $this->callAPIFailure('address', 'create', $params);
119 * Create an address with a master ID and ensure that a relationship is created
121 public function testCreateAddressWithMasterRelationshipHousehold() {
122 $householdID = $this->householdCreate();
123 $address = $this->callAPISuccess('address', 'create', array_merge($this->_params
, $this->_params
, array('contact_id' => $householdID)));
124 $individualID = $this->individualCreate();
125 $individualParams = array(
126 'contact_id' => $individualID,
127 'master_id' => $address['id'],
129 $this->callAPISuccess('address', 'create', array_merge($this->_params
, $individualParams));
130 $this->callAPISuccess('relationship', 'getcount', array(
131 'contact_id_a' => $individualID,
132 'contact_id_b' => $this->_contactID
,
137 * Create an address with a master ID and ensure that a relationship is created
139 public function testCreateAddressWithMasterRelationshipOrganization() {
140 $address = $this->callAPISuccess('address', 'create', $this->_params
);
141 $individualID = $this->individualCreate();
142 $individualParams = array(
143 'contact_id' => $individualID,
144 'master_id' => $address['id'],
146 $this->callAPISuccess('address', 'create', array_merge($this->_params
, $individualParams));
147 $this->callAPISuccess('relationship', 'getcount', array(
148 'contact_id_a' => $individualID,
149 'contact_id_b' => $this->_contactID
,
154 * Create an address with a master ID and ensure that a relationship is created
156 public function testCreateAddressWithMasterRelationshipChangingOrganization() {
157 $address = $this->callAPISuccess('address', 'create', $this->_params
);
158 $organisation2ID = $this->organizationCreate();
159 $address2 = $this->callAPISuccess('address', 'create', array_merge($this->_params
, array('contact_id' => $organisation2ID)));
160 $individualID = $this->individualCreate();
161 $individualParams = array_merge($this->_params
, array(
162 'contact_id' => $individualID,
163 'master_id' => $address['id'],
165 $individualAddress = $this->callAPISuccess('address', 'create', $individualParams);
166 $individualParams['master_id'] = $address2['id'];
167 $individualParams['id'] = $individualAddress['id'];
168 $this->callAPISuccess('address', 'create', $individualParams);
169 $this->callAPISuccessGetCount('relationship', array('contact_id_a' => $individualID), 2);
170 $this->markTestIncomplete('Remainder of test checks that employer relationship is disabled when new one is created but turns out to be not happening - by design?');
171 $this->callAPISuccessGetCount('relationship', array('contact_id_a' => $individualID, 'is_active' => FALSE), 1);
172 $this->callAPISuccessGetCount('relationship', array(
173 'contact_id_a' => $individualID,
175 'contact_id_b' => $organisation2ID,
181 * Is_primary should be set as a default. ie. create the address, unset the params & recreate.
182 * is_primary should be 0 before & after the update. ie - having no other address
183 * is_primary is invalid
185 public function testCreateAddressTestDefaultWithID() {
186 $params = $this->_params
;
187 $params['is_primary'] = 0;
188 $result = $this->callAPISuccess('address', 'create', $params);
189 unset($params['is_primary']);
190 $params['id'] = $result['id'];
191 $result = $this->callAPISuccess('address', 'create', $params);
192 $this->callAPISuccess('address', 'get', array('contact_id' => $params['contact_id']));
193 $this->assertEquals(1, $result['count']);
194 $this->assertEquals(1, $result['values'][$result['id']]['is_primary']);
195 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__
);
199 * test address deletion
201 public function testDeleteAddress() {
202 //check there are no address to start with
203 $get = $this->callAPISuccess('address', 'get', array(
204 'location_type_id' => $this->_locationType
->id
,
206 $this->assertEquals(0, $get['count'], 'Contact already exists ');
209 $create = $this->callAPISuccess('address', 'create', $this->_params
);
211 $result = $this->callAPIAndDocument('address', 'delete', array('id' => $create['id']), __FUNCTION__
, __FILE__
);
212 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
213 $get = $this->callAPISuccess('address', 'get', array(
214 'location_type_id' => $this->_locationType
->id
,
216 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
220 * Test civicrm_address_get - success expected.
222 public function testGetAddress() {
223 $address = $this->callAPISuccess('address', 'create', $this->_params
);
226 'contact_id' => $this->_contactID
,
227 'street_name' => $address['values'][$address['id']]['street_name'],
229 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
);
230 $this->callAPISuccess('Address', 'delete', array('id' => $result['id']));
231 $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id']);
232 $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary']);
233 $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address']);
237 * Test civicrm_address_get - success expected.
239 public function testGetSingleAddress() {
240 $this->callAPISuccess('address', 'create', $this->_params
);
242 'contact_id' => $this->_contactID
,
244 $address = $this->callAPISuccess('Address', 'getsingle', ($params));
245 $this->assertEquals($address['location_type_id'], $this->_params
['location_type_id'], 'In line ' . __LINE__
);
246 $this->callAPISuccess('address', 'delete', array('id' => $address['id']));
250 * Test civicrm_address_get with sort option- success expected.
252 public function testGetAddressSort() {
253 $create = $this->callAPISuccess('address', 'create', $this->_params
);
254 $this->callAPISuccess('address', 'create', array_merge($this->_params
, array('street_address' => 'yzy')));
255 $subfile = "AddressSort";
256 $description = "Demonstrates Use of sort filter";
259 'sort' => 'street_address DESC',
264 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
265 $this->assertEquals(2, $result['count']);
266 $this->assertEquals('Ambachtstraat 23', $result['values'][1]['street_address']);
267 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
271 * Test civicrm_address_get with sort option- success expected.
273 public function testGetAddressLikeSuccess() {
274 $this->callAPISuccess('address', 'create', $this->_params
);
275 $subfile = "AddressLike";
276 $description = "Demonstrates Use of Like";
278 'street_address' => array('LIKE' => '%mb%'),
281 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
282 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
283 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__
);
284 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
288 * Test civicrm_address_get with sort option- success expected.
290 public function testGetAddressLikeFail() {
291 $create = $this->callAPISuccess('address', 'create', $this->_params
);
293 'street_address' => array('LIKE' => "'%xy%'"),
296 $result = $this->callAPISuccess('Address', 'Get', ($params));
297 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__
);
298 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
303 public function testGetWithCustom() {
304 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
306 $params = $this->_params
;
307 $params['custom_' . $ids['custom_field_id']] = "custom string";
309 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
311 $getParams = array('id' => $result['id'], 'return' => array('custom'));
312 $check = $this->callAPISuccess($this->_entity
, 'get', $getParams);
314 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
316 $this->customFieldDelete($ids['custom_field_id']);
317 $this->customGroupDelete($ids['custom_group_id']);
318 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
323 public function testCreateAddressPrimaryHandlingChangeToPrimary() {
324 $params = $this->_params
;
325 unset($params['is_primary']);
326 $address1 = $this->callAPISuccess('address', 'create', $params);
327 $this->assertApiSuccess($address1, 'In line ' . __LINE__
);
328 //now we check & make sure it has been set to primary
329 $check = $this->callAPISuccess('address', 'getcount', array(
331 'id' => $address1['id'],
333 $this->assertEquals(1, $check);
334 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
339 public function testCreateAddressPrimaryHandlingChangeExisting() {
340 $address1 = $this->callAPISuccess('address', 'create', $this->_params
);
341 $this->callAPISuccess('address', 'create', $this->_params
);
342 $check = $this->callAPISuccess('address', 'getcount', array(
344 'contact_id' => $this->_contactID
,
346 $this->assertEquals(1, $check);
347 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));