]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/ModuleBuilder/Bug45339Test.php
Release 6.5.6
[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
68     public function setUp()
69     {
70         SugarTestHelper::setUp('current_user', array(true, 1));
71         SugarTestHelper::setUp('beanFiles');
72         SugarTestHelper::setUp('beanList');
73         SugarTestHelper::setUp('app_list_strings');
74         SugarTestHelper::setUp('app_strings');
75
76         $_REQUEST = $this->keys;
77
78         $_REQUEST['view_module'] = "Accounts";
79         $_REQUEST['lhs_module'] = "Accounts";
80         $_REQUEST['rhs_module'] = "Contacts";
81         $_REQUEST['lhs_label'] = "Accounts";
82         $_REQUEST['rhs_label'] = "Contacts";
83
84         $relationAccountContact = new DeployedRelationships($_REQUEST['view_module']);
85         $this->relationAccountContact = $relationAccountContact->addFromPost();
86         $relationAccountContact->save();
87         $relationAccountContact->build();
88
89         $_REQUEST['view_module'] = "Contacts";
90         $_REQUEST['lhs_module'] = "Contacts";
91         $_REQUEST['rhs_module'] = "Accounts";
92         $_REQUEST['lhs_label'] = "Contacts";
93         $_REQUEST['rhs_label'] = "Accounts";
94
95         $relationContactAccount = new DeployedRelationships($_REQUEST['view_module']);
96         $this->relationContactAccount = $relationContactAccount->addFromPost();
97         $relationContactAccount->save();
98         $relationContactAccount->build();
99
100         $this->mbPackage = new Bug45339MBPackageMock($this->packName);
101     }
102
103     public function tearDown()
104     {
105         $relationshipAccountContact = new DeployedRelationships($this->relationAccountContact->getLhsModule());
106         $relationshipAccountContact->delete($this->relationAccountContact->getName());
107         $relationshipAccountContact->save();
108
109         $relationshipContactAccount = new DeployedRelationships($this->relationContactAccount->getLhsModule());
110         $relationshipContactAccount->delete($this->relationContactAccount->getName());
111         $relationshipContactAccount->save();
112
113         SugarRelationshipFactory::deleteCache();
114
115         unset($_REQUEST);
116
117         SugarTestHelper::tearDown();
118     }
119
120     /**
121      * @group 45339
122      */
123     public function testGetCustomRelationshipsByModuleName()
124     {
125         /* @var $this->mbPackage MBPackage */
126         $accountsAllCustomRelationships = $this->mbPackage->getCustomRelationshipsByModuleNameTest('Accounts');
127         // Created in the Account module.
128         $accountsLhsCustomRelationships = $this->mbPackage->getCustomRelationshipsByModuleNameTest('Accounts', true);
129         $wrongModuleName = $this->mbPackage->getCustomRelationshipsByModuleNameTest('Wrong_module_name');
130
131         $this->assertArrayHasKey($this->relationAccountContact->getName(), $accountsAllCustomRelationships);
132         $this->assertArrayHasKey($this->relationContactAccount->getName(), $accountsAllCustomRelationships);
133
134         $this->assertArrayHasKey($this->relationAccountContact->getName(), $accountsLhsCustomRelationships);
135         $this->assertArrayNotHasKey($this->relationContactAccount->getName(), $accountsLhsCustomRelationships);
136
137         $this->assertFalse($wrongModuleName); // check
138     }
139
140     /**
141      * @group 45339
142      */
143     public function testGetCustomRelationshipsMetaFilesByModuleName()
144     {
145         $accountContactMetaPath = sprintf(
146                 'custom%1$smetadata%1$s' . $this->relationAccountContact->getName() . 'MetaData.php',
147                 DIRECTORY_SEPARATOR
148         );
149         $accountContactTablePath = sprintf(
150                 'custom%1$sExtension%1$sapplication%1$sExt%1$sTableDictionary%1$s' . $this->relationAccountContact->getName() . '.php',
151                 DIRECTORY_SEPARATOR
152         );
153         $contactAccountMetaPath = sprintf(
154                 'custom%1$smetadata%1$s' . $this->relationContactAccount->getName() . 'MetaData.php',
155                 DIRECTORY_SEPARATOR
156         );
157
158         /* @var $this->mbPackage MBPackage */
159         $accountsAllFiles = $this->mbPackage->getCustomRelationshipsMetaFilesByModuleNameTest('Accounts');
160         $accountsOnlyMetaFile = $this->mbPackage->getCustomRelationshipsMetaFilesByModuleNameTest('Accounts', true, true);
161         $wrongModuleName = $this->mbPackage->getCustomRelationshipsMetaFilesByModuleNameTest('Wrong_module_name');
162
163         $this->assertContains($accountContactMetaPath, $accountsAllFiles);
164         $this->assertContains($accountContactTablePath, $accountsAllFiles);
165         $this->assertContains($contactAccountMetaPath, $accountsAllFiles);
166
167         $this->assertContains($accountContactMetaPath, $accountsOnlyMetaFile);
168         $this->assertNotContains($contactAccountMetaPath, $accountsOnlyMetaFile);
169
170         $this->assertInternalType('array', $wrongModuleName);
171         $this->assertEmpty($wrongModuleName);
172     }
173
174     /**
175      * @group 45339
176      */
177     public function testGetExtensionsList()
178     {
179         // Create new relationship between Leads and Accounts
180         $_REQUEST['view_module'] = "Leads";
181         $_REQUEST['lhs_module'] = "Leads";
182         $_REQUEST['rhs_module'] = "Accounts";
183         $_REQUEST['lhs_label'] = "Leads";
184         $_REQUEST['rhs_label'] = "Accounts";
185
186         $deployedRelation = new DeployedRelationships($_REQUEST['view_module']);
187         $relationLeadAccount = $deployedRelation->addFromPost();
188         $deployedRelation->save();
189         $deployedRelation->build();
190
191         $accountContactRelInAccountVardefExtensions = sprintf(
192                 'custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . $this->relationAccountContact->getName() . '_Accounts.php',
193                 DIRECTORY_SEPARATOR
194         );
195         $contactAccountRelInAccountVardefExtensions = sprintf(
196                 'custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . $this->relationContactAccount->getName() . '_Accounts.php',
197                 DIRECTORY_SEPARATOR
198         );
199         $leadAccountRelInAccountVardefExtensions = sprintf(
200                 'custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . $relationLeadAccount->getName() . '_Accounts.php',
201                 DIRECTORY_SEPARATOR
202         );
203
204         /* @var $this->mbPackage MBPackage */
205         $accountAllExtensions = $this->mbPackage->getExtensionsListTest('Accounts');
206         $accountExtContacts = $this->mbPackage->getExtensionsListTest('Accounts', array('Contacts'));
207         $accountExtWithWrongRelationship = $this->mbPackage->getExtensionsListTest('Accounts', array(''));
208         $wrongModuleName = $this->mbPackage->getExtensionsListTest('Wrong_module_name');
209
210         // Remove relationship
211         $deployedRelation->delete($relationLeadAccount->getName());
212         $deployedRelation->save();
213         SugarRelationshipFactory::deleteCache();
214
215         $this->assertContains($accountContactRelInAccountVardefExtensions, $accountAllExtensions);
216         $this->assertContains($contactAccountRelInAccountVardefExtensions, $accountAllExtensions);
217         $this->assertContains($leadAccountRelInAccountVardefExtensions, $accountAllExtensions);
218
219         $this->assertContains($accountContactRelInAccountVardefExtensions, $accountExtContacts);
220         $this->assertContains($contactAccountRelInAccountVardefExtensions, $accountExtContacts);
221         $this->assertNotContains($leadAccountRelInAccountVardefExtensions, $accountExtContacts);
222
223         $this->assertEmpty($accountExtWithWrongRelationship);
224
225         $this->assertInternalType('array', $wrongModuleName);
226         $this->assertEmpty($wrongModuleName);
227     }
228
229     /**
230      * @group 45339
231      */
232     public function testGetExtensionsManifestForPackage()
233     {
234         /* @var $this->mbPackage MBPackage */
235         $this->mbPackage->exportCustom(array('Accounts'), false, false);
236         $installDefs = array();
237         $packExtentionsPath = $this->mbPackage->getBuildDir() . DIRECTORY_SEPARATOR . 'Extension' . DIRECTORY_SEPARATOR . 'modules';
238         $expected = 0;
239
240         $this->mbPackage->getExtensionsManifestForPackageTest($this->mbPackage->getBuildDir(), $installDefs);
241
242         $recursiveIterator = new RecursiveIteratorIterator(
243                         new RecursiveDirectoryIterator($packExtentionsPath),
244                         RecursiveIteratorIterator::SELF_FIRST
245         );
246
247         /* @var $fInfo SplFileInfo */
248         foreach (new RegexIterator($recursiveIterator, "/\.php$/i") as $fInfo)
249         {
250             if ($fInfo->isFile())
251             {
252                 ++$expected;
253             }
254         }
255
256         $this->mbPackage->delete();
257         $this->mbPackage->deleteBuild();
258
259         $this->assertEquals($expected, count($installDefs['copy']));
260     }
261
262     /**
263      * @group 45339
264      */
265     public function testCustomBuildInstall()
266     {
267         /* @var $this->mbPackage MBPackage */
268         $this->mbPackage->exportCustom(array('Accounts'), false, false);
269         $installDefString = $this->mbPackage->customBuildInstall(array('Accounts'), $this->mbPackage->getBuildDir());
270
271         eval($installDefString);
272
273         $this->mbPackage->delete();
274         $this->mbPackage->deleteBuild();
275         
276         $this->assertArrayHasKey('relationships', $installdefs);
277     }
278
279 }
280
281 class Bug45339MBPackageMock extends MBPackage
282 {
283
284     public function getExtensionsManifestForPackageTest($path, &$installdefs)
285     {
286         return $this->getExtensionsManifestForPackage($path, $installdefs);
287     }
288
289     public function getExtensionsListTest($module, $includeRelationships = true)
290     {
291         return $this->getExtensionsList($module, $includeRelationships);
292     }
293
294     public function getCustomRelationshipsMetaFilesByModuleNameTest($moduleName, $lhs = false, $metadataOnly = false)
295     {
296         return $this->getCustomRelationshipsMetaFilesByModuleName($moduleName, $lhs, $metadataOnly);
297     }
298
299     public function getCustomRelationshipsByModuleNameTest($moduleName, $lhs = false)
300     {
301         return $this->getCustomRelationshipsByModuleName($moduleName, $lhs);
302     }
303
304 }