2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 require_once 'include/SugarObjects/SugarConfig.php';
40 class SugarConfigTest extends Sugar_PHPUnit_Framework_TestCase
42 private $_old_sugar_config = null;
44 public function setUp()
46 $this->_old_sugar_config = $GLOBALS['sugar_config'];
47 $GLOBALS['sugar_config'] = array();
50 public function tearDown()
52 $config = SugarConfig::getInstance();
53 $config->clearCache();
54 $GLOBALS['sugar_config'] = $this->_old_sugar_config;
58 * Stores a key/value pair in the config
60 * @internal override this in sub-classes if you are testing with the
61 * config data stored somewhere other than the $sugar_config
64 * @param string $value
66 private function _addKeyValueToConfig(
71 $GLOBALS['sugar_config'][$key] = $value;
74 private function _generateRandomValue()
76 $this->_random = 'Some Random Foobar: ' . rand(10000, 20000);
77 return $this->_getLastRandomValue();
80 private function _getLastRandomValue()
82 return $this->_random;
85 public function testGetInstanceReturnsASugarConfigObject()
87 $this->assertTrue(SugarConfig::getInstance() instanceOf SugarConfig, 'Returned object is not a SugarConfig object');
90 public function testGetInstanceReturnsASingleton()
92 $one = SugarConfig::getInstance();
93 $two = SugarConfig::getInstance();
94 $this->assertSame($one, $two);
97 public function testReadsGlobalSugarConfigArray()
99 for ($i = 0; $i < 10; $i++) {
100 $anonymous_key = 'key-' . $i;
101 $random_value = rand(10000, 20000);
102 $rawConfigArray[$anonymous_key] = $random_value;
103 $this->_addKeyValueToConfig($anonymous_key, $random_value);
106 $config = SugarConfig::getInstance();
107 foreach ($rawConfigArray as $key => $value) {
109 $config->get($key), $value,
110 "SugarConfig::get({$key}) should be equal to {$value}, got " . $config->get($key)
115 public function testAllowDotNotationForSubValuesWithinTheConfig()
117 $random_value = 'Some Random Integer: ' . rand(1000, 2000);
118 $this->_addKeyValueToConfig('grandparent', array(
120 'child' => $random_value,
124 $config = SugarConfig::getInstance();
125 $this->assertEquals($random_value, $config->get('grandparent.parent.child'));
128 public function testReturnsNullOnUnknownKey()
130 $config = SugarConfig::getInstance();
131 $this->assertNull($config->get('unknown-and-unknowable'));
134 public function testReturnsNullOnUnknownKeyWithinAHeirarchy()
136 $this->_addKeyValueToConfig('grandparent', array(
141 $config= SugarConfig::getInstance();
143 $this->assertNull($config->get('some-unknown-grandparent.parent.child'));
144 $this->assertNull($config->get('grandparent.some-unknown-parent.child'));
145 $this->assertNull($config->get('grandparent.parent.some-unknown-child'));
148 public function testAllowSpecifyingDefault()
150 $config = SugarConfig::getInstance();
152 $random = rand(10000, 20000);
153 $this->assertSame($random, $config->get('unknown-and-unknowable', $random));
156 public function testAllowSpecifyingDefaultForSubValues()
158 $this->_addKeyValueToConfig('grandparent', array(
163 $config = SugarConfig::getInstance();
166 $this->_generateRandomValue(),
168 'some-unknown-grandparent.parent.child',
169 $this->_getLastRandomValue()
173 $this->_generateRandomValue(),
175 'grandparent.some-unknown-parent.child',
176 $this->_getLastRandomValue()
180 $this->_generateRandomValue(),
182 'grandparent.parent.some-unknown-child',
183 $this->_getLastRandomValue()
188 public function testStoresValuesInMemoryAfterFirstLookup()
190 $this->_addKeyValueToConfig('foobar', 'barfoo');
192 $config = SugarConfig::getInstance();
193 $this->assertEquals($config->get('foobar'), 'barfoo');
195 $this->_addKeyValueToConfig('foobar', 'foobar');
196 $this->assertEquals($config->get('foobar'), 'barfoo', 'should still be equal "barfoo": got ' . $config->get('foobar'));
199 public function testCanClearsCachedValues()
201 $this->_addKeyValueToConfig('foobar', 'barfoo');
203 $config = SugarConfig::getInstance();
204 $this->assertEquals($config->get('foobar'), 'barfoo', 'sanity check');
205 $this->_addKeyValueToConfig('foobar', 'foobar');
206 $this->assertEquals($config->get('foobar'), 'barfoo', 'sanity check');
208 $config->clearCache();
209 $this->assertEquals($config->get('foobar'), 'foobar', 'after clearCache() call, new value should be used');
212 public function testCanCherryPickKeyToClear()
214 $this->_addKeyValueToConfig('foobar', 'barfoo');
215 $this->_addKeyValueToConfig('barfoo', 'barfoo');
217 $config = SugarConfig::getInstance();
218 $this->assertEquals($config->get('foobar'), 'barfoo', 'sanity check, got: ' . $config->get('foobar'));
219 $this->assertEquals($config->get('barfoo'), 'barfoo', 'sanity check');
221 $this->_addKeyValueToConfig('foobar', 'foobar');
222 $this->_addKeyValueToConfig('barfoo', 'foobar');
223 $this->assertEquals($config->get('foobar'), 'barfoo', 'should still be equal to "barfoo", got: ' . $config->get('barfoo'));
224 $this->assertEquals($config->get('barfoo'), 'barfoo', 'should still be equal to "barfoo", got: ' . $config->get('barfoo'));
226 $config->clearCache('barfoo');
227 $this->assertEquals($config->get('barfoo'), 'foobar', 'should be equal to "foobar" after cherry picked for clearing');
228 $this->assertEquals($config->get('foobar'), 'barfoo', 'should not be effected by cherry picked clearCache() call');
231 public function testDemonstrateGrabbingSiblingNodes()
233 $this->_addKeyValueToConfig('foobar', array(
244 $config = SugarConfig::getInstance();
245 $this->assertEquals($config->get('foobar.foo.0.first'), 'one');
246 $this->assertEquals($config->get('foobar.foo.1.first'), 'uno');