]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarObjects/SugarConfigTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / include / SugarObjects / SugarConfigTest.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37  
38 require_once 'include/SugarObjects/SugarConfig.php';
39
40 class SugarConfigTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42     private $_old_sugar_config = null;
43
44     public function setUp() 
45     {
46         $this->_old_sugar_config = $GLOBALS['sugar_config'];
47         $GLOBALS['sugar_config'] = array();
48     }
49
50     public function tearDown() 
51     {
52         $config = SugarConfig::getInstance();
53         $config->clearCache();
54         $GLOBALS['sugar_config'] = $this->_old_sugar_config;
55     }
56
57     /**
58      * Stores a key/value pair in the config
59      *
60      * @internal override this in sub-classes if you are testing with the
61      *           config data stored somewhere other than the $sugar_config
62      *           super global
63      * @param string $key
64      * @param string $value
65      */
66     private function _addKeyValueToConfig(
67         $key, 
68         $value
69         ) 
70     {
71         $GLOBALS['sugar_config'][$key] = $value;
72     }
73
74     private function _generateRandomValue() 
75     {
76         $this->_random = 'Some Random Foobar: ' . rand(10000, 20000);
77         return $this->_getLastRandomValue();
78     }
79
80     private function _getLastRandomValue() 
81     {
82         return $this->_random;
83     }
84
85     public function testGetInstanceReturnsASugarConfigObject() 
86     {
87         $this->assertTrue(SugarConfig::getInstance() instanceOf SugarConfig, 'Returned object is not a SugarConfig object');
88     }
89
90     public function testGetInstanceReturnsASingleton() 
91     {
92         $one = SugarConfig::getInstance();
93         $two = SugarConfig::getInstance();
94         $this->assertSame($one, $two);
95     }
96
97     public function testReadsGlobalSugarConfigArray() 
98     {
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);
104         }
105
106         $config = SugarConfig::getInstance();
107         foreach ($rawConfigArray as $key => $value) {
108             $this->assertEquals(
109                 $config->get($key), $value,
110                 "SugarConfig::get({$key}) should be equal to {$value}, got " . $config->get($key)
111             );
112         }
113     }
114
115     public function testAllowDotNotationForSubValuesWithinTheConfig() 
116     {
117         $random_value = 'Some Random Integer: ' . rand(1000, 2000);
118         $this->_addKeyValueToConfig('grandparent', array(
119                 'parent' => array(
120                 'child' => $random_value,
121             ),
122         ));
123
124         $config = SugarConfig::getInstance();
125         $this->assertEquals($random_value, $config->get('grandparent.parent.child'));
126     }
127
128     public function testReturnsNullOnUnknownKey() 
129     {
130         $config = SugarConfig::getInstance();
131         $this->assertNull($config->get('unknown-and-unknowable'));
132     }
133
134     public function testReturnsNullOnUnknownKeyWithinAHeirarchy() 
135     {
136         $this->_addKeyValueToConfig('grandparent', array(
137             'parent' => array(
138                 'child' => 'foobar',
139             ),
140         ));
141         $config= SugarConfig::getInstance();
142
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'));
146     }
147
148     public function testAllowSpecifyingDefault() 
149     {
150         $config = SugarConfig::getInstance();
151
152         $random = rand(10000, 20000);
153         $this->assertSame($random, $config->get('unknown-and-unknowable', $random));
154     }
155
156     public function testAllowSpecifyingDefaultForSubValues() 
157     {
158         $this->_addKeyValueToConfig('grandparent', array(
159             'parent' => array(
160                 'child' => 'foobar',
161             ),
162         ));
163         $config = SugarConfig::getInstance();
164
165         $this->assertEquals(
166             $this->_generateRandomValue(),
167             $config->get(
168                 'some-unknown-grandparent.parent.child',
169                 $this->_getLastRandomValue()
170             )
171         );
172         $this->assertEquals(
173             $this->_generateRandomValue(),
174             $config->get(
175                 'grandparent.some-unknown-parent.child',
176                 $this->_getLastRandomValue()
177             )
178         );
179         $this->assertEquals(
180             $this->_generateRandomValue(),
181             $config->get(
182                 'grandparent.parent.some-unknown-child',
183                 $this->_getLastRandomValue()
184             )
185         );
186     }
187
188     public function testStoresValuesInMemoryAfterFirstLookup() 
189     {
190         $this->_addKeyValueToConfig('foobar', 'barfoo');
191
192         $config = SugarConfig::getInstance();
193         $this->assertEquals($config->get('foobar'), 'barfoo');
194
195         $this->_addKeyValueToConfig('foobar', 'foobar');
196         $this->assertEquals($config->get('foobar'), 'barfoo', 'should still be equal "barfoo": got ' . $config->get('foobar'));
197     }
198
199     public function testCanClearsCachedValues() 
200     {
201         $this->_addKeyValueToConfig('foobar', 'barfoo');
202
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');
207
208         $config->clearCache();
209         $this->assertEquals($config->get('foobar'), 'foobar', 'after clearCache() call, new value should be used');
210     }
211
212     public function testCanCherryPickKeyToClear() 
213     {
214         $this->_addKeyValueToConfig('foobar', 'barfoo');
215         $this->_addKeyValueToConfig('barfoo', 'barfoo');
216
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');
220
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'));
225
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');
229     }
230
231     public function testDemonstrateGrabbingSiblingNodes() 
232     {
233         $this->_addKeyValueToConfig('foobar', array(
234             'foo' => array(
235                 array(
236                     'first' => 'one',
237                 ),
238                 array(
239                     'first' => 'uno',
240                 ),
241             ),
242         ));
243
244         $config = SugarConfig::getInstance();
245         $this->assertEquals($config->get('foobar.foo.0.first'), 'one');
246         $this->assertEquals($config->get('foobar.foo.1.first'), 'uno');
247     }
248 }
249