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