]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/MVC/SugarApplicationTest.php
Release 6.4.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         if ( isset($GLOBALS['sugar_config']['http_referer']) ) {
57             $this->prevRefererList = $GLOBALS['sugar_config']['http_referer'];
58         }
59
60         $GLOBALS['sugar_config']['http_referer'] = array('list' => array(), 'actions' => array());
61     }
62
63     private function _loadUser()
64     {
65         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
66         $_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['gridline'] = 'on';
67     }
68
69     private function _removeUser()
70     {
71         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
72         unset($GLOBALS['current_user']);
73     }
74
75
76     public function tearDown()
77     {
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'];
94
95         if ( isset($this->prevRefererList)) {
96             $GLOBALS['sugar_config']['http_referer'] = $this->prevRefererList;
97         } else {
98             unset ($GLOBALS['sugar_config']['http_referer']);
99         }
100     }
101
102     public function testSetupPrint()
103     {
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'
109         );
110     }
111
112     /*
113      * @ticket 40277
114      */
115     public function testSetupPrintWithMultidimensionalArray()
116     {
117         $_GET['foo'] = array(
118                             '0' => array(
119                                    '0'=>'bar',
120                                    'a' => 'hej'),
121                             '1' => 'notMultidemensional',
122                             '2' => 'notMultidemensional',
123                            );
124         $_POST['dog'] = 'cat';
125         $this->_app->setupPrint();
126         $this->assertEquals('foo[1]=notMultidemensional&foo[2]=notMultidemensional&dog=cat&print=true', $GLOBALS['request_string']
127         );
128     }
129
130     public function testLoadDisplaySettingsDefault()
131     {
132         $this->_loadUser();
133
134         $this->_app->loadDisplaySettings();
135
136         $this->assertEquals($GLOBALS['theme'],
137             $GLOBALS['sugar_config']['default_theme']);
138
139         $this->_removeUser();
140     }
141
142     public function testLoadDisplaySettingsAuthUserTheme()
143     {
144         $this->_loadUser();
145
146         $_SESSION['authenticated_user_theme'] = 'Sugar';
147
148         $this->_app->loadDisplaySettings();
149
150         $this->assertEquals($GLOBALS['theme'],
151             $_SESSION['authenticated_user_theme']);
152
153         $this->_removeUser();
154     }
155
156     public function testLoadDisplaySettingsUserTheme()
157     {
158         $this->_loadUser();
159         $_REQUEST['usertheme'] = (string) SugarThemeRegistry::getDefault();
160
161         $this->_app->loadDisplaySettings();
162
163         $this->assertEquals($GLOBALS['theme'],
164             $_REQUEST['usertheme']);
165
166         $this->_removeUser();
167     }
168
169     public function testLoadGlobals()
170     {
171         $this->_app->controller =
172             ControllerFactory::getController($this->_app->default_module);
173         $this->_app->loadGlobals();
174
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);
178     }
179
180     /**
181      * @ticket 33283
182      */
183     public function testCheckDatabaseVersion()
184     {
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;
191
192         // first test a valid value
193         $GLOBALS['sugar_db_version'] = $sugar_db_version;
194         $this->assertTrue($this->_app->checkDatabaseVersion(false));
195
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));
199
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));
203
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;
208     }
209
210     public function testLoadLanguages()
211     {
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.");
219     }
220
221     public function testCheckHTTPRefererReturnsTrueIfRefererNotSet()
222     {
223         $_SERVER['HTTP_REFERER'] = '';
224         $_SERVER['SERVER_NAME'] = 'dog';
225         $this->_app->controller->action = 'index';
226
227         $this->assertTrue($this->_app->checkHTTPReferer());
228     }
229
230     /**
231      * @ticket 39691
232      */
233     public function testCheckHTTPRefererReturnsTrueIfRefererIsLocalhost()
234     {
235         $_SERVER['HTTP_REFERER'] = 'http://localhost';
236         $_SERVER['SERVER_NAME'] = 'localhost';
237         $this->_app->controller->action = 'poo';
238
239         $this->assertTrue($this->_app->checkHTTPReferer());
240
241         $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
242         $_SERVER['SERVER_NAME'] = 'localhost';
243         $this->_app->controller->action = 'poo';
244
245         $this->assertTrue($this->_app->checkHTTPReferer());
246
247         $_SERVER['HTTP_REFERER'] = 'http://localhost';
248         $_SERVER['SERVER_NAME'] = '127.0.0.1';
249         $this->_app->controller->action = 'poo';
250
251         $this->assertTrue($this->_app->checkHTTPReferer());
252
253         $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
254         $_SERVER['SERVER_NAME'] = '127.0.0.1';
255         $this->_app->controller->action = 'poo';
256
257         $this->assertTrue($this->_app->checkHTTPReferer());
258     }
259
260     public function testCheckHTTPRefererReturnsTrueIfRefererIsServerName()
261     {
262         $_SERVER['HTTP_REFERER'] = 'http://dog';
263         $_SERVER['SERVER_NAME'] = 'dog';
264         $this->_app->controller->action = 'index';
265
266         $this->assertTrue($this->_app->checkHTTPReferer());
267     }
268
269     public function testCheckHTTPRefererReturnsTrueIfRefererIsInWhitelist()
270     {
271         $_SERVER['HTTP_REFERER'] = 'http://dog';
272         $_SERVER['SERVER_NAME'] = 'cat';
273         $this->_app->controller->action = 'index';
274
275         $GLOBALS['sugar_config']['http_referer']['list'][] = 'http://dog';
276
277         $this->assertTrue($this->_app->checkHTTPReferer());
278     }
279
280     public function testCheckHTTPRefererReturnsFalseIfRefererIsNotInWhitelist()
281     {
282         $_SERVER['HTTP_REFERER'] = 'http://dog';
283         $_SERVER['SERVER_NAME'] = 'cat';
284         $this->_app->controller->action = 'poo';
285
286         $GLOBALS['sugar_config']['http_referer']['list'] = array();
287
288         $this->assertFalse($this->_app->checkHTTPReferer());
289     }
290
291     public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIs()
292     {
293         $_SERVER['HTTP_REFERER'] = 'http://dog';
294         $_SERVER['SERVER_NAME'] = 'cat';
295         $this->_app->controller->action = 'index';
296
297         $this->assertTrue($this->_app->checkHTTPReferer());
298     }
299
300     public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIsInConfig()
301     {
302         $_SERVER['HTTP_REFERER'] = 'http://dog';
303         $_SERVER['SERVER_NAME'] = 'cat';
304         $this->_app->controller->action = 'poo';
305
306         $GLOBALS['sugar_config']['http_referer']['actions'][] = 'poo';
307         $this->assertTrue($this->_app->checkHTTPReferer());
308     }
309 }
310
311 class SugarApplicationMock extends SugarApplication
312 {
313     public function __construct()
314     {
315         parent::SugarApplication();
316         $this->controller = new stdClass();
317     }
318
319     public function checkHTTPReferer()
320         {
321             return parent::checkHTTPReferer(false);
322         }
323 }