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
37 class api_v3_CustomValueContactTypeTest
extends CiviUnitTestCase
{
38 protected $_contactID;
39 protected $_apiversion =3;
40 protected $CustomGroupIndividual;
41 protected $individualStudent;
43 public function setUp() {
45 // Create Group For Individual Contact Type
46 $groupIndividual = array(
47 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
48 'extends' => array('Individual'),
53 $this->CustomGroupIndividual
= $this->customGroupCreate($groupIndividual);
55 $this->IndividualField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual
['id']));
57 // Create Group For Individual-Student Contact Sub Type
58 $groupIndiStudent = array(
59 'title' => 'Student Test' . substr(sha1(rand()), 0, 5),
60 'extends' => array('Individual', array('Student')),
65 $this->CustomGroupIndiStudent
= $this->customGroupCreate($groupIndiStudent);
67 $this->IndiStudentField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent
['id']));
70 'first_name' => 'Mathev',
71 'last_name' => 'Adison',
72 'contact_type' => 'Individual',
75 $this->individual
= $this->individualCreate($params);
78 'first_name' => 'Steve',
79 'last_name' => 'Tosun',
80 'contact_type' => 'Individual',
81 'contact_sub_type' => 'Student',
83 $this->individualStudent
= $this->individualCreate($params);
86 'first_name' => 'Mark',
87 'last_name' => 'Dawson',
88 'contact_type' => 'Individual',
89 'contact_sub_type' => 'Parent',
91 $this->individualParent
= $this->individualCreate($params);
94 'organization_name' => 'Wellspring',
95 'contact_type' => 'Organization',
97 $this->organization
= $this->organizationCreate($params);
100 'organization_name' => 'SubUrban',
101 'contact_type' => 'Organization',
102 'contact_sub_type' => 'Sponsor',
104 $this->organizationSponsor
= $this->organizationCreate($params);
105 //refresh php cached variables
106 CRM_Core_PseudoConstant
::flush();
107 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndividualField
['id'], True);
108 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndiStudentField
['id'], True);
111 public function tearDown() {
112 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
113 $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('action' => 'create', 'contact_type' => 'Individual'), 'in line' . __LINE__
);
122 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']);
123 $result = $this->callAPISuccess('Contact', 'getfields', array('action' => 'create', 'contact_type' => 'Organization'));
124 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__
. print_r(array_keys($result['values']), TRUE));
125 $result = $this->callAPISuccess('Relationship', 'getfields', array('action' => 'create'), 'in line' . __LINE__
);
126 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values']);
130 * Add Custom data of Contact Type : Individual to a Contact type: Organization
132 public function testAddIndividualCustomDataToOrganization() {
135 'id' => $this->organization
,
136 'contact_type' => 'Organization',
137 "custom_{$this->IndividualField['id']}" => 'Test String', 'debug' => 1,// so that undefined_fields is returned
140 $contact = $this->callAPISuccess('contact', 'create', $params);
141 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
142 $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__
);
146 * Add valid Empty params to a Contact Type : Individual
147 * note - don't copy & paste this - is of marginal value
149 public function testAddCustomDataEmptyToIndividual() {
150 $contact = $this->callAPIFailure('contact', 'create', array(),
151 'Mandatory key(s) missing from params array: contact_type'
156 * Add valid custom data to a Contact Type : Individual
158 public function testAddValidCustomDataToIndividual() {
161 'contact_id' => $this->individual
,
162 'contact_type' => 'Individual',
163 "custom_{$this->IndividualField['id']}" => 'Test String',
165 $contact = $this->callAPISuccess('contact', 'create', $params);
167 $this->assertNotNull($contact['id'], 'In line ' . __LINE__
);
168 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individual
);
169 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
171 // Check the Value in Database
172 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__
);
176 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
178 public function testAddIndividualStudentCustomDataToOrganizationSponsor() {
181 'contact_id' => $this->organizationSponsor
,
182 'contact_type' => 'Organization',
183 "custom_{$this->IndiStudentField['id']}" => 'Test String',
184 'debug' => 1,// so that undefined_fields is returned
187 $contact = $this->callAPISuccess('contact', 'create', $params);
188 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
189 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
193 * Add valid custom data to a Contact Type : Individual Subtype: Student
195 public function testCreateValidCustomDataToIndividualStudent() {
198 'contact_id' => $this->individualStudent
,
199 'contact_type' => 'Individual',
200 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
202 $result = $this->callAPISuccess('contact', 'create', $params);
204 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
205 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individualStudent
);
206 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
208 // Check the Value in Database
209 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__
);
213 * Add custom data of Individual Student to a Contact Type : Individual - parent
215 public function testAddIndividualStudentCustomDataToIndividualParent() {
218 'contact_id' => $this->individualParent
,
219 'contact_type' => 'Individual',
220 "custom_{$this->IndiStudentField['id']}" => 'Test String', 'debug' => 1,// so that undefined_fields is returned
222 $contact = $this->callAPISuccess('contact', 'create', $params);
223 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
224 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
232 * Retrieve Valid custom Data added to Individual Contact Type
234 public function testRetrieveValidCustomDataToIndividual() {
237 'contact_id' => $this->individual
,
238 'contact_type' => 'Individual',
239 "custom_" . $this->IndividualField
['id'] => 'Test String', );
241 $contact = $this->callAPISuccess('contact', 'create', $params);
243 $this->assertAPISuccess($contact);
245 'contact_id' => $this->individual
,
246 'contact_type' => 'Individual',
247 "return.custom_{$this->IndividualField['id']}" => 1,
250 $getContact = $this->callAPISuccess('contact', 'get', $params);
252 $this->assertEquals($getContact['values'][$this->individual
]["custom_" . $this->IndividualField
['id']], 'Test String', 'In line ' . __LINE__
);
256 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
258 public function testRetrieveValidCustomDataToIndividualStudent() {
261 'contact_id' => $this->individualStudent
,
262 'contact_type' => 'Individual',
263 'contact_sub_type' => 'Student',
264 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
266 $contact = $this->callAPISuccess('contact', 'create', $params);
267 $this->assertAPISuccess($contact);
269 'contact_id' => $this->individualStudent
,
270 'contact_type' => 'Individual',
271 'contact_sub_type' => 'Student', "return.custom_{$this->IndiStudentField['id']}" => 1,
274 $getContact = $this->callAPISuccess('contact', 'get', $params);
277 $this->assertEquals($getContact['values'][$this->individualStudent
]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__
);