]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/ModuleBuilder/Bug45339Test.php
Release 6.5.8
[Github/sugarcrm.git] / tests / modules / ModuleBuilder / Bug45339Test.php
1 <?php
2
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38
39 /**
40  * Bug #45339
41  * Export Customizations Does Not Cleanly Handle Relationships.
42  *
43  * @ticket 45339
44  */
45 class Bug45339Test extends Sugar_PHPUnit_Framework_TestCase
46 {
47
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",
55         'view_module' => "",
56         'relationship_lang' => "en_us",
57         'relationship_name' => "",
58         'lhs_module' => "",
59         'relationship_type' => "many-to-many",
60         'rhs_module' => "",
61         'lhs_label' => "",
62         'rhs_label' => "",
63         'lhs_subpanel' => "default",
64         'rhs_subpanel' => "default",
65     );
66     private $packName = 'test_package';
67     private $df = null;
68     private $field = null;
69
70     public function setUp()
71     {
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');
77
78         $_REQUEST = $this->keys;
79
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";
85
86         $relationAccountContact = new DeployedRelationships($_REQUEST['view_module']);
87         $this->relationAccountContact = $relationAccountContact->addFromPost();
88         $relationAccountContact->save();
89         $relationAccountContact->build();
90
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";
96
97         $relationContactAccount = new DeployedRelationships($_REQUEST['view_module']);
98         $this->relationContactAccount = $relationContactAccount->addFromPost();
99         $relationContactAccount->save();
100         $relationContactAccount->build();
101         SugarTestHelper::setUp('relation', array(
102             'Contacts',
103             'Accounts'
104         ));
105
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;
129
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');
137
138
139         $this->mbPackage = new Bug45339MBPackageMock($this->packName);
140     }
141
142     public function tearDown()
143     {
144         $this->df->deleteField($this->field);
145         $relationshipAccountContact = new DeployedRelationships($this->relationAccountContact->getLhsModule());
146         $relationshipAccountContact->delete($this->relationAccountContact->getName());
147         $relationshipAccountContact->save();
148
149         $relationshipContactAccount = new DeployedRelationships($this->relationContactAccount->getLhsModule());
150         $relationshipContactAccount->delete($this->relationContactAccount->getName());
151         $relationshipContactAccount->save();
152
153         SugarRelationshipFactory::deleteCache();
154
155         $_REQUEST = array();
156
157         SugarTestHelper::tearDown();
158     }
159
160     /**
161      * @group 45339
162      */
163     public function testGetCustomRelationshipsByModuleName()
164     {
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');
170
171         $this->assertArrayHasKey($this->relationAccountContact->getName(), $accountsAllCustomRelationships);
172         $this->assertArrayHasKey($this->relationContactAccount->getName(), $accountsAllCustomRelationships);
173
174         $this->assertArrayHasKey($this->relationAccountContact->getName(), $accountsLhsCustomRelationships);
175         $this->assertArrayNotHasKey($this->relationContactAccount->getName(), $accountsLhsCustomRelationships);
176
177         $this->assertFalse($wrongModuleName); // check
178     }
179
180     /**
181      * @group 45339
182      */
183     public function testGetCustomRelationshipsMetaFilesByModuleName()
184     {
185         $accountContactMetaPath = sprintf(
186                 'custom%1$smetadata%1$s' . $this->relationAccountContact->getName() . 'MetaData.php',
187                 DIRECTORY_SEPARATOR
188         );
189         $accountContactTablePath = sprintf(
190                 'custom%1$sExtension%1$sapplication%1$sExt%1$sTableDictionary%1$s' . $this->relationAccountContact->getName() . '.php',
191                 DIRECTORY_SEPARATOR
192         );
193         $contactAccountMetaPath = sprintf(
194                 'custom%1$smetadata%1$s' . $this->relationContactAccount->getName() . 'MetaData.php',
195                 DIRECTORY_SEPARATOR
196         );
197
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');
202
203         $this->assertContains($accountContactMetaPath, $accountsAllFiles);
204         $this->assertContains($accountContactTablePath, $accountsAllFiles);
205         $this->assertContains($contactAccountMetaPath, $accountsAllFiles);
206
207         $this->assertNotContains($accountContactMetaPath, $accountsOnlyMetaFile);
208         $this->assertNotContains($contactAccountMetaPath, $accountsOnlyMetaFile);
209
210         $this->assertInternalType('array', $wrongModuleName);
211         $this->assertEmpty($wrongModuleName);
212     }
213
214     /**
215      * @group 45339
216      */
217    public function testGetExtensionsList()
218     {
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";
225
226         $deployedRelation = new DeployedRelationships($_REQUEST['view_module']);
227         $relationLeadAccount = $deployedRelation->addFromPost();
228         $deployedRelation->save();
229         $deployedRelation->build();
230
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',
234                 DIRECTORY_SEPARATOR
235         );
236         $contactAccountRelInAccountVardefExtensions = sprintf(
237                 'custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . $this->relationContactAccount->getName() . '_Accounts.php',
238                 DIRECTORY_SEPARATOR
239         );
240         $leadAccountRelInAccountVardefExtensions = sprintf(
241                 'custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . $relationLeadAccount->getName() . '_Accounts.php',
242                 DIRECTORY_SEPARATOR
243         );
244         $sugarfieldAccountVardefExtensions = sprintf(
245                 'custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' .'sugarfield_'. $this->field->name . '.php',
246                 DIRECTORY_SEPARATOR
247         );
248
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');
255
256         // Remove relationship
257         $deployedRelation->delete($relationLeadAccount->getName());
258         $deployedRelation->save();
259         SugarRelationshipFactory::deleteCache();
260
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');
263
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');
266
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');
269
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');
274
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');
278
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');
282     }
283
284     /**
285      * @group 45339
286      */
287     public function testGetExtensionsManifestForPackage()
288     {
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';
293         $expected = 0;
294
295         $this->mbPackage->getExtensionsManifestForPackageTest($this->mbPackage->getBuildDir(), $installDefs);
296
297         $recursiveIterator = new RecursiveIteratorIterator(
298                         new RecursiveDirectoryIterator($packExtentionsPath),
299                         RecursiveIteratorIterator::SELF_FIRST
300         );
301
302         /* @var $fInfo SplFileInfo */
303         foreach (new RegexIterator($recursiveIterator, "/\.php$/i") as $fInfo)
304         {
305             if ($fInfo->isFile())
306             {
307                 ++$expected;
308             }
309         }
310
311         $this->mbPackage->delete();
312         $this->mbPackage->deleteBuild();
313
314         $this->assertEquals($expected, count($installDefs['copy']));
315     }
316
317     /**
318      * @group 45339
319      */
320     public function testCustomBuildInstall()
321     {
322         /* @var $this->mbPackage MBPackage */
323         $this->mbPackage->exportCustom(array('Accounts'), false, false);
324         $installDefString = $this->mbPackage->customBuildInstall(array('Accounts'), $this->mbPackage->getBuildDir());
325
326         eval($installDefString);
327
328         $this->mbPackage->delete();
329         $this->mbPackage->deleteBuild();
330         
331         $this->assertArrayHasKey('relationships', $installdefs);
332     }
333
334 }
335
336 class Bug45339MBPackageMock extends MBPackage
337 {
338
339     public function getExtensionsManifestForPackageTest($path, &$installdefs)
340     {
341         return $this->getExtensionsManifestForPackage($path, $installdefs);
342     }
343
344     public function getExtensionsListTest($module, $includeRelationships = true)
345     {
346         return $this->getExtensionsList($module, $includeRelationships);
347     }
348
349     public function getCustomRelationshipsMetaFilesByModuleNameTest($moduleName, $lhs = false, $metadataOnly = false,$exportModules=array())
350     {
351         return $this->getCustomRelationshipsMetaFilesByModuleName($moduleName, $lhs, $metadataOnly,$exportModules);
352     }
353
354     public function getCustomRelationshipsByModuleNameTest($moduleName, $lhs = false)
355     {
356         return $this->getCustomRelationshipsByModuleName($moduleName, $lhs);
357     }
358
359
360 }