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/MVC/SugarApplication.php';
40 class SugarApplicationTest extends Sugar_PHPUnit_Framework_TestCase
43 // Run in isolation so that it doesn't mess up other tests
44 public $inIsolation = true;
46 public function setUp()
48 $startTime = microtime();
49 $system_config = new Administration();
50 $system_config->retrieveSettings();
51 $GLOBALS['system_config'] = $system_config;
52 $this->_app = new SugarApplicationMock();
53 if ( isset($_SESSION['authenticated_user_theme']) )
54 unset($_SESSION['authenticated_user_theme']);
57 private function _loadUser()
59 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
60 $_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['gridline'] = 'on';
63 private function _removeUser()
65 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
66 unset($GLOBALS['current_user']);
69 public function tearDown()
71 unset($GLOBALS['current_user']);
72 unset($GLOBALS['moduleList']);
73 unset($GLOBALS['request_string']);
74 unset($GLOBALS['adminOnlyList']);
75 unset($GLOBALS['modListHeader']);
76 unset($GLOBALS['modInvisList']);
77 unset($GLOBALS['app_strings']);
78 unset($GLOBALS['system_config']);
79 unset($GLOBALS['app_list_strings']);
80 unset($GLOBALS['mod_strings']);
81 unset($GLOBALS['theme']);
82 unset($GLOBALS['image_path']);
83 unset($GLOBALS['starttTime']);
84 unset($GLOBALS['sugar_version']);
85 unset($GLOBALS['sugar_flavor']);
86 $GLOBALS['current_language'] = $GLOBALS['sugar_config']['default_language'];
89 public function testSetupPrint()
92 $_POST['dog'] = 'cat';
93 $this->_app->setupPrint();
94 $this->assertEquals($GLOBALS['request_string'],
95 'foo=bar&dog=cat&print=true'
102 public function testSetupPrintWithMultidimensionalArray()
104 $_GET['foo'] = array(
108 '1' => 'notMultidemensional',
109 '2' => 'notMultidemensional',
111 $_POST['dog'] = 'cat';
112 $this->_app->setupPrint();
113 $this->assertEquals('foo[1]=notMultidemensional&foo[2]=notMultidemensional&dog=cat&print=true', $GLOBALS['request_string']
117 public function testLoadDisplaySettingsDefault()
121 $this->_app->loadDisplaySettings();
123 $this->assertEquals($GLOBALS['theme'],
124 $GLOBALS['sugar_config']['default_theme']);
126 $this->_removeUser();
129 public function testLoadDisplaySettingsAuthUserTheme()
133 $_SESSION['authenticated_user_theme'] = 'Sugar';
135 $this->_app->loadDisplaySettings();
137 $this->assertEquals($GLOBALS['theme'],
138 $_SESSION['authenticated_user_theme']);
140 $this->_removeUser();
143 public function testLoadDisplaySettingsUserTheme()
146 $_REQUEST['usertheme'] = (string) SugarThemeRegistry::getDefault();
148 $this->_app->loadDisplaySettings();
150 $this->assertEquals($GLOBALS['theme'],
151 $_REQUEST['usertheme']);
153 $this->_removeUser();
156 public function testLoadGlobals()
158 $this->_app->controller =
159 ControllerFactory::getController($this->_app->default_module);
160 $this->_app->loadGlobals();
162 $this->assertEquals($GLOBALS['currentModule'],$this->_app->default_module);
163 $this->assertEquals($_REQUEST['module'],$this->_app->default_module);
164 $this->assertEquals($_REQUEST['action'],$this->_app->default_action);
170 public function testCheckDatabaseVersion()
172 if ( isset($GLOBALS['sugar_db_version']) )
173 $old_sugar_db_version = $GLOBALS['sugar_db_version'];
174 if ( isset($GLOBALS['sugar_version']) )
175 $old_sugar_version = $GLOBALS['sugar_version'];
176 include 'sugar_version.php';
177 $GLOBALS['sugar_version'] = $sugar_version;
179 // first test a valid value
180 $GLOBALS['sugar_db_version'] = $sugar_db_version;
181 $this->assertTrue($this->_app->checkDatabaseVersion(false));
183 $GLOBALS['sugar_db_version'] = '1.1.1';
184 // then test to see if we pull against the cache the valid value
185 $this->assertTrue($this->_app->checkDatabaseVersion(false));
187 // now retest to be sure we actually do the check again
188 sugar_cache_put('checkDatabaseVersion_row_count', 0);
189 $this->assertFalse($this->_app->checkDatabaseVersion(false));
191 if ( isset($old_sugar_db_version) )
192 $GLOBALS['sugar_db_version'] = $old_sugar_db_version;
193 if ( isset($old_sugar_version) )
194 $GLOBALS['sugar_version'] = $old_sugar_version;
197 public function testLoadLanguages()
199 $this->_app->controller->module = 'Contacts';
200 $this->_app->loadLanguages();
201 //since there is a logged in user, the welcome screen should not be empty
202 $this->assertEmpty($GLOBALS['app_strings']['NTC_WELCOME'], 'Testing that Welcome message is not empty');
203 $this->assertNotEmpty($GLOBALS['app_strings'], "App Strings is not empty.");
204 $this->assertNotEmpty($GLOBALS['app_list_strings'], "App List Strings is not empty.");
205 $this->assertNotEmpty($GLOBALS['mod_strings'], "Mod Strings is not empty.");
208 public function testCheckHTTPRefererReturnsTrueIfRefererNotSet()
210 $_SERVER['HTTP_REFERER'] = '';
211 $_SERVER['SERVER_NAME'] = 'dog';
212 $this->_app->controller->action = 'index';
214 $this->assertTrue($this->_app->checkHTTPReferer());
220 public function testCheckHTTPRefererReturnsTrueIfRefererIsLocalhost()
222 $_SERVER['HTTP_REFERER'] = 'http://localhost';
223 $_SERVER['SERVER_NAME'] = 'localhost';
224 $this->_app->controller->action = 'poo';
226 $this->assertTrue($this->_app->checkHTTPReferer());
228 $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
229 $_SERVER['SERVER_NAME'] = 'localhost';
230 $this->_app->controller->action = 'poo';
232 $this->assertTrue($this->_app->checkHTTPReferer());
234 $_SERVER['HTTP_REFERER'] = 'http://localhost';
235 $_SERVER['SERVER_NAME'] = '127.0.0.1';
236 $this->_app->controller->action = 'poo';
238 $this->assertTrue($this->_app->checkHTTPReferer());
240 $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
241 $_SERVER['SERVER_NAME'] = '127.0.0.1';
242 $this->_app->controller->action = 'poo';
244 $this->assertTrue($this->_app->checkHTTPReferer());
247 public function testCheckHTTPRefererReturnsTrueIfRefererIsServerName()
249 $_SERVER['HTTP_REFERER'] = 'http://dog';
250 $_SERVER['SERVER_NAME'] = 'dog';
251 $this->_app->controller->action = 'index';
253 $this->assertTrue($this->_app->checkHTTPReferer());
256 public function testCheckHTTPRefererReturnsTrueIfRefererIsInWhitelist()
258 $_SERVER['HTTP_REFERER'] = 'http://dog';
259 $_SERVER['SERVER_NAME'] = 'cat';
260 $this->_app->controller->action = 'index';
262 if ( !empty($GLOBALS['sugar_config']['http_referer']['list']) ) {
263 $prevRefererList = $GLOBALS['sugar_config']['http_referer']['list'];
265 $GLOBALS['sugar_config']['http_referer']['list'][] = 'http://dog';
267 $this->assertTrue($this->_app->checkHTTPReferer());
269 if ( isset($prevRefererList) ) {
270 $GLOBALS['sugar_config']['http_referer']['list'] = $prevRefererList;
274 public function testCheckHTTPRefererReturnsFalseIfRefererIsNotInWhitelist()
276 $_SERVER['HTTP_REFERER'] = 'http://dog';
277 $_SERVER['SERVER_NAME'] = 'cat';
278 $this->_app->controller->action = 'poo';
280 $this->assertFalse($this->_app->checkHTTPReferer());
283 public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIs()
285 $_SERVER['HTTP_REFERER'] = 'http://dog';
286 $_SERVER['SERVER_NAME'] = 'cat';
287 $this->_app->controller->action = 'index';
289 $this->assertTrue($this->_app->checkHTTPReferer());
292 public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIsInConfig()
294 $_SERVER['HTTP_REFERER'] = 'http://dog';
295 $_SERVER['SERVER_NAME'] = 'cat';
296 $this->_app->controller->action = 'poo';
298 if ( !empty($GLOBALS['sugar_config']['http_referer']['actions']) ) {
299 $prevRefererList = $GLOBALS['sugar_config']['http_referer']['actions'];
301 $GLOBALS['sugar_config']['http_referer']['actions'][] = 'poo';
303 $this->assertTrue($this->_app->checkHTTPReferer());
305 if ( isset($prevRefererList) ) {
306 $GLOBALS['sugar_config']['http_referer']['actions'] = $prevRefererList;
311 class SugarApplicationMock extends SugarApplication
313 public function checkHTTPReferer()
315 return parent::checkHTTPReferer(false);