CRM-14043 - cid=0 contribution form giving contribution to logged in user
[civicrm-core.git] / tests / phpunit / api / v3 / SettingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
30
31 /**
32 * Test APIv3 civicrm_setting_* functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Core
36 */
37
38 /**
39 * Class contains api test cases for civicrm settings
40 *
41 */
42 class api_v3_SettingTest extends CiviUnitTestCase {
43
44 protected $_apiversion = 3;
45 protected $_contactID;
46 protected $_params;
47 protected $_currentDomain;
48 protected $_domainID2;
49 protected $_domainID3;
50
51 function __construct() {
52 parent::__construct();
53
54 }
55
56 function get_info() {
57 return array(
58 'name' => 'Settings Tests',
59 'description' => 'Settings API',
60 'group' => 'CiviCRM API Tests',
61 );
62 }
63
64 function setUp() {
65 parent::setUp();
66 $params = array(
67 'name' => 'Default Domain Name',
68 );
69 $result = $this->callAPISuccess( 'domain','get',$params);
70 if(empty($result['id'])){
71 $result = $this->callAPISuccess( 'domain','create',$params );
72 }
73
74 $params['name'] = 'Second Domain';
75 $result = $this->callAPISuccess( 'domain','get',$params);
76 if(empty($result['id'])){
77 $result = $this->callAPISuccess( 'domain','create',$params );
78 }
79 $this->_domainID2 = $result['id'];
80 $params['name'] = 'A-team domain';
81 $result = $this->callAPISuccess( 'domain','get',$params);
82 if(empty($result['id'])){
83 $result = $this->callAPISuccess( 'domain','create',$params );
84 }
85 $this->_domainID3 = $result['id'];
86 $this->_currentDomain = CRM_Core_Config::domainID();
87 }
88
89 function tearDown() {
90 parent::tearDown();
91 $this->callAPISuccess('system','flush', array());
92 $this->quickCleanup(array('civicrm_domain'));
93 }
94
95 /**
96 * check getfields works
97 */
98 function testGetFields() {
99 $description = 'Demonstrate return from getfields - see subfolder for variants';
100 $result = $this->callAPIAndDocument('setting', 'getfields', array(), __FUNCTION__, __FILE__, $description);
101 $this->assertArrayHasKey('customCSSURL', $result['values']);
102
103 $description = 'Demonstrate return from getfields';
104 $result = $this->callAPISuccess('setting', 'getfields', array());
105 $this->assertArrayHasKey('customCSSURL', $result['values']);
106 $this->callAPISuccess('system','flush', array());
107 }
108
109 /**
110 * let's check it's loading from cache by meddling with the cache
111 */
112 function testGetFieldsCaching() {
113 $settingsMetadata = array();
114 CRM_Core_BAO_Cache::setItem($settingsMetadata,'CiviCRM setting Specs', 'settingsMetadata__');
115 CRM_Core_BAO_Cache::setItem($settingsMetadata,'CiviCRM setting Spec', 'All');
116 $result = $this->callAPISuccess('setting', 'getfields', array());
117 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
118 $this->quickCleanup(array('civicrm_cache'));
119 }
120
121 function testGetFieldsFilters() {
122 $params = array('name' => 'advanced_search_options');
123 $result = $this->callAPISuccess('setting', 'getfields', $params);
124 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
125 $this->assertArrayHasKey('advanced_search_options',$result['values']);
126 }
127
128 /**
129 * Ensure that on_change callbacks fire.
130 *
131 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
132 * are very similar, but they exercise different codepaths. The first uses the API
133 * and setItems [plural]; the second uses setItem [singular].
134 */
135 function testOnChange() {
136 global $_testOnChange_hookCalls;
137 $this->setMockSettingsMetaData(array(
138 'onChangeExample' => array(
139 'group_name' => 'CiviCRM Preferences',
140 'group' => 'core',
141 'name' => 'onChangeExample',
142 'type' => 'Array',
143 'quick_form_type' => 'Element',
144 'html_type' => 'advmultiselect',
145 'default' => array('CiviEvent', 'CiviContribute'),
146 'add' => '4.4',
147 'title' => 'List of Components',
148 'is_domain' => '1',
149 'is_contact' => 0,
150 'description' => NULL,
151 'help_text' => NULL,
152 'on_change' => array( // list of callbacks
153 array(__CLASS__, '_testOnChange_onChangeExample')
154 ),
155 ),
156 ));
157
158 // set initial value
159 $_testOnChange_hookCalls = array('count' => 0);
160 $this->callAPISuccess('setting', 'create', array(
161 'onChangeExample' => array('First', 'Value'),
162 ));
163 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
164 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
165 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
166
167 // change value
168 $_testOnChange_hookCalls = array('count' => 0);
169 $this->callAPISuccess('setting', 'create', array(
170 'onChangeExample' => array('Second', 'Value'),
171 ));
172 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
173 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
174 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
175 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
176 }
177
178 /**
179 * Mock callback for a setting's on_change handler
180 *
181 * @param $oldValue
182 * @param $newValue
183 * @param $metadata
184 */
185 static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
186 global $_testOnChange_hookCalls;
187 $_testOnChange_hookCalls['count']++;
188 $_testOnChange_hookCalls['oldValue'] = $oldValue;
189 $_testOnChange_hookCalls['newValue'] = $newValue;
190 $_testOnChange_hookCalls['metadata'] = $metadata;
191 }
192
193 /**
194 * check getfields works
195 */
196 function testCreateSetting() {
197 $description = "shows setting a variable for a given domain - if no domain is set current is assumed";
198
199 $params = array(
200 'domain_id' => $this->_domainID2,
201 'uniq_email_per_site' => 1,
202 );
203 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__);
204
205 $params = array('uniq_email_per_site' => 1,);
206 $description = "shows setting a variable for a current domain";
207 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__, $description, 'CreateSettingCurrentDomain');
208 $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']);
209 }
210
211 /**
212 * check getfields works
213 */
214 function testCreateInvalidSettings() {
215
216 $params = array(
217 'domain_id' => $this->_domainID2,
218 'invalid_key' => 1,
219 );
220 $result = $this->callAPIFailure('setting', 'create', $params);
221 }
222
223 /**
224 * check invalid settings rejected -
225 */
226
227 function testCreateInvalidURLSettings() {
228
229 $params = array(
230 'domain_id' => $this->_domainID2,
231 'userFrameworkResourceURL' => 'dfhkdhfd',
232 );
233 $result = $this->callAPIFailure('setting', 'create', $params);
234 $params = array(
235 'domain_id' => $this->_domainID2,
236 'userFrameworkResourceURL' => 'http://blah.com',
237 );
238 $result = $this->callAPISuccess('setting', 'create', $params);
239 }
240
241 /**
242 * check getfields works
243 */
244 function testCreateInvalidBooleanSettings() {
245
246 $params = array(
247 'domain_id' => $this->_domainID2,
248 'track_civimail_replies' => 'dfhkdhfd',
249 );
250 $result = $this->callAPIFailure('setting', 'create', $params);
251
252 $params = array('track_civimail_replies' => '0',);
253 $result = $this->callAPISuccess('setting', 'create', $params);
254 $getResult = $this->callAPISuccess('setting','get',$params);
255 $this->assertEquals(0, $getResult['values'][$this->_currentDomain]['track_civimail_replies']);
256
257 $getResult = $this->callAPISuccess('setting','get',$params);
258 $this->assertEquals(0, $getResult['values'][$this->_currentDomain]['track_civimail_replies']);
259 $params = array( 'domain_id' => $this->_domainID2,
260 'track_civimail_replies' => '1',
261 );
262 $result = $this->callAPISuccess('setting', 'create', $params);
263 $getResult = $this->callAPISuccess('setting','get',$params);
264 $this->assertEquals(1, $getResult['values'][$this->_domainID2]['track_civimail_replies']);
265
266 $params = array(
267 'domain_id' => $this->_domainID2,
268 'track_civimail_replies' => 'TRUE',
269 );
270 $result = $this->callAPISuccess('setting', 'create', $params);
271 $getResult = $this->callAPISuccess('setting','get',$params);
272
273 $this->assertEquals(1, $getResult['values'][$this->_domainID2]['track_civimail_replies'], "check TRUE is converted to 1");
274
275
276 }
277
278 /**
279 * check getfields works
280 */
281 function testCreateSettingMultipleDomains() {
282 $description = "shows setting a variable for all domains";
283
284 $params = array(
285 'domain_id' => 'all',
286 'uniq_email_per_site' => 1,
287 );
288 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__,$description, 'CreateAllDomains');
289
290 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
291 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
292 $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing');
293 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.');
294
295 $params = array(
296 'domain_id' => 'all',
297 'return' => 'uniq_email_per_site'
298 );
299 // we'll check it with a 'get'
300 $description = "shows getting a variable for all domains";
301 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__,$description, 'GetAllDomains', 'Get');
302
303 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
304 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
305 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']);
306
307 $params = array(
308 'domain_id' => array(1,3),
309 'uniq_email_per_site' => 0,
310 );
311 $description = "shows setting a variable for specified domains";
312 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__,$description, 'CreateSpecifiedDomains');
313
314 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
315 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
316 $params = array(
317 'domain_id' => array(1,2),
318 'return' => array('uniq_email_per_site'),
319 );
320 $description = "shows getting a variable for specified domains";
321 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__,$description, 'GetSpecifiedDomains', 'Get');
322 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
323 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
324
325 }
326
327 function testGetSetting() {
328
329 $params = array(
330 'domain_id' => $this->_domainID2,
331 'return' => 'uniq_email_per_site',
332 );
333 $description = "shows get setting a variable for a given domain - if no domain is set current is assumed";
334
335 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__);
336
337 $params = array(
338 'return' => 'uniq_email_per_site',
339 );
340 $description = "shows getting a variable for a current domain";
341 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__, $description, 'GetSettingCurrentDomain');
342 $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']);
343 }
344 /**
345 * setting api should set & fetch settings stored in config as well as those in settings table
346 */
347 function testSetConfigSetting() {
348 $config = CRM_Core_Config::singleton();
349 $this->assertFalse($config->debug == 1);
350 $params = array(
351 'domain_id' => $this->_domainID2,
352 'debug_enabled' => 1,
353 );
354 $result = $this->callAPISuccess('setting', 'create', $params);
355 CRM_Core_BAO_Domain::setDomain($this->_domainID2);
356 $config = CRM_Core_Config::singleton(TRUE, TRUE);
357 CRM_Core_BAO_Domain::resetDomain();
358 $this->assertTrue($config->debug == 1);
359 // this should NOT be stored in the settings table now - only in config
360 $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%debug%'";
361 $dao = CRM_Core_DAO::executeQuery($sql);
362 $dao->fetch();
363 $this->assertEquals($dao->c, 0);
364 }
365 /**
366 * setting api should set & fetch settings stored in config as well as those in settings table
367 */
368 function testGetConfigSetting() {
369 $settings = $this->callAPISuccess('setting', 'get', array(
370 'name' => 'defaultCurrency', 'sequential' => 1,)
371 );
372 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
373 }
374
375 /**
376 * setting api should set & fetch settings stored in config as well as those in settings table
377 */
378 function testGetSetConfigSettingMultipleDomains() {
379 $settings = $this->callAPISuccess('setting', 'create', array(
380 'defaultCurrency' => 'USD', 'domain_id' => $this->_currentDomain)
381 );
382 $settings = $this->callAPISuccess('setting', 'create', array(
383 'defaultCurrency' => 'CAD', 'domain_id' => $this->_domainID2)
384 );
385 $settings = $this->callAPISuccess('setting', 'get', array(
386 'return' => 'defaultCurrency', 'domain_id' => 'all',
387 )
388 );
389 $this->assertEquals('USD', $settings['values'][$this->_currentDomain]['defaultCurrency']);
390 $this->assertEquals('CAD', $settings['values'][$this->_domainID2]['defaultCurrency'],
391 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
392
393 }
394
395 /*
396 * Use getValue against a config setting
397 */
398 function testGetValueConfigSetting() {
399 $params = array( 'name' => 'monetaryThousandSeparator',
400 'group' => 'Localization Setting',
401 );
402 $result = $this->callAPISuccess('setting', 'getvalue', $params);
403 $this->assertEquals(',', $result);
404 }
405
406 function testGetValue() {
407 $params = array( 'name' => 'petition_contacts',
408 'group' => 'Campaign Preferences'
409 );
410 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get";
411
412 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__, __FILE__, $description);
413 $this->assertEquals('Petition Contacts', $result);
414 }
415
416 function testGetDefaults() {
417 $description = "gets defaults setting a variable for a given domain - if no domain is set current is assumed";
418
419 $params = array(
420 'name' => 'address_format',
421 );
422 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__, __FILE__,$description,'GetDefaults','getdefaults');
423 $this->assertEquals("{contact.address_name}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['address_format']);
424 $params = array('name' => 'mailing_format',);
425 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
426 $this->assertEquals("{contact.addressee}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['mailing_format']);
427 $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']);
428 }
429 /*
430 * Function tests reverting a specific parameter
431 */
432 function testRevert() {
433
434 $params = array( 'address_format' => 'xyz',
435 'mailing_format' => 'bcs',
436 );
437 $result = $this->callAPISuccess('setting', 'create', $params);
438 $this->assertAPISuccess($result, "in line " . __LINE__);
439 $revertParams = array( 'name' => 'address_format'
440 );
441 $result = $this->callAPISuccess('setting', 'get', $params);
442 //make sure it's set
443 $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']);
444 $description = "Demonstrates reverting a parameter to default value";
445 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__, __FILE__,$description,'','revert');
446 //make sure it's reverted
447 $result = $this->callAPISuccess('setting', 'get', $params);
448 $this->assertEquals("{contact.address_name}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['address_format']);
449 $params = array( 'return' => array('mailing_format'),
450 );
451 $result = $this->callAPISuccess('setting', 'get', $params);
452 //make sure it's unchanged
453 $this->assertEquals('bcs', $result['values'][CRM_Core_Config::domainID()]['mailing_format']);
454 }
455 /*
456 * Tests reverting ALL parameters (specific domain)
457 */
458 function testRevertAll() {
459
460 $params = array( 'address_format' => 'xyz',
461 'mailing_format' => 'bcs',
462 );
463 $result = $this->callAPISuccess('setting', 'create', $params);
464 $revertParams = array( );
465 $result = $this->callAPISuccess('setting', 'get', $params);
466 //make sure it's set
467 $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']);
468
469 $this->callAPISuccess('setting', 'revert', $revertParams);
470 //make sure it's reverted
471 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
472 $this->assertEquals("{contact.address_name}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['address_format']);
473 $this->assertEquals("{contact.addressee}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['mailing_format']);
474 }
475
476 /*
477 * Tests filling missing params
478 */
479 function testFill() {
480 $domparams = array(
481 'name' => 'B Team Domain',
482 );
483 $dom = $this->callAPISuccess('domain', 'create', $domparams);
484 $params = array( 'domain_id' => 'all',
485 );
486 $result = $this->callAPISuccess('setting', 'get', $params);
487 $params = array( 'address_format' => 'xyz',
488 'mailing_format' => 'bcs',
489 'domain_id' => $this->_domainID2,
490 );
491 $result = $this->callAPISuccess('setting', 'create', $params);
492 $params = array( 'domain_id' => $dom['id'],
493 );
494 $result = $this->callAPISuccess('setting', 'get', $params);
495 $this->assertAPISuccess($result, "in line " . __LINE__);
496 $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']],'setting for domain 3 should not be set. Debug this IF domain test is passing');
497 $result = $this->callAPISuccess('setting', 'fill', $params);
498 $this->assertAPISuccess($result, "in line " . __LINE__);
499 $result = $this->callAPISuccess('setting', 'get', $params);
500 $this->assertAPISuccess($result, "in line " . __LINE__);
501 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
502 $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
503 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
504 }
505 }
506