Refactor cache flush CRM-12464
[civicrm-core.git] / tests / phpunit / api / v3 / AddressTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * Test APIv3 civicrm_activity_* functions
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_Contact
34 */
35
36 require_once 'CiviTest/CiviUnitTestCase.php';
37 class api_v3_AddressTest extends CiviUnitTestCase {
38 protected $_apiversion;
39 protected $_contactID;
40 protected $_locationType;
41 protected $_params;
42 public $_eNoticeCompliant = TRUE;
43 protected $_entity; function setUp() {
44 $this->_apiversion = 3;
45 $this->_entity = 'Address';
46 parent::setUp();
47
48 $this->_contactID = $this->organizationCreate();
49 $this->_locationType = $this->locationTypeCreate();
50 CRM_Core_PseudoConstant::flush();
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,
62 'version' => $this->_apiversion,
63 );
64 }
65
66 function tearDown() {
67 $this->locationTypeDelete($this->_locationType->id);
68 $this->contactDelete($this->_contactID);
69 }
70
71 public function testCreateAddress() {
72
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');
79 }
80 public function testCreateAddressParsing() {
81 $params = array(
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,
87 );
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']));
98
99 }
100
101 /*
102 * is_primary should be set as a default
103 */
104
105
106
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');
115 }
116
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']);
123 }
124 /*
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
128 */
129
130
131
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__);
144 }
145 public function testDeleteAddress() {
146
147 //check there are no addresss to start with
148 $get = civicrm_api('address', 'get', array(
149 'version' => 3,
150 'location_type_id' => $this->_locationType->id,
151 ));
152 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__);
153 $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__);
154
155 //create one
156 $create = civicrm_api('address', 'create', $this->_params);
157
158 $this->assertEquals(0, $create['is_error'], 'In line ' . __LINE__);
159
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(
165 'version' => 3,
166 'location_type_id' => $this->_locationType->id,
167 ));
168 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__);
169 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
170 }
171
172 /**
173 * Test civicrm_address_get - success expected.
174 */
175 public function testGetAddress() {
176 $address = civicrm_api('address', 'create', $this->_params);
177 $this->assertAPISuccess($address, 'In line ' . __LINE__);
178
179 $params = array(
180 'contact_id' => $this->_contactID,
181 'street_name' => $address['values'][$address['id']]['street_name'],
182 'version' => $this->_apiversion,
183 );
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__);
191 }
192
193 /**
194 * Test civicrm_address_get - success expected.
195 */
196 public function testGetSingleAddress() {
197 civicrm_api('address', 'create', $this->_params);
198 $params = array(
199 'contact_id' => $this->_contactID,
200 'version' => $this->_apiversion,
201 );
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']));
205 }
206
207 /**
208 * Test civicrm_address_get with sort option- success expected.
209 */
210 public function testGetAddressSort() {
211 $create = civicrm_api('address', 'create', $this->_params);
212 $subfile = "AddressSort";
213 $description = "Demonstrates Use of sort filter";
214 $params = array(
215 'options' => array(
216 'sort' => 'street_address DESC',
217 'limit' => 2,
218 ),
219 'version' => $this->_apiversion,
220 'sequential' => 1,
221 );
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']));
228 }
229
230 /**
231 * Test civicrm_address_get with sort option- success expected.
232 */
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,
239 'sequential' => 1,
240 );
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']));
247 }
248
249 /**
250 * Test civicrm_address_get with sort option- success expected.
251 */
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,
258 'sequential' => 1,
259 );
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']));
264 }
265
266 function testGetWithCustom() {
267 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
268
269 $params = $this->_params;
270 $params['custom_' . $ids['custom_field_id']] = "custom string";
271
272 $result = civicrm_api($this->_entity, 'create', $params);
273 $this->assertAPISuccess($result, ' in line ' . __LINE__);
274
275 $getParams = array('version' => 3, 'id' => $result['id'], 'return' => array('custom'));
276 $check = civicrm_api($this->_entity, 'get', $getParams);
277
278 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
279
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']));
283 }
284
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(
292 'version' => 3,
293 'is_primary' => 1,
294 'id' => $address1['id'],
295 ));
296 $this->assertEquals(1, $check);
297 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id']));
298 }
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(
305 'version' => 3,
306 'is_primary' => 1,
307 'contact_id' => $this->_contactID,
308 ));
309 $this->assertEquals(1, $check);
310 civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id']));
311 }
312 }
313