2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 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 'modules/UpgradeWizard/uw_utils.php';
40 class Bug41058Test extends Sugar_PHPUnit_Framework_OutputTestCase {
44 var $backupSystemLocaleNameFormat;
47 public function setUp() {
50 $this->backupConfig = $sugar_config;
52 $this->user = SugarTestUserUtilities::createAnonymousUser(true);
54 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
55 $GLOBALS['current_user']->is_admin = '1';
56 $GLOBALS['current_user']->save();
58 $this->loc = new Localization();
59 if ($this->loc->invalidLocaleNameFormatUpgrade()) {
60 rename($this->loc->invalidNameFormatUpgradeFilename, $this->loc->invalidNameFormatUpgradeFilename.'.backup');
64 public function tearDown() {
65 global $sugar_config, $sugar_version, $mod_strings;
67 if (file_exists($this->loc->invalidNameFormatUpgradeFilename)) {
68 unlink($this->loc->invalidNameFormatUpgradeFilename);
70 if (file_exists($this->loc->invalidNameFormatUpgradeFilename.'.backup')) {
71 rename($this->loc->invalidNameFormatUpgradeFilename.'.backup', $this->loc->invalidNameFormatUpgradeFilename);
75 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
77 unset($GLOBALS['current_user']);
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'];
85 unset($this->backupSystemLocaleNameFormat);
87 unset($sugar_version);
90 unset($app_list_strings);
97 * Tests that Localization::isAllowedNameFormat returns true for valid name formats
98 * @param $name_format valid name format from dataProvider
99 * @dataProvider goodLocaleNameFormatProvider
101 public function testCheckReturnsTrueForValidNameFormats($name_format) {
102 $this->assertTrue($this->loc->isAllowedNameFormat($name_format));
106 * Tests that Localization::isAllowedNameFormat returns false for invalid name formats
107 * @param $name_format invalid name format from dataProvider
108 * @dataProvider badLocaleNameFormatProvider
110 public function testCheckReturnsFalseForInvalidNameFormats($name_format) {
111 $this->assertFalse($this->loc->isAllowedNameFormat($name_format));
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
120 public function testUserPreferenceForLocaleNameFormatUpgrade($name_format) {
121 global $sugar_config;
123 $this->user->setPreference('default_locale_name_format', $name_format);
124 $this->user->savePreferencesToDB();
127 $ulnf = $this->user->getPreference('default_locale_name_format');
128 $this->assertSame($name_format, $ulnf);
130 $this->assertArrayNotHasKey($name_format, $sugar_config['name_formats']);
131 upgradeUserPreferences();
132 $this->assertArrayHasKey($name_format, $sugar_config['name_formats']);
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
141 public function testBadUserPreferenceForLocaleNameFormatUpgrade($name_format) {
142 global $sugar_config;
144 $this->user->setPreference('default_locale_name_format', $name_format);
145 $this->user->savePreferencesToDB();
148 $ulnf = $this->user->getPreference('default_locale_name_format');
149 $this->assertSame($name_format, $ulnf);
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'));
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
165 public function testGlobalPreferenceForLocaleNameFormatUpgrade($name_format) {
166 global $sugar_config, $sugar_version;
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.");
175 upgradeUserPreferences();
176 require ('config.php');
177 $this->assertSame($name_format, $sugar_config['default_locale_name_format']);
178 $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
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
187 public function testInvalidGlobalPreferenceForLocaleNameFormatUpgrade($name_format) {
188 global $sugar_config, $sugar_version;
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.");
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);
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
211 public function testMessageIsShownWhenInvalidLocaleNameFormatIsFoundInUpgrade($name_format) {
212 global $sugar_config, $locale, $app_strings, $app_list_strings;
214 require('modules/Administration/language/en_us.lang.php');
216 $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
217 $sugar_config['default_locale_name_format'] = $name_format;
218 upgradeUserPreferences();
219 $this->assertFileExists($this->loc->invalidNameFormatUpgradeFilename);
221 $this->expectOutputRegex('/'.$mod_strings['ERR_INVALID_LOCALE_NAME_FORMAT_UPGRADE'].'/');
222 require('modules/Administration/Locale.php');
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
231 public function testMessageIsNotShownWhenNoInvalidLocaleNameFormatIsFoundInUpgrade($name_format) {
232 global $sugar_config, $locale, $app_strings, $app_list_strings;
234 require('modules/Administration/language/en_us.lang.php');
236 $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
237 $sugar_config['default_locale_name_format'] = $name_format;
238 upgradeUserPreferences();
239 $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
241 $this->expectOutputNotRegex('/'.$mod_strings['ERR_INVALID_LOCALE_NAME_FORMAT_UPGRADE'].'/');
242 require('modules/Administration/Locale.php');
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
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');
256 $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
257 $sugar_config['default_locale_name_format'] = $name_format;
258 upgradeUserPreferences();
259 $this->assertFileExists($this->loc->invalidNameFormatUpgradeFilename);
261 $_REQUEST['process'] = 'true';
262 require('modules/Administration/Locale.php');
263 } catch (Exception $e) {
264 $this->assertFileNotExists($this->loc->invalidNameFormatUpgradeFilename);
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
273 public function testBadFormatsInConfigAreNotIncludedInDropdown($name_format) {
274 $localeDefaults = $this->loc->getLocaleConfigDefaults();
275 $formats = $localeDefaults['name_formats'];
277 $formats[$name_format] = $name_format;
279 $list = $this->loc->getUsableLocaleNameOptions($formats);
280 $this->assertArrayHasKey($name_format, $formats);
281 $this->assertArrayNotHasKey($name_format, $list);
285 * Data provider of allowed name formats
286 * @return array of allowed name format strings
288 public function goodLocaleNameFormatProvider() {
289 $goodFormatsArray = array(
290 array('`l` `f` `s`'),
293 array('{[`~!@#$%^&*()_-+=;:\'"/?\\|.>s, f, l <]}'),
296 return $goodFormatsArray;
300 * Data provider of disallowed name formats
301 * @return array of disallowed name format strings
303 public function badLocaleNameFormatProvider() {
304 $badFormatsArray = array(
305 array('`l` `f` `s`: `t`'),
307 array('*-s-f-l-*-bad_name_format'),
308 array('bad{[`~!@#$%^&*()_-+=;:\'"/?\\|.>s, f, l <]}'),
311 return $badFormatsArray;