| 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 | |
| 31 | |
| 32 | |
| 33 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 34 | class api_v3_ContactTypeTest extends CiviUnitTestCase { |
| 35 | protected $_apiversion; |
| 36 | public $_eNoticeCompliant = TRUE; |
| 37 | function setUp() { |
| 38 | parent::setUp(); |
| 39 | $this->_apiversion = 3; |
| 40 | $params = array( |
| 41 | 'label' => 'sub_individual', |
| 42 | 'name' => 'sub_individual', |
| 43 | // Individual |
| 44 | 'parent_id' => 1, |
| 45 | 'is_active' => 1, |
| 46 | ); |
| 47 | $result = CRM_Contact_BAO_ContactType::add($params); |
| 48 | $this->subTypeIndividual = $params['name']; |
| 49 | $this->_subTypeIndividualId = $result->id; |
| 50 | |
| 51 | $params = array( |
| 52 | 'label' => 'sub_organization', |
| 53 | 'name' => 'sub_organization', |
| 54 | // Organization |
| 55 | 'parent_id' => 3, |
| 56 | 'is_active' => 1, |
| 57 | ); |
| 58 | $result = CRM_Contact_BAO_ContactType::add($params); |
| 59 | $this->subTypeOrganization = $params['name']; |
| 60 | $this->_subTypeOrganizationId = $result->id; |
| 61 | |
| 62 | $params = array( |
| 63 | 'label' => 'sub_household', |
| 64 | 'name' => 'sub_household', |
| 65 | // Household |
| 66 | 'parent_id' => 2, |
| 67 | 'is_active' => 1, |
| 68 | ); |
| 69 | $result = CRM_Contact_BAO_ContactType::add($params); |
| 70 | $this->subTypeHousehold = $params['name']; |
| 71 | $this->_subTypeHouseholdId = $result->id; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Tears down the fixture, for example, closes a network connection. |
| 76 | * This method is called after a test is executed. |
| 77 | * |
| 78 | */ |
| 79 | function tearDown() { |
| 80 | $contactTypeIds = array( |
| 81 | $this->_subTypeIndividualId, |
| 82 | $this->_subTypeOrganizationId, |
| 83 | $this->_subTypeHouseholdId, |
| 84 | ); |
| 85 | foreach ($contactTypeIds as $typeId) { |
| 86 | $this->contactTypeDelete($typeId); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * test add methods with valid data |
| 92 | * success expected |
| 93 | */ |
| 94 | function testContactCreate() { |
| 95 | |
| 96 | // check for Type:Individual Subtype:sub_individual |
| 97 | $contactParams = array( |
| 98 | 'first_name' => 'Anne', |
| 99 | 'last_name' => 'Grant', |
| 100 | 'contact_type' => 'Individual', |
| 101 | 'contact_sub_type' => $this->subTypeIndividual, |
| 102 | 'version' => $this->_apiversion, |
| 103 | ); |
| 104 | $contact = civicrm_api('contact', 'create', $contactParams); |
| 105 | $this->assertEquals($contact['is_error'], 0, "In line " . __LINE__); |
| 106 | $params = array( |
| 107 | 'contact_id' => $contact['id'], |
| 108 | 'version' => $this->_apiversion, |
| 109 | ); |
| 110 | $result = civicrm_api('contact', 'get', $params); |
| 111 | $this->assertEquals($result['values'][$contact['id']]['first_name'], $contactParams['first_name'], "In line " . __LINE__); |
| 112 | $this->assertEquals($result['values'][$contact['id']]['last_name'], $contactParams['last_name'], "In line " . __LINE__); |
| 113 | $this->assertEquals($result['values'][$contact['id']]['contact_type'], $contactParams['contact_type'], "In line " . __LINE__); |
| 114 | $this->assertEquals(end($result['values'][$contact['id']]['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__); |
| 115 | civicrm_api('contact', 'delete', $params); |
| 116 | |
| 117 | // check for Type:Organization Subtype:sub_organization |
| 118 | $contactParams = array( |
| 119 | 'organization_name' => 'Compumentor', |
| 120 | 'contact_type' => 'Organization', |
| 121 | 'contact_sub_type' => $this->subTypeOrganization, |
| 122 | 'version' => $this->_apiversion, |
| 123 | ); |
| 124 | $contact = civicrm_api('contact', 'create', $contactParams); |
| 125 | $this->assertEquals($contact['is_error'], 0, "In line " . __LINE__); |
| 126 | |
| 127 | $params = array( |
| 128 | 'contact_id' => $contact['id'], |
| 129 | 'version' => $this->_apiversion, |
| 130 | ); |
| 131 | $getContacts = civicrm_api('contact', 'get', $params); |
| 132 | $result = $getContacts['values'][$contact['id']]; |
| 133 | $this->assertEquals($result['organization_name'], $contactParams['organization_name'], "In line " . __LINE__); |
| 134 | $this->assertEquals($result['contact_type'], $contactParams['contact_type'], "In line " . __LINE__); |
| 135 | $this->assertEquals(end($result['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__); |
| 136 | civicrm_api('contact', 'delete', $params); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /* |
| 141 | * test add with invalid data |
| 142 | */ |
| 143 | function testContactAddInvalidData() { |
| 144 | |
| 145 | // check for Type:Individual Subtype:sub_household |
| 146 | $contactParams = array( |
| 147 | 'first_name' => 'Anne', |
| 148 | 'last_name' => 'Grant', |
| 149 | 'contact_type' => 'Individual', |
| 150 | 'contact_sub_type' => $this->subTypeHousehold, |
| 151 | 'version' => $this->_apiversion, |
| 152 | ); |
| 153 | $contact = $this->callAPIFailure('contact', 'create', $contactParams); |
| 154 | |
| 155 | // check for Type:Organization Subtype:sub_individual |
| 156 | $contactParams = array( |
| 157 | 'organization_name' => 'Compumentor', |
| 158 | 'contact_type' => 'Organization', |
| 159 | 'contact_sub_type' => $this->subTypeIndividual, |
| 160 | 'version' => $this->_apiversion, |
| 161 | ); |
| 162 | $contact = $this->callAPIFailure('contact', 'create', $contactParams); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /* |
| 167 | * test update with no subtype to valid subtype |
| 168 | * success expected |
| 169 | */ |
| 170 | function testContactUpdateNoSubtypeValid() { |
| 171 | |
| 172 | // check for Type:Individual |
| 173 | $contactParams = array( |
| 174 | 'first_name' => 'Anne', |
| 175 | 'last_name' => 'Grant', |
| 176 | 'contact_type' => 'Individual', |
| 177 | 'version' => $this->_apiversion, |
| 178 | ); |
| 179 | $contact = civicrm_api('contact', 'create', $contactParams); |
| 180 | // subype:sub_individual |
| 181 | $updateParams = array( |
| 182 | 'first_name' => 'John', |
| 183 | 'last_name' => 'Grant', |
| 184 | 'contact_id' => $contact['id'], |
| 185 | 'contact_type' => 'Individual', |
| 186 | 'contact_sub_type' => $this->subTypeIndividual, |
| 187 | 'version' => $this->_apiversion, |
| 188 | ); |
| 189 | $updateContact = civicrm_api('contact', 'create', $updateParams); |
| 190 | $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__); |
| 191 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
| 192 | |
| 193 | $params = array( |
| 194 | 'contact_id' => $contact['id'], |
| 195 | 'version' => $this->_apiversion, |
| 196 | ); |
| 197 | $getContacts = civicrm_api('contact', 'get', $params); |
| 198 | $result = $getContacts['values'][$contact['id']]; |
| 199 | |
| 200 | $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__); |
| 201 | $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__); |
| 202 | $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__); |
| 203 | $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__); |
| 204 | civicrm_api('contact', 'delete', $params); |
| 205 | |
| 206 | // check for Type:Organization |
| 207 | $contactParams = array( |
| 208 | 'organization_name' => 'Compumentor', |
| 209 | 'contact_type' => 'Organization', |
| 210 | 'version' => $this->_apiversion, |
| 211 | ); |
| 212 | $contact = civicrm_api('contact', 'create', $contactParams); |
| 213 | |
| 214 | // subype:sub_organization |
| 215 | $updateParams = array( |
| 216 | 'organization_name' => 'Intel Arts', |
| 217 | 'contact_id' => $contact['id'], |
| 218 | 'contact_type' => 'Organization', |
| 219 | 'contact_sub_type' => $this->subTypeOrganization, |
| 220 | 'version' => $this->_apiversion, |
| 221 | ); |
| 222 | $updateContact = civicrm_api('contact', 'create', $updateParams); |
| 223 | |
| 224 | $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__); |
| 225 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
| 226 | |
| 227 | $params = array( |
| 228 | 'contact_id' => $contact['id'], |
| 229 | 'version' => $this->_apiversion, |
| 230 | ); |
| 231 | $getContacts = civicrm_api('contact', 'get', $params); |
| 232 | $result = $getContacts['values'][$contact['id']]; |
| 233 | |
| 234 | $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__); |
| 235 | $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__); |
| 236 | $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__); |
| 237 | civicrm_api('contact', 'delete', $params); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /* |
| 242 | * test update with no subtype to invalid subtype |
| 243 | */ |
| 244 | function testContactUpdateNoSubtypeInvalid() { |
| 245 | |
| 246 | // check for Type:Individual |
| 247 | $contactParams = array( |
| 248 | 'first_name' => 'Anne', |
| 249 | 'last_name' => 'Grant', |
| 250 | 'contact_type' => 'Individual', |
| 251 | 'version' => $this->_apiversion, |
| 252 | ); |
| 253 | $contact = civicrm_api('contact', 'create', $contactParams); |
| 254 | |
| 255 | // subype:sub_household |
| 256 | $updateParams = array( |
| 257 | 'first_name' => 'John', |
| 258 | 'last_name' => 'Grant', |
| 259 | 'contact_id' => $contact['id'], |
| 260 | 'contact_type' => 'Individual', |
| 261 | 'contact_sub_type' => $this->subTypeHousehold, |
| 262 | 'version' => $this->_apiversion, |
| 263 | ); |
| 264 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
| 265 | $params = array( |
| 266 | 'contact_id' => $contact['id'], |
| 267 | 'version' => $this->_apiversion, |
| 268 | ); |
| 269 | civicrm_api('contact', 'delete', $params); |
| 270 | |
| 271 | // check for Type:Organization |
| 272 | $contactParams = array( |
| 273 | 'organization_name' => 'Compumentor', |
| 274 | 'contact_type' => 'Organization', |
| 275 | 'version' => $this->_apiversion, |
| 276 | ); |
| 277 | $contact = civicrm_api('contact', 'create', $contactParams); |
| 278 | |
| 279 | $updateParams = array( |
| 280 | 'organization_name' => 'Intel Arts', |
| 281 | 'contact_id' => $contact['id'], |
| 282 | 'contact_type' => 'Organization', |
| 283 | 'contact_sub_type' => $this->subTypeIndividual, |
| 284 | 'version' => $this->_apiversion, |
| 285 | ); |
| 286 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
| 287 | $params = array( |
| 288 | 'contact_id' => $contact['id'], |
| 289 | 'version' => $this->_apiversion, |
| 290 | ); |
| 291 | civicrm_api('contact', 'delete', $params); |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * test update with no subtype to valid subtype |
| 296 | * success expected |
| 297 | */ |
| 298 | function testContactUpdateSubtypeValid() { |
| 299 | |
| 300 | $params = array( |
| 301 | 'label' => 'sub2_individual', |
| 302 | 'name' => 'sub2_individual', |
| 303 | // Individual |
| 304 | 'parent_id' => 1, |
| 305 | 'is_active' => 1, |
| 306 | ); |
| 307 | $getSubtype = CRM_Contact_BAO_ContactType::add($params); |
| 308 | $subtype = $params['name']; |
| 309 | |
| 310 | // check for Type:Individual subype:sub_individual |
| 311 | $contactParams = array( |
| 312 | 'first_name' => 'Anne', |
| 313 | 'last_name' => 'Grant', |
| 314 | 'contact_type' => 'Individual', |
| 315 | 'contact_sub_type' => $this->subTypeIndividual, |
| 316 | 'version' => $this->_apiversion, |
| 317 | ); |
| 318 | $contact = civicrm_api('contact', 'create', $contactParams); |
| 319 | // subype:sub2_individual |
| 320 | $updateParams = array( |
| 321 | 'id' => $contact['id'], |
| 322 | 'first_name' => 'John', |
| 323 | 'last_name' => 'Grant', |
| 324 | 'contact_id' => $contact['id'], |
| 325 | 'contact_type' => 'Individual', |
| 326 | 'contact_sub_type' => $subtype, |
| 327 | 'version' => $this->_apiversion, |
| 328 | ); |
| 329 | $updateContact = civicrm_api('contact', 'create', $updateParams); |
| 330 | |
| 331 | $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__); |
| 332 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
| 333 | |
| 334 | $params = array( |
| 335 | 'contact_id' => $contact['id'], |
| 336 | 'version' => $this->_apiversion, |
| 337 | ); |
| 338 | $getContacts = civicrm_api('contact', 'get', $params); |
| 339 | $result = $getContacts['values'][$contact['id']]; |
| 340 | |
| 341 | $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__); |
| 342 | $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__); |
| 343 | $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__); |
| 344 | $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__); |
| 345 | civicrm_api('contact', 'delete', $params); |
| 346 | |
| 347 | |
| 348 | $params = array( |
| 349 | 'label' => 'sub2_organization', |
| 350 | 'name' => 'sub2_organization', |
| 351 | // Organization |
| 352 | 'parent_id' => 3, |
| 353 | 'is_active' => 1, |
| 354 | ); |
| 355 | $getSubtype = CRM_Contact_BAO_ContactType::add($params); |
| 356 | $subtype = $params['name']; |
| 357 | |
| 358 | // check for Type:Organization subype:sub_organization |
| 359 | $contactParams = array( |
| 360 | 'organization_name' => 'Compumentor', |
| 361 | 'contact_type' => 'Organization', |
| 362 | 'contact_sub_type' => $this->subTypeOrganization, |
| 363 | 'version' => $this->_apiversion, |
| 364 | ); |
| 365 | $contact = civicrm_api('contact', 'create', $contactParams); |
| 366 | |
| 367 | // subype:sub2_organization |
| 368 | $updateParams = array( |
| 369 | 'organization_name' => 'Intel Arts', |
| 370 | 'contact_id' => $contact['id'], |
| 371 | 'contact_type' => 'Organization', |
| 372 | 'contact_sub_type' => $subtype, |
| 373 | 'version' => $this->_apiversion, |
| 374 | ); |
| 375 | $updateContact = civicrm_api('contact', 'create', $updateParams); |
| 376 | |
| 377 | $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__); |
| 378 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
| 379 | |
| 380 | $params = array( |
| 381 | 'contact_id' => $contact['id'], |
| 382 | 'version' => $this->_apiversion, |
| 383 | ); |
| 384 | $getContacts = civicrm_api('contact', 'get', $params); |
| 385 | $result = $getContacts['values'][$contact['id']]; |
| 386 | |
| 387 | $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__); |
| 388 | $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__); |
| 389 | $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__); |
| 390 | civicrm_api('contact', 'delete', $params); |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * test update with no subtype to invalid subtype |
| 395 | */ |
| 396 | function testContactUpdateSubtypeInvalid() { |
| 397 | |
| 398 | // check for Type:Individual subtype:sub_individual |
| 399 | $contactParams = array( |
| 400 | 'first_name' => 'Anne', |
| 401 | 'last_name' => 'Grant', |
| 402 | 'contact_type' => 'Individual', |
| 403 | 'contact_sub_type' => $this->subTypeIndividual, |
| 404 | 'version' => $this->_apiversion, |
| 405 | ); |
| 406 | $contact = civicrm_api('contact', 'create', $contactParams); |
| 407 | |
| 408 | // subype:sub_household |
| 409 | $updateParams = array( |
| 410 | 'first_name' => 'John', |
| 411 | 'last_name' => 'Grant', |
| 412 | 'contact_id' => $contact['id'], |
| 413 | 'contact_type' => 'Individual', |
| 414 | 'contact_sub_type' => $this->subTypeHousehold, |
| 415 | 'version' => $this->_apiversion, |
| 416 | ); |
| 417 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
| 418 | $params = array( |
| 419 | 'contact_id' => $contact['id'], |
| 420 | 'version' => $this->_apiversion, |
| 421 | ); |
| 422 | civicrm_api('contact', 'delete', $params); |
| 423 | |
| 424 | // check for Type:Organization subtype: |
| 425 | $contactParams = array( |
| 426 | 'organization_name' => 'Compumentor', |
| 427 | 'contact_type' => 'Organization', |
| 428 | 'contact_sub_type' => $this->subTypeOrganization, |
| 429 | 'version' => $this->_apiversion, |
| 430 | ); |
| 431 | $contact = civicrm_api('contact', 'create', $contactParams); |
| 432 | |
| 433 | $updateParams = array( |
| 434 | 'organization_name' => 'Intel Arts', |
| 435 | 'contact_id' => $contact['id'], |
| 436 | 'contact_sub_type' => $this->subTypeIndividual, |
| 437 | 'version' => $this->_apiversion, |
| 438 | ); |
| 439 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
| 440 | $params = array( |
| 441 | 'contact_id' => $contact['id'], |
| 442 | 'version' => $this->_apiversion, |
| 443 | ); |
| 444 | civicrm_api('contact', 'delete', $params); |
| 445 | } |
| 446 | } |
| 447 | |