]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/UpgradeWizard/Bug43208Test.php
Release 6.2.0
[Github/sugarcrm.git] / tests / modules / UpgradeWizard / Bug43208Test.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  * Bug43208Test
39  * 
40  * This test checks to see if the function repairTableDictionaryExtFile in uw_utils.php is working correctly.
41  * There were some scenarios in 6.0.x whereby the files loaded in the extension tabledictionary.ext.php file 
42  * did not exist.  This would cause warnings to appear during the upgrade.  As a result, we added the 
43  * repairTableDictionaryExtFile function to scan the contents of tabledictionary.ext.php and then remove entries
44  * where the file does not exist.
45  */
46 class Bug43208Test extends Sugar_PHPUnit_Framework_TestCase 
47 {
48
49 var $tableDictionaryExtFile1 = 'custom/Extension/application/Ext/TableDictionary/tabledictionary.ext.php';              
50 var $tableDictionaryExtFile2 = 'custom/application/Ext/TableDictionary/tabledictionary.ext.php';        
51 var $corruptExtModuleFile = 'custom/Extension/application/Ext/TableDictionary/Bug43208_module.php';
52
53 function setUp() {
54
55     if(file_exists($this->tableDictionaryExtFile1)) {
56        copy($this->tableDictionaryExtFile1, $this->tableDictionaryExtFile1 . '.backup');
57        unlink($this->tableDictionaryExtFile1);
58     } else if(!file_exists('custom/Extension/application/Ext/TableDictionary')){
59        mkdir_recursive('custom/Extension/application/Ext/TableDictionary');
60     }
61
62     if( $fh = @fopen($this->tableDictionaryExtFile1, 'w+') )
63     {
64 $string = <<<EOQ
65 <?php
66
67 //WARNING: The contents of this file are auto-generated
68 include('custom/metadata/bug43208Test_productsMetaData.php');
69
70 //WARNING: The contents of this file are auto-generated
71 include('custom/Extension/application/Ext/TableDictionary/Bug43208_module.php');
72 ?>
73 EOQ;
74        fputs( $fh, $string);
75        fclose( $fh );
76     }     
77     
78
79     if(file_exists($this->tableDictionaryExtFile2)) {
80        copy($this->tableDictionaryExtFile2, $this->tableDictionaryExtFile2 . '.backup');
81        unlink($this->tableDictionaryExtFile2);
82     } else if(!file_exists('custom/application/Ext/TableDictionary')){
83        mkdir_recursive('custom/application/Ext/TableDictionary');
84     }    
85     
86     if( $fh = @fopen($this->tableDictionaryExtFile2, 'w+') )
87     {
88 $string = <<<EOQ
89 <?php
90
91 //WARNING: The contents of this file are auto-generated
92 include('custom/metadata/bug43208Test_productsMetaData.php');
93
94 //WARNING: The contents of this file are auto-generated
95 include('custom/Extension/application/Ext/TableDictionary/Bug43208_module.php');
96 ?>
97 EOQ;
98        fputs( $fh, $string);
99        fclose( $fh );
100     } 
101     
102     if( $fh = @fopen($this->corruptExtModuleFile, 'w+') )
103     {
104 $string = <<<EOQ
105 <?php
106  //WARNING: The contents of this file are auto-generated
107         include( "custom/metadata/bug43208Test_productsMetaData.php" ); 
108 ?>
109 EOQ;
110        fputs( $fh, $string);
111        fclose( $fh );
112     }        
113     
114 }
115
116 function tearDown() {
117     if(file_exists($this->tableDictionaryExtFile1 . '.backup')) 
118     {
119        copy($this->tableDictionaryExtFile1 . '.backup', $this->tableDictionaryExtFile1);
120        unlink($this->tableDictionaryExtFile1 . '.backup');  
121     } else {
122        unlink($this->tableDictionaryExtFile1);
123     }
124
125     if(file_exists($this->tableDictionaryExtFile2 . '.backup')) 
126     {
127        copy($this->tableDictionaryExtFile2 . '.backup', $this->tableDictionaryExtFile2);
128        unlink($this->tableDictionaryExtFile2 . '.backup');  
129     } else {
130        unlink($this->tableDictionaryExtFile2);
131     }    
132     
133     if(file_exists($this->corruptExtModuleFile)) {
134        unlink($this->corruptExtModuleFile);
135     }
136     
137 }
138
139
140 function testRepairTableDictionaryExtFile() 
141 {       
142         require_once('ModuleInstall/ModuleInstaller.php');
143         repairTableDictionaryExtFile();
144         
145         if(function_exists('sugar_fopen'))
146         {
147                 $fp = @sugar_fopen($this->tableDictionaryExtFile1, 'r');
148         } else {
149                 $fp = fopen($this->tableDictionaryExtFile1, 'r');
150         }                       
151                 
152         $matches = 0;
153     if($fp)
154     {
155          while($line = fgets($fp))
156              {
157                 if(preg_match('/\s*include\s*\(\s*[\'|\"](.*?)[\'\"]\s*\)\s*;/', $line, $match))
158                 {
159                    $matches++;
160                    $this->assertTrue(file_exists($match[1]), 'Assert that entry for file ' . $line . ' exists');
161                 }
162              }  
163                  fclose($fp); 
164    }    
165    
166    $this->assertEquals($matches, 1, 'Assert that there was one match for correct entries in file ' . $this->tableDictionaryExtFile1);
167
168    
169         if(function_exists('sugar_fopen'))
170         {
171                 $fp = @sugar_fopen($this->tableDictionaryExtFile2, 'r');
172         } else {
173                 $fp = fopen($this->tableDictionaryExtFile2, 'r');
174         }                       
175                 
176         $matches = 0;
177     if($fp)
178     {
179          while($line = fgets($fp))
180              {
181                 if(preg_match('/\s*include\s*\(\s*[\'|\"](.*?)[\'\"]\s*\)\s*;/', $line, $match))
182                 {
183                    $matches++;
184                    $this->assertTrue(file_exists($match[1]), 'Assert that entry for file ' . $line . ' exists');
185                 }
186              }  
187                  fclose($fp); 
188    }    
189    
190    $this->assertEquals($matches, 1, 'Assert that there was one match for correct entries in file ' . $this->tableDictionaryExtFile2);
191       
192    
193         if(function_exists('sugar_fopen'))
194         {
195                 $fp = @sugar_fopen($this->corruptExtModuleFile, 'r');
196         } else {
197                 $fp = fopen($this->corruptExtModuleFile, 'r');
198         }                       
199                 
200         $matches = 0;
201     if($fp)
202     {
203          while($line = fgets($fp))
204              {
205                 if(preg_match('/\s*include\s*\(\s*[\'|\"](.*?)[\'\"]\s*\)\s*;/', $line, $match))
206                 {
207                    $matches++;
208                    $this->assertTrue(file_exists($match[1]), 'Assert that entry for file ' . $line . ' exists');
209                 }
210              }  
211                  fclose($fp); 
212    }    
213    
214    $this->assertEquals($matches, 0, 'Assert that there was one match for correct entries in file ' . $this->corruptExtModuleFile);   
215    
216 }
217
218
219 }
220
221 /**
222  * repairTableDictionaryExtFile
223  * 
224  * There were some scenarios in 6.0.x whereby the files loaded in the extension tabledictionary.ext.php file 
225  * did not exist.  This would cause warnings to appear during the upgrade.  As a result, this
226  * function scans the contents of tabledictionary.ext.php and then remove entries where the file does exist.
227  */
228 function repairTableDictionaryExtFile()
229 {
230         $tableDictionaryExtDirs = array('custom/Extension/application/Ext/TableDictionary', 'custom/application/Ext/TableDictionary');
231         
232         foreach($tableDictionaryExtDirs as $tableDictionaryExt)
233         {
234         
235                 if(is_dir($tableDictionaryExt) && is_writable($tableDictionaryExt)){
236                         $dir = dir($tableDictionaryExt);
237                         while(($entry = $dir->read()) !== false)
238                         {
239                                 $entry = $tableDictionaryExt . '/' . $entry;
240                                 if(is_file($entry) && preg_match('/\.php$/i', $entry) && is_writeable($entry))
241                                 {
242                         
243                                                 if(function_exists('sugar_fopen'))
244                                                 {
245                                                         $fp = @sugar_fopen($entry, 'r');
246                                                 } else {
247                                                         $fp = fopen($entry, 'r');
248                                                 }                       
249                                                 
250                                                 
251                                             if($fp)
252                                         {
253                                              $altered = false;
254                                              $contents = '';
255                                                      
256                                              while($line = fgets($fp))
257                                                      {
258                                                         if(preg_match('/\s*include\s*\(\s*[\'|\"](.*?)[\"|\']\s*\)\s*;/', $line, $match))
259                                                         {
260                                                            if(!file_exists($match[1]))
261                                                            {
262                                                               $altered = true;
263                                                            } else {
264                                                                   $contents .= $line;
265                                                            }
266                                                         } else {
267                                                            $contents .= $line;
268                                                         }
269                                                      }
270                                                      
271                                                      fclose($fp); 
272                                         }
273                                         
274                                         
275                                             if($altered)
276                                             {
277                                                         if(function_exists('sugar_fopen'))
278                                                         {
279                                                                 $fp = @sugar_fopen($entry, 'w');
280                                                         } else {
281                                                                 $fp = fopen($entry, 'w');
282                                                         }                       
283                                             
284                                                         if($fp && fwrite($fp, $contents))
285                                                         {
286                                                                 fclose($fp);
287                                                         }
288                                             }                                   
289                                 } //if
290                         } //while
291                 } //if
292         }
293 }
294
295 ?>