Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-25-10-57-01
[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
103
104
105 public function testCreateAddressTestDefaults() {
106 $params = $this->_params;
107 unset($params['is_primary']);
108 $result = $this->callAPISuccess('address', 'create', $params);
109 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
110 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__);
111 $this->getAndCheck($this->_params, $result['id'], 'address');
112 }
113
114 public function testCreateAddressTooLongSuffix() {
115 $params = $this->_params;
116 $params['street_number_suffix'] = 'really long string';
117 $result = $this->callAPIFailure('address', 'create', $params);
118 }
119 /*
120 * is_primary shoule be set as a default. ie. create the address, unset the params & recreate.
121 * is_primary should be 0 before & after the update. ie - having no other address
122 * is_primary is invalid
123 */
124
125
126
127 public function testCreateAddressTestDefaultWithID() {
128 $params = $this->_params;
129 $params['is_primary'] = 0;
130 $result = $this->callAPISuccess('address', 'create', $params);
131 unset($params['is_primary']);
132 $params['id'] = $result['id'];
133 $result = $this->callAPISuccess('address', 'create', $params);
134 $address = $this->callAPISuccess('address', 'get', array('contact_id' => $params['contact_id']));
135 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
136 $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__);
137 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__);
138 }
139 public function testDeleteAddress() {
140
141 //check there are no addresss to start with
142 $get = $this->callAPISuccess('address', 'get', array(
143 'location_type_id' => $this->_locationType->id,
144 ));
145 $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__);
146
147 //create one
148 $create = $this->callAPISuccess('address', 'create', $this->_params);
149
150 $result = $this->callAPIAndDocument('address', 'delete', array('id' => $create['id'],), __FUNCTION__, __FILE__);
151 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
152 $get = $this->callAPISuccess('address', 'get', array(
153 'location_type_id' => $this->_locationType->id,
154 ));
155 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
156 }
157
158 /**
159 * Test civicrm_address_get - success expected.
160 */
161 public function testGetAddress() {
162 $address = $this->callAPISuccess('address', 'create', $this->_params);
163
164 $params = array(
165 'contact_id' => $this->_contactID,
166 'street_name' => $address['values'][$address['id']]['street_name'],
167 );
168 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__);
169 $this->callAPISuccess('Address', 'delete', array('id' => $result['id']));
170 $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id'], 'In line ' . __LINE__);
171 $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary'], 'In line ' . __LINE__);
172 $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address'], 'In line ' . __LINE__);
173 }
174
175 /**
176 * Test civicrm_address_get - success expected.
177 */
178 public function testGetSingleAddress() {
179 $this->callAPISuccess('address', 'create', $this->_params);
180 $params = array(
181 'contact_id' => $this->_contactID,
182 );
183 $address = $this->callAPISuccess('Address', 'getsingle', ($params));
184 $this->assertEquals($address['location_type_id'], $this->_params['location_type_id'], 'In line ' . __LINE__);
185 $this->callAPISuccess('address', 'delete', array('id' => $address['id']));
186 }
187
188 /**
189 * Test civicrm_address_get with sort option- success expected.
190 */
191 public function testGetAddressSort() {
192 $create = $this->callAPISuccess('address', 'create', $this->_params);
193 $subfile = "AddressSort";
194 $description = "Demonstrates Use of sort filter";
195 $params = array(
196 'options' => array(
197 'sort' => 'street_address DESC',
198 'limit' => 2,
199 ),
200 'sequential' => 1,
201 );
202 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
203 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__);
204 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__);
205 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
206 }
207
208 /**
209 * Test civicrm_address_get with sort option- success expected.
210 */
211 public function testGetAddressLikeSuccess() {
212 $this->callAPISuccess('address', 'create', $this->_params);
213 $subfile = "AddressLike";
214 $description = "Demonstrates Use of Like";
215 $params = array('street_address' => array('LIKE' => '%mb%'),
216 'sequential' => 1,
217 );
218 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
219 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
220 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__);
221 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
222 }
223
224 /**
225 * Test civicrm_address_get with sort option- success expected.
226 */
227 public function testGetAddressLikeFail() {
228 $create = $this->callAPISuccess('address', 'create', $this->_params);
229 $subfile = "AddressLike";
230 $description = "Demonstrates Use of Like";
231 $params = array('street_address' => array('LIKE' => "'%xy%'"),
232 'sequential' => 1,
233 );
234 $result = $this->callAPISuccess('Address', 'Get', ($params));
235 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__);
236 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
237 }
238
239 function testGetWithCustom() {
240 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
241
242 $params = $this->_params;
243 $params['custom_' . $ids['custom_field_id']] = "custom string";
244
245 $result = $this->callAPISuccess($this->_entity, 'create', $params);
246
247 $getParams = array('id' => $result['id'], 'return' => array('custom'));
248 $check = $this->callAPISuccess($this->_entity, 'get', $getParams);
249
250 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
251
252 $this->customFieldDelete($ids['custom_field_id']);
253 $this->customGroupDelete($ids['custom_group_id']);
254 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
255 }
256
257 public function testCreateAddressPrimaryHandlingChangeToPrimary() {
258 $params = $this->_params;
259 unset($params['is_primary']);
260 $address1 = $this->callAPISuccess('address', 'create', $params);
261 $this->assertApiSuccess($address1, 'In line ' . __LINE__);
262 //now we check & make sure it has been set to primary
263 $check = $this->callAPISuccess('address', 'getcount', array(
264 'is_primary' => 1,
265 'id' => $address1['id'],
266 ));
267 $this->assertEquals(1, $check);
268 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
269 }
270 public function testCreateAddressPrimaryHandlingChangeExisting() {
271 $address1 = $this->callAPISuccess('address', 'create', $this->_params);
272 $address2 = $this->callAPISuccess('address', 'create', $this->_params);
273 $check = $this->callAPISuccess('address', 'getcount', array(
274 'is_primary' => 1,
275 'contact_id' => $this->_contactID,
276 ));
277 $this->assertEquals(1, $check);
278 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
279 }
280 }
281