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