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