]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/Localization/Bug41058Test.php
Release 6.4.0
[Github/sugarcrm.git] / tests / include / Localization / Bug41058Test.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 'modules/UpgradeWizard/uw_utils.php';
39
40 class Bug41058Test extends Sugar_PHPUnit_Framework_OutputTestCase {
41
42     var $user;
43     var $backupConfig;
44     var $backupSystemLocaleNameFormat;
45     var $loc;
46
47     public function setUp() {
48         global $sugar_config;
49
50         $this->backupConfig = $sugar_config;
51
52         $this->user = SugarTestUserUtilities::createAnonymousUser(true);
53
54         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
55         $GLOBALS['current_user']->is_admin = '1';
56             $GLOBALS['current_user']->save();
57
58         $this->loc = new Localization();
59         if ($this->loc->invalidLocaleNameFormatUpgrade()) {
60             rename($this->loc->invalidNameFormatUpgradeFilename, $this->loc->invalidNameFormatUpgradeFilename.'.backup');
61         }
62     }
63
64     public function tearDown() {
65         global $sugar_config, $sugar_version, $mod_strings;
66
67         if (file_exists($this->loc->invalidNameFormatUpgradeFilename)) {
68             unlink($this->loc->invalidNameFormatUpgradeFilename);
69         }
70         if (file_exists($this->loc->invalidNameFormatUpgradeFilename.'.backup')) {
71             rename($this->loc->invalidNameFormatUpgradeFilename.'.backup', $this->loc->invalidNameFormatUpgradeFilename);
72         }
73         unset($this->loc);
74
75         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
76         unset($this->user);
77         unset($GLOBALS['current_user']);
78
79         $sugar_config = $this->backupConfig;
80         if(!rebuildConfigFile($sugar_config, $sugar_version)) {
81             logThis('*** ERROR: could not write config.php!');
82             $errors[] = $mod_strings['ERR_UW_CONFIG_WRITE'];
83         }
84
85         unset($this->backupSystemLocaleNameFormat);
86         unset($sugar_config);
87         unset($sugar_version);
88         unset($mod_strings);
89         unset($app_strings);
90         unset($app_list_strings);
91         unset($locale);
92         unset($_REQUEST);
93     }
94
95
96     /**
97      * Tests that Localization::isAllowedNameFormat returns true for valid name formats
98      * @param $name_format valid name format from dataProvider
99      * @dataProvider goodLocaleNameFormatProvider
100      */
101     public function testCheckReturnsTrueForValidNameFormats($name_format) {
102         $this->assertTrue($this->loc->isAllowedNameFormat($name_format));
103     }
104
105     /**
106      * Tests that Localization::isAllowedNameFormat returns false for invalid name formats
107      * @param $name_format invalid name format from dataProvider
108      * @dataProvider badLocaleNameFormatProvider
109      */
110     public function testCheckReturnsFalseForInvalidNameFormats($name_format) {
111         $this->assertFalse($this->loc->isAllowedNameFormat($name_format));
112     }
113
114     /**
115      * Tests that good Locale Name Format strings from user preferences get added to the config
116      * @param $name_format valid name format from data provider
117      * @dataProvider goodLocaleNameFormatProvider
118      * @depends testCheckReturnsTrueForValidNameFormats
119      */
120     public function testUserPreferenceForLocaleNameFormatUpgrade($name_format) {
121         global $sugar_config;
122
123         $this->user->setPreference('default_locale_name_format', $name_format);
124         $this->user->savePreferencesToDB();
125         $this->user->save();
126
127         $ulnf = $this->user->getPreference('default_locale_name_format');
128         $this->assertSame($name_format, $ulnf);
129
130         $this->assertArrayNotHasKey($name_format, $sugar_config['name_formats']);
131         upgradeUserPreferences();
132         $this->assertArrayHasKey($name_format, $sugar_config['name_formats']);
133     }
134
135     /**
136      * Tests that bad Locale Name Format strings from user preferences do not get added to the config
137      * @param $name_format invalid name format from data provider
138      * @dataProvider badLocaleNameFormatProvider
139      * @depends testCheckReturnsFalseForInvalidNameFormats
140      */
141     public function testBadUserPreferenceForLocaleNameFormatUpgrade($name_format) {
142         global $sugar_config;
143
144         $this->user->setPreference('default_locale_name_format', $name_format);
145         $this->user->savePreferencesToDB();
146         $this->user->save();
147
148         $ulnf = $this->user->getPreference('default_locale_name_format');
149         $this->assertSame($name_format, $ulnf);
150
151         $this->assertArrayNotHasKey($name_format, $sugar_config['name_formats']);
152         upgradeUserPreferences();
153         $this->assertArrayNotHasKey($name_format, $sugar_config['name_formats']);
154         $coreDefaults = $this->loc->getLocaleConfigDefaults();
155         $this->assertSame($coreDefaults['default_locale_name_format'], $this->user->getPreference('default_locale_name_format'));
156     }
157
158
159     /**
160      * Tests that good Locale Name Format strings from global preference get added to the config
161      * @param $name_format valid name format from data provider
162      * @dataProvider goodLocaleNameFormatProvider
163      * @depends testCheckReturnsTrueForValidNameFormats
164      */
165     public function testGlobalPreferenceForLocaleNameFormatUpgrade($name_format) {
166         global $sugar_config, $sugar_version;
167
168         $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
169         $this->assertNotSame($name_format, $sugar_config['default_locale_name_format']);
170         $sugar_config['default_locale_name_format'] = $name_format;
171         if(!rebuildConfigFile($sugar_config, $sugar_version)) {
172             $errors[] = $mod_strings['ERR_UW_CONFIG_WRITE'];
173             $this->fail("Could not rebuild config file, please check your installation.");
174         }
175         upgradeUserPreferences();
176         require ('config.php');
177         $this->assertSame($name_format, $sugar_config['default_locale_name_format']);
178         $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
179     }
180
181     /**
182      * Tests that bad Locale Name Format strings from global preference do not get added to the config
183      * @param $name_format invalid name format from data provider
184      * @dataProvider badLocaleNameFormatProvider
185      * @depends testCheckReturnsFalseForInvalidNameFormats
186      */
187     public function testInvalidGlobalPreferenceForLocaleNameFormatUpgrade($name_format) {
188         global $sugar_config, $sugar_version;
189
190         $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
191         $this->assertNotSame($name_format, $sugar_config['default_locale_name_format']);
192         $sugar_config['default_locale_name_format'] = $name_format;
193         if(!rebuildConfigFile($sugar_config, $sugar_version)) {
194             $errors[] = $mod_strings['ERR_UW_CONFIG_WRITE'];
195             $this->fail("Could not rebuild config file, please check your installation.");
196         }
197         upgradeUserPreferences();
198         $this->assertNotSame($name_format, $sugar_config['default_locale_name_format']);
199         require ('config.php');
200         $coreDefaults = $this->loc->getLocaleConfigDefaults();
201         $this->assertSame($coreDefaults['default_locale_name_format'], $sugar_config['default_locale_name_format']);
202         $this->assertFileExists($this->loc->invalidNameFormatUpgradeFilename);
203     }
204
205     /**
206      * Tests that UI presents a message on the locale settings page when there was an invalid name format during an upgrade
207      * @param $name_format invalid name format from data provider
208      * @dataProvider badLocaleNameFormatProvider
209      * @depends testCheckReturnsFalseForInvalidNameFormats
210      */
211     public function testMessageIsShownWhenInvalidLocaleNameFormatIsFoundInUpgrade($name_format) {
212         global $sugar_config, $locale, $app_strings, $app_list_strings;
213
214         require('modules/Administration/language/en_us.lang.php');
215
216         $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
217         $sugar_config['default_locale_name_format'] = $name_format;
218         upgradeUserPreferences();
219         $this->assertFileExists($this->loc->invalidNameFormatUpgradeFilename);
220
221         $this->expectOutputRegex('/'.$mod_strings['ERR_INVALID_LOCALE_NAME_FORMAT_UPGRADE'].'/');
222         require('modules/Administration/Locale.php');
223     }
224
225     /**
226      * Tests that UI does not present a message on the locale settings page when there wasn't an invalid name format during an upgrade
227      * @param $name_format valid name format from data provider
228      * @dataProvider goodLocaleNameFormatProvider
229      * @depends testCheckReturnsTrueForValidNameFormats
230      */
231     public function testMessageIsNotShownWhenNoInvalidLocaleNameFormatIsFoundInUpgrade($name_format) {
232         global $sugar_config, $locale, $app_strings, $app_list_strings;
233
234         require('modules/Administration/language/en_us.lang.php');
235
236         $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
237         $sugar_config['default_locale_name_format'] = $name_format;
238         upgradeUserPreferences();
239         $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
240
241         $this->expectOutputNotRegex('/'.$mod_strings['ERR_INVALID_LOCALE_NAME_FORMAT_UPGRADE'].'/');
242         require('modules/Administration/Locale.php');
243         
244     }
245
246     /**
247      * Test that file gets removed after a save from Locale page
248      * @param $name_format invalid name format from data provider
249      * @dataProvider badLocaleNameFormatProvider
250      * @depends testCheckReturnsFalseForInvalidNameFormats
251      */
252     public function testFileGetsRemovedAfterLocaleSave($name_format) {
253         global $sugar_config, $locale, $app_strings, $app_list_strings;
254         require('modules/Administration/language/en_us.lang.php');
255
256         $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
257         $sugar_config['default_locale_name_format'] = $name_format;
258         upgradeUserPreferences();
259         $this->assertFileExists($this->loc->invalidNameFormatUpgradeFilename);
260         try {
261             $_REQUEST['process'] = 'true';
262             require('modules/Administration/Locale.php');
263         } catch (Exception $e) {
264             $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
265         }
266     }
267
268     /**
269      * Test that bad formats are not added to the list for the dropdown
270      * @param $name_format invalid name format from data provider
271      * @dataProvider badLocaleNameFormatProvider
272      */
273     public function testBadFormatsInConfigAreNotIncludedInDropdown($name_format) {
274         $localeDefaults = $this->loc->getLocaleConfigDefaults();
275         $formats = $localeDefaults['name_formats'];
276
277         $formats[$name_format] = $name_format;
278
279         $list = $this->loc->getUsableLocaleNameOptions($formats);
280         $this->assertArrayHasKey($name_format, $formats);
281         $this->assertArrayNotHasKey($name_format, $list);
282     }
283
284     /**
285      * Data provider of allowed name formats
286      * @return array of allowed name format strings
287      */
288     public function goodLocaleNameFormatProvider() {
289         $goodFormatsArray = array(
290             array('`l` `f` `s`'),
291             array('l_f_s'),
292             array('*-s-f-l-*'),
293             array('{[`~!@#$%^&*()_-+=;:\'"/?\\|.>s, f, l    <]}'),
294         );
295
296         return $goodFormatsArray;
297     }
298
299     /**
300      * Data provider of disallowed name formats
301      * @return array of disallowed name format strings
302      */
303     public function badLocaleNameFormatProvider() {
304         $badFormatsArray = array(
305             array('`l` `f` `s`: `t`'),
306             array('alpha-bits'),
307             array('*-s-f-l-*-bad_name_format'),
308             array('bad{[`~!@#$%^&*()_-+=;:\'"/?\\|.>s, f, l    <]}'),
309         );
310
311         return $badFormatsArray;
312     }
313
314 }
315 ?>