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