3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
41 * Export Customizations Does Not Cleanly Handle Relationships.
45 class Bug45339Test extends Sugar_PHPUnit_Framework_TestCase
48 private $relationAccountContact = null;
49 private $relationContactAccount = null;
50 private $mbPackage = null;
51 private $keys = array(
52 'module' => "ModuleBuilder",
53 'action' => "SaveRelationship",
54 'remove_tables' => "true",
56 'relationship_lang' => "en_us",
57 'relationship_name' => "",
59 'relationship_type' => "many-to-many",
63 'lhs_subpanel' => "default",
64 'rhs_subpanel' => "default",
66 private $packName = 'test_package';
68 private $field = null;
70 public function setUp()
72 SugarTestHelper::setUp('current_user', array(true, 1));
73 SugarTestHelper::setUp('beanFiles');
74 SugarTestHelper::setUp('beanList');
75 SugarTestHelper::setUp('app_list_strings');
76 SugarTestHelper::setUp('app_strings');
78 $_REQUEST = $this->keys;
80 $_REQUEST['view_module'] = "Accounts";
81 $_REQUEST['lhs_module'] = "Accounts";
82 $_REQUEST['rhs_module'] = "Contacts";
83 $_REQUEST['lhs_label'] = "Accounts";
84 $_REQUEST['rhs_label'] = "Contacts";
86 $relationAccountContact = new DeployedRelationships($_REQUEST['view_module']);
87 $this->relationAccountContact = $relationAccountContact->addFromPost();
88 $relationAccountContact->save();
89 $relationAccountContact->build();
91 $_REQUEST['view_module'] = "Contacts";
92 $_REQUEST['lhs_module'] = "Contacts";
93 $_REQUEST['rhs_module'] = "Accounts";
94 $_REQUEST['lhs_label'] = "Contacts";
95 $_REQUEST['rhs_label'] = "Accounts";
97 $relationContactAccount = new DeployedRelationships($_REQUEST['view_module']);
98 $this->relationContactAccount = $relationContactAccount->addFromPost();
99 $relationContactAccount->save();
100 $relationContactAccount->build();
101 SugarTestHelper::setUp('relation', array(
106 //create a new field for accounts
107 $this->field = get_widget('varchar');
108 $this->field->id = 'Accountstest_45339333_c';
109 $this->field->name = 'test_45339333_c';
110 $this->field->vname = 'LBL_TEST_CUSTOM_C';
111 $this->field->help = NULL;
112 $this->field->custom_module = 'Accounts';
113 $this->field->type = 'varchar';
114 $this->field->label = 'LBL_TEST_CUSTOM_C';
115 $this->field->len = 255;
116 $this->field->required = 0;
117 $this->field->default_value = NULL;
118 $this->field->date_modified = '2012-10-31 02:23:23';
119 $this->field->deleted = 0;
120 $this->field->audited = 0;
121 $this->field->massupdate = 0;
122 $this->field->duplicate_merge = 0;
123 $this->field->reportable = 1;
124 $this->field->importable = 'true';
125 $this->field->ext1 = NULL;
126 $this->field->ext2 = NULL;
127 $this->field->ext3 = NULL;
128 $this->field->ext4 = NULL;
130 //add field to metadata
131 $this->df = new DynamicField('Accounts');
132 $this->df->setup(new Account());
133 $this->df->addFieldObject($this->field);
134 $this->df->buildCache('Accounts');
135 VardefManager::clearVardef();
136 VardefManager::refreshVardefs('Accounts', 'Account');
139 $this->mbPackage = new Bug45339MBPackageMock($this->packName);
142 public function tearDown()
144 $this->df->deleteField($this->field);
145 $relationshipAccountContact = new DeployedRelationships($this->relationAccountContact->getLhsModule());
146 $relationshipAccountContact->delete($this->relationAccountContact->getName());
147 $relationshipAccountContact->save();
149 $relationshipContactAccount = new DeployedRelationships($this->relationContactAccount->getLhsModule());
150 $relationshipContactAccount->delete($this->relationContactAccount->getName());
151 $relationshipContactAccount->save();
153 SugarRelationshipFactory::deleteCache();
157 SugarTestHelper::tearDown();
163 public function testGetCustomRelationshipsByModuleName()
165 /* @var $this->mbPackage MBPackage */
166 $accountsAllCustomRelationships = $this->mbPackage->getCustomRelationshipsByModuleNameTest('Accounts');
167 // Created in the Account module.
168 $accountsLhsCustomRelationships = $this->mbPackage->getCustomRelationshipsByModuleNameTest('Accounts', true);
169 $wrongModuleName = $this->mbPackage->getCustomRelationshipsByModuleNameTest('Wrong_module_name');
171 $this->assertArrayHasKey($this->relationAccountContact->getName(), $accountsAllCustomRelationships);
172 $this->assertArrayHasKey($this->relationContactAccount->getName(), $accountsAllCustomRelationships);
174 $this->assertArrayHasKey($this->relationAccountContact->getName(), $accountsLhsCustomRelationships);
175 $this->assertArrayNotHasKey($this->relationContactAccount->getName(), $accountsLhsCustomRelationships);
177 $this->assertFalse($wrongModuleName); // check
183 public function testGetCustomRelationshipsMetaFilesByModuleName()
185 $accountContactMetaPath = sprintf(
186 'custom%1$smetadata%1$s' . $this->relationAccountContact->getName() . 'MetaData.php',
189 $accountContactTablePath = sprintf(
190 'custom%1$sExtension%1$sapplication%1$sExt%1$sTableDictionary%1$s' . $this->relationAccountContact->getName() . '.php',
193 $contactAccountMetaPath = sprintf(
194 'custom%1$smetadata%1$s' . $this->relationContactAccount->getName() . 'MetaData.php',
198 /* @var $this->mbPackage MBPackage */
199 $accountsAllFiles = $this->mbPackage->getCustomRelationshipsMetaFilesByModuleNameTest('Accounts',false,false, array('Accounts','Contacts'));
200 $accountsOnlyMetaFile = $this->mbPackage->getCustomRelationshipsMetaFilesByModuleNameTest('Accounts', true, true, array('Accounts'));
201 $wrongModuleName = $this->mbPackage->getCustomRelationshipsMetaFilesByModuleNameTest('Wrong_module_name');
203 $this->assertContains($accountContactMetaPath, $accountsAllFiles);
204 $this->assertContains($accountContactTablePath, $accountsAllFiles);
205 $this->assertContains($contactAccountMetaPath, $accountsAllFiles);
207 $this->assertNotContains($accountContactMetaPath, $accountsOnlyMetaFile);
208 $this->assertNotContains($contactAccountMetaPath, $accountsOnlyMetaFile);
210 $this->assertInternalType('array', $wrongModuleName);
211 $this->assertEmpty($wrongModuleName);
217 public function testGetExtensionsList()
219 // Create new relationship between Leads and Accounts
220 $_REQUEST['view_module'] = "Leads";
221 $_REQUEST['lhs_module'] = "Leads";
222 $_REQUEST['rhs_module'] = "Accounts";
223 $_REQUEST['lhs_label'] = "Leads";
224 $_REQUEST['rhs_label'] = "Accounts";
226 $deployedRelation = new DeployedRelationships($_REQUEST['view_module']);
227 $relationLeadAccount = $deployedRelation->addFromPost();
228 $deployedRelation->save();
229 $deployedRelation->build();
231 //create expected file paths from custom extensions
232 $accountContactRelInAccountVardefExtensions = sprintf(
233 'custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . $this->relationAccountContact->getName() . '_Accounts.php',
236 $contactAccountRelInAccountVardefExtensions = sprintf(
237 'custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . $this->relationContactAccount->getName() . '_Accounts.php',
240 $leadAccountRelInAccountVardefExtensions = sprintf(
241 'custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . $relationLeadAccount->getName() . '_Accounts.php',
244 $sugarfieldAccountVardefExtensions = sprintf(
245 'custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' .'sugarfield_'. $this->field->name . '.php',
249 //call mbPackage to retrieve arrays of Files to be exported using different test parameters
250 $accountAllExtensions = $this->mbPackage->getExtensionsListTest('Accounts',array('Accounts','Contacts','Leads'));
251 $accountExtContacts = $this->mbPackage->getExtensionsListTest('Accounts',array('Accounts','Contacts'));
252 $accountExtOnly = $this->mbPackage->getExtensionsListTest('Accounts', array('Accounts'));
253 $contactExtWithWrongRelationship = $this->mbPackage->getExtensionsListTest('Contacts', array(''));
254 $wrongModuleName = $this->mbPackage->getExtensionsListTest('Wrong_module_name');
256 // Remove relationship
257 $deployedRelation->delete($relationLeadAccount->getName());
258 $deployedRelation->save();
259 SugarRelationshipFactory::deleteCache();
261 //assert that contact rels are exported when all rels were defined
262 $this->assertContains($accountContactRelInAccountVardefExtensions, $accountAllExtensions,'Contact Relationship should have been exported when accounts and contacts modules are exported');
264 //assert that contact rels are not exported when contact is not defined
265 $this->assertNotContains($accountContactRelInAccountVardefExtensions, $accountExtOnly,'Contact Relationship should NOT have been exported when exporting accounts only');
267 //assert that non relationship change is exported when no related module is defined
268 $this->assertContains($sugarfieldAccountVardefExtensions, $accountExtOnly,'Sugarfield change should have been exported when exporting Accounts only');
270 //assert only contact and Account modules are present when both contact and Accounts are defined
271 $this->assertContains($accountContactRelInAccountVardefExtensions, $accountExtContacts,'Accounts rels should be present when exporting Contacts and Accounts');
272 $this->assertContains($contactAccountRelInAccountVardefExtensions, $accountExtContacts,'Contacts rels should be present when exporting Contacts and Accounts');
273 $this->assertNotContains($leadAccountRelInAccountVardefExtensions, $accountExtContacts,'Leads rels should NOT be present when exporting Contacts and Accounts');
275 //assert that requesting a wrong relationship returns an empty array
276 $this->assertInternalType('array', $contactExtWithWrongRelationship,'array type should be returned when no relationships are exported, and no other changes exist');
277 $this->assertEmpty($contactExtWithWrongRelationship,'An empty array should be returned when no relationships are exported, and no other changes exist');
279 //assert that requesting a wrong module name returns an empty array
280 $this->assertInternalType('array', $wrongModuleName,'An array type should be returned when a bad module is requested for export');
281 $this->assertEmpty($wrongModuleName,'An empty array should be returned when a bad module is requested for export');
287 public function testGetExtensionsManifestForPackage()
289 /* @var $this->mbPackage MBPackage */
290 $this->mbPackage->exportCustom(array('Accounts'), false, false);
291 $installDefs = array();
292 $packExtentionsPath = $this->mbPackage->getBuildDir() . DIRECTORY_SEPARATOR . 'Extension' . DIRECTORY_SEPARATOR . 'modules';
295 $this->mbPackage->getExtensionsManifestForPackageTest($this->mbPackage->getBuildDir(), $installDefs);
297 $recursiveIterator = new RecursiveIteratorIterator(
298 new RecursiveDirectoryIterator($packExtentionsPath),
299 RecursiveIteratorIterator::SELF_FIRST
302 /* @var $fInfo SplFileInfo */
303 foreach (new RegexIterator($recursiveIterator, "/\.php$/i") as $fInfo)
305 if ($fInfo->isFile())
311 $this->mbPackage->delete();
312 $this->mbPackage->deleteBuild();
314 $this->assertEquals($expected, count($installDefs['copy']));
320 public function testCustomBuildInstall()
322 /* @var $this->mbPackage MBPackage */
323 $this->mbPackage->exportCustom(array('Accounts'), false, false);
324 $installDefString = $this->mbPackage->customBuildInstall(array('Accounts'), $this->mbPackage->getBuildDir());
326 eval($installDefString);
328 $this->mbPackage->delete();
329 $this->mbPackage->deleteBuild();
331 $this->assertArrayHasKey('relationships', $installdefs);
336 class Bug45339MBPackageMock extends MBPackage
339 public function getExtensionsManifestForPackageTest($path, &$installdefs)
341 return $this->getExtensionsManifestForPackage($path, $installdefs);
344 public function getExtensionsListTest($module, $includeRelationships = true)
346 return $this->getExtensionsList($module, $includeRelationships);
349 public function getCustomRelationshipsMetaFilesByModuleNameTest($moduleName, $lhs = false, $metadataOnly = false,$exportModules=array())
351 return $this->getCustomRelationshipsMetaFilesByModuleName($moduleName, $lhs, $metadataOnly,$exportModules);
354 public function getCustomRelationshipsByModuleNameTest($moduleName, $lhs = false)
356 return $this->getCustomRelationshipsByModuleName($moduleName, $lhs);