tests/phpunit/** - Remove unnecessary "require_once" statements
[civicrm-core.git] / tests / phpunit / api / v3 / UFFieldTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 /**
29 * Test class for UFGroup API - civicrm_uf_*
30 * @todo Split UFGroup and UFJoin tests
31 *
32 * @package CiviCRM
33 */
34 class api_v3_UFFieldTest extends CiviUnitTestCase {
35 // ids from the uf_group_test.xml fixture
36 protected $_ufGroupId = 11;
37 protected $_ufFieldId;
38 protected $_contactId = 69;
39 protected $_apiversion = 3;
40 protected $_params;
41 protected $_entity = 'uf_field';
42
43 /**
44 * Set up for test.
45 *
46 * @throws \Exception
47 */
48 protected function setUp() {
49 parent::setUp();
50 $this->quickCleanup(
51 array(
52 'civicrm_group',
53 'civicrm_contact',
54 'civicrm_uf_group',
55 'civicrm_uf_field',
56 'civicrm_uf_join',
57 'civicrm_uf_match',
58 )
59 );
60
61 $op = new PHPUnit_Extensions_Database_Operation_Insert();
62 $op->execute(
63 $this->_dbconn,
64 $this->createFlatXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml')
65 );
66
67 $this->callAPISuccess('uf_field', 'getfields', array('cache_clear' => 1));
68
69 $this->_params = array(
70 'field_name' => 'phone',
71 'field_type' => 'Contact',
72 'visibility' => 'Public Pages and Listings',
73 'weight' => 1,
74 'label' => 'Test Phone',
75 'is_searchable' => 1,
76 'is_active' => 1,
77 'location_type_id' => 1,
78 'phone_type_id' => 1,
79 'uf_group_id' => $this->_ufGroupId,
80 );
81 }
82
83 /**
84 * Tear down function.
85 *
86 * @throws \Exception
87 */
88 public function tearDown() {
89 $this->quickCleanup(
90 array(
91 'civicrm_group',
92 'civicrm_contact',
93 'civicrm_uf_group',
94 'civicrm_uf_join',
95 'civicrm_uf_match',
96 )
97 );
98 }
99
100 /**
101 * Create / updating field.
102 */
103 public function testCreateUFField() {
104 $params = $this->_params;
105 $ufField = $this->callAPIAndDocument('uf_field', 'create', $params, __FUNCTION__, __FILE__);
106 unset($params['uf_group_id']);
107 $this->_ufFieldId = $ufField['id'];
108 foreach ($params as $key => $value) {
109 $this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]);
110 }
111 }
112
113 /**
114 * Failure test for field_name.
115 */
116 public function testCreateUFFieldWithBadFieldName() {
117 $params = $this->_params;
118 $params['field_name'] = 'custom_98789';
119 $this->callAPIFailure('uf_field', 'create', $params);
120 }
121
122 /**
123 * Failure test for bad parameters.
124 */
125 public function testCreateUFFieldWithWrongParams() {
126 $this->callAPIFailure('uf_field', 'create', array('field_name' => 'test field'));
127 $this->callAPIFailure('uf_field', 'create', array('label' => 'name-less field'));
128 }
129
130 /**
131 * Create a field with 'weight=1' and then a second with 'weight=1'.
132 *
133 * The second field winds up with weight=1, and the first field gets bumped to 'weight=2'.
134 */
135 public function testCreateUFFieldWithDefaultAutoWeight() {
136 $params1 = $this->_params;
137 $ufField1 = $this->callAPISuccess('uf_field', 'create', $params1);
138 $this->assertEquals(1, $ufField1['values'][$ufField1['id']]['weight']);
139 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
140 1 => array($ufField1['id'], 'Int'),
141 ));
142
143 $params2 = $this->_params;
144 // needs to be a different field
145 $params2['location_type_id'] = 2;
146 $ufField2 = $this->callAPISuccess('uf_field', 'create', $params2);
147 $this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']);
148 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
149 1 => array($ufField2['id'], 'Int'),
150 ));
151 $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
152 1 => array($ufField1['id'], 'Int'),
153 ));
154 }
155
156 /**
157 * Deleting field.
158 */
159 public function testDeleteUFField() {
160 $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params);
161 $params = array(
162 'field_id' => $ufField['id'],
163 );
164 $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__, __FILE__);
165 }
166
167 /**
168 * Test getting ufField.
169 */
170 public function testGetUFFieldSuccess() {
171 $this->callAPISuccess($this->_entity, 'create', $this->_params);
172 $result = $this->callAPIAndDocument($this->_entity, 'get', array(), __FUNCTION__, __FILE__);
173 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
174 }
175
176 /**
177 * Create / updating field.
178 */
179 public function testReplaceUFFields() {
180 $baseFields = array();
181 $baseFields[] = array(
182 'field_name' => 'first_name',
183 'field_type' => 'Contact',
184 'visibility' => 'Public Pages and Listings',
185 'weight' => 3,
186 'label' => 'Test First Name',
187 'is_searchable' => 1,
188 'is_active' => 1,
189 );
190 $baseFields[] = array(
191 'field_name' => 'country',
192 'field_type' => 'Contact',
193 'visibility' => 'Public Pages and Listings',
194 'weight' => 2,
195 'label' => 'Test Country',
196 'is_searchable' => 1,
197 'is_active' => 1,
198 'location_type_id' => 1,
199 );
200 $baseFields[] = array(
201 'field_name' => 'phone',
202 'field_type' => 'Contact',
203 'visibility' => 'Public Pages and Listings',
204 'weight' => 1,
205 'label' => 'Test Phone',
206 'is_searchable' => 1,
207 'is_active' => 1,
208 'location_type_id' => 1,
209 'phone_type_id' => 1,
210 );
211
212 $params = array(
213 'uf_group_id' => $this->_ufGroupId,
214 'option.autoweight' => FALSE,
215 'values' => $baseFields,
216 'check_permissions' => TRUE,
217 );
218
219 $result = $this->callAPIAndDocument('uf_field', 'replace', $params, __FUNCTION__, __FILE__);
220 $inputsByName = CRM_Utils_Array::index(array('field_name'), $params['values']);
221 $this->assertEquals(count($params['values']), count($result['values']));
222 foreach ($result['values'] as $outUfField) {
223 $this->assertTrue(is_string($outUfField['field_name']));
224 $inUfField = $inputsByName[$outUfField['field_name']];
225 foreach ($inUfField as $key => $inValue) {
226 $this->assertEquals($inValue, $outUfField[$key],
227 sprintf("field_name=[%s] key=[%s] expected=[%s] actual=[%s]",
228 $outUfField['field_name'],
229 $key,
230 $inValue,
231 $outUfField[$key]
232 )
233 );
234 }
235 }
236 }
237
238 /**
239 * Check Profile API permission without ACL.
240 */
241 public function testProfilesWithoutACL() {
242 $this->createLoggedInUser();
243 $baseFields[] = array(
244 'field_name' => 'first_name',
245 'field_type' => 'Contact',
246 'visibility' => 'Public Pages and Listings',
247 'weight' => 3,
248 'label' => 'Test First Name',
249 'is_searchable' => 1,
250 'is_active' => 1,
251 );
252 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
253 $params = array(
254 'uf_group_id' => $this->_ufGroupId,
255 'option.autoweight' => FALSE,
256 'values' => $baseFields,
257 'check_permissions' => TRUE,
258 );
259 $this->_loggedInUser = CRM_Core_Session::singleton()->get('userID');
260 $this->callAPIFailure('uf_field', 'replace', $params);
261 }
262
263 /**
264 * Check Profile ACL for API permission.
265 */
266 public function testACLPermissionforProfiles() {
267 $this->createLoggedInUser();
268 $this->_permissionedGroup = $this->groupCreate(array(
269 'title' => 'Edit Profiles',
270 'is_active' => 1,
271 'name' => 'edit-profiles',
272 ));
273 $this->setupACL(TRUE);
274 $this->testReplaceUFFields();
275 }
276
277 }