]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/UpgradeWizard/SugarMerge/Bug37841Test.php
Release 6.2.0
[Github/sugarcrm.git] / tests / modules / UpgradeWizard / SugarMerge / Bug37841Test.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 'include/dir_inc.php';
39
40 class Bug37841Test extends Sugar_PHPUnit_Framework_TestCase  
41 {
42
43     var $merge;
44     var $has_dir;
45     var $modules;
46     
47     function setUp() {
48        $this->modules = array('Accounts');
49        $this->has_dir = array();
50        
51        foreach($this->modules as $module) {
52            if(!file_exists("custom/modules/{$module}/metadata")){
53                   mkdir_recursive("custom/modules/{$module}/metadata", true);
54            }
55            
56            if(file_exists("custom/modules/{$module}")) {
57                   $this->has_dir[$module] = true;
58            }
59        } //foreach
60        $this->clearFilesInDirectory('custom/modules/Accounts/metadata');
61        $this->clearFilesInDirectory('custom/history/modules/Accounts/metadata');
62     }
63     
64     
65     function tearDown() {
66        $this->clearFilesInDirectory('custom/history/modules/Accounts/metadata');
67        foreach($this->modules as $module) {
68            if(!$this->has_dir[$module]) {
69                   rmdir_recursive("custom/modules/{$module}");
70            }  else {
71                    $files = array('editviewdefs','detailviewdefs');
72                    foreach($files as $file) {
73                       if(file_exists("custom/modules/{$module}/metadata/{$file}.php.bak")) {
74                          copy("custom/modules/{$module}/metadata/{$file}.php.bak", "custom/modules/{$module}/metadata/{$file}.php");
75                      unlink("custom/modules/{$module}/metadata/{$file}.php.bak");
76                       } else if(file_exists("custom/modules/{$module}/metadata/{$file}.php")) {
77                          unlink("custom/modules/{$module}/metadata/{$file}.php");
78                       }
79                       
80                           if(file_exists("custom/modules/{$module}/metadata/{$module}.php.suback.bak")) {
81                          copy("custom/modules/{$module}/metadata/{$file}.php.suback.bak", "custom/modules/{$module}/metadata/{$file}.php.suback.php");
82                      unlink("custom/modules/{$module}/metadata/{$file}.php.suback.bak");
83                       } else if(file_exists("custom/modules/{$module}/metadata/{$file}.php.suback.php")) {
84                          unlink("custom/modules/{$module}/metadata/{$file}.php.suback.php");
85                       }  
86                    }
87            }
88        } //foreach
89     }
90     
91     
92     
93     /**
94      * Ensure that no custom metadata is created and no history item created.
95      *
96      */
97     function testHistoryCreationForNonUpgradedMetadataFiles() 
98     {           
99        $this->clearFilesInDirectory('custom/modules/Accounts/metadata');
100        $this->clearFilesInDirectory('custom/history/modules/Accounts/metadata');
101        require_once('modules/UpgradeWizard/SugarMerge/SugarMerge.php');
102        $sugar_merge = new SugarMerge('tests/modules/UpgradeWizard/SugarMerge/od_metadata_files/610/oob');
103        
104        //Using oob defs make sure nothing is merged
105        $mergedFiles = $sugar_merge->mergeModule('Accounts');
106        $this->assertFalse(file_exists('custom/modules/Accounts/metadata/detailviewdefs.php'));
107        $this->assertFalse($this->checkForHistoryRecords('Accounts'));
108     }
109     
110     /**
111      * Ensure that a history item is created when SugarMerge executes and that the file contents are identical.
112      *
113      */
114     function testHistoryCreationForUpgradedMetadataFiles() 
115     {   
116         
117         $accountsHistoryMetadataLocation = 'custom/history/modules/Accounts/metadata';
118         $this->clearFilesInDirectory('custom/modules/Accounts/metadata');
119         $this->clearFilesInDirectory($accountsHistoryMetadataLocation);
120         $customFile = "tests/modules/UpgradeWizard/SugarMerge/od_metadata_files/610/custom/modules/Accounts/metadata/detailviewdefs.php";
121         $customFileTo = "custom/modules/Accounts/metadata/detailviewdefs.php";
122         copy($customFile, $customFileTo);
123         require_once('modules/UpgradeWizard/SugarMerge/SugarMerge.php');
124         $sugar_merge = new SugarMerge('tests/modules/UpgradeWizard/SugarMerge/od_metadata_files/610/custom');
125         $mergedFiles = $sugar_merge->mergeModule('Accounts');
126     
127         $this->assertTrue(file_exists('custom/modules/Accounts/metadata/detailviewdefs.php'), "Custom metadata file not created.");
128         $this->assertTrue($this->checkForHistoryRecords('Accounts'));
129         //Ensure history file and custom file are the same.
130         $oldCustomFile = file_get_contents($customFile);
131         $newHistoryFile = $this->getFirstFileContentsInDirectory($accountsHistoryMetadataLocation);
132         $this->assertEquals($oldCustomFile, $newHistoryFile, "Error previous custom file before merge and new history record are not identical.");
133     }
134     
135     
136     private function clearFilesInDirectory($path)
137     {
138         $dir_handle = @opendir($path);
139         if ($dir_handle === false) 
140             return;
141         while (($filename = readdir($dir_handle)) !== false) 
142         {
143             if ($filename == '.' || $filename == '..')
144                 continue;
145             else 
146                 unlink("{$path}/{$filename}");
147         }
148     }
149     
150     private function getFirstFileContentsInDirectory($path)
151     {
152         $results = "";
153         $dir_handle = opendir($path);
154         if ($dir_handle === false)
155             return "";
156         while (($filename = readdir($dir_handle)) !== false)
157         {
158             if ($filename == '.' || $filename == '..')
159                 continue;
160             else
161                return file_get_contents("{$path}/{$filename}");
162         }
163         return $results;
164     }
165     private function checkForHistoryRecords($module_dir)
166     {
167         $parth = "custom/history/modules/$module_dir/metadata";
168         $dir_handle = @opendir($parth);
169         if ($dir_handle === false) 
170             return FALSE;
171         $found = FALSE;
172         while (($filename = readdir($dir_handle)) !== false) 
173         {
174             if ($filename == '.' || $filename == '..')
175                 continue;
176             else 
177                 return TRUE;
178         }
179         return $found;
180     }
181 }
182 ?>