]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/Localization/LocalizationTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / Localization / LocalizationTest.php
1 <?php
2 require_once 'include/Localization/Localization.php';
3
4 class LocalizationTest extends Sugar_PHPUnit_Framework_TestCase
5 {
6     public function setUp() 
7     {
8         $this->_locale = new Localization();
9         $this->_user = SugarTestUserUtilities::createAnonymousUser();
10     }
11     
12     public function tearDown()
13     {
14         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
15     }
16     
17     public function providerGetLocaleFormattedName()
18     {
19         return array(
20             array(
21                 't s f l',
22                 'Mason',
23                 'Hu',
24                 'Mr.',
25                 'Saler',
26                 'Saler Mr. Mason Hu',
27                 ),
28             array(
29                 'l f',
30                 'Mason',
31                 'Hu',
32                 '',
33                 '',
34                 'Hu Mason',
35                 ),
36                     
37             );
38     }
39     
40     /**
41      * @dataProvider providerGetLocaleFormattedName
42      */
43     public function testGetLocaleFormattedNameUsingFormatInUserPreference($nameFormat,$firstName,$lastName,$salutation,$title,$expectedOutput)
44     {
45         $this->_user->setPreference('default_locale_name_format', $nameFormat);
46         $outputName = $this->_locale->getLocaleFormattedName($firstName, $lastName, $salutation, $title, '',$this->_user);
47         $this->assertEquals($expectedOutput, $outputName);
48     }
49     
50     /**
51      * @dataProvider providerGetLocaleFormattedName
52      */
53     public function testGetLocaleFormattedNameUsingFormatSpecified($nameFormat,$firstName,$lastName,$salutation,$title,$expectedOutput)
54     {
55         $outputName = $this->_locale->getLocaleFormattedName($firstName, $lastName, $salutation, $title, $nameFormat,$this->_user);
56         $this->assertEquals($expectedOutput, $outputName);
57     }
58     
59     /**
60      * @group bug26803
61      */
62     public function testGetLocaleFormattedNameWhenNameIsEmpty()
63     {
64         $this->_user->setPreference('default_locale_name_format', 'l f');
65         $expectedOutput = ' ';
66         $outputName = $this->_locale->getLocaleFormattedName('', '', '', '', '',$this->_user);
67         
68         $this->assertEquals($expectedOutput, $outputName);
69     }
70     
71     /**
72      * @group bug26803
73      */
74     public function testGetLocaleFormattedNameWhenNameIsEmptyAndReturningEmptyString()
75     {
76         $this->_user->setPreference('default_locale_name_format', 'l f');
77         $expectedOutput = '';
78         $outputName = $this->_locale->getLocaleFormattedName('', '', '', '', '',$this->_user,true);
79         
80         $this->assertEquals($expectedOutput, $outputName);
81     }
82     
83     public function testCurrenciesLoadingCorrectly()
84     {
85         global $sugar_config;
86         
87         $currencies = $this->_locale->getCurrencies();
88         
89         $this->assertEquals($currencies['-99']['name'],$sugar_config['default_currency_name']);
90         $this->assertEquals($currencies['-99']['symbol'],$sugar_config['default_currency_symbol']);
91         $this->assertEquals($currencies['-99']['conversion_rate'],1);
92     }
93     
94     public function testConvertingUnicodeStringBetweenCharsets()
95     {
96         $string = "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモガギグゲゴザジズゼゾダヂヅデド";
97         
98         $convertedString = $this->_locale->translateCharset($string,'UTF-8','EUC-CN');
99         $this->assertNotEquals($string,$convertedString);
100         
101         // test for this working by being able to convert back and the string match
102         $convertedString = $this->_locale->translateCharset($convertedString,'EUC-CN','UTF-8');
103         $this->assertEquals($string,$convertedString);
104     }
105     
106     public function testCanDetectAsciiEncoding()
107     {
108         $string = 'string';
109         
110         $this->assertEquals(
111             $this->_locale->detectCharset($string),
112             'ASCII'
113             );
114     }
115     
116     public function testCanDetectUtf8Encoding()
117     {
118         $string = 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモガギグゲゴザジズゼゾダヂヅデド';
119         
120         $this->assertEquals(
121             $this->_locale->detectCharset($string),
122             'UTF-8'
123             );
124     }
125     
126     public function testGetNameJsCorrectlySpecifiesMissingOrEmptyParameters()
127     {
128         global $app_strings;
129         
130         $app_strings = return_application_language($GLOBALS['current_language']);
131         
132         $first = 'First';
133         $last = 'Last';
134         $salutation = 'Sal';
135         $title = 'Title';
136         
137         $ret = $this->_locale->getNameJs($first,$last,$salutation);
138         
139         $this->assertRegExp("/stuff\['s'\] = '$salutation';/",$ret);
140         $this->assertRegExp("/stuff\['f'\] = '$first';/",$ret);
141         $this->assertRegExp("/stuff\['l'\] = '$last';/",$ret);
142         $this->assertRegExp("/stuff\['t'\] = '{$app_strings['LBL_LOCALE_NAME_EXAMPLE_TITLE']}';/",$ret);
143         
144         $ret = $this->_locale->getNameJs('',$last,$salutation);
145         
146         $this->assertRegExp("/stuff\['s'\] = '$salutation';/",$ret);
147         $this->assertRegExp("/stuff\['f'\] = '{$app_strings['LBL_LOCALE_NAME_EXAMPLE_FIRST']}';/",$ret);
148         $this->assertRegExp("/stuff\['l'\] = '$last';/",$ret);
149         $this->assertRegExp("/stuff\['t'\] = '{$app_strings['LBL_LOCALE_NAME_EXAMPLE_TITLE']}';/",$ret);
150     }
151     
152     public function testGetPrecedentPreferenceWithUserPreference()
153     {
154         $backup = $GLOBALS['sugar_config']['export_delimiter'];
155         $GLOBALS['sugar_config']['export_delimiter'] = 'John is Cool';
156         $this->_user->setPreference('export_delimiter','John is Really Cool');
157         
158         $this->assertEquals(
159             $this->_locale->getPrecedentPreference('export_delimiter',$this->_user),
160             $this->_user->getPreference('export_delimiter')
161             );
162         
163         $GLOBALS['sugar_config']['export_delimiter'] = $backup;
164     }
165     
166     public function testGetPrecedentPreferenceWithNoUserPreference()
167     {
168         $backup = $GLOBALS['sugar_config']['export_delimiter'];
169         $GLOBALS['sugar_config']['export_delimiter'] = 'John is Cool';
170         
171         $this->assertEquals(
172             $this->_locale->getPrecedentPreference('export_delimiter',$this->_user),
173             $GLOBALS['sugar_config']['export_delimiter']
174             );
175         
176         $GLOBALS['sugar_config']['export_delimiter'] = $backup;
177     }
178     
179     /**
180      * @ticket 33086
181      */
182     public function testGetPrecedentPreferenceWithUserPreferenceAndSpecifiedConfigKey()
183     {
184         $backup = $GLOBALS['sugar_config']['export_delimiter'];
185         $GLOBALS['sugar_config']['export_delimiter'] = 'John is Cool';
186         $this->_user->setPreference('export_delimiter','');
187         $GLOBALS['sugar_config']['default_random_setting_for_localization_test'] = 'John is not Cool at all';
188         
189         $this->assertEquals(
190             $this->_locale->getPrecedentPreference('export_delimiter',$this->_user,'default_random_setting_for_localization_test'),
191             $GLOBALS['sugar_config']['default_random_setting_for_localization_test']
192             );
193         
194         $backup = $GLOBALS['sugar_config']['export_delimiter'];
195         unset($GLOBALS['sugar_config']['default_random_setting_for_localization_test']);
196     }
197     
198     /**
199      * @ticket 39171
200      */
201     public function testGetPrecedentPreferenceForDefaultEmailCharset()
202     {
203         $emailSettings = array('defaultOutboundCharset' => 'something fun');
204         $this->_user->setPreference('emailSettings',$emailSettings, 0, 'Emails');
205         
206         $this->assertEquals(
207             $this->_locale->getPrecedentPreference('default_email_charset',$this->_user),
208             $emailSettings['defaultOutboundCharset']
209             );
210     }
211 }