3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Test APIv3 civicrm_activity_* functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_Contact
36 class api_v3_CustomValueContactTypeTest
extends CiviUnitTestCase
{
37 protected $_contactID;
38 protected $_apiversion = 3;
39 protected $CustomGroupIndividual;
40 protected $individualStudent;
42 public function 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'),
52 $this->CustomGroupIndividual
= $this->customGroupCreate($groupIndividual);
54 $this->IndividualField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual
['id']));
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')),
64 $this->CustomGroupIndiStudent
= $this->customGroupCreate($groupIndiStudent);
66 $this->IndiStudentField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent
['id']));
69 'first_name' => 'Mathev',
70 'last_name' => 'Adison',
71 'contact_type' => 'Individual',
74 $this->individual
= $this->individualCreate($params);
77 'first_name' => 'Steve',
78 'last_name' => 'Tosun',
79 'contact_type' => 'Individual',
80 'contact_sub_type' => 'Student',
82 $this->individualStudent
= $this->individualCreate($params);
85 'first_name' => 'Mark',
86 'last_name' => 'Dawson',
87 'contact_type' => 'Individual',
88 'contact_sub_type' => 'Parent',
90 $this->individualParent
= $this->individualCreate($params);
93 'organization_name' => 'Wellspring',
94 'contact_type' => 'Organization',
96 $this->organization
= $this->organizationCreate($params);
99 'organization_name' => 'SubUrban',
100 'contact_type' => 'Organization',
101 'contact_sub_type' => 'Sponsor',
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);
110 public function tearDown() {
111 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
112 $this->quickCleanup($tablesToTruncate, TRUE);
116 * Test that custom fields is returned for correct contact type only
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',
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']);
136 * Add Custom data of Contact Type : Individual to a Contact type: Organization
138 public function testAddIndividualCustomDataToOrganization() {
141 'id' => $this->organization
,
142 'contact_type' => 'Organization',
143 "custom_{$this->IndividualField['id']}" => 'Test String',
144 'debug' => 1, // so that undefined_fields is returned
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__
);
153 * Add valid Empty params to a Contact Type : Individual
154 * note - don't copy & paste this - is of marginal value
156 public function testAddCustomDataEmptyToIndividual() {
157 $contact = $this->callAPIFailure('contact', 'create', array(),
158 'Mandatory key(s) missing from params array: contact_type'
163 * Add valid custom data to a Contact Type : Individual
165 public function testAddValidCustomDataToIndividual() {
168 'contact_id' => $this->individual
,
169 'contact_type' => 'Individual',
170 "custom_{$this->IndividualField['id']}" => 'Test String',
172 $contact = $this->callAPISuccess('contact', 'create', $params);
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']}"];
178 // Check the Value in Database
179 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__
);
183 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
185 public function testAddIndividualStudentCustomDataToOrganizationSponsor() {
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
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__
);
200 * Add valid custom data to a Contact Type : Individual Subtype: Student
202 public function testCreateValidCustomDataToIndividualStudent() {
205 'contact_id' => $this->individualStudent
,
206 'contact_type' => 'Individual',
207 "custom_{$this->IndiStudentField['id']}" => 'Test String',
210 $result = $this->callAPISuccess('contact', 'create', $params);
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']}"];
216 // Check the Value in Database
217 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__
);
221 * Add custom data of Individual Student to a Contact Type : Individual - parent
223 public function testAddIndividualStudentCustomDataToIndividualParent() {
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
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__
);
239 * Retrieve Valid custom Data added to Individual Contact Type
241 public function testRetrieveValidCustomDataToIndividual() {
244 'contact_id' => $this->individual
,
245 'contact_type' => 'Individual',
246 "custom_" . $this->IndividualField
['id'] => 'Test String',
249 $contact = $this->callAPISuccess('contact', 'create', $params);
251 $this->assertAPISuccess($contact);
253 'contact_id' => $this->individual
,
254 'contact_type' => 'Individual',
255 "return.custom_{$this->IndividualField['id']}" => 1,
258 $getContact = $this->callAPISuccess('contact', 'get', $params);
260 $this->assertEquals($getContact['values'][$this->individual
]["custom_" . $this->IndividualField
['id']], 'Test String', 'In line ' . __LINE__
);
264 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
266 public function testRetrieveValidCustomDataToIndividualStudent() {
269 'contact_id' => $this->individualStudent
,
270 'contact_type' => 'Individual',
271 'contact_sub_type' => 'Student',
272 "custom_{$this->IndiStudentField['id']}" => 'Test String',
275 $contact = $this->callAPISuccess('contact', 'create', $params);
276 $this->assertAPISuccess($contact);
278 'contact_id' => $this->individualStudent
,
279 'contact_type' => 'Individual',
280 'contact_sub_type' => 'Student',
281 "return.custom_{$this->IndiStudentField['id']}" => 1,
284 $getContact = $this->callAPISuccess('contact', 'get', $params);
286 $this->assertEquals($getContact['values'][$this->individualStudent
]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__
);