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']);
56 if ( isset($GLOBALS['sugar_config']['http_referer']) ) {
57 $this->prevRefererList = $GLOBALS['sugar_config']['http_referer'];
60 $GLOBALS['sugar_config']['http_referer'] = array('list' => array(), 'actions' => array());
63 private function _loadUser()
65 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
66 $_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['gridline'] = 'on';
69 private function _removeUser()
71 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
72 unset($GLOBALS['current_user']);
76 public function tearDown()
78 unset($GLOBALS['current_user']);
79 unset($GLOBALS['moduleList']);
80 unset($GLOBALS['request_string']);
81 unset($GLOBALS['adminOnlyList']);
82 unset($GLOBALS['modListHeader']);
83 unset($GLOBALS['modInvisList']);
84 unset($GLOBALS['app_strings']);
85 unset($GLOBALS['system_config']);
86 unset($GLOBALS['app_list_strings']);
87 unset($GLOBALS['mod_strings']);
88 unset($GLOBALS['theme']);
89 unset($GLOBALS['image_path']);
90 unset($GLOBALS['starttTime']);
91 unset($GLOBALS['sugar_version']);
92 unset($GLOBALS['sugar_flavor']);
93 $GLOBALS['current_language'] = $GLOBALS['sugar_config']['default_language'];
95 if ( isset($this->prevRefererList)) {
96 $GLOBALS['sugar_config']['http_referer'] = $this->prevRefererList;
98 unset ($GLOBALS['sugar_config']['http_referer']);
102 public function testSetupPrint()
104 $_GET['foo'] = 'bar';
105 $_POST['dog'] = 'cat';
106 $this->_app->setupPrint();
107 $this->assertEquals($GLOBALS['request_string'],
108 'foo=bar&dog=cat&print=true'
115 public function testSetupPrintWithMultidimensionalArray()
117 $_GET['foo'] = array(
121 '1' => 'notMultidemensional',
122 '2' => 'notMultidemensional',
124 $_POST['dog'] = 'cat';
125 $this->_app->setupPrint();
126 $this->assertEquals('foo[1]=notMultidemensional&foo[2]=notMultidemensional&dog=cat&print=true', $GLOBALS['request_string']
130 public function testLoadDisplaySettingsDefault()
134 $this->_app->loadDisplaySettings();
136 $this->assertEquals($GLOBALS['theme'],
137 $GLOBALS['sugar_config']['default_theme']);
139 $this->_removeUser();
142 public function testLoadDisplaySettingsAuthUserTheme()
146 $_SESSION['authenticated_user_theme'] = 'Sugar';
148 $this->_app->loadDisplaySettings();
150 $this->assertEquals($GLOBALS['theme'],
151 $_SESSION['authenticated_user_theme']);
153 $this->_removeUser();
156 public function testLoadDisplaySettingsUserTheme()
159 $_REQUEST['usertheme'] = (string) SugarThemeRegistry::getDefault();
161 $this->_app->loadDisplaySettings();
163 $this->assertEquals($GLOBALS['theme'],
164 $_REQUEST['usertheme']);
166 $this->_removeUser();
169 public function testLoadGlobals()
171 $this->_app->controller =
172 ControllerFactory::getController($this->_app->default_module);
173 $this->_app->loadGlobals();
175 $this->assertEquals($GLOBALS['currentModule'],$this->_app->default_module);
176 $this->assertEquals($_REQUEST['module'],$this->_app->default_module);
177 $this->assertEquals($_REQUEST['action'],$this->_app->default_action);
183 public function testCheckDatabaseVersion()
185 if ( isset($GLOBALS['sugar_db_version']) )
186 $old_sugar_db_version = $GLOBALS['sugar_db_version'];
187 if ( isset($GLOBALS['sugar_version']) )
188 $old_sugar_version = $GLOBALS['sugar_version'];
189 include 'sugar_version.php';
190 $GLOBALS['sugar_version'] = $sugar_version;
192 // first test a valid value
193 $GLOBALS['sugar_db_version'] = $sugar_db_version;
194 $this->assertTrue($this->_app->checkDatabaseVersion(false));
196 $GLOBALS['sugar_db_version'] = '1.1.1';
197 // then test to see if we pull against the cache the valid value
198 $this->assertTrue($this->_app->checkDatabaseVersion(false));
200 // now retest to be sure we actually do the check again
201 sugar_cache_put('checkDatabaseVersion_row_count', 0);
202 $this->assertFalse($this->_app->checkDatabaseVersion(false));
204 if ( isset($old_sugar_db_version) )
205 $GLOBALS['sugar_db_version'] = $old_sugar_db_version;
206 if ( isset($old_sugar_version) )
207 $GLOBALS['sugar_version'] = $old_sugar_version;
210 public function testLoadLanguages()
212 $this->_app->controller->module = 'Contacts';
213 $this->_app->loadLanguages();
214 //since there is a logged in user, the welcome screen should not be empty
215 $this->assertEmpty($GLOBALS['app_strings']['NTC_WELCOME'], 'Testing that Welcome message is not empty');
216 $this->assertNotEmpty($GLOBALS['app_strings'], "App Strings is not empty.");
217 $this->assertNotEmpty($GLOBALS['app_list_strings'], "App List Strings is not empty.");
218 $this->assertNotEmpty($GLOBALS['mod_strings'], "Mod Strings is not empty.");
221 public function testCheckHTTPRefererReturnsTrueIfRefererNotSet()
223 $_SERVER['HTTP_REFERER'] = '';
224 $_SERVER['SERVER_NAME'] = 'dog';
225 $this->_app->controller->action = 'index';
227 $this->assertTrue($this->_app->checkHTTPReferer());
233 public function testCheckHTTPRefererReturnsTrueIfRefererIsLocalhost()
235 $_SERVER['HTTP_REFERER'] = 'http://localhost';
236 $_SERVER['SERVER_NAME'] = 'localhost';
237 $this->_app->controller->action = 'poo';
239 $this->assertTrue($this->_app->checkHTTPReferer());
241 $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
242 $_SERVER['SERVER_NAME'] = 'localhost';
243 $this->_app->controller->action = 'poo';
245 $this->assertTrue($this->_app->checkHTTPReferer());
247 $_SERVER['HTTP_REFERER'] = 'http://localhost';
248 $_SERVER['SERVER_NAME'] = '127.0.0.1';
249 $this->_app->controller->action = 'poo';
251 $this->assertTrue($this->_app->checkHTTPReferer());
253 $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
254 $_SERVER['SERVER_NAME'] = '127.0.0.1';
255 $this->_app->controller->action = 'poo';
257 $this->assertTrue($this->_app->checkHTTPReferer());
260 public function testCheckHTTPRefererReturnsTrueIfRefererIsServerName()
262 $_SERVER['HTTP_REFERER'] = 'http://dog';
263 $_SERVER['SERVER_NAME'] = 'dog';
264 $this->_app->controller->action = 'index';
266 $this->assertTrue($this->_app->checkHTTPReferer());
269 public function testCheckHTTPRefererReturnsTrueIfRefererIsInWhitelist()
271 $_SERVER['HTTP_REFERER'] = 'http://dog';
272 $_SERVER['SERVER_NAME'] = 'cat';
273 $this->_app->controller->action = 'index';
275 $GLOBALS['sugar_config']['http_referer']['list'][] = 'http://dog';
277 $this->assertTrue($this->_app->checkHTTPReferer());
280 public function testCheckHTTPRefererReturnsFalseIfRefererIsNotInWhitelist()
282 $_SERVER['HTTP_REFERER'] = 'http://dog';
283 $_SERVER['SERVER_NAME'] = 'cat';
284 $this->_app->controller->action = 'poo';
286 $GLOBALS['sugar_config']['http_referer']['list'] = array();
288 $this->assertFalse($this->_app->checkHTTPReferer());
291 public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIs()
293 $_SERVER['HTTP_REFERER'] = 'http://dog';
294 $_SERVER['SERVER_NAME'] = 'cat';
295 $this->_app->controller->action = 'index';
297 $this->assertTrue($this->_app->checkHTTPReferer());
300 public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIsInConfig()
302 $_SERVER['HTTP_REFERER'] = 'http://dog';
303 $_SERVER['SERVER_NAME'] = 'cat';
304 $this->_app->controller->action = 'poo';
306 $GLOBALS['sugar_config']['http_referer']['actions'][] = 'poo';
307 $this->assertTrue($this->_app->checkHTTPReferer());
311 class SugarApplicationMock extends SugarApplication
313 public function __construct()
315 parent::SugarApplication();
316 $this->controller = new stdClass();
319 public function checkHTTPReferer()
321 return parent::checkHTTPReferer(false);