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