| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 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 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 29 | |
| 30 | /** |
| 31 | * Test APIv3 civicrm_activity_* functions |
| 32 | * |
| 33 | * @package CiviCRM_APIv3 |
| 34 | * @subpackage API_Contact |
| 35 | */ |
| 36 | class api_v3_CustomValueContactTypeTest extends CiviUnitTestCase { |
| 37 | protected $_contactID; |
| 38 | protected $_apiversion = 3; |
| 39 | protected $CustomGroupIndividual; |
| 40 | protected $individualStudent; |
| 41 | |
| 42 | public function setUp() { |
| 43 | parent::setUp(); |
| 44 | // Create Group For Individual Contact Type |
| 45 | $groupIndividual = array( |
| 46 | 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5), |
| 47 | 'extends' => array('Individual'), |
| 48 | 'style' => 'Inline', |
| 49 | 'is_active' => 1, |
| 50 | ); |
| 51 | |
| 52 | $this->CustomGroupIndividual = $this->customGroupCreate($groupIndividual); |
| 53 | |
| 54 | $this->IndividualField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual['id'])); |
| 55 | |
| 56 | // Create Group For Individual-Student Contact Sub Type |
| 57 | $groupIndiStudent = array( |
| 58 | 'title' => 'Student Test' . substr(sha1(rand()), 0, 5), |
| 59 | 'extends' => array('Individual', array('Student')), |
| 60 | 'style' => 'Inline', |
| 61 | 'is_active' => 1, |
| 62 | ); |
| 63 | |
| 64 | $this->CustomGroupIndiStudent = $this->customGroupCreate($groupIndiStudent); |
| 65 | |
| 66 | $this->IndiStudentField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent['id'])); |
| 67 | |
| 68 | $params = array( |
| 69 | 'first_name' => 'Mathev', |
| 70 | 'last_name' => 'Adison', |
| 71 | 'contact_type' => 'Individual', |
| 72 | ); |
| 73 | |
| 74 | $this->individual = $this->individualCreate($params); |
| 75 | |
| 76 | $params = array( |
| 77 | 'first_name' => 'Steve', |
| 78 | 'last_name' => 'Tosun', |
| 79 | 'contact_type' => 'Individual', |
| 80 | 'contact_sub_type' => 'Student', |
| 81 | ); |
| 82 | $this->individualStudent = $this->individualCreate($params); |
| 83 | |
| 84 | $params = array( |
| 85 | 'first_name' => 'Mark', |
| 86 | 'last_name' => 'Dawson', |
| 87 | 'contact_type' => 'Individual', |
| 88 | 'contact_sub_type' => 'Parent', |
| 89 | ); |
| 90 | $this->individualParent = $this->individualCreate($params); |
| 91 | |
| 92 | $params = array( |
| 93 | 'organization_name' => 'Wellspring', |
| 94 | 'contact_type' => 'Organization', |
| 95 | ); |
| 96 | $this->organization = $this->organizationCreate($params); |
| 97 | |
| 98 | $params = array( |
| 99 | 'organization_name' => 'SubUrban', |
| 100 | 'contact_type' => 'Organization', |
| 101 | 'contact_sub_type' => 'Sponsor', |
| 102 | ); |
| 103 | $this->organizationSponsor = $this->organizationCreate($params); |
| 104 | //refresh php cached variables |
| 105 | CRM_Core_PseudoConstant::flush(); |
| 106 | CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndividualField['id'], TRUE); |
| 107 | CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndiStudentField['id'], TRUE); |
| 108 | } |
| 109 | |
| 110 | public function tearDown() { |
| 111 | $tablesToTruncate = array('civicrm_contact', 'civicrm_cache'); |
| 112 | $this->quickCleanup($tablesToTruncate, TRUE); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Test that custom fields is returned for correct contact type only |
| 117 | */ |
| 118 | public function testGetFields() { |
| 119 | $result = $this->callAPISuccess('Contact', 'getfields', array()); |
| 120 | $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'If This fails there is probably a caching issue - failure in line' . __LINE__ . print_r(array_keys($result['values']), TRUE)); |
| 121 | $result = $this->callAPISuccess('Contact', 'getfields', array( |
| 122 | 'action' => 'create', |
| 123 | 'contact_type' => 'Individual', |
| 124 | ), 'in line' . __LINE__); |
| 125 | $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']); |
| 126 | $result = $this->callAPISuccess('Contact', 'getfields', array( |
| 127 | 'action' => 'create', |
| 128 | 'contact_type' => 'Organization', |
| 129 | )); |
| 130 | $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__ . print_r(array_keys($result['values']), TRUE)); |
| 131 | $result = $this->callAPISuccess('Relationship', 'getfields', array('action' => 'create'), 'in line' . __LINE__); |
| 132 | $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values']); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Add Custom data of Contact Type : Individual to a Contact type: Organization |
| 137 | */ |
| 138 | public function testAddIndividualCustomDataToOrganization() { |
| 139 | |
| 140 | $params = array( |
| 141 | 'id' => $this->organization, |
| 142 | 'contact_type' => 'Organization', |
| 143 | "custom_{$this->IndividualField['id']}" => 'Test String', |
| 144 | 'debug' => 1, // so that undefined_fields is returned |
| 145 | ); |
| 146 | |
| 147 | $contact = $this->callAPISuccess('contact', 'create', $params); |
| 148 | $this->assertTrue(is_array($contact['undefined_fields']), __LINE__); |
| 149 | $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Add valid Empty params to a Contact Type : Individual |
| 154 | * note - don't copy & paste this - is of marginal value |
| 155 | */ |
| 156 | public function testAddCustomDataEmptyToIndividual() { |
| 157 | $contact = $this->callAPIFailure('contact', 'create', array(), |
| 158 | 'Mandatory key(s) missing from params array: contact_type' |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Add valid custom data to a Contact Type : Individual |
| 164 | */ |
| 165 | public function testAddValidCustomDataToIndividual() { |
| 166 | |
| 167 | $params = array( |
| 168 | 'contact_id' => $this->individual, |
| 169 | 'contact_type' => 'Individual', |
| 170 | "custom_{$this->IndividualField['id']}" => 'Test String', |
| 171 | ); |
| 172 | $contact = $this->callAPISuccess('contact', 'create', $params); |
| 173 | |
| 174 | $this->assertNotNull($contact['id'], 'In line ' . __LINE__); |
| 175 | $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individual); |
| 176 | $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"]; |
| 177 | |
| 178 | // Check the Value in Database |
| 179 | $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor |
| 184 | */ |
| 185 | public function testAddIndividualStudentCustomDataToOrganizationSponsor() { |
| 186 | |
| 187 | $params = array( |
| 188 | 'contact_id' => $this->organizationSponsor, |
| 189 | 'contact_type' => 'Organization', |
| 190 | "custom_{$this->IndiStudentField['id']}" => 'Test String', |
| 191 | 'debug' => 1, // so that undefined_fields is returned |
| 192 | ); |
| 193 | |
| 194 | $contact = $this->callAPISuccess('contact', 'create', $params); |
| 195 | $this->assertTrue(is_array($contact['undefined_fields']), __LINE__); |
| 196 | $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Add valid custom data to a Contact Type : Individual Subtype: Student |
| 201 | */ |
| 202 | public function testCreateValidCustomDataToIndividualStudent() { |
| 203 | |
| 204 | $params = array( |
| 205 | 'contact_id' => $this->individualStudent, |
| 206 | 'contact_type' => 'Individual', |
| 207 | "custom_{$this->IndiStudentField['id']}" => 'Test String', |
| 208 | ); |
| 209 | |
| 210 | $result = $this->callAPISuccess('contact', 'create', $params); |
| 211 | |
| 212 | $this->assertNotNull($result['id'], 'In line ' . __LINE__); |
| 213 | $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individualStudent); |
| 214 | $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"]; |
| 215 | |
| 216 | // Check the Value in Database |
| 217 | $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Add custom data of Individual Student to a Contact Type : Individual - parent |
| 222 | */ |
| 223 | public function testAddIndividualStudentCustomDataToIndividualParent() { |
| 224 | |
| 225 | $params = array( |
| 226 | 'contact_id' => $this->individualParent, |
| 227 | 'contact_type' => 'Individual', |
| 228 | "custom_{$this->IndiStudentField['id']}" => 'Test String', |
| 229 | 'debug' => 1, // so that undefined_fields is returned |
| 230 | ); |
| 231 | $contact = $this->callAPISuccess('contact', 'create', $params); |
| 232 | $this->assertTrue(is_array($contact['undefined_fields']), __LINE__); |
| 233 | $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__); |
| 234 | } |
| 235 | |
| 236 | // Retrieve Methods |
| 237 | |
| 238 | /** |
| 239 | * Retrieve Valid custom Data added to Individual Contact Type |
| 240 | */ |
| 241 | public function testRetrieveValidCustomDataToIndividual() { |
| 242 | |
| 243 | $params = array( |
| 244 | 'contact_id' => $this->individual, |
| 245 | 'contact_type' => 'Individual', |
| 246 | "custom_" . $this->IndividualField['id'] => 'Test String', |
| 247 | ); |
| 248 | |
| 249 | $contact = $this->callAPISuccess('contact', 'create', $params); |
| 250 | |
| 251 | $this->assertAPISuccess($contact); |
| 252 | $params = array( |
| 253 | 'contact_id' => $this->individual, |
| 254 | 'contact_type' => 'Individual', |
| 255 | "return.custom_{$this->IndividualField['id']}" => 1, |
| 256 | ); |
| 257 | |
| 258 | $getContact = $this->callAPISuccess('contact', 'get', $params); |
| 259 | |
| 260 | $this->assertEquals($getContact['values'][$this->individual]["custom_" . $this->IndividualField['id']], 'Test String', 'In line ' . __LINE__); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student. |
| 265 | */ |
| 266 | public function testRetrieveValidCustomDataToIndividualStudent() { |
| 267 | |
| 268 | $params = array( |
| 269 | 'contact_id' => $this->individualStudent, |
| 270 | 'contact_type' => 'Individual', |
| 271 | 'contact_sub_type' => 'Student', |
| 272 | "custom_{$this->IndiStudentField['id']}" => 'Test String', |
| 273 | ); |
| 274 | |
| 275 | $contact = $this->callAPISuccess('contact', 'create', $params); |
| 276 | $this->assertAPISuccess($contact); |
| 277 | $params = array( |
| 278 | 'contact_id' => $this->individualStudent, |
| 279 | 'contact_type' => 'Individual', |
| 280 | 'contact_sub_type' => 'Student', |
| 281 | "return.custom_{$this->IndiStudentField['id']}" => 1, |
| 282 | ); |
| 283 | |
| 284 | $getContact = $this->callAPISuccess('contact', 'get', $params); |
| 285 | |
| 286 | $this->assertEquals($getContact['values'][$this->individualStudent]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__); |
| 287 | } |
| 288 | |
| 289 | } |