Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
06a1bc01 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06a1bc01 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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'; | |
4cbe18b8 EM |
36 | |
37 | /** | |
38 | * Class api_v3_AddressTest | |
39 | */ | |
6a488035 | 40 | class api_v3_AddressTest extends CiviUnitTestCase { |
4e420887 | 41 | protected $_apiversion =3; |
6a488035 TO |
42 | protected $_contactID; |
43 | protected $_locationType; | |
44 | protected $_params; | |
b7c9bc4c | 45 | |
430ae6dd TO |
46 | protected $_entity; |
47 | ||
48 | function setUp() { | |
6a488035 TO |
49 | $this->_entity = 'Address'; |
50 | parent::setUp(); | |
51 | ||
52 | $this->_contactID = $this->organizationCreate(); | |
53 | $this->_locationType = $this->locationTypeCreate(); | |
2683ce94 | 54 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
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, | |
6a488035 TO |
66 | ); |
67 | } | |
68 | ||
69 | function tearDown() { | |
70 | $this->locationTypeDelete($this->_locationType->id); | |
71 | $this->contactDelete($this->_contactID); | |
72 | } | |
73 | ||
74 | public function testCreateAddress() { | |
4e420887 | 75 | $result = $this->callAPIAndDocument('address', 'create', $this->_params, __FUNCTION__, __FILE__); |
6a488035 TO |
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 | } | |
4e420887 | 80 | |
6a488035 TO |
81 | public function testCreateAddressParsing() { |
82 | $params = array( | |
6a488035 TO |
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"; | |
4e420887 | 90 | $result = $this->callAPIAndDocument('address', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
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__); | |
4e420887 | 95 | $this->callAPISuccess('address', 'delete', array('id' => $result['id'])); |
6a488035 TO |
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']); | |
4e420887 | 108 | $result = $this->callAPISuccess('address', 'create', $params); |
6a488035 TO |
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'; | |
d0e1eff2 | 117 | $result = $this->callAPIFailure('address', 'create', $params); |
4e420887 | 118 | } |
6a488035 TO |
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; | |
4e420887 | 130 | $result = $this->callAPISuccess('address', 'create', $params); |
6a488035 TO |
131 | unset($params['is_primary']); |
132 | $params['id'] = $result['id']; | |
4e420887 | 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__); | |
6a488035 TO |
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 | |
4e420887 | 142 | $get = $this->callAPISuccess('address', 'get', array( |
143 | 'location_type_id' => $this->_locationType->id, | |
144 | )); | |
6a488035 TO |
145 | $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__); |
146 | ||
147 | //create one | |
4e420887 | 148 | $create = $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 | 149 | |
4e420887 | 150 | $result = $this->callAPIAndDocument('address', 'delete', array('id' => $create['id'],), __FUNCTION__, __FILE__); |
6a488035 | 151 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
4e420887 | 152 | $get = $this->callAPISuccess('address', 'get', array( |
153 | 'location_type_id' => $this->_locationType->id, | |
154 | )); | |
6a488035 TO |
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() { | |
4e420887 | 162 | $address = $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 TO |
163 | |
164 | $params = array( | |
165 | 'contact_id' => $this->_contactID, | |
166 | 'street_name' => $address['values'][$address['id']]['street_name'], | |
6a488035 | 167 | ); |
4e420887 | 168 | $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__); |
169 | $this->callAPISuccess('Address', 'delete', array('id' => $result['id'])); | |
6a488035 TO |
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() { | |
4e420887 | 179 | $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 TO |
180 | $params = array( |
181 | 'contact_id' => $this->_contactID, | |
6a488035 | 182 | ); |
4e420887 | 183 | $address = $this->callAPISuccess('Address', 'getsingle', ($params)); |
6a488035 | 184 | $this->assertEquals($address['location_type_id'], $this->_params['location_type_id'], 'In line ' . __LINE__); |
4e420887 | 185 | $this->callAPISuccess('address', 'delete', array('id' => $address['id'])); |
6a488035 TO |
186 | } |
187 | ||
188 | /** | |
189 | * Test civicrm_address_get with sort option- success expected. | |
190 | */ | |
191 | public function testGetAddressSort() { | |
4e420887 | 192 | $create = $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 TO |
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 | ), | |
6a488035 TO |
200 | 'sequential' => 1, |
201 | ); | |
4e420887 | 202 | $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
203 | $this->assertEquals(2, $result['count'], 'In line ' . __LINE__); |
204 | $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__); | |
4e420887 | 205 | $this->callAPISuccess('address', 'delete', array('id' => $create['id'])); |
6a488035 TO |
206 | } |
207 | ||
208 | /** | |
209 | * Test civicrm_address_get with sort option- success expected. | |
210 | */ | |
211 | public function testGetAddressLikeSuccess() { | |
4e420887 | 212 | $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 TO |
213 | $subfile = "AddressLike"; |
214 | $description = "Demonstrates Use of Like"; | |
215 | $params = array('street_address' => array('LIKE' => '%mb%'), | |
6a488035 TO |
216 | 'sequential' => 1, |
217 | ); | |
4e420887 | 218 | $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
219 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
220 | $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__); | |
4e420887 | 221 | $this->callAPISuccess('address', 'delete', array('id' => $result['id'])); |
6a488035 TO |
222 | } |
223 | ||
224 | /** | |
225 | * Test civicrm_address_get with sort option- success expected. | |
226 | */ | |
227 | public function testGetAddressLikeFail() { | |
4e420887 | 228 | $create = $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 TO |
229 | $subfile = "AddressLike"; |
230 | $description = "Demonstrates Use of Like"; | |
231 | $params = array('street_address' => array('LIKE' => "'%xy%'"), | |
6a488035 TO |
232 | 'sequential' => 1, |
233 | ); | |
4e420887 | 234 | $result = $this->callAPISuccess('Address', 'Get', ($params)); |
6a488035 | 235 | $this->assertEquals(0, $result['count'], 'In line ' . __LINE__); |
4e420887 | 236 | $this->callAPISuccess('address', 'delete', array('id' => $create['id'])); |
6a488035 TO |
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 | ||
4e420887 | 245 | $result = $this->callAPISuccess($this->_entity, 'create', $params); |
6a488035 | 246 | |
4e420887 | 247 | $getParams = array('id' => $result['id'], 'return' => array('custom')); |
248 | $check = $this->callAPISuccess($this->_entity, 'get', $getParams); | |
6a488035 TO |
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']); | |
4e420887 | 254 | $this->callAPISuccess('address', 'delete', array('id' => $result['id'])); |
6a488035 TO |
255 | } |
256 | ||
257 | public function testCreateAddressPrimaryHandlingChangeToPrimary() { | |
258 | $params = $this->_params; | |
259 | unset($params['is_primary']); | |
4e420887 | 260 | $address1 = $this->callAPISuccess('address', 'create', $params); |
6a488035 TO |
261 | $this->assertApiSuccess($address1, 'In line ' . __LINE__); |
262 | //now we check & make sure it has been set to primary | |
4e420887 | 263 | $check = $this->callAPISuccess('address', 'getcount', array( |
6a488035 TO |
264 | 'is_primary' => 1, |
265 | 'id' => $address1['id'], | |
266 | )); | |
267 | $this->assertEquals(1, $check); | |
4e420887 | 268 | $this->callAPISuccess('address', 'delete', array('id' => $address1['id'])); |
6a488035 TO |
269 | } |
270 | public function testCreateAddressPrimaryHandlingChangeExisting() { | |
4e420887 | 271 | $address1 = $this->callAPISuccess('address', 'create', $this->_params); |
272 | $address2 = $this->callAPISuccess('address', 'create', $this->_params); | |
273 | $check = $this->callAPISuccess('address', 'getcount', array( | |
6a488035 TO |
274 | 'is_primary' => 1, |
275 | 'contact_id' => $this->_contactID, | |
276 | )); | |
277 | $this->assertEquals(1, $check); | |
4e420887 | 278 | $this->callAPISuccess('address', 'delete', array('id' => $address1['id'])); |
6a488035 TO |
279 | } |
280 | } | |
281 |