fix e-Notice on deprecated utils
[civicrm-core.git] / tests / phpunit / api / v3 / AddressTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28/**
29 * Test APIv3 civicrm_activity_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
33 */
34
35require_once 'CiviTest/CiviUnitTestCase.php';
36class api_v3_AddressTest extends CiviUnitTestCase {
4e420887 37 protected $_apiversion =3;
6a488035
TO
38 protected $_contactID;
39 protected $_locationType;
40 protected $_params;
41 public $_eNoticeCompliant = TRUE;
430ae6dd
TO
42 protected $_entity;
43
44 function setUp() {
6a488035
TO
45 $this->_entity = 'Address';
46 parent::setUp();
47
48 $this->_contactID = $this->organizationCreate();
49 $this->_locationType = $this->locationTypeCreate();
2683ce94 50 CRM_Core_PseudoConstant::flush();
6a488035
TO
51
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',
60 'city' => 'Brummen',
61 'is_primary' => 1,
6a488035
TO
62 );
63 }
64
65 function tearDown() {
66 $this->locationTypeDelete($this->_locationType->id);
67 $this->contactDelete($this->_contactID);
68 }
69
70 public function testCreateAddress() {
4e420887 71 $result = $this->callAPIAndDocument('address', 'create', $this->_params, __FUNCTION__, __FILE__);
6a488035
TO
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');
75 }
4e420887 76
6a488035
TO
77 public function testCreateAddressParsing() {
78 $params = array(
6a488035
TO
79 'street_parsing' => 1,
80 'street_address' => '54A Excelsior Ave. Apt 1C',
81 'location_type_id' => $this->_locationType->id,
82 'contact_id' => $this->_contactID,
83 );
84 $subfile = "AddressParse";
85 $description = "Demonstrates Use of address parsing param";
4e420887 86 $result = $this->callAPIAndDocument('address', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
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__);
4e420887 91 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
6a488035
TO
92
93 }
94
95 /*
96 * is_primary should be set as a default
97 */
98
99
100
101 public function testCreateAddressTestDefaults() {
102 $params = $this->_params;
103 unset($params['is_primary']);
4e420887 104 $result = $this->callAPISuccess('address', 'create', $params);
6a488035
TO
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');
108 }
109
110 public function testCreateAddressTooLongSuffix() {
111 $params = $this->_params;
112 $params['street_number_suffix'] = 'really long string';
d0e1eff2 113 $result = $this->callAPIFailure('address', 'create', $params);
4e420887 114 }
6a488035
TO
115 /*
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
119 */
120
121
122
123 public function testCreateAddressTestDefaultWithID() {
124 $params = $this->_params;
125 $params['is_primary'] = 0;
4e420887 126 $result = $this->callAPISuccess('address', 'create', $params);
6a488035
TO
127 unset($params['is_primary']);
128 $params['id'] = $result['id'];
4e420887 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__);
6a488035
TO
132 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__);
133 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__);
134 }
135 public function testDeleteAddress() {
136
137 //check there are no addresss to start with
4e420887 138 $get = $this->callAPISuccess('address', 'get', array(
139 'location_type_id' => $this->_locationType->id,
140 ));
6a488035
TO
141 $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__);
142
143 //create one
4e420887 144 $create = $this->callAPISuccess('address', 'create', $this->_params);
6a488035 145
4e420887 146 $result = $this->callAPIAndDocument('address', 'delete', array('id' => $create['id'],), __FUNCTION__, __FILE__);
6a488035 147 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
4e420887 148 $get = $this->callAPISuccess('address', 'get', array(
149 'location_type_id' => $this->_locationType->id,
150 ));
6a488035
TO
151 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
152 }
153
154 /**
155 * Test civicrm_address_get - success expected.
156 */
157 public function testGetAddress() {
4e420887 158 $address = $this->callAPISuccess('address', 'create', $this->_params);
6a488035
TO
159
160 $params = array(
161 'contact_id' => $this->_contactID,
162 'street_name' => $address['values'][$address['id']]['street_name'],
6a488035 163 );
4e420887 164 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__);
165 $this->callAPISuccess('Address', 'delete', array('id' => $result['id']));
6a488035
TO
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__);
169 }
170
171 /**
172 * Test civicrm_address_get - success expected.
173 */
174 public function testGetSingleAddress() {
4e420887 175 $this->callAPISuccess('address', 'create', $this->_params);
6a488035
TO
176 $params = array(
177 'contact_id' => $this->_contactID,
6a488035 178 );
4e420887 179 $address = $this->callAPISuccess('Address', 'getsingle', ($params));
6a488035 180 $this->assertEquals($address['location_type_id'], $this->_params['location_type_id'], 'In line ' . __LINE__);
4e420887 181 $this->callAPISuccess('address', 'delete', array('id' => $address['id']));
6a488035
TO
182 }
183
184 /**
185 * Test civicrm_address_get with sort option- success expected.
186 */
187 public function testGetAddressSort() {
4e420887 188 $create = $this->callAPISuccess('address', 'create', $this->_params);
6a488035
TO
189 $subfile = "AddressSort";
190 $description = "Demonstrates Use of sort filter";
191 $params = array(
192 'options' => array(
193 'sort' => 'street_address DESC',
194 'limit' => 2,
195 ),
6a488035
TO
196 'sequential' => 1,
197 );
4e420887 198 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
199 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__);
200 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__);
4e420887 201 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
6a488035
TO
202 }
203
204 /**
205 * Test civicrm_address_get with sort option- success expected.
206 */
207 public function testGetAddressLikeSuccess() {
4e420887 208 $this->callAPISuccess('address', 'create', $this->_params);
6a488035
TO
209 $subfile = "AddressLike";
210 $description = "Demonstrates Use of Like";
211 $params = array('street_address' => array('LIKE' => '%mb%'),
6a488035
TO
212 'sequential' => 1,
213 );
4e420887 214 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
215 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
216 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__);
4e420887 217 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
6a488035
TO
218 }
219
220 /**
221 * Test civicrm_address_get with sort option- success expected.
222 */
223 public function testGetAddressLikeFail() {
4e420887 224 $create = $this->callAPISuccess('address', 'create', $this->_params);
6a488035
TO
225 $subfile = "AddressLike";
226 $description = "Demonstrates Use of Like";
227 $params = array('street_address' => array('LIKE' => "'%xy%'"),
6a488035
TO
228 'sequential' => 1,
229 );
4e420887 230 $result = $this->callAPISuccess('Address', 'Get', ($params));
6a488035 231 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__);
4e420887 232 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
6a488035
TO
233 }
234
235 function testGetWithCustom() {
236 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
237
238 $params = $this->_params;
239 $params['custom_' . $ids['custom_field_id']] = "custom string";
240
4e420887 241 $result = $this->callAPISuccess($this->_entity, 'create', $params);
6a488035 242
4e420887 243 $getParams = array('id' => $result['id'], 'return' => array('custom'));
244 $check = $this->callAPISuccess($this->_entity, 'get', $getParams);
6a488035
TO
245
246 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
247
248 $this->customFieldDelete($ids['custom_field_id']);
249 $this->customGroupDelete($ids['custom_group_id']);
4e420887 250 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
6a488035
TO
251 }
252
253 public function testCreateAddressPrimaryHandlingChangeToPrimary() {
254 $params = $this->_params;
255 unset($params['is_primary']);
4e420887 256 $address1 = $this->callAPISuccess('address', 'create', $params);
6a488035
TO
257 $this->assertApiSuccess($address1, 'In line ' . __LINE__);
258 //now we check & make sure it has been set to primary
4e420887 259 $check = $this->callAPISuccess('address', 'getcount', array(
6a488035
TO
260 'is_primary' => 1,
261 'id' => $address1['id'],
262 ));
263 $this->assertEquals(1, $check);
4e420887 264 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
6a488035
TO
265 }
266 public function testCreateAddressPrimaryHandlingChangeExisting() {
4e420887 267 $address1 = $this->callAPISuccess('address', 'create', $this->_params);
268 $address2 = $this->callAPISuccess('address', 'create', $this->_params);
269 $check = $this->callAPISuccess('address', 'getcount', array(
6a488035
TO
270 'is_primary' => 1,
271 'contact_id' => $this->_contactID,
272 ));
273 $this->assertEquals(1, $check);
4e420887 274 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
6a488035
TO
275 }
276}
277