2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 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']);
101 global $sugar_version, $sugar_db_version, $sugar_flavor, $sugar_build, $sugar_timestamp;
102 require('sugar_version.php');
105 public function testSetupPrint()
107 $_GET['foo'] = 'bar';
108 $_POST['dog'] = 'cat';
109 $this->_app->setupPrint();
110 $this->assertEquals($GLOBALS['request_string'],
111 'foo=bar&dog=cat&print=true'
118 public function testSetupPrintWithMultidimensionalArray()
120 $_GET['foo'] = array(
124 '1' => 'notMultidemensional',
125 '2' => 'notMultidemensional',
127 $_POST['dog'] = 'cat';
128 $this->_app->setupPrint();
129 $this->assertEquals('foo[1]=notMultidemensional&foo[2]=notMultidemensional&dog=cat&print=true', $GLOBALS['request_string']
133 public function testLoadDisplaySettingsDefault()
137 $this->_app->loadDisplaySettings();
139 $this->assertEquals($GLOBALS['theme'],
140 $GLOBALS['sugar_config']['default_theme']);
142 $this->_removeUser();
145 public function testLoadDisplaySettingsAuthUserTheme()
149 $_SESSION['authenticated_user_theme'] = 'Sugar';
151 $this->_app->loadDisplaySettings();
153 $this->assertEquals($GLOBALS['theme'],
154 $_SESSION['authenticated_user_theme']);
156 $this->_removeUser();
159 public function testLoadDisplaySettingsUserTheme()
162 $_REQUEST['usertheme'] = (string) SugarThemeRegistry::getDefault();
164 $this->_app->loadDisplaySettings();
166 $this->assertEquals($GLOBALS['theme'],
167 $_REQUEST['usertheme']);
169 $this->_removeUser();
172 public function testLoadGlobals()
174 $this->_app->controller =
175 ControllerFactory::getController($this->_app->default_module);
176 $this->_app->loadGlobals();
178 $this->assertEquals($GLOBALS['currentModule'],$this->_app->default_module);
179 $this->assertEquals($_REQUEST['module'],$this->_app->default_module);
180 $this->assertEquals($_REQUEST['action'],$this->_app->default_action);
186 public function testCheckDatabaseVersion()
188 if ( isset($GLOBALS['sugar_db_version']) )
189 $old_sugar_db_version = $GLOBALS['sugar_db_version'];
190 if ( isset($GLOBALS['sugar_version']) )
191 $old_sugar_version = $GLOBALS['sugar_version'];
192 include 'sugar_version.php';
193 $GLOBALS['sugar_version'] = $sugar_version;
195 // first test a valid value
196 $GLOBALS['sugar_db_version'] = $sugar_db_version;
197 $this->assertTrue($this->_app->checkDatabaseVersion(false));
199 $GLOBALS['sugar_db_version'] = '1.1.1';
200 // then test to see if we pull against the cache the valid value
201 $this->assertTrue($this->_app->checkDatabaseVersion(false));
203 // now retest to be sure we actually do the check again
204 sugar_cache_put('checkDatabaseVersion_row_count', 0);
205 $this->assertFalse($this->_app->checkDatabaseVersion(false));
207 if ( isset($old_sugar_db_version) )
208 $GLOBALS['sugar_db_version'] = $old_sugar_db_version;
209 if ( isset($old_sugar_version) )
210 $GLOBALS['sugar_version'] = $old_sugar_version;
213 public function testLoadLanguages()
215 $this->_app->controller->module = 'Contacts';
216 $this->_app->loadLanguages();
217 //since there is a logged in user, the welcome screen should not be empty
218 $this->assertEmpty($GLOBALS['app_strings']['NTC_WELCOME'], 'Testing that Welcome message is not empty');
219 $this->assertNotEmpty($GLOBALS['app_strings'], "App Strings is not empty.");
220 $this->assertNotEmpty($GLOBALS['app_list_strings'], "App List Strings is not empty.");
221 $this->assertNotEmpty($GLOBALS['mod_strings'], "Mod Strings is not empty.");
224 public function testCheckHTTPRefererReturnsTrueIfRefererNotSet()
226 $_SERVER['HTTP_REFERER'] = '';
227 $_SERVER['SERVER_NAME'] = 'dog';
228 $this->_app->controller->action = 'index';
230 $this->assertTrue($this->_app->checkHTTPReferer());
236 public function testCheckHTTPRefererReturnsTrueIfRefererIsLocalhost()
238 $_SERVER['HTTP_REFERER'] = 'http://localhost';
239 $_SERVER['SERVER_NAME'] = 'localhost';
240 $this->_app->controller->action = 'poo';
242 $this->assertTrue($this->_app->checkHTTPReferer());
244 $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
245 $_SERVER['SERVER_NAME'] = 'localhost';
246 $this->_app->controller->action = 'poo';
248 $this->assertTrue($this->_app->checkHTTPReferer());
250 $_SERVER['HTTP_REFERER'] = 'http://localhost';
251 $_SERVER['SERVER_NAME'] = '127.0.0.1';
252 $this->_app->controller->action = 'poo';
254 $this->assertTrue($this->_app->checkHTTPReferer());
256 $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
257 $_SERVER['SERVER_NAME'] = '127.0.0.1';
258 $this->_app->controller->action = 'poo';
260 $this->assertTrue($this->_app->checkHTTPReferer());
263 public function testCheckHTTPRefererReturnsTrueIfRefererIsServerName()
265 $_SERVER['HTTP_REFERER'] = 'http://dog';
266 $_SERVER['SERVER_NAME'] = 'dog';
267 $this->_app->controller->action = 'index';
269 $this->assertTrue($this->_app->checkHTTPReferer());
272 public function testCheckHTTPRefererReturnsTrueIfRefererIsInWhitelist()
274 $_SERVER['HTTP_REFERER'] = 'http://dog';
275 $_SERVER['SERVER_NAME'] = 'cat';
276 $this->_app->controller->action = 'index';
278 $GLOBALS['sugar_config']['http_referer']['list'][] = 'http://dog';
280 $this->assertTrue($this->_app->checkHTTPReferer());
283 public function testCheckHTTPRefererReturnsFalseIfRefererIsNotInWhitelist()
285 $_SERVER['HTTP_REFERER'] = 'http://dog';
286 $_SERVER['SERVER_NAME'] = 'cat';
287 $this->_app->controller->action = 'poo';
289 $GLOBALS['sugar_config']['http_referer']['list'] = array();
291 $this->assertFalse($this->_app->checkHTTPReferer());
294 public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIs()
296 $_SERVER['HTTP_REFERER'] = 'http://dog';
297 $_SERVER['SERVER_NAME'] = 'cat';
298 $this->_app->controller->action = 'index';
300 $this->assertTrue($this->_app->checkHTTPReferer());
303 public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIsInConfig()
305 $_SERVER['HTTP_REFERER'] = 'http://dog';
306 $_SERVER['SERVER_NAME'] = 'cat';
307 $this->_app->controller->action = 'poo';
309 $GLOBALS['sugar_config']['http_referer']['actions'][] = 'poo';
310 $this->assertTrue($this->_app->checkHTTPReferer());
316 public function testWhitelistDefaults()
318 $_SERVER['HTTP_REFERER'] = 'http://dog';
319 $_SERVER['SERVER_NAME'] = 'cat';
320 $GLOBALS['sugar_config']['http_referer']['actions'] = array('poo');
321 $this->_app->controller->action = 'oauth';
322 $this->assertTrue($this->_app->checkHTTPReferer());
323 $this->_app->controller->action = 'index';
324 $this->assertTrue($this->_app->checkHTTPReferer());
325 $this->_app->controller->action = 'save';
326 $this->assertFalse($this->_app->checkHTTPReferer());
330 class SugarApplicationMock extends SugarApplication
332 public function __construct()
334 parent::SugarApplication();
335 $this->controller = new stdClass();
338 public function checkHTTPReferer()
340 return parent::checkHTTPReferer(false);