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