Merge pull request #4906 from eileenmcnaughton/minor-tidies
[civicrm-core.git] / tests / phpunit / api / v3 / UtilsTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29require_once 'CRM/Utils/DeprecatedUtils.php';
30
31/**
32 * Test class for API utils
33 *
34 * @package CiviCRM
35 */
36class api_v3_UtilsTest extends CiviUnitTestCase {
ca985406 37 protected $_apiversion = 3;
6a488035 38 public $DBResetRequired = FALSE;
b7c9bc4c 39
6a488035
TO
40 public $_contactID = 1;
41
42 /**
43 * Sets up the fixture, for example, opens a network connection.
44 * This method is called before a test is executed.
6a488035
TO
45 */
46 protected function setUp() {
47 parent::setUp();
d1e734a5 48 $this->useTransaction(TRUE);
6a488035
TO
49 }
50
00be9182 51 public function testAddFormattedParam() {
6a488035
TO
52 $values = array('contact_type' => 'Individual');
53 $params = array('something' => 1);
54 $result = _civicrm_api3_deprecated_add_formatted_param($values, $params);
55 $this->assertTrue($result);
56 }
57
00be9182 58 public function testCheckPermissionReturn() {
6a488035
TO
59 $check = array('check_permissions' => TRUE);
60 $config = CRM_Core_Config::singleton();
61 $config->userPermissionClass->permissions = array();
d0c9daa4 62 $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'empty permissions should not be enough');
6a488035 63 $config->userPermissionClass->permissions = array('access CiviCRM');
d0c9daa4 64 $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'lacking permissions should not be enough');
6a488035 65 $config->userPermissionClass->permissions = array('add contacts');
d0c9daa4 66 $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'lacking permissions should not be enough');
6a488035
TO
67
68 $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts');
d0c9daa4 69 $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'exact permissions should be enough');
6a488035
TO
70
71 $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'import contacts');
d0c9daa4 72 $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'overfluous permissions should be enough');
6a488035
TO
73 }
74
00be9182 75 public function testCheckPermissionThrow() {
6a488035
TO
76 $check = array('check_permissions' => TRUE);
77 $config = CRM_Core_Config::singleton();
78 try {
79 $config->userPermissionClass->permissions = array('access CiviCRM');
d0c9daa4 80 $this->runPermissionCheck('contact', 'create', $check, TRUE);
6a488035 81 }
92915c55 82 catch (Exception $e) {
6a488035
TO
83 $message = $e->getMessage();
84 }
60ec9f43 85 $this->assertEquals($message, 'API permission check failed for contact/create call; insufficient permission: require access CiviCRM and add contacts', 'lacking permissions should throw an exception');
6a488035
TO
86
87 $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'import contacts');
d0c9daa4 88 $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'overfluous permissions should return true');
6a488035
TO
89 }
90
00be9182 91 public function testCheckPermissionSkip() {
6a488035
TO
92 $config = CRM_Core_Config::singleton();
93 $config->userPermissionClass->permissions = array('access CiviCRM');
94 $params = array('check_permissions' => TRUE);
d0c9daa4 95 $this->assertFalse($this->runPermissionCheck('contact', 'create', $params), 'lacking permissions should not be enough');
6a488035 96 $params = array('check_permissions' => FALSE);
d0c9daa4
TO
97 $this->assertTrue($this->runPermissionCheck('contact', 'create', $params), 'permission check should be skippable');
98 }
99
100 /**
101 * @param string $entity
102 * @param string $action
103 * @param array $params
e16033b4
TO
104 * @param bool $throws
105 * Whether we should pass any exceptions for authorization failures.
cbdcc634
EM
106 *
107 * @throws API_Exception
108 * @throws Exception
a6c01b45
CW
109 * @return bool
110 * TRUE or FALSE depending on the outcome of the authorization check
d0c9daa4 111 */
00be9182 112 public function runPermissionCheck($entity, $action, $params, $throws = FALSE) {
d0c9daa4
TO
113 $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
114 $dispatcher->addSubscriber(new \Civi\API\Subscriber\PermissionCheck());
115 $kernel = new \Civi\API\Kernel($dispatcher);
cde12543 116 $apiRequest = \Civi\API\Request::create($entity, $action, $params, NULL);
d0c9daa4
TO
117 try {
118 $kernel->authorize(NULL, $apiRequest);
119 return TRUE;
0db6c3e1
TO
120 }
121 catch (\API_Exception $e) {
d0c9daa4
TO
122 $extra = $e->getExtraParams();
123 if (!$throws && $extra['error_code'] == API_Exception::UNAUTHORIZED) {
124 return FALSE;
0db6c3e1
TO
125 }
126 else {
d0c9daa4
TO
127 throw $e;
128 }
129 }
6a488035
TO
130 }
131
c490a46a
CW
132 /**
133 * Test verify mandatory - includes DAO & passed as well as empty & NULL fields
134 */
00be9182 135 public function testVerifyMandatory() {
6a488035
TO
136 _civicrm_api3_initialize(TRUE);
137 $params = array(
138 'entity_table' => 'civicrm_contact',
139 'note' => '',
140 'contact_id' => $this->_contactID,
141 'modified_date' => '2011-01-31',
142 'subject' => NULL,
21dfd5f5 143 'version' => $this->_apiversion,
6a488035
TO
144 );
145 try {
146 $result = civicrm_api3_verify_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject'));
147 }
92915c55 148 catch (Exception $expected) {
6a488035
TO
149 $this->assertEquals('Mandatory key(s) missing from params array: entity_id, note, subject', $expected->getMessage());
150 return;
151 }
152
153 $this->fail('An expected exception has not been raised.');
154 }
155
c490a46a
CW
156 /**
157 * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields
158 */
00be9182 159 public function testVerifyOneMandatory() {
6a488035
TO
160 _civicrm_api3_initialize(TRUE);
161 $params = array(
162 'entity_table' => 'civicrm_contact',
163 'note' => '',
164 'contact_id' => $this->_contactID,
165 'modified_date' => '2011-01-31',
166 'subject' => NULL,
167 'version' => $this->_apiversion,
168 );
169
170 try {
171 $result = civicrm_api3_verify_one_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject'));
172 }
92915c55 173 catch (Exception $expected) {
6a488035
TO
174 $this->assertEquals('Mandatory key(s) missing from params array: entity_id, one of (note, subject)', $expected->getMessage());
175 return;
176 }
177
178 $this->fail('An expected exception has not been raised.');
179 }
180
c490a46a
CW
181 /**
182 * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields
183 */
00be9182 184 public function testVerifyOneMandatoryOneSet() {
6a488035 185 _civicrm_api3_initialize(TRUE);
92915c55
TO
186 $params = array(
187 'version' => 3,
188 'entity_table' => 'civicrm_contact',
189 'note' => 'note',
190 'contact_id' => $this->_contactID,
191 'modified_date' => '2011-01-31',
192 'subject' => NULL
193 );
6a488035
TO
194
195 try {
196 civicrm_api3_verify_one_mandatory($params, NULL, array('note', 'subject'));
197 }
92915c55 198 catch (Exception$expected) {
6a488035
TO
199 $this->fail('Exception raised when it shouldn\'t have been in line ' . __LINE__);
200 }
201 }
202
203
c490a46a 204 /**
b6708aeb 205 * Test GET DAO function returns DAO
206 */
00be9182 207 public function testGetDAO() {
49e101d0
CW
208 $params = array(
209 'civicrm_api3_custom_group_get' => 'CRM_Core_DAO_CustomGroup',
210 'custom_group' => 'CRM_Core_DAO_CustomGroup',
211 'CustomGroup' => 'CRM_Core_DAO_CustomGroup',
212 'civicrm_api3_custom_field_get' => 'CRM_Core_DAO_CustomField',
213 'civicrm_api3_survey_get' => 'CRM_Campaign_DAO_Survey',
214 'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_DAO_PledgePayment',
215 'civicrm_api3_website_get' => 'CRM_Core_DAO_Website',
216 'Membership' => 'CRM_Member_DAO_Membership',
217 );
218 foreach ($params as $input => $expected) {
219 $result = _civicrm_api3_get_DAO($input);
220 $this->assertEquals($expected, $result);
221 }
6a488035 222 }
c490a46a
CW
223
224 /**
49e101d0 225 * Test GET BAO function returns BAO when it exists
b6708aeb 226 */
00be9182 227 public function testGetBAO() {
49e101d0
CW
228 $params = array(
229 'civicrm_api3_website_get' => 'CRM_Core_BAO_Website',
230 'civicrm_api3_survey_get' => 'CRM_Campaign_BAO_Survey',
231 'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_BAO_PledgePayment',
232 'Household' => 'CRM_Contact_BAO_Contact',
233 // Note this one DOES NOT have a BAO so we expect to fall back on returning the DAO
234 'mailing_group' => 'CRM_Mailing_DAO_MailingGroup',
5c1174d3
CW
235 // Make sure we get null back with nonexistant entities
236 'civicrm_this_does_not_exist' => NULL,
49e101d0
CW
237 );
238 foreach ($params as $input => $expected) {
239 $result = _civicrm_api3_get_BAO($input);
240 $this->assertEquals($expected, $result);
241 }
6a488035
TO
242 }
243
00be9182 244 public function test_civicrm_api3_validate_fields() {
6a488035 245 $params = array('start_date' => '2010-12-20', 'end_date' => '');
7f8c98cd
E
246 $fields = civicrm_api3('relationship', 'getfields', array('action' => 'get'));
247 _civicrm_api3_validate_fields('relationship', 'get', $params, $fields['values']);
6a488035
TO
248 $this->assertEquals('20101220000000', $params['start_date']);
249 $this->assertEquals('', $params['end_date']);
250 }
251
00be9182 252 public function test_civicrm_api3_validate_fields_membership() {
92915c55
TO
253 $params = array(
254 'start_date' => '2010-12-20',
255 'end_date' => '',
256 'membership_end_date' => '0',
257 'join_date' => '2010-12-20',
258 'membership_start_date' => '2010-12-20'
259 );
7f8c98cd
E
260 $fields = civicrm_api3('Membership', 'getfields', array('action' => 'get'));
261 _civicrm_api3_validate_fields('Membership', 'get', $params, $fields['values']);
903d0d38 262 $this->assertEquals('2010-12-20', $params['start_date']);
263 $this->assertEquals('20101220000000', $params['membership_start_date'], 'in line ' . __LINE__);
6a488035
TO
264 $this->assertEquals('', $params['end_date']);
265 $this->assertEquals('20101220000000', $params['join_date'], 'join_date not set in line ' . __LINE__);
266 }
267
00be9182 268 public function test_civicrm_api3_validate_fields_event() {
6a488035
TO
269
270 $params = array(
271 'registration_start_date' => 20080601,
6c6e6187 272 'registration_end_date' => '2008-10-15',
92915c55
TO
273 'start_date' => '2010-12-20',
274 'end_date' => '',
6a488035 275 );
7f8c98cd
E
276 $fields = civicrm_api3('Event', 'getfields', array('action' => 'create'));
277 _civicrm_api3_validate_fields('event', 'create', $params, $fields['values']);
6a488035
TO
278 $this->assertEquals('20101220000000', $params['start_date'], 'in line ' . __LINE__);
279 $this->assertEquals('20081015000000', $params['registration_end_date'], 'in line ' . __LINE__);
280 $this->assertEquals('', $params['end_date'], 'in line ' . __LINE__);
281 $this->assertEquals('20080601000000', $params['registration_start_date']);
282 }
283
00be9182 284 public function test_civicrm_api3_validate_fields_exception() {
6a488035
TO
285 $params = array(
286 'join_date' => 'abc',
287 );
288 try {
7f8c98cd
E
289 $fields = civicrm_api3('Membership', 'getfields', array('action' => 'get'));
290 _civicrm_api3_validate_fields('Membership', 'get', $params, $fields['values']);
6a488035 291 }
92915c55 292 catch (Exception$expected) {
6a488035
TO
293 $this->assertEquals('join_date is not a valid date: abc', $expected->getMessage());
294 }
295 }
296
00be9182 297 public function testGetFields() {
ca985406 298 $result = $this->callAPISuccess('membership', 'getfields', array());
6a488035 299 $this->assertArrayHasKey('values', $result);
ca985406 300 $result = $this->callAPISuccess('relationship', 'getfields', array());
6a488035 301 $this->assertArrayHasKey('values', $result);
ca985406 302 $result = $this->callAPISuccess('event', 'getfields', array());
6a488035
TO
303 $this->assertArrayHasKey('values', $result);
304 }
41d89fcb 305
00be9182 306 public function testGetFields_AllOptions() {
ca985406 307 $result = $this->callAPISuccess('contact', 'getfields', array(
41d89fcb
TO
308 'options' => array(
309 'get_options' => 'all',
310 ),
41d89fcb 311 ));
41d89fcb
TO
312 $this->assertEquals('Household', $result['values']['contact_type']['options']['Household']);
313 $this->assertEquals('HTML', $result['values']['preferred_mail_format']['options']['HTML']);
314 }
6a488035 315}