]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/MVC/SugarApplicationTest.php
Release 6.5.10
[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-2013 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         global $sugar_version, $sugar_db_version, $sugar_flavor, $sugar_build, $sugar_timestamp;
102         require('sugar_version.php');
103     }
104
105     public function testSetupPrint()
106     {
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'
112         );
113     }
114
115     /*
116      * @ticket 40277
117      */
118     public function testSetupPrintWithMultidimensionalArray()
119     {
120         $_GET['foo'] = array(
121                             '0' => array(
122                                    '0'=>'bar',
123                                    'a' => 'hej'),
124                             '1' => 'notMultidemensional',
125                             '2' => 'notMultidemensional',
126                            );
127         $_POST['dog'] = 'cat';
128         $this->_app->setupPrint();
129         $this->assertEquals('foo[1]=notMultidemensional&foo[2]=notMultidemensional&dog=cat&print=true', $GLOBALS['request_string']
130         );
131     }
132
133     public function testLoadDisplaySettingsDefault()
134     {
135         $this->_loadUser();
136
137         $this->_app->loadDisplaySettings();
138
139         $this->assertEquals($GLOBALS['theme'],
140             $GLOBALS['sugar_config']['default_theme']);
141
142         $this->_removeUser();
143     }
144
145     public function testLoadDisplaySettingsAuthUserTheme()
146     {
147         $this->_loadUser();
148
149         $_SESSION['authenticated_user_theme'] = 'Sugar';
150
151         $this->_app->loadDisplaySettings();
152
153         $this->assertEquals($GLOBALS['theme'],
154             $_SESSION['authenticated_user_theme']);
155
156         $this->_removeUser();
157     }
158
159     public function testLoadDisplaySettingsUserTheme()
160     {
161         $this->_loadUser();
162         $_REQUEST['usertheme'] = (string) SugarThemeRegistry::getDefault();
163
164         $this->_app->loadDisplaySettings();
165
166         $this->assertEquals($GLOBALS['theme'],
167             $_REQUEST['usertheme']);
168
169         $this->_removeUser();
170     }
171
172     public function testLoadGlobals()
173     {
174         $this->_app->controller =
175             ControllerFactory::getController($this->_app->default_module);
176         $this->_app->loadGlobals();
177
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);
181     }
182
183     /**
184      * @ticket 33283
185      */
186     public function testCheckDatabaseVersion()
187     {
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;
194
195         // first test a valid value
196         $GLOBALS['sugar_db_version'] = $sugar_db_version;
197         $this->assertTrue($this->_app->checkDatabaseVersion(false));
198
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));
202
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));
206
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;
211     }
212
213     public function testLoadLanguages()
214     {
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.");
222     }
223
224     public function testCheckHTTPRefererReturnsTrueIfRefererNotSet()
225     {
226         $_SERVER['HTTP_REFERER'] = '';
227         $_SERVER['SERVER_NAME'] = 'dog';
228         $this->_app->controller->action = 'index';
229
230         $this->assertTrue($this->_app->checkHTTPReferer());
231     }
232
233     /**
234      * @ticket 39691
235      */
236     public function testCheckHTTPRefererReturnsTrueIfRefererIsLocalhost()
237     {
238         $_SERVER['HTTP_REFERER'] = 'http://localhost';
239         $_SERVER['SERVER_NAME'] = 'localhost';
240         $this->_app->controller->action = 'poo';
241
242         $this->assertTrue($this->_app->checkHTTPReferer());
243
244         $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
245         $_SERVER['SERVER_NAME'] = 'localhost';
246         $this->_app->controller->action = 'poo';
247
248         $this->assertTrue($this->_app->checkHTTPReferer());
249
250         $_SERVER['HTTP_REFERER'] = 'http://localhost';
251         $_SERVER['SERVER_NAME'] = '127.0.0.1';
252         $this->_app->controller->action = 'poo';
253
254         $this->assertTrue($this->_app->checkHTTPReferer());
255
256         $_SERVER['HTTP_REFERER'] = 'http://127.0.0.1';
257         $_SERVER['SERVER_NAME'] = '127.0.0.1';
258         $this->_app->controller->action = 'poo';
259
260         $this->assertTrue($this->_app->checkHTTPReferer());
261     }
262
263     public function testCheckHTTPRefererReturnsTrueIfRefererIsServerName()
264     {
265         $_SERVER['HTTP_REFERER'] = 'http://dog';
266         $_SERVER['SERVER_NAME'] = 'dog';
267         $this->_app->controller->action = 'index';
268
269         $this->assertTrue($this->_app->checkHTTPReferer());
270     }
271
272     public function testCheckHTTPRefererReturnsTrueIfRefererIsInWhitelist()
273     {
274         $_SERVER['HTTP_REFERER'] = 'http://dog';
275         $_SERVER['SERVER_NAME'] = 'cat';
276         $this->_app->controller->action = 'index';
277
278         $GLOBALS['sugar_config']['http_referer']['list'][] = 'http://dog';
279
280         $this->assertTrue($this->_app->checkHTTPReferer());
281     }
282
283     public function testCheckHTTPRefererReturnsFalseIfRefererIsNotInWhitelist()
284     {
285         $_SERVER['HTTP_REFERER'] = 'http://dog';
286         $_SERVER['SERVER_NAME'] = 'cat';
287         $this->_app->controller->action = 'poo';
288
289         $GLOBALS['sugar_config']['http_referer']['list'] = array();
290
291         $this->assertFalse($this->_app->checkHTTPReferer());
292     }
293
294     public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIs()
295     {
296         $_SERVER['HTTP_REFERER'] = 'http://dog';
297         $_SERVER['SERVER_NAME'] = 'cat';
298         $this->_app->controller->action = 'index';
299
300         $this->assertTrue($this->_app->checkHTTPReferer());
301     }
302
303     public function testCheckHTTPRefererReturnsTrueIfRefererIsNotInWhitelistButActionIsInConfig()
304     {
305         $_SERVER['HTTP_REFERER'] = 'http://dog';
306         $_SERVER['SERVER_NAME'] = 'cat';
307         $this->_app->controller->action = 'poo';
308
309         $GLOBALS['sugar_config']['http_referer']['actions'][] = 'poo';
310         $this->assertTrue($this->_app->checkHTTPReferer());
311     }
312
313     /**
314      * @bug 50302
315      */
316     public function testWhitelistDefaults()
317     {
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());
327     }
328 }
329
330 class SugarApplicationMock extends SugarApplication
331 {
332     public function __construct()
333     {
334         parent::SugarApplication();
335         $this->controller = new stdClass();
336     }
337
338     public function checkHTTPReferer()
339         {
340             return parent::checkHTTPReferer(false);
341         }
342 }