Merge pull request #1625 from pradpnayak/CRM-13340
[civicrm-core.git] / tests / phpunit / api / v3 / SettingTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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
28require_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 */
42class 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 public $_eNoticeCompliant = TRUE;
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',
7fbb4198 68 );
69 $result = $this->callAPISuccess( 'domain','get',$params);
6a488035 70 if(empty($result['id'])){
7fbb4198 71 $result = $this->callAPISuccess( 'domain','create',$params );
6a488035
TO
72 }
73
74 $params['name'] = 'Second Domain';
7fbb4198 75 $result = $this->callAPISuccess( 'domain','get',$params);
6a488035 76 if(empty($result['id'])){
7fbb4198 77 $result = $this->callAPISuccess( 'domain','create',$params );
6a488035
TO
78 }
79 $this->_domainID2 = $result['id'];
80 $params['name'] = 'A-team domain';
7fbb4198 81 $result = $this->callAPISuccess( 'domain','get',$params);
6a488035 82 if(empty($result['id'])){
7fbb4198 83 $result = $this->callAPISuccess( 'domain','create',$params );
6a488035
TO
84 }
85 $this->_domainID3 = $result['id'];
86 $this->_currentDomain = CRM_Core_Config::domainID();
87 }
88
89 function tearDown() {
90 parent::tearDown();
7fbb4198 91 $this->callAPISuccess('system','flush', array());
6a488035 92 $this->quickCleanup(array('civicrm_domain'));
6a488035
TO
93 }
94
95 /**
96 * check getfields works
97 */
98 function testGetFields() {
6a488035 99 $description = 'Demonstrate return from getfields - see subfolder for variants';
7fbb4198 100 $result = $this->callAPIAndDocument('setting', 'getfields', array(), __FUNCTION__, __FILE__, $description);
6a488035
TO
101 $this->assertArrayHasKey('customCSSURL', $result['values']);
102
103 $description = 'Demonstrate return from getfields';
7fbb4198 104 $result = $this->callAPISuccess('setting', 'getfields', array());
6a488035 105 $this->assertArrayHasKey('customCSSURL', $result['values']);
7fbb4198 106 $this->callAPISuccess('system','flush', array());
6a488035
TO
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');
7fbb4198 116 $result = $this->callAPISuccess('setting', 'getfields', array());
6a488035
TO
117 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
118 $this->quickCleanup(array('civicrm_cache'));
119 }
120
121 function testGetFieldsFilters() {
7fbb4198 122 $params = array('name' => 'advanced_search_options');
123 $result = $this->callAPISuccess('setting', 'getfields', $params);
6a488035
TO
124 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
125 $this->assertArrayHasKey('advanced_search_options',$result['values']);
126 }
127 /**
128 * check getfields works
129 */
130 function testCreateSetting() {
7fbb4198 131 $description = "shows setting a variable for a given domain - if no domain is set current is assumed";
6a488035 132
7fbb4198 133 $params = array(
6a488035
TO
134 'domain_id' => $this->_domainID2,
135 'uniq_email_per_site' => 1,
136 );
7fbb4198 137 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__);
6a488035 138
7fbb4198 139 $params = array('uniq_email_per_site' => 1,);
6a488035 140 $description = "shows setting a variable for a current domain";
7fbb4198 141 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__, $description, 'CreateSettingCurrentDomain');
6a488035
TO
142 $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']);
143 }
144
145 /**
146 * check getfields works
147 */
148 function testCreateInvalidSettings() {
149
7fbb4198 150 $params = array(
6a488035
TO
151 'domain_id' => $this->_domainID2,
152 'invalid_key' => 1,
153 );
d0e1eff2 154 $result = $this->callAPIFailure('setting', 'create', $params);
6a488035
TO
155 }
156
157 /**
158 * check invalid settings rejected -
159 */
160
161 function testCreateInvalidURLSettings() {
162
7fbb4198 163 $params = array(
6a488035
TO
164 'domain_id' => $this->_domainID2,
165 'userFrameworkResourceURL' => 'dfhkdhfd',
166 );
d0e1eff2 167 $result = $this->callAPIFailure('setting', 'create', $params);
7fbb4198 168 $params = array(
6a488035
TO
169 'domain_id' => $this->_domainID2,
170 'userFrameworkResourceURL' => 'http://blah.com',
171 );
7fbb4198 172 $result = $this->callAPISuccess('setting', 'create', $params);
6a488035
TO
173 }
174
175 /**
176 * check getfields works
177 */
178 function testCreateInvalidBooleanSettings() {
179
7fbb4198 180 $params = array(
6a488035
TO
181 'domain_id' => $this->_domainID2,
182 'track_civimail_replies' => 'dfhkdhfd',
183 );
d0e1eff2 184 $result = $this->callAPIFailure('setting', 'create', $params);
6a488035 185
7fbb4198 186 $params = array('track_civimail_replies' => '0',);
187 $result = $this->callAPISuccess('setting', 'create', $params);
188 $getResult = $this->callAPISuccess('setting','get',$params);
6a488035
TO
189 $this->assertEquals(0, $getResult['values'][$this->_currentDomain]['track_civimail_replies']);
190
7fbb4198 191 $getResult = $this->callAPISuccess('setting','get',$params);
6a488035 192 $this->assertEquals(0, $getResult['values'][$this->_currentDomain]['track_civimail_replies']);
7fbb4198 193 $params = array( 'domain_id' => $this->_domainID2,
6a488035
TO
194 'track_civimail_replies' => '1',
195 );
7fbb4198 196 $result = $this->callAPISuccess('setting', 'create', $params);
197 $getResult = $this->callAPISuccess('setting','get',$params);
6a488035
TO
198 $this->assertEquals(1, $getResult['values'][$this->_domainID2]['track_civimail_replies']);
199
7fbb4198 200 $params = array(
6a488035
TO
201 'domain_id' => $this->_domainID2,
202 'track_civimail_replies' => 'TRUE',
203 );
7fbb4198 204 $result = $this->callAPISuccess('setting', 'create', $params);
205 $getResult = $this->callAPISuccess('setting','get',$params);
6a488035
TO
206
207 $this->assertEquals(1, $getResult['values'][$this->_domainID2]['track_civimail_replies'], "check TRUE is converted to 1");
208
209
210 }
211
212 /**
213 * check getfields works
214 */
215 function testCreateSettingMultipleDomains() {
7fbb4198 216 $description = "shows setting a variable for all domains";
6a488035 217
7fbb4198 218 $params = array(
6a488035
TO
219 'domain_id' => 'all',
220 'uniq_email_per_site' => 1,
6a488035 221 );
7fbb4198 222 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__,$description, 'CreateAllDomains');
223
6a488035
TO
224 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
225 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
226 $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing');
227 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.');
228
7fbb4198 229 $params = array(
6a488035
TO
230 'domain_id' => 'all',
231 'return' => 'uniq_email_per_site'
232 );
233 // we'll check it with a 'get'
6a488035 234 $description = "shows getting a variable for all domains";
7fbb4198 235 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__,$description, 'GetAllDomains', 'Get');
236
6a488035
TO
237 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
238 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
239 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']);
240
7fbb4198 241 $params = array(
6a488035
TO
242 'domain_id' => array(1,3),
243 'uniq_email_per_site' => 0,
244 );
6a488035 245 $description = "shows setting a variable for specified domains";
7fbb4198 246 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__,$description, 'CreateSpecifiedDomains');
247
6a488035
TO
248 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
249 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
7fbb4198 250 $params = array(
6a488035
TO
251 'domain_id' => array(1,2),
252 'return' => array('uniq_email_per_site'),
253 );
6a488035 254 $description = "shows getting a variable for specified domains";
7fbb4198 255 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__,$description, 'GetSpecifiedDomains', 'Get');
6a488035
TO
256 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
257 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
258
259 }
260
261 function testGetSetting() {
262
7fbb4198 263 $params = array(
264 'domain_id' => $this->_domainID2,
265 'return' => 'uniq_email_per_site',
6a488035 266 );
6a488035 267 $description = "shows get setting a variable for a given domain - if no domain is set current is assumed";
7fbb4198 268
269 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
270
271 $params = array(
6a488035
TO
272 'return' => 'uniq_email_per_site',
273 );
6a488035 274 $description = "shows getting a variable for a current domain";
7fbb4198 275 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__, $description, 'GetSettingCurrentDomain');
6a488035
TO
276 $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']);
277 }
278/**
279 * setting api should set & fetch settings stored in config as well as those in settings table
280 */
281 function testSetConfigSetting() {
282 $config = CRM_Core_Config::singleton();
283 $this->assertFalse($config->debug == 1);
7fbb4198 284 $params = array(
6a488035
TO
285 'domain_id' => $this->_domainID2,
286 'debug_enabled' => 1,
287 );
7fbb4198 288 $result = $this->callAPISuccess('setting', 'create', $params);
5b887f26 289 CRM_Core_BAO_Domain::setDomain($this->_domainID2);
290 $config = CRM_Core_Config::singleton(TRUE, TRUE);
291 CRM_Core_BAO_Domain::resetDomain();
6a488035
TO
292 $this->assertTrue($config->debug == 1);
293 // this should NOT be stored in the settings table now - only in config
294 $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%debug%'";
295 $dao = CRM_Core_DAO::executeQuery($sql);
296 $dao->fetch();
297 $this->assertEquals($dao->c, 0);
298 }
299 /**
300 * setting api should set & fetch settings stored in config as well as those in settings table
301 */
302 function testGetConfigSetting() {
7fbb4198 303 $settings = $this->callAPISuccess('setting', 'get', array(
304 'name' => 'defaultCurrency', 'sequential' => 1,)
6a488035 305 );
6a488035
TO
306 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
307 }
2ba45310 308
309 /**
310 * setting api should set & fetch settings stored in config as well as those in settings table
311 */
312 function testGetSetConfigSettingMultipleDomains() {
7fbb4198 313 $settings = $this->callAPISuccess('setting', 'create', array(
314 'defaultCurrency' => 'USD', 'domain_id' => $this->_currentDomain)
2ba45310 315 );
7fbb4198 316 $settings = $this->callAPISuccess('setting', 'create', array(
317 'defaultCurrency' => 'CAD', 'domain_id' => $this->_domainID2)
2ba45310 318 );
7fbb4198 319 $settings = $this->callAPISuccess('setting', 'get', array(
320 'return' => 'defaultCurrency', 'domain_id' => 'all',
2ba45310 321 )
322 );
323 $this->assertEquals('USD', $settings['values'][$this->_currentDomain]['defaultCurrency']);
324 $this->assertEquals('CAD', $settings['values'][$this->_domainID2]['defaultCurrency'],
325 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
326
327 }
328
6a488035
TO
329/*
330 * Use getValue against a config setting
331 */
332 function testGetValueConfigSetting() {
7fbb4198 333 $params = array( 'name' => 'monetaryThousandSeparator',
6a488035
TO
334 'group' => 'Localization Setting',
335 );
7fbb4198 336 $result = $this->callAPISuccess('setting', 'getvalue', $params);
6a488035
TO
337 $this->assertEquals(',', $result);
338 }
339
340 function testGetValue() {
7fbb4198 341 $params = array( 'name' => 'petition_contacts',
6a488035
TO
342 'group' => 'Campaign Preferences'
343 );
6a488035 344 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get";
7fbb4198 345
346 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__, __FILE__, $description);
347 $this->assertEquals('Petition Contacts', $result);
6a488035
TO
348 }
349
350 function testGetDefaults() {
7fbb4198 351 $description = "gets defaults setting a variable for a given domain - if no domain is set current is assumed";
6a488035 352
7fbb4198 353 $params = array(
6a488035
TO
354 'name' => 'address_format',
355 );
7fbb4198 356 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__, __FILE__,$description,'GetDefaults','getdefaults');
6a488035 357 $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']);
7fbb4198 358 $params = array('name' => 'mailing_format',);
359 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
6a488035
TO
360 $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']);
361 $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']);
362 }
363 /*
364 * Function tests reverting a specific parameter
365 */
366 function testRevert() {
367
7fbb4198 368 $params = array( 'address_format' => 'xyz',
6a488035
TO
369 'mailing_format' => 'bcs',
370 );
7fbb4198 371 $result = $this->callAPISuccess('setting', 'create', $params);
6a488035 372 $this->assertAPISuccess($result, "in line " . __LINE__);
7fbb4198 373 $revertParams = array( 'name' => 'address_format'
6a488035 374 );
7fbb4198 375 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035
TO
376 //make sure it's set
377 $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']);
378 $description = "Demonstrates reverting a parameter to default value";
7fbb4198 379 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__, __FILE__,$description,'','revert');
6a488035 380 //make sure it's reverted
7fbb4198 381 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035 382 $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']);
7fbb4198 383 $params = array( 'return' => array('mailing_format'),
6a488035 384 );
7fbb4198 385 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035
TO
386 //make sure it's unchanged
387 $this->assertEquals('bcs', $result['values'][CRM_Core_Config::domainID()]['mailing_format']);
388 }
389 /*
390 * Tests reverting ALL parameters (specific domain)
391 */
392 function testRevertAll() {
393
7fbb4198 394 $params = array( 'address_format' => 'xyz',
6a488035
TO
395 'mailing_format' => 'bcs',
396 );
7fbb4198 397 $result = $this->callAPISuccess('setting', 'create', $params);
398 $revertParams = array( );
399 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035
TO
400 //make sure it's set
401 $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']);
402
7fbb4198 403 $this->callAPISuccess('setting', 'revert', $revertParams);
6a488035 404 //make sure it's reverted
7fbb4198 405 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
6a488035
TO
406 $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']);
407 $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']);
408 }
409
410 /*
411 * Tests filling missing params
412 */
413 function testFill() {
414 $domparams = array(
415 'name' => 'B Team Domain',
7fbb4198 416 );
417 $dom = $this->callAPISuccess('domain', 'create', $domparams);
418 $params = array( 'domain_id' => 'all',
6a488035 419 );
7fbb4198 420 $result = $this->callAPISuccess('setting', 'get', $params);
421 $params = array( 'address_format' => 'xyz',
6a488035
TO
422 'mailing_format' => 'bcs',
423 'domain_id' => $this->_domainID2,
424 );
7fbb4198 425 $result = $this->callAPISuccess('setting', 'create', $params);
426 $params = array( 'domain_id' => $dom['id'],
6a488035 427 );
7fbb4198 428 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035
TO
429 $this->assertAPISuccess($result, "in line " . __LINE__);
430 $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']],'setting for domain 3 should not be set. Debug this IF domain test is passing');
7fbb4198 431 $result = $this->callAPISuccess('setting', 'fill', $params);
6a488035 432 $this->assertAPISuccess($result, "in line " . __LINE__);
7fbb4198 433 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035
TO
434 $this->assertAPISuccess($result, "in line " . __LINE__);
435 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
f64cd471 436 $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
6a488035
TO
437 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
438 }
439}
440