]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/SugarTestLangPackCreator.php
Added unit tests.
[Github/sugarcrm.git] / tests / SugarTestLangPackCreator.php
1 <?php
2
3 class SugarTestLangPackCreator
4 {
5     public function __construct()
6     {
7     }
8     
9     public function __destruct()
10     {
11         $this->clearLangCache();
12     }
13     
14     /**
15      * Set a string for the app_strings array
16      *
17      * @param $key   string
18      * @param $value string
19      */
20     public function setAppString(
21         $key,
22         $value
23         )
24     {
25         $this->_strings['app_strings'][$key] = $value;
26     }
27     
28     /**
29      * Set a string for the app_list_strings array
30      *
31      * @param $key   string
32      * @param $value string
33      */
34     public function setAppListString(
35         $key,
36         $value
37         )
38     {
39         $this->_strings['app_list_strings'][$key] = $value;
40     }
41     
42     /**
43      * Set a string for the mod_strings array
44      *
45      * @param $key    string
46      * @param $value  string
47      * @param $module string
48      */
49     public function setModString(
50         $key,
51         $value,
52         $module
53         )
54     {
55         $this->_strings['mod_strings'][$module][$key] = $value;
56     }
57     
58     /**
59      * Saves the created strings
60      *
61      * Here, we cheat the system by storing our string overrides in the sugar_cache where
62      * we normally stored the cached language strings.
63      */
64     public function save()
65     {
66         $language = $GLOBALS['current_language'];
67         if ( isset($this->_strings['app_strings']) ) {
68             $cache_key = 'app_strings.'.$language;
69             $app_strings = sugar_cache_retrieve($cache_key);
70             if ( empty($app_strings) )
71                 $app_strings = return_application_language($language);
72             foreach ( $this->_strings['app_strings'] as $key => $value )
73                 $app_strings[$key] = $value;
74             sugar_cache_put($cache_key, $app_strings);
75             $GLOBALS['app_strings'] = $app_strings;
76         }
77         
78         if ( isset($this->_strings['app_list_strings']) ) {
79             $cache_key = 'app_list_strings.'.$language;
80             $app_list_strings = sugar_cache_retrieve($cache_key);
81             if ( empty($app_list_strings) )
82                 $app_list_strings = return_app_list_strings_language($language);
83             foreach ( $this->_strings['app_list_strings'] as $key => $value )
84                 $app_list_strings[$key] = $value;
85             sugar_cache_put($cache_key, $app_list_strings);
86             $GLOBALS['app_list_strings'] = $app_list_strings;
87         }
88         
89         if ( isset($this->_strings['mod_strings']) ) {
90             foreach ( $this->_strings['mod_strings'] as $module => $strings ) {
91                 $cache_key = "LanguageManager.$module.$language";
92                 $mod_strings = sugar_cache_retrieve($cache_key);
93                 if ( empty($mod_strings) )
94                     $mod_strings = return_module_language($language, $module);
95                 foreach ( $strings as $key => $value )
96                     $mod_strings[$key] = $value;
97                 sugar_cache_put($cache_key, $mod_strings);
98             }
99         }
100     }
101     
102     /**
103      * Clear the language string cache in sugar_cache, which will get rid of our
104      * language file overrides.
105      */
106     protected function clearLangCache()
107     {
108         $language = $GLOBALS['current_language'];
109         
110         if ( isset($this->_strings['app_strings']) ) {
111             $cache_key = 'app_strings.'.$language;
112             sugar_cache_clear($cache_key);
113         }
114         
115         if ( isset($this->_strings['app_list_strings']) ) {
116             $cache_key = 'app_list_strings.'.$language;
117             sugar_cache_clear($cache_key);
118         }
119         
120         if ( isset($this->_strings['mod_strings']) ) {
121             foreach ( $this->_strings['mod_strings'] as $module => $strings ) {
122                 $cache_key = "LanguageManager.$module.$language";
123                 sugar_cache_clear($cache_key);
124             }
125         }
126     }
127 }