3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
29 * Test class for UFGroup API - civicrm_uf_*
30 * @todo Split UFGroup and UFJoin tests
35 class api_v3_UFFieldTest
extends CiviUnitTestCase
{
36 // ids from the uf_group_test.xml fixture
37 protected $_ufGroupId = 11;
38 protected $_ufFieldId;
39 protected $_contactId = 69;
40 protected $_apiversion = 3;
42 protected $_entity = 'uf_field';
49 protected function setUp() {
62 $op = new PHPUnit_Extensions_Database_Operation_Insert();
65 $this->createFlatXMLDataSet(dirname(__FILE__
) . '/dataset/uf_group_test.xml')
68 $this->callAPISuccess('uf_field', 'getfields', array('cache_clear' => 1));
70 $this->_params
= array(
71 'field_name' => 'phone',
72 'field_type' => 'Contact',
73 'visibility' => 'Public Pages and Listings',
75 'label' => 'Test Phone',
78 'location_type_id' => 1,
80 'uf_group_id' => $this->_ufGroupId
,
89 public function tearDown() {
102 * Create / updating field.
104 public function testCreateUFField() {
105 $params = $this->_params
;
106 $ufField = $this->callAPIAndDocument('uf_field', 'create', $params, __FUNCTION__
, __FILE__
);
107 unset($params['uf_group_id']);
108 $this->_ufFieldId
= $ufField['id'];
109 foreach ($params as $key => $value) {
110 $this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]);
115 * Failure test for field_name.
117 public function testCreateUFFieldWithBadFieldName() {
118 $params = $this->_params
;
119 $params['field_name'] = 'custom_98789';
120 $this->callAPIFailure('uf_field', 'create', $params);
124 * Failure test for bad parameters.
126 public function testCreateUFFieldWithWrongParams() {
127 $this->callAPIFailure('uf_field', 'create', array('field_name' => 'test field'));
128 $this->callAPIFailure('uf_field', 'create', array('label' => 'name-less field'));
132 * Create a field with 'weight=1' and then a second with 'weight=1'.
134 * The second field winds up with weight=1, and the first field gets bumped to 'weight=2'.
136 public function testCreateUFFieldWithDefaultAutoWeight() {
137 $params1 = $this->_params
;
138 $ufField1 = $this->callAPISuccess('uf_field', 'create', $params1);
139 $this->assertEquals(1, $ufField1['values'][$ufField1['id']]['weight']);
140 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
141 1 => array($ufField1['id'], 'Int'),
144 $params2 = $this->_params
;
145 // needs to be a different field
146 $params2['location_type_id'] = 2;
147 $ufField2 = $this->callAPISuccess('uf_field', 'create', $params2);
148 $this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']);
149 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
150 1 => array($ufField2['id'], 'Int'),
152 $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
153 1 => array($ufField1['id'], 'Int'),
160 public function testDeleteUFField() {
161 $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params
);
163 'field_id' => $ufField['id'],
165 $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__
, __FILE__
);
169 * Test getting ufField.
171 public function testGetUFFieldSuccess() {
172 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
173 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(), __FUNCTION__
, __FILE__
);
174 $this->getAndCheck($this->_params
, $result['id'], $this->_entity
);
178 * Create / updating field.
180 public function testReplaceUFFields() {
181 $baseFields = array();
182 $baseFields[] = array(
183 'field_name' => 'first_name',
184 'field_type' => 'Contact',
185 'visibility' => 'Public Pages and Listings',
187 'label' => 'Test First Name',
188 'is_searchable' => 1,
191 $baseFields[] = array(
192 'field_name' => 'country',
193 'field_type' => 'Contact',
194 'visibility' => 'Public Pages and Listings',
196 'label' => 'Test Country',
197 'is_searchable' => 1,
199 'location_type_id' => 1,
201 $baseFields[] = array(
202 'field_name' => 'phone',
203 'field_type' => 'Contact',
204 'visibility' => 'Public Pages and Listings',
206 'label' => 'Test Phone',
207 'is_searchable' => 1,
209 'location_type_id' => 1,
210 'phone_type_id' => 1,
214 'uf_group_id' => $this->_ufGroupId
,
215 'option.autoweight' => FALSE,
216 'values' => $baseFields,
217 'check_permissions' => TRUE,
220 $result = $this->callAPIAndDocument('uf_field', 'replace', $params, __FUNCTION__
, __FILE__
);
221 $inputsByName = CRM_Utils_Array
::index(array('field_name'), $params['values']);
222 $this->assertEquals(count($params['values']), count($result['values']));
223 foreach ($result['values'] as $outUfField) {
224 $this->assertTrue(is_string($outUfField['field_name']));
225 $inUfField = $inputsByName[$outUfField['field_name']];
226 foreach ($inUfField as $key => $inValue) {
227 $this->assertEquals($inValue, $outUfField[$key],
228 sprintf("field_name=[%s] key=[%s] expected=[%s] actual=[%s]",
229 $outUfField['field_name'],
240 * Check Profile API permission without ACL.
242 public function testProfilesWithoutACL() {
243 $this->createLoggedInUser();
244 $baseFields[] = array(
245 'field_name' => 'first_name',
246 'field_type' => 'Contact',
247 'visibility' => 'Public Pages and Listings',
249 'label' => 'Test First Name',
250 'is_searchable' => 1,
253 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
255 'uf_group_id' => $this->_ufGroupId
,
256 'option.autoweight' => FALSE,
257 'values' => $baseFields,
258 'check_permissions' => TRUE,
260 $this->_loggedInUser
= CRM_Core_Session
::singleton()->get('userID');
261 $this->callAPIFailure('uf_field', 'replace', $params);
265 * Check Profile ACL for API permission.
267 public function testACLPermissionforProfiles() {
268 $this->createLoggedInUser();
269 $this->_permissionedGroup
= $this->groupCreate(array(
270 'title' => 'Edit Profiles',
272 'name' => 'edit-profiles',
274 $this->setupACL(TRUE);
275 $this->testReplaceUFFields();