Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-01-14-40-22
[civicrm-core.git] / tests / phpunit / api / v3 / AddressTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
35 require_once 'CiviTest/CiviUnitTestCase.php';
36
37 /**
38 * Class api_v3_AddressTest
39 */
40 class api_v3_AddressTest extends CiviUnitTestCase {
41 protected $_apiversion =3;
42 protected $_contactID;
43 protected $_locationType;
44 protected $_params;
45
46 protected $_entity;
47
48 function setUp() {
49 $this->_entity = 'Address';
50 parent::setUp();
51
52 $this->_contactID = $this->organizationCreate();
53 $this->_locationType = $this->locationTypeCreate();
54 CRM_Core_PseudoConstant::flush();
55
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',
64 'city' => 'Brummen',
65 'is_primary' => 1,
66 );
67 }
68
69 function tearDown() {
70 $this->locationTypeDelete($this->_locationType->id);
71 $this->contactDelete($this->_contactID);
72 }
73
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');
79 }
80
81 public function testCreateAddressParsing() {
82 $params = array(
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 = $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']));
96
97 }
98
99 /**
100 * is_primary should be set as a default
101 */
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');
109 }
110
111 public function testCreateAddressTooLongSuffix() {
112 $params = $this->_params;
113 $params['street_number_suffix'] = 'really long string';
114 $result = $this->callAPIFailure('address', 'create', $params);
115 }
116
117 /**
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
121 */
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__);
133 }
134 public function testDeleteAddress() {
135
136 //check there are no addresss to start with
137 $get = $this->callAPISuccess('address', 'get', array(
138 'location_type_id' => $this->_locationType->id,
139 ));
140 $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__);
141
142 //create one
143 $create = $this->callAPISuccess('address', 'create', $this->_params);
144
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,
149 ));
150 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
151 }
152
153 /**
154 * Test civicrm_address_get - success expected.
155 */
156 public function testGetAddress() {
157 $address = $this->callAPISuccess('address', 'create', $this->_params);
158
159 $params = array(
160 'contact_id' => $this->_contactID,
161 'street_name' => $address['values'][$address['id']]['street_name'],
162 );
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__);
168 }
169
170 /**
171 * Test civicrm_address_get - success expected.
172 */
173 public function testGetSingleAddress() {
174 $this->callAPISuccess('address', 'create', $this->_params);
175 $params = array(
176 'contact_id' => $this->_contactID,
177 );
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']));
181 }
182
183 /**
184 * Test civicrm_address_get with sort option- success expected.
185 */
186 public function testGetAddressSort() {
187 $create = $this->callAPISuccess('address', 'create', $this->_params);
188 $subfile = "AddressSort";
189 $description = "Demonstrates Use of sort filter";
190 $params = array(
191 'options' => array(
192 'sort' => 'street_address DESC',
193 'limit' => 2,
194 ),
195 'sequential' => 1,
196 );
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']));
201 }
202
203 /**
204 * Test civicrm_address_get with sort option- success expected.
205 */
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%'),
211 'sequential' => 1,
212 );
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']));
217 }
218
219 /**
220 * Test civicrm_address_get with sort option- success expected.
221 */
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%'"),
227 'sequential' => 1,
228 );
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']));
232 }
233
234 function testGetWithCustom() {
235 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
236
237 $params = $this->_params;
238 $params['custom_' . $ids['custom_field_id']] = "custom string";
239
240 $result = $this->callAPISuccess($this->_entity, 'create', $params);
241
242 $getParams = array('id' => $result['id'], 'return' => array('custom'));
243 $check = $this->callAPISuccess($this->_entity, 'get', $getParams);
244
245 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
246
247 $this->customFieldDelete($ids['custom_field_id']);
248 $this->customGroupDelete($ids['custom_group_id']);
249 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
250 }
251
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(
259 'is_primary' => 1,
260 'id' => $address1['id'],
261 ));
262 $this->assertEquals(1, $check);
263 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
264 }
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(
269 'is_primary' => 1,
270 'contact_id' => $this->_contactID,
271 ));
272 $this->assertEquals(1, $check);
273 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
274 }
275 }
276