2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 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 'include/dir_inc.php';
40 class Bug37841Test extends Sugar_PHPUnit_Framework_TestCase
48 $this->modules = array('Accounts');
49 $this->has_dir = array();
51 foreach($this->modules as $module) {
52 if(!file_exists("custom/modules/{$module}/metadata")){
53 mkdir_recursive("custom/modules/{$module}/metadata", true);
56 if(file_exists("custom/modules/{$module}")) {
57 $this->has_dir[$module] = true;
60 $this->clearFilesInDirectory('custom/modules/Accounts/metadata');
61 $this->clearFilesInDirectory('custom/history/modules/Accounts/metadata');
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}");
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");
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");
94 * Ensure that no custom metadata is created and no history item created.
97 function testHistoryCreationForNonUpgradedMetadataFiles()
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');
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'));
111 * Ensure that a history item is created when SugarMerge executes and that the file contents are identical.
114 function testHistoryCreationForUpgradedMetadataFiles()
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');
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.");
136 private function clearFilesInDirectory($path)
138 $dir_handle = @opendir($path);
139 if ($dir_handle === false)
141 while (($filename = readdir($dir_handle)) !== false)
143 if ($filename == '.' || $filename == '..' || is_file("{$path}/{$filename}") == false)
147 unlink("{$path}/{$filename}");
151 private function getFirstFileContentsInDirectory($path)
154 $dir_handle = opendir($path);
155 if ($dir_handle === false)
157 while (($filename = readdir($dir_handle)) !== false)
159 if ($filename == '.' || $filename == '..')
162 return file_get_contents("{$path}/{$filename}");
166 private function checkForHistoryRecords($module_dir)
168 $parth = "custom/history/modules/$module_dir/metadata";
169 $dir_handle = @opendir($parth);
170 if ($dir_handle === false)
173 while (($filename = readdir($dir_handle)) !== false)
175 if ($filename == '.' || $filename == '..')