]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/MVC/SugarApplicationTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / include / MVC / SugarApplicationTest.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/MVC/SugarApplication.php';
39
40 class SugarApplicationTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42     private $_app;
43     // Run in isolation so that it doesn't mess up other tests
44     public $inIsolation = true;
45
46     public function setUp()
47     {
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']);
55     }
56
57     private function _loadUser()
58     {
59         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
60         $_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['gridline'] = 'on';
61     }
62
63     private function _removeUser()
64     {
65         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
66         unset($GLOBALS['current_user']);
67     }
68
69     public function tearDown()
70     {
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'];
87     }
88
89     public function testSetupPrint()
90     {
91         $_GET['foo'] = 'bar';
92         $_POST['dog'] = 'cat';
93         $this->_app->setupPrint();
94         $this->assertEquals($GLOBALS['request_string'],
95             'foo=bar&dog=cat&print=true'
96         );
97     }
98
99     /*
100      * @ticket 40277
101      */
102     public function testSetupPrintWithMultidimensionalArray()
103     {
104         $_GET['foo'] = array(
105                             '0' => array(
106                                    '0'=>'bar',
107                                    'a' => 'hej'),
108                             '1' => 'notMultidemensional',
109                             '2' => 'notMultidemensional',
110                            );
111         $_POST['dog'] = 'cat';
112         $this->_app->setupPrint();
113         $this->assertEquals('foo[1]=notMultidemensional&foo[2]=notMultidemensional&dog=cat&print=true', $GLOBALS['request_string']
114         );
115     }
116
117     public function testLoadDisplaySettingsDefault()
118     {
119         $this->_loadUser();
120
121         $this->_app->loadDisplaySettings();
122
123         $this->assertEquals($GLOBALS['theme'],
124             $GLOBALS['sugar_config']['default_theme']);
125
126         $this->_removeUser();
127     }
128
129     public function testLoadDisplaySettingsAuthUserTheme()
130     {
131         $this->_loadUser();
132
133         $_SESSION['authenticated_user_theme'] = 'Sugar';
134
135         $this->_app->loadDisplaySettings();
136
137         $this->assertEquals($GLOBALS['theme'],
138             $_SESSION['authenticated_user_theme']);
139
140         $this->_removeUser();
141     }
142
143     public function testLoadDisplaySettingsUserTheme()
144     {
145         $this->_loadUser();
146         $_REQUEST['usertheme'] = (string) SugarThemeRegistry::getDefault();
147         
148         $this->_app->loadDisplaySettings();
149
150         $this->assertEquals($GLOBALS['theme'],
151             $_REQUEST['usertheme']);
152
153         $this->_removeUser();
154     }
155
156     public function testLoadGlobals()
157     {
158         $this->_app->controller =
159             ControllerFactory::getController($this->_app->default_module);
160         $this->_app->loadGlobals();
161
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);
165     }
166
167     /**
168      * @ticket 33283
169      */
170     public function testCheckDatabaseVersion()
171     {
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;
178
179         // first test a valid value
180         $GLOBALS['sugar_db_version'] = $sugar_db_version;
181         $this->assertTrue($this->_app->checkDatabaseVersion(false));
182
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));
186
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));
190
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;
195     }
196
197     public function testLoadLanguages()
198     {
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.");
206     }
207     
208     public function testCheckHTTPRefererReturnsTrueIfRefererNotSet()
209     {
210         $_SERVER['HTTP_REFERER'] = '';
211         $_SERVER['SERVER_NAME'] = 'dog';
212         $this->_app->controller->action = 'index';
213         
214         $this->assertTrue($this->_app->checkHTTPReferer());
215     }
216     
217     /**
218      * @ticket 39691
219      */
220     public function testCheckHTTPRefererReturnsTrueIfRefererIsLocalhost()
221     {
222         $_SERVER['HTTP_REFERER'] = 'http://localhost';
223         $_SERVER['SERVER_NAME'] = 'localhost';
224         $this->_app->controller->action = 'poo';
225         
226         $this->assertTrue($this->_app->checkHTTPReferer());
227         
228         $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
229         $_SERVER['SERVER_NAME'] = 'localhost';
230         $this->_app->controller->action = 'poo';
231         
232         $this->assertTrue($this->_app->checkHTTPReferer());
233         
234         $_SERVER['HTTP_REFERER'] = 'http://localhost';
235         $_SERVER['SERVER_NAME'] = '127.0.0.1';
236         $this->_app->controller->action = 'poo';
237         
238         $this->assertTrue($this->_app->checkHTTPReferer());
239         
240         $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
241         $_SERVER['SERVER_NAME'] = '127.0.0.1';
242         $this->_app->controller->action = 'poo';
243         
244         $this->assertTrue($this->_app->checkHTTPReferer());
245     }
246     
247     public function testCheckHTTPRefererReturnsTrueIfRefererIsServerName()
248     {
249         $_SERVER['HTTP_REFERER'] = 'http://dog';
250         $_SERVER['SERVER_NAME'] = 'dog';
251         $this->_app->controller->action = 'index';
252         
253         $this->assertTrue($this->_app->checkHTTPReferer());
254     }
255     
256     public function testCheckHTTPRefererReturnsTrueIfRefererIsInWhitelist()
257     {
258         $_SERVER['HTTP_REFERER'] = 'http://dog';
259         $_SERVER['SERVER_NAME'] = 'cat';
260         $this->_app->controller->action = 'index';
261         
262         if ( !empty($GLOBALS['sugar_config']['http_referer']['list']) ) {
263             $prevRefererList = $GLOBALS['sugar_config']['http_referer']['list'];
264         }
265         $GLOBALS['sugar_config']['http_referer']['list'][] = 'http://dog';
266         
267         $this->assertTrue($this->_app->checkHTTPReferer());
268         
269         if ( isset($prevRefererList) ) {
270             $GLOBALS['sugar_config']['http_referer']['list'] = $prevRefererList;
271         }
272     }
273     
274     public function testCheckHTTPRefererReturnsFalseIfRefererIsNotInWhitelist()
275     {
276         $_SERVER['HTTP_REFERER'] = 'http://dog';
277         $_SERVER['SERVER_NAME'] = 'cat';
278         $this->_app->controller->action = 'poo';
279         
280         $this->assertFalse($this->_app->checkHTTPReferer());
281     }
282     
283     public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIs()
284     {
285         $_SERVER['HTTP_REFERER'] = 'http://dog';
286         $_SERVER['SERVER_NAME'] = 'cat';
287         $this->_app->controller->action = 'index';
288         
289         $this->assertTrue($this->_app->checkHTTPReferer());
290     }
291     
292     public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIsInConfig()
293     {
294         $_SERVER['HTTP_REFERER'] = 'http://dog';
295         $_SERVER['SERVER_NAME'] = 'cat';
296         $this->_app->controller->action = 'poo';
297         
298         if ( !empty($GLOBALS['sugar_config']['http_referer']['actions']) ) {
299             $prevRefererList = $GLOBALS['sugar_config']['http_referer']['actions'];
300         }
301         $GLOBALS['sugar_config']['http_referer']['actions'][] = 'poo';
302         
303         $this->assertTrue($this->_app->checkHTTPReferer());
304         
305         if ( isset($prevRefererList) ) {
306             $GLOBALS['sugar_config']['http_referer']['actions'] = $prevRefererList;
307         }
308     }
309 }
310
311 class SugarApplicationMock extends SugarApplication
312 {
313     public function checkHTTPReferer()
314         {
315             return parent::checkHTTPReferer(false);
316         }
317 }