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 | ||
c490a46a | 99 | /** |
100fef9d | 100 | * Is_primary should be set as a default |
c490a46a | 101 | */ |
6a488035 TO |
102 | public function testCreateAddressTestDefaults() { |
103 | $params = $this->_params; | |
104 | unset($params['is_primary']); | |
4e420887 | 105 | $result = $this->callAPISuccess('address', 'create', $params); |
6a488035 TO |
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'; | |
d0e1eff2 | 114 | $result = $this->callAPIFailure('address', 'create', $params); |
4e420887 | 115 | } |
6a488035 | 116 | |
c490a46a | 117 | /** |
100fef9d | 118 | * Is_primary shoule be set as a default. ie. create the address, unset the params & recreate. |
c490a46a CW |
119 | * is_primary should be 0 before & after the update. ie - having no other address |
120 | * is_primary is invalid | |
121 | */ | |
6a488035 TO |
122 | public function testCreateAddressTestDefaultWithID() { |
123 | $params = $this->_params; | |
124 | $params['is_primary'] = 0; | |
4e420887 | 125 | $result = $this->callAPISuccess('address', 'create', $params); |
6a488035 TO |
126 | unset($params['is_primary']); |
127 | $params['id'] = $result['id']; | |
4e420887 | 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__); | |
6a488035 TO |
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 | |
4e420887 | 137 | $get = $this->callAPISuccess('address', 'get', array( |
138 | 'location_type_id' => $this->_locationType->id, | |
139 | )); | |
6a488035 TO |
140 | $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__); |
141 | ||
142 | //create one | |
4e420887 | 143 | $create = $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 | 144 | |
4e420887 | 145 | $result = $this->callAPIAndDocument('address', 'delete', array('id' => $create['id'],), __FUNCTION__, __FILE__); |
6a488035 | 146 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
4e420887 | 147 | $get = $this->callAPISuccess('address', 'get', array( |
148 | 'location_type_id' => $this->_locationType->id, | |
149 | )); | |
6a488035 TO |
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() { | |
4e420887 | 157 | $address = $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 TO |
158 | |
159 | $params = array( | |
160 | 'contact_id' => $this->_contactID, | |
161 | 'street_name' => $address['values'][$address['id']]['street_name'], | |
6a488035 | 162 | ); |
4e420887 | 163 | $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__); |
164 | $this->callAPISuccess('Address', 'delete', array('id' => $result['id'])); | |
6a488035 TO |
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() { | |
4e420887 | 174 | $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 TO |
175 | $params = array( |
176 | 'contact_id' => $this->_contactID, | |
6a488035 | 177 | ); |
4e420887 | 178 | $address = $this->callAPISuccess('Address', 'getsingle', ($params)); |
6a488035 | 179 | $this->assertEquals($address['location_type_id'], $this->_params['location_type_id'], 'In line ' . __LINE__); |
4e420887 | 180 | $this->callAPISuccess('address', 'delete', array('id' => $address['id'])); |
6a488035 TO |
181 | } |
182 | ||
183 | /** | |
184 | * Test civicrm_address_get with sort option- success expected. | |
185 | */ | |
186 | public function testGetAddressSort() { | |
4e420887 | 187 | $create = $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 TO |
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 | ), | |
6a488035 TO |
195 | 'sequential' => 1, |
196 | ); | |
4e420887 | 197 | $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
198 | $this->assertEquals(2, $result['count'], 'In line ' . __LINE__); |
199 | $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__); | |
4e420887 | 200 | $this->callAPISuccess('address', 'delete', array('id' => $create['id'])); |
6a488035 TO |
201 | } |
202 | ||
203 | /** | |
204 | * Test civicrm_address_get with sort option- success expected. | |
205 | */ | |
206 | public function testGetAddressLikeSuccess() { | |
4e420887 | 207 | $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 TO |
208 | $subfile = "AddressLike"; |
209 | $description = "Demonstrates Use of Like"; | |
210 | $params = array('street_address' => array('LIKE' => '%mb%'), | |
6a488035 TO |
211 | 'sequential' => 1, |
212 | ); | |
4e420887 | 213 | $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
214 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
215 | $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__); | |
4e420887 | 216 | $this->callAPISuccess('address', 'delete', array('id' => $result['id'])); |
6a488035 TO |
217 | } |
218 | ||
219 | /** | |
220 | * Test civicrm_address_get with sort option- success expected. | |
221 | */ | |
222 | public function testGetAddressLikeFail() { | |
4e420887 | 223 | $create = $this->callAPISuccess('address', 'create', $this->_params); |
6a488035 TO |
224 | $subfile = "AddressLike"; |
225 | $description = "Demonstrates Use of Like"; | |
226 | $params = array('street_address' => array('LIKE' => "'%xy%'"), | |
6a488035 TO |
227 | 'sequential' => 1, |
228 | ); | |
4e420887 | 229 | $result = $this->callAPISuccess('Address', 'Get', ($params)); |
6a488035 | 230 | $this->assertEquals(0, $result['count'], 'In line ' . __LINE__); |
4e420887 | 231 | $this->callAPISuccess('address', 'delete', array('id' => $create['id'])); |
6a488035 TO |
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 | ||
4e420887 | 240 | $result = $this->callAPISuccess($this->_entity, 'create', $params); |
6a488035 | 241 | |
4e420887 | 242 | $getParams = array('id' => $result['id'], 'return' => array('custom')); |
243 | $check = $this->callAPISuccess($this->_entity, 'get', $getParams); | |
6a488035 TO |
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']); | |
4e420887 | 249 | $this->callAPISuccess('address', 'delete', array('id' => $result['id'])); |
6a488035 TO |
250 | } |
251 | ||
252 | public function testCreateAddressPrimaryHandlingChangeToPrimary() { | |
253 | $params = $this->_params; | |
254 | unset($params['is_primary']); | |
4e420887 | 255 | $address1 = $this->callAPISuccess('address', 'create', $params); |
6a488035 TO |
256 | $this->assertApiSuccess($address1, 'In line ' . __LINE__); |
257 | //now we check & make sure it has been set to primary | |
4e420887 | 258 | $check = $this->callAPISuccess('address', 'getcount', array( |
6a488035 TO |
259 | 'is_primary' => 1, |
260 | 'id' => $address1['id'], | |
261 | )); | |
262 | $this->assertEquals(1, $check); | |
4e420887 | 263 | $this->callAPISuccess('address', 'delete', array('id' => $address1['id'])); |
6a488035 TO |
264 | } |
265 | public function testCreateAddressPrimaryHandlingChangeExisting() { | |
4e420887 | 266 | $address1 = $this->callAPISuccess('address', 'create', $this->_params); |
267 | $address2 = $this->callAPISuccess('address', 'create', $this->_params); | |
268 | $check = $this->callAPISuccess('address', 'getcount', array( | |
6a488035 TO |
269 | 'is_primary' => 1, |
270 | 'contact_id' => $this->_contactID, | |
271 | )); | |
272 | $this->assertEquals(1, $check); | |
4e420887 | 273 | $this->callAPISuccess('address', 'delete', array('id' => $address1['id'])); |
6a488035 TO |
274 | } |
275 | } | |
276 |